[ros-users] How to get copy of message using MessageEvent callback?

Patrick Bouffard bouffard at eecs.berkeley.edu
Mon Oct 25 18:35:53 UTC 2010


On Mon, Oct 25, 2010 at 11:08 AM, Josh Faust <jfaust at willowgarage.com> wrote:
> Same way you dereference any pointer:
> var = *msg;

Hmm, thanks. I thought that was one of the many things I tried but I
guess not. That solves the second error.

> Sorry, that was a typo in my email.  That works fine, and is correct.

The first error is still present for me, though:

/home/bouffard/dev/ros/ros_flyer/flyer_controller/nodes/controller.cpp:
In member function ‘void
flyer_controller::Controller::controlModesStatusCallback(const
ros::MessageEvent<const
flyer_controller::control_mode_status_<std::allocator<void> > >&)’:
/home/bouffard/dev/ros/ros_flyer/flyer_controller/nodes/controller.cpp:198:
error: invalid initialization of reference of type ‘const
flyer_controller::control_mode_statusPtr&’ from expression of type
‘boost::shared_ptr<const
flyer_controller::control_mode_status_<std::allocator<void> > >’

Here is the code. Just to take away the last little bit of difference
I explicitly included the namespace when my message is mentioned:

    void controlModesStatusCallback(const
ros::MessageEvent<flyer_controller::control_mode_status const>& event)
    {
      const std::string& publisher_name = event.getPublisherName();
      const ros::M_string& header = event.getConnectionHeader();
      ros::Time receipt_time = event.getReceiptTime();

      const flyer_controller::control_mode_statusPtr& msg = event.getMessage();

      ROS_INFO_STREAM("Got control mode status, from: " << publisher_name);
      latest_mode_status[node_mode_map[publisher_name]] = *msg;
    }

BUT, I tried one more permutation, and this one compiles and runs!

    void controlModesStatusCallback(const
ros::MessageEvent<flyer_controller::control_mode_status const>& event)
    {
      const std::string& publisher_name = event.getPublisherName();
      const ros::M_string& header = event.getConnectionHeader();
      ros::Time receipt_time = event.getReceiptTime();

      flyer_controller::control_mode_status msg;
      msg = *(event.getMessage());

      ROS_INFO_STREAM("Got control mode status, from: " << publisher_name);
      latest_mode_status[node_mode_map[publisher_name]] = msg;
    }

I'm still not sure why the message type is making a difference but now
that I have something that works and doesn't rely on
__connection_header I'm all set. Thanks again for the tips!

Pat



More information about the ros-users mailing list