0
$\begingroup$

Rosanswers logo

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


Originally posted by amittleider on ROS Answers with karma: 227 on 2012-05-30

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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

}


Originally posted by amittleider with karma: 227 on 2012-05-31

This answer was ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.