Ask Your Question

publish message in main class instead of talker

asked 2012-04-10 03:43:16 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

Hi all,

I'm working on a project that receives data from a sensor and sends the data back to pc using rosjava.

I'm trying to realize this function on the base of rosjava_tutorial_pubsub. I realized almost all the tutorials tend to use NodeMainExecute to run a node, and the publish function is implemented in the itself.

Since my data are retrieved and stored in the main class and updates constantly, i want to publish my message in main class, which means moving the following snippet from into

org.ros.message.std_msgs.String str = publisher.newMessage(); = mdata;

how should I modify the Should I add a variable Publisher publisher in the beginning?

actually I want to run roscore on PC.

edit retag flag offensive close merge delete


as far as I understand, I should create a main class which implements NodeMain. Then link my own application as source?

rosdai gravatar image rosdai  ( 2012-04-11 03:35:59 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2012-04-10 23:08:10 -0500

damonkohler gravatar image

You should think of your NodeMain as the main method. In there, you can use Node.executeCancelableLoop() to start a thread which will do the work for you and be shut down once the Node is shut down.

You should not try to write code in the main method of your application. In fact, you should use org.ros.RosRun as your application's main class as is done in the pubsub tutorial.

edit flag offensive delete link more


what is the advantage of doing so? I currently wrote a new method called publish in NodeMain, which fills the function of public. and in my main method i call this publish method every time my data updates

rosdai gravatar image rosdai  ( 2012-04-12 04:27:02 -0500 )edit

what is the advantage of doing so?

rosdai gravatar image rosdai  ( 2012-04-12 04:27:05 -0500 )edit

There are several reasons. One is that it makes your NodeMain self contained so it can be easily reused in another application. Another is that it allows you to easily stop the thread that's doing work by shutting down the NodeMain.

damonkohler gravatar image damonkohler  ( 2012-04-23 02:27:17 -0500 )edit

thank you damon

rosdai gravatar image rosdai  ( 2012-04-24 04:30:38 -0500 )edit

Your Answer

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

Add Answer

Question Tools


Asked: 2012-04-10 03:43:16 -0500

Seen: 177 times

Last updated: Apr 11 '12