ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
Well as nobody answered I'm going to post the solution I found. The main code "node" running all the "nodelets" (like listener or talker) is called RosRun You can find it in RosRun.java. Talker and listener classes are the same as the one I posted on my question. You can find them in rosjava tutorials. To be able to use RosRun.java separately in eclipse I had to do some modifications and I also renamed my class in this case to rosjavaPubSub:
package rosjavaPubSub;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import org.ros.exception.RosRuntimeException;
import org.ros.internal.loader.CommandLineLoader;
import org.ros.node.DefaultNodeMainExecutor;
import org.ros.node.NodeConfiguration;
import org.ros.node.NodeMain;
import org.ros.node.NodeMainExecutor;
import java.net.URI;
// This class will run a publisher and subscriber, and relay data between them.
public class rosjavaPubSub {
//Create instances for Talker and Listener
private Talker pubNodeMain = new Talker();
private Listener subNodeMain = new Listener();
//Here everything is done in constructor but doesn't have to be done like that, you could do it directly in a main function to be able to run the code directly
public rosjavaPubSub() {
// Set up the executor for both of the nodes
NodeMainExecutor nodeMainExecutor = DefaultNodeMainExecutor.newDefault();
//This is because I work on a remote machine masteruri has the roscore machine IP and host is the local IP
URI masteruri = URI.create("http://192.168.10.2:11311");
String host = "192.168.10.3";
// Load the publisher(talker)
NodeConfiguration pubNodeConfiguration = NodeConfiguration.newPublic(host, masteruri);
//Check if Talker class correctly instantiated
Preconditions.checkState(pubNodeMain != null);
//execute the nodelet talker (this will run the method onStart of Talker.java)
nodeMainExecutor.execute(pubNodeMain, pubNodeConfiguration);
// Load the subscriber(listener) same as for publisher
NodeConfiguration subNodeConfiguration = NodeConfiguration.newPublic(host, masteruri);
Preconditions.checkState(subNodeMain != null);
nodeMainExecutor.execute(subNodeMain, subNodeConfiguration);
}
}