Obtaining pointer types from message attributes in callbacks
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?