rosjava network configuration [closed]
Hi I am using rosjava to pub/sub topics between different computers however I can list the topic but can not echo the message. I referred to this post but the link to the solution is not available now. Does any one know how to solve it. By the way I test the network using turtlesim it works ok but just can not work in rosjava. Under java, the rostopic list -a command gives the following output:
http://127.0.0.1:43190/ rosjava_tutorial_pubsub/talker
Here is the java code I use, the main java file is used to run the node alone, and need the class name of the node that I want to execute, which is the talker class. The talker and the master run on one computer, I just use rostopic to check the talker on another computer.
main
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import org.ros.address.InetAddressFactory;
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;
import static java.lang.System.exit;
public class tstAlone {
public static void printUsage() {
//the jar file is the one with the main class and also the node classes that need to be executed, the
//arg behind jar is the name of the node or nodemain class
System.err.println("Usage: java -jar my_package.jar com.example.MyNodeMain [args]");
}
public static void main(String[] argv) throws Exception {
if (argv.length == 0) {
printUsage();
exit(1);
}
CommandLineLoader loader = new CommandLineLoader(Lists.newArrayList(argv));
String nodeClassName = loader.getNodeClassName();
System.out.println("Loading node class: " + loader.getNodeClassName());
NodeConfiguration nodeConfiguration = loader.build();
NodeMain nodeMain = null;
try {
nodeMain = loader.loadClass(nodeClassName);
} catch (ClassNotFoundException e) {
throw new RosRuntimeException("Unable to locate node: " + nodeClassName, e);
} catch (InstantiationException e) {
throw new RosRuntimeException("Unable to instantiate node: " + nodeClassName, e);
} catch (IllegalAccessException e) {
throw new RosRuntimeException("Unable to instantiate node: " + nodeClassName, e);
}
Preconditions.checkState(nodeMain != null);
String host = InetAddressFactory.newNonLoopback().getHostAddress()
.toString();
final NodeMainExecutor nodeMainExecutor = DefaultNodeMainExecutor.newDefault();
nodeConfiguration.newPublic(host,URI.create("http://IPofMaster:11311/"));
nodeMainExecutor.execute(nodeMain, nodeConfiguration);
}
}
the talker
import org.ros.concurrent.CancellableLoop;
import org.ros.namespace.GraphName;
import org.ros.node.AbstractNodeMain;
import org.ros.node.ConnectedNode;
import org.ros.node.NodeMain;
import org.ros.node.topic.Publisher;
public class TalkerOri extends AbstractNodeMain {
@Override
public GraphName getDefaultNodeName() {
return GraphName.of("rosjava_tutorial_pubsub/talker");
}
@Override
public void onStart(final ConnectedNode connectedNode) {
System.out.println(connectedNode.getUri().toString());
final Publisher<std_msgs.String> publisher =
connectedNode.newPublisher("chatter", std_msgs.String._TYPE);
std_msgs.String str = publisher.newMessage();
str.setData("H++++++++++++++++++++++++++++++ello world! ");
publisher.publish(str);
// This CancellableLoop will be canceled automatically when the node shuts
// down.
connectedNode.executeCancellableLoop(new CancellableLoop() {
private int sequenceNumber;
@Override
protected void setup() {
sequenceNumber = 0;
}
@Override
protected void loop() throws InterruptedException {
std_msgs.String str = publisher.newMessage();
str.setData("**************Hello world! " + sequenceNumber);
publisher.publish(str);
sequenceNumber++;
Thread.sleep(1000);
}
});
}
}
Solution code:
public class newMain {
public static void ...