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

euler to quaternion c++

asked 2021-02-16 06:35:46 -0600

amaTMR gravatar image

updated 2021-02-16 14:44:49 -0600

jayess gravatar image

Hi, because I'm quite new to world of ROS, I'm struggling with transforming this Python line to c++ -

quaternion = quaternion_from_euler(roll, pitch, yaw)

I have tried multiple approaches. I used :

setEuler(), setRPY and other,but when I conver the RPY to quaternion and compare the results in online calculators it doesn't match. Could someone give me a clue how to solve this issue?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2021-02-16 11:10:08 -0600

updated 2021-02-16 15:14:08 -0600

I'll just post my code i am using for this since there isn't much to explain about it:

tf2::Quaternion myQuaternion;



and you are done.

you then get the quaternion parts with e.g myQuaternion.getX() ...

The normalize part is only in there since i had issues in the past with not normalized quaternions being rejected by some libraries so i just normalize everything. But this can be the reason, why your result looked different than the one from the online calculator. Hope this helps

EDIT: you need the following libraries: tf2/LinearMath/Quaternion.h

edit flag offensive delete link more


Thank you,I used your code,but I'm oddly getting different results. This is what I've got :

amaTMR gravatar image amaTMR  ( 2021-02-17 02:37:50 -0600 )edit

tf2::Quaternion q; q.setRPY(0,0,0); q= q.normalize();

ROS_INFO_STREAM("\n qUART unchanged **********" );
ROS_INFO_STREAM("qUART  x " << q[0] );
ROS_INFO_STREAM("qUART  y"  <<q[1]);
ROS_INFO_STREAM("qUART  z " <<q[2]);
ROS_INFO_STREAM("qUART  w"  <<q[3]);
ROS_INFO_STREAM("qUART transformed ********* " );
q.setRPY(20.000,-60.000, 30.000);
q= q.normalize();

ROS_INFO_STREAM("qUART  x " << q[0] );
ROS_INFO_STREAM("qUART  y"  <<q[1]);
ROS_INFO_STREAM("qUART  z " <<q[2]);
ROS_INFO_STREAM("qUART  w"  <<q[3]);

ROS_INFO_STREAM("qUART transformed ********* \n" );
amaTMR gravatar image amaTMR  ( 2021-02-17 02:40:05 -0600 )edit

amaTMR gravatar image amaTMR  ( 2021-02-17 02:41:47 -0600 )edit

Unfortunately i cant see the result of your conversion.

But it seems like you want to set rpy in degree.

setRPY() function expects radian (Hint basically everywhere in ros where u use euler angles these are radian).

Try multiplying your angles by pi/180 to convert them and check again.

I'd maybe even write a small function for the conversion, not sure if thats your style though.

In the end it should look something like this:

Tristan9497 gravatar image Tristan9497  ( 2021-02-17 08:35:09 -0600 )edit

Thanks,I solved this issue by using this :

quaternion_ = quaternion_.normalize();
transformStamped_.transform.rotation.x = quaternion_.x();
transformStamped_.transform.rotation.y = quaternion_.y();
transformStamped_.transform.rotation.z = quaternion_.z();
transformStamped_.transform.rotation.w = quaternion_.w();

My mistake was that I compared my data to the online calculator.which differs and I have no clue why. But my node works now ,thanks again...

amaTMR gravatar image amaTMR  ( 2021-02-19 05:51:01 -0600 )edit

You're welcome happy i was able to help.

Tristan9497 gravatar image Tristan9497  ( 2021-02-19 06:34:30 -0600 )edit

Question Tools

1 follower


Asked: 2021-02-16 06:35:46 -0600

Seen: 7,640 times

Last updated: Feb 16 '21