Ask Your Question
0

ROSJAVA preconditions exceptions in updateLoop() method outside of main

asked 2011-10-11 09:09:29 -0500

morrowsend gravatar image

I'm modifying the ROSjava publisher example to run within some other code I've written. The other code I've written has its own updateLoop method that is called by main and runs continuously until the program is exited. I am doing calculations within this updateLoop and would like to publish the results.

I can run the example publishing code just fine if it is in main before I make the call to the updateLoop() method, however if the publishing code is inside the updateLoop(), the program crashes when it attempts the first loop. The crash occurs when checking the preconditions. Here's the exact output:

SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main] java.lang.NullPointerException at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:187) at ROS.Talker2.simpleUpdate(Unknown Source)

I modified the code to make the NodeConditions and some other stuff global so I can pass it to the updateLoop without doing to explicitly. The code below (minus the extraneous code of my program) is what produced the error above.

public class Talker2 implements NodeMain {

        private Node node; 
        private NodeConfiguration globalConfiguration;
        private int seq=0;
        org.ros.message.std_msgs.String str = new org.ros.message.std_msgs.String();
        Publisher<org.ros.message.std_msgs.String> publisher;


      @Override 
      public void main(NodeConfiguration configuration) {
      globalConfiguration = configuration;

      Talker 2 app = new Talker2();
      app.updateLoop();
      }// end Main

// ... other methods in here ...

public void updateLoop()
{
/*  my calcs are done before the publish here */
 Preconditions.checkState(node == null);
      Preconditions.checkNotNull(globalConfiguration);
      try {
        node = new DefaultNodeFactory().newNode("talker", globalConfiguration);
       publisher = node.newPublisher("chatter", "std_msgs/String");
        int seq = 0;
        {
          org.ros.message.std_msgs.String str = new org.ros.message.std_msgs.String();
          str.data = "INSIDE LOOP "+ seq++;
          System.out.println("\n\n INSIDE LOOP FLAGEN!!!"+ seq++);
          publisher.publish(str);
          //Thread.sleep(1000);
        }
      } catch (Exception e) {
        if (node != null) {
          node.getLog().fatal(e);
        } else {
          e.printStackTrace();
        }
      }
}//end updateLoop();

}/ end Talker2 class

How can I publish the data from within my updateLoop()? Also, I plan to have several publishers in this loop in the future. Is there anything I should look out for with that?

edit retag flag offensive close merge delete

Comments

It seems that the stack trace does not match the code you posted. What exactly is failing the null check?
damonkohler gravatar imagedamonkohler ( 2011-10-27 00:21:17 -0500 )edit
I think the main problem I was having with this was that I was not passing the NodeConfiguration to the main method. I didn't get this to work as posted originally, but I now have something cobbled together. I'll post it in the Answer to this question.
morrowsend gravatar imagemorrowsend ( 2011-10-27 07:57:10 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2011-10-27 08:01:00 -0500

morrowsend gravatar image

I finally just ran ROS as a separate thread, then shared data between the updateLoop() and the ROS Node via volatile variables. Before doing this, however, I had to implement NodeMain in the starting class, and create a global volatile to hold the NodeConfiguration passed to the main method. Then I created and ran the ROS thread and passed the NodeConfiguration as mentioned in this post. This does the job just fine.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-10-11 09:09:29 -0500

Seen: 240 times

Last updated: Oct 27 '11