ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Declare a message outside of a node (scope problems?)

asked 2012-06-08 04:02:14 -0600

amittleider gravatar image

updated 2014-04-20 14:09:25 -0600

ngrennan gravatar image

I am trying to declare a new message outside of my ROS node, then pass it into the ROS node.

Multiple pieces of my program can generate GPS messages, but all the messages should be published to the one single publisher. Here are my publishers within my "Talker" node.

final Publisher<collab_msgs.SubjectPose> statePublisher 
            = connectedNode.newPublisher("/a/subject_pose", collab_msgs.SubjectPose._TYPE);

final Publisher<collab_msgs.SubjectGps> gpsPublisher  
            = connectedNode.newPublisher("/a/task_gps", collab_msgs.SubjectGps._TYPE);

Now, to create a new message within the node, I can of course do something like

collab_msgs.SubjectGps gpsMsg = gpsPublisher.newMessage();

However, if I want to create a message outside of this class, I no longer have access to the publisher to instantiate the message (I cannot make the publisher a class variable since it must be a constant and you must have the connectedNode parameter to instantiate a Publisher object).

It would be nice to be able to do something like

collab_msgs.SubjectGps a = new collab_msgs.SubjectGps();

and I saw some code from back in October '11 where this worked, but it appears that this has been changed since then.

Thanks for the help

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2012-06-10 09:54:39 -0600

jbohren gravatar image

updated 2012-06-10 09:55:20 -0600

Currently in rosjava, messages need to be constructed with message factories. See this answer for the syntax that worked then (April 2012), and might still work now:

edit flag offensive delete link more

Question Tools


Asked: 2012-06-08 04:02:14 -0600

Seen: 278 times

Last updated: Jun 10 '12