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

Connect to master URI and launch ROSJava nodes

asked 2012-05-30 08:19:27 -0500

amittleider gravatar image

updated 2014-01-28 17:12:30 -0500

ngrennan gravatar image

I asked a similar question before, but I don't think I was clear enough as no one had a response. I've dug around a ton in the javadocs to figure out how to launch the master URI and register ROS nodes within straight java code (not android, just rosjava). I found some old tutorials here: https://mirror.umd.edu/roswiki/rosjava(2f)Overview(2f)Nodes.html but it appears that they are outdated and the code no longer compiles.

Here is my attempt, but note that I create new Talker(), but I never use it. Obviously this is incorrect as I should be required to pass the talker node as a parameter somewhere:

    RosCore rosCore = RosCore.newPublic();
    NodeConfiguration nodeConfiguration = NodeConfiguration.newPrivate();
    rosCore.start();
    try {
      rosCore.awaitStart();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
    Talker talker = new Talker();
    nodeConfiguration.setMasterUri(rosCore.getUri());

    GraphName nodeName = new GraphName("Talker");
    GraphName topicName = new GraphName("chatter");
    String topicMessageType = "String";

    rosCore.getMasterServer().registerPublisher(nodeName, rosCore.getUri(), topicName, topicMessageType);

    int i = 0;
    while(i < 5)
    {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        i++;
    }

    rosCore.shutdown();

I just want this to publish the chatter topic with a hello world message. Thanks for helping.

-Andrew

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2012-05-31 16:46:29 -0500

amittleider gravatar image

updated 2012-05-31 16:48:32 -0500

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 (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);
}

}

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-05-30 08:19:27 -0500

Seen: 2,457 times

Last updated: May 31 '12