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

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

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

amittleider gravatar image

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

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
1

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

jbohren gravatar image

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

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: http://answers.ros.org/question/31399/how-do-you-construct-composite-messages-in-rosjava/?answer=31516#post-id-31516

edit flag offensive delete link more

Question Tools

Stats

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

Seen: 283 times

Last updated: Jun 10 '12