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

Efficient way to convert from roll, pitch yaw to geometry_msgs::Quaternion

asked 2020-11-09 00:58:36 -0600

JeffR1992 gravatar image

updated 2020-11-09 00:59:49 -0600

In order to convert roll, pitch and yaw angles to a geometry_msgs::Quaternion object, which is appropriate for publishing, I currently do the following:

tf2::Quaternion quaternion_tf2;

quaternion_tf2.setRPY(roll, pitch, yaw);

geometry_msgs::Quaternion quaternion = tf2::toMsg(quaternion_tf2);

However, is there a more efficient way to do this? In particular, is there a way to bypass having to create a tf2::Quaternion object, and instead simply initialize the geometry_msgs::Quaternion object with roll, pitch and yaw using something similar to the .setRPY() function that is available for tf2::Quaternion?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-11-09 07:59:44 -0600

felixN gravatar image

updated 2020-11-09 08:01:22 -0600

Hi,

As far as I know, there are no real constructors (in the sens that they fill the values) for ros messages, because it would be to complicated to do (because the classes for ros messages are automatically generated from the .msg files). And according to the following git issue, it seems that the idea was definitively abondoned for ROS 1 (development is now foccused on ROS 2). :

https://github.com/ros/ros_comm/issue...

If you use a lot of quaternions, you can simply write your own function :

geometry_msgs::Quaternion quaternion_from_rpy(double roll, double pitch, double yaw)
{
  tf2::Quaternion quaternion_tf2;
  quaternion_tf2.setRPY(roll, pitch, yaw);
  geometry_msgs::Quaternion quaternion = tf2::toMsg(quaternion_tf2);
  return quaternion;
}

and use it to creat your quaternions

If you don't want to write your own function, you can create directly a constructor with arguments for the tf2::Quaternion in order to have one line less. They are 3 options according to

http://docs.ros.org/en/jade/api/tf2/h...

Quaternion (const tf2Scalar &x, const tf2Scalar &y, const tf2Scalar &z, const tf2Scalar &w)
Quaternion (const Vector3 &axis, const tf2Scalar &angle)
Quaternion (const tf2Scalar &yaw, const tf2Scalar &pitch, const tf2Scalar &roll) __attribute__((deprecated))

Note however that the one you would like to use (the one from yaw, pitch and roll) is deprecated. So up to you to decide if you still want to use it or not

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2020-11-09 00:58:36 -0600

Seen: 2,603 times

Last updated: Nov 09 '20