Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags. |
Rosjava publisher and subscriber
Description:Tutorial Level: BEGINNER
Contents
Introduction
The rosjava documentation already comes with useful tutorials. This wiki page is only to complement information from those, and to allow the community to gather helpful advice in one place, to maybe be included in the generated docs later.
If you encounter any error, please first try to enter the error message at http://answers.ros.org
Prerequisites
Make sure you have followed the instructions of how to install and build rosjava.
Again, if you encounter errors, try searching on http://answers.ros.org.
Some hints on fixing errors:
Check that you are using a java6 JDK, that your JAVA_HOME is set correctly, that ROS_PACKAGE_PATH is set and contains rosjava, that CLASSPATH is set correctly (and has no reference to rt.jar).
$ which java $ echo $CLASSPATH $ echo $ROS_PACKAGE_PATH $ sudo update-alternatives --config java
gradle
For this tutorial I used ROS electric on Ubuntu oneiric.
$ echo $ROS_ROOT /opt/ros/electric/ros $ cat /etc/*-release DISTRIB_CODENAME=oneiric DISTRIB_DESCRIPTION="Ubuntu 11.10"
Before running gradle, make sure to have followed the instructions on installing rosjava, and make sure you have called source setup.bash in the terminal.
You have the choice of using gradle, or a wrapper for gradle.
You can use the gradlew script in rosjava instead of a gradle installation, but in linux that means you always have to provide the full path to gradlew, e.g.
../../rosjava/gradlew installApp
Or you'd have to copy the wrapper from rosjava to your project, meaning the gradlew file and the gradle folder.
If you want to use installed gradle instead, note that Ubuntu oneiric ships only with Gradle 1.0-milestone-3, which is too old for this tutorial. Check your version with gradle --version.
To install gradle from source:
Download zip file from http://www.gradle.org/downloads.html and unzip to folder
$ unzip gradle-... -d ~/local/gradle/
add these lines to your ~/.bashrc
cd export HOME=`pwd` export GRADLE_HOME="${HOME}/local/gradle" PATH="... :$GRADLE_HOME/bin"
start a new terminal for the changes to take effect.
This is only if you do not want to use the gradle wrapper script gradlew located in rosjava.
Setting up an independent gradle project for this tutorial
We will create a project similar to rosjava_tutorials_pubsub that is shipped with rosjava. The only difference is that we shall create is as an independent gradle project, like you would do for your own projects.
Gradle is an tool alternative to Ant and Maven. if you are new to the Java world, consider it something like GNU Make.
Instead of Makefiles, gradle uses gradle files (obviously), written in the Groovy Language.
So first we create a ROS package using roscreate-pkg, we delete the files CMakelists.txt and Makefile to avoid getting confused:
$ roscreate-pkg rosjava_wiki_tutorial_pubsub $ cd rosjava_wiki_tutorial_pubsub $ rm Makefile $ rm CMakelists.txt
Create a file build.gradle with this contents:
// The Application plugin and mainClassName attribute are only required if // your package is used as a binary. apply plugin: 'application' mainClassName = 'org.ros.RosRun' apply plugin: 'java' apply plugin: 'maven' // uncomment to create an eclipse project using 'gradle eclipse' //apply plugin: 'eclipse' sourceCompatibility = 1.6 targetCompatibility = 1.6 // custom maven repository for some rosjava dependencies repositories { mavenLocal() maven { url 'http://robotbrains.hideho.org/nexus/content/groups/ros-public' } } // we need this for maven installation version = '0.0.0-SNAPSHOT' group = 'ros.my_stack' dependencies { compile 'ros.rosjava_core:rosjava:0.0.0-SNAPSHOT' compile 'ros.rosjava_core:rosjava_messages:0.0.0-SNAPSHOT' compile 'ros.rosjava_core:rosjava_bootstrap:0.0.0-SNAPSHOT' }
Some things for you to notice here:
This file is different from the build.gradle in the rosjava tutorial project rosjava_tutorial_pubsub, because that one is configured by its parent project rosjava. Since you want to create independent projects, you need to supply a little more information to gradle.
Gradle can use Maven. In case you did not notice, running ./gradlew install created some jar files in the folder named by your M2_REPO environment variable, usually ~/.m2. Those are rosjava libraries we want to use. In the dependencies section we declare those dependencies, and we need apply plugin: 'maven' so that gradle knows what to do. In case this wiki page gets outdated, check your ~/.m2/repositories folder for the true IDs of rosjava projects. Gradle will then make sure to use these libraries (and their dependencies) for compiling and installing our app.
We declare the main Class to be org.ros.RosRun, which is part of rosjava. This will create an application to which you can pass the name of a node to run.
Then create the folder structure that gradle expects:
$ mkdir -p src/main/java
If you want to use the Eclipse IDE, you can also generate the necessary project files now. Uncomment the line apply plugin: 'eclipse' in build.gradle and call:
$ gradle eclipse
This can take a while. See notes on using gradlew instead of gradle in prerequisites. Once it is done, in Eclipse, call File > Import ... > Existing Projects into workspace > .../rosjava_wiki_tutorial_pubsub
Creating Publisher and subscriber
Now we create a Ros topic publisher and Subscriber Node in our project as in the original rosjava tutorials. See those tutorials for a description of what they do.
Create the files * src/main/java/org/ros/rosjava_tutorial_pubsub/Talker.java: * src/main/java/org/ros/rosjava_tutorial_pubsub/Listener.java:
as in the rosjava documentation
Running the code
First we we compile the code. We do it in the command line, in Eclipse you'd need a groovy and a Gradle plugin for that purpose (Extend this tutorial if you know how to get them).
$ roscd rosjava_wiki_tutorial_pubsub $ gradle installApp
(Again, you can use gradlew from rosjava instead if you do not have gradle or it is too old.)
If you want, have a look into the build folder to see what has been generated, in particular in build/install
Then run a roscore and the Talker and Publisher:
$ roscore $ ./build/install/rosjava_wiki_tutorial_pubsub/bin/rosjava_wiki_tutorial_pubsub org.ros.rosjava_tutorial_pubsub.Talker $ ./build/install/rosjava_wiki_tutorial_pubsub/bin/rosjava_wiki_tutorial_pubsub org.ros.rosjava_tutorial_pubsub.Listener
Note that the call is a little different from the rosjava documentation at the time of this writing.
The file we call, ./build/install/rosjava_wiki_tutorial_pubsub/bin/rosjava_wiki_tutorial_pubsub is a shell script generated by gradle. It calls RosRun as main class. We need to pass the Class name of a java class that extends Node. RosRun will run any such node.