rosjava - errors setting up a simple publisher
I'm trying to set up a simple publisher using rosjava (pure java, I don't need Android). I've been following some tutorials and making lots of progress, but now I'm stuck. Here is the code I have so far:
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;
import org.ros.node.NodeConfiguration;
import org.ros.node.Node;
import org.ros.internal.node.DefaultNode;
import org.ros.node.DefaultNodeFactory;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.Executors;
public class Publisher_Test {
private DefaultNode node;
//private Publisher<std_msgs.String> publisher;
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
public Publisher_Test() {
NodeConfiguration node_cfg = NodeConfiguration.newPrivate();
DefaultNodeFactory factory = new DefaultNodeFactory( scheduler );
node = (DefaultNode)factory.newNode( node_cfg );
System.out.println("In Constructor");
final Publisher<std_msgs.String> publisher = node.newPublisher("chatter", std_msgs.String._TYPE);
node.executeCancellableLoop( new CancellableLoop() {
private int seq;
@Override
protected void setup() {
seq=0;
System.out.println("Setting up loop");
}
@Override
protected void loop() throws InterruptedException {
System.out.println("Looping");
try {
while (true) {
org.ros.message.std_msgs.String();
std_msgs.String str = publisher.newMessage();
str.setData( "Hello world! " + seq++ );
publisher.publish(str);
Thread.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
I've been basing this off of this question and this tutorial. Some of the syntax used is out of data, so I've been looking through the source code here to get things to compile.
I'm using ROS Hydro on an Ubuntu 13.04 machine. My goal is to get a publisher and subscriber implemented in pure java running in jython. I've compiled my class using javac, and can import it into jython fine, I get an error when I try to set up my node in the constructor for Publisher_Test:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
at org.ros.concurrent.ListenerGroup.addAll(ListenerGroup.java:85)
at org.ros.concurrent.ListenerGroup.addAll(ListenerGroup.java:101)
at org.ros.internal.node.DefaultNode.<init>(DefaultNode.java:132)
at org.ros.node.DefaultNodeFactory.newNode(DefaultNodeFactory.java:41)
at org.ros.node.DefaultNodeFactory.newNode(DefaultNodeFactory.java:46)
at Publisher_Test.<init>(Publisher_Test.java:31)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.python.core.PyReflectedConstructor.constructProxy(PyReflectedConstructor.java:210)
java.lang.NullPointerException: java.lang.NullPointerException
From what I understand so far, it looks like DefaultNodeFactory is setting up the DefaultNode with the Collection<nodelistener> to be null (which looking at the source code seems to be a valid thing to do), and then the addAll() method which is called in the DefaultNode constructor throws this exception when it tries to iterate on null.
I'm thinking I need to give a Listener to the DefaultNodeFactory, but I am unsure how I would go about doing this. What is a ...