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

Revision history [back]

Two things are happening here.

First, there's some behind-the-scenes stuff ROS does. Specifically, when you subscribe to something, your node communicates with the ROS master to figure out who's publishing on that topic. Then, your node sets up a peer-to-peer connection with every publisher, and gets ready to receive messages. When a message comes in, it deserializes it and then calls your callback with the message as the argument.

Second, your callback runs, and you do whatever you want.

So, the message is in the msg argument of the callback you defined above. To avoid an extraneous copy, it's actually in a const boost::shared_ptr (typedef'd to message_type::ConstPtr). You can then do with this message whatever you'd like; copy it, just copy the pointer, process it, or whatever.

There are myriad reasons not to just keep it in a variable: the simplest is that that would require you to implement polling yourself, rather than just a callback function that's called whenever a message arrives. The race conditions &c that polling introduces are fairly painful; the callback technique is much simpler.

Something to keep in mind: every time your callback is called, it's with a different message: if you want to save older versions (say, to average over so that your cmd_vel results are smooth), you'll have to do that yourself.