ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I am surprised that I couldn't find detailed documentation for this anywhere (There is some at the project home, but you definitely cannot just copy/paste to get it working). I read a lot of the source code to attempt to figure this out. So far, so good. Here is some code that you can run in Java to launch your nodes. Keep in mind that you have to first compile all the nodes that come with the rosjava source... This will create the JAR files. Then you must add the JAR files to your project.

public class RosConnect {

RosCore rosCore;
NodeMainExecutor e = DefaultNodeMainExecutor.newDefault();

public RosConnect()
{
    rosCore = RosCore.newPublic(13111); //13111 is the ROS port.  if you are launching ros files from roscpp or rospy make sure to  specify the argument '-p 13111' when you run the launch file.
    rosCore.start();

    try {
        rosCore.awaitStart();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

public void publishChatter()
{
    System.out.println("Starting talker node...");
    NodeConfiguration talkerConfig = NodeConfiguration.newPrivate();
    talkerConfig.setMasterUri(rosCore.getUri());
    talkerConfig.setNodeName("Talker");
    NodeMain talker = new Talker();
    e.execute(talker, talkerConfig);

    System.out.println("Starting listener node....");
    NodeConfiguration listenerConfig = NodeConfiguration.newPrivate();
    listenerConfig.setMasterUri(rosCore.getUri());
    listenerConfig.setNodeName("Listener");
    NodeMain listener = new Listener();
    e.execute(listener, listenerConfig);
}

}

I am surprised that I couldn't find detailed documentation for this anywhere (There is some at the project home, but you definitely cannot just copy/paste to get it working). I read a lot of the source code to attempt to figure this out. So far, so good. Here is some code that you can run in Java to launch your nodes. Keep in mind that you have to first compile all the nodes that come with the rosjava source... This will create the JAR files. Then you must add the JAR files to your project.project (in this example, I took the Talker and Listener source files from the rosjava_tutorial_pubsub and added them to my project as well).

public class RosConnect {

RosCore rosCore;
NodeMainExecutor e = DefaultNodeMainExecutor.newDefault();

public RosConnect()
{
    rosCore = RosCore.newPublic(13111); //13111 is the ROS port.  if you are launching ros files from roscpp or rospy make sure to  specify the argument '-p 13111' when you run the launch file.
    rosCore.start();

    try {
        rosCore.awaitStart();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

public void publishChatter()
{
    System.out.println("Starting talker node...");
    NodeConfiguration talkerConfig = NodeConfiguration.newPrivate();
    talkerConfig.setMasterUri(rosCore.getUri());
    talkerConfig.setNodeName("Talker");
    NodeMain talker = new Talker();
    e.execute(talker, talkerConfig);

    System.out.println("Starting listener node....");
    NodeConfiguration listenerConfig = NodeConfiguration.newPrivate();
    listenerConfig.setMasterUri(rosCore.getUri());
    listenerConfig.setNodeName("Listener");
    NodeMain listener = new Listener();
    e.execute(listener, listenerConfig);
}

}