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

Obtaining pointer types from message attributes in callbacks

asked 2020-03-10 09:37:18 -0600

heuristicus gravatar image

updated 2020-03-11 11:06:49 -0600

I am curious to know the preferred method for obtaining one of the pointer types that is defined for each message type, such as typeConstPtr or typePtr, when receiving a message in a callback.

As far as I can tell, when a message contains some field, the value in msg->field is const field_type.

For example, I have a callback which receives messages from an IMU, and some function which processes a vector3 in the form of a const geometry_msgs::Vector3ConstPtr&, a common method of receiving message types.

Note that I'm assuming the processVector3 function is something that is provided by an external library, so we have no control over it.

void callback(const sensor_msgs::ImuConstPtr& msg) {
    processVector3(msg->angular_velocity);
}

void processVector3(const geometry_msgs::Vector3ConstPtr& vec) {
    // do processing here
}

This of course does not work as msg->angular-velocity is of type const geometry_msgs::Vector3_<std::allocator<void>>, which, as we are helpfully informed by the compiler, cannot be converted to the type required by the function.

As far as I can tell, there is no function provided by the message type that allows us to retrieve a Ptr or ConstPtr version of the message, so we have to create such a pointer and pass that to the processing function.

So we end up with something like

  void callback(const sensor_msgs::ImuConstPtr& msg) {
    // processVector3(msg->angular_velocity); Doesn't work because of type mismatch
    geometry_msgs::Vector3ConstPtr vec_ptr(boost::make_shared<const geometry_msgs::Vector3>(msg->angular_velocity));
    processVector3(vec_ptr);
  }

  void processVector3(const geometry_msgs::Vector3ConstPtr& vec) {
    // do processing here
  }

My specific interest here is in the conversion of msg->angular_velocity to geometry_msgs::Vector3ConstPtr. Is the method in the example above the preferred method of doing this conversion, or are there other ways that might be better?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2020-03-10 10:38:26 -0600

So, we could have a discussion about ConstPtr and Ptr (and the when why what how) but it sounds like your actual issue is that you have 2 callbacks taking in Ptr-types and you’d like to be able to call them both without duplicating code.

What you suggest in doing msg->angular_velocity doesn’t work because that’s an object not a pointer. But &msg->angular_velocity is a pointer. So how do we bridge the gap?

Templates. Your vector callback should be a pointer template type.

But a nugget of info on ConstPtr: that’s really important for sensor data processing. If you have a pipeline to condition data from a camera, depth, etc you make use of Nodelets. And with nodelets, the ConstPtr will save you a copy in the intraprocess communication which is substantial at 100hz 1MP data streams.

edit flag offensive delete link more

Comments

I agree about the template option but why not simply a const reference to a sensor_msgs::Vector3 ? I'm not sure that processVector3 is a callback function so I don't see why bothering with reusing ConstPtr.

Delb gravatar image Delb  ( 2020-03-10 11:21:51 -0600 )edit

You can do a const reference is you want, no one's stopping you. Its just reducing copies and having control of the object in the local scope.

stevemacenski gravatar image stevemacenski  ( 2020-03-10 11:55:26 -0600 )edit

Thanks for the suggestion - this would be fine if I had control over the function parameters, but if the function I'm calling is out of my control, then I can't change it to make use of your suggestion. I'll update the question to clarify.

heuristicus gravatar image heuristicus  ( 2020-03-11 04:38:11 -0600 )edit

I don’t understand what you're looking for then. If you have functions you can’t change, why are you concerned with the “why”?

stevemacenski gravatar image stevemacenski  ( 2020-03-11 10:44:16 -0600 )edit

I'm specifically interested in the conversion of the const Vector3 that we get from the message, to vector3ConstPtr, and wondering if the method I am using is the best approach.

heuristicus gravatar image heuristicus  ( 2020-03-11 11:07:32 -0600 )edit

If you can't change the functions at all, then yes, you're going to need to allocate on the heap.

stevemacenski gravatar image stevemacenski  ( 2020-03-11 11:15:13 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2020-03-10 09:37:18 -0600

Seen: 513 times

Last updated: Mar 11 '20