ROS Answers: Open Source Q&A Forum - RSS feedhttps://answers.ros.org/questions/Open source question and answer forum written in Python and DjangoenROS Answers is licensed under Creative Commons Attribution 3.0Sun, 05 Jan 2014 03:23:24 -0600quaternions with pythonhttps://answers.ros.org/question/12903/quaternions-with-python/I have recently started using rospy after being working with roscpp. I wanted to reproduce this piece of C++ code:
geometry_msgs::Quaternion quat = tf::createQuaternionMsgFromYaw(th);
geometry_msgs::PoseStamped goal;
goal.pose.orientation = quat;
For what I have found, the closest form would be something like:
quat = quaternion_to_msg(tf.transformations.quaternion_from_euler(0, 0, th))
goal = PoseStamped()
goal.pose.orientation = quat
But this actually does not work because 'quaternion_from_euler' returns a four float array, instead of an object with {x, y, z, w} fields.
I have searched for some common function to do this easy transform, but I have not found it within the ros libraries. What I have seen is that many people make his own implementation:
def quaternion_to_msg(q):
msg = Quaternion()
msg.x = q[0]
msg.y = q[1]
msg.z = q[2]
msg.w = q[3]
return msg
goal.pose.orientation = quaternion_to_msg(quat)
Does ROS provide a common solution for this situation?
Mon, 06 Feb 2012 00:43:04 -0600https://answers.ros.org/question/12903/quaternions-with-python/Answer by Dan Lazewatsky for <p>I have recently started using rospy after being working with roscpp. I wanted to reproduce this piece of C++ code:</p>
<pre><code>geometry_msgs::Quaternion quat = tf::createQuaternionMsgFromYaw(th);
geometry_msgs::PoseStamped goal;
goal.pose.orientation = quat;
</code></pre>
<p>For what I have found, the closest form would be something like:</p>
<pre><code>quat = quaternion_to_msg(tf.transformations.quaternion_from_euler(0, 0, th))
goal = PoseStamped()
goal.pose.orientation = quat
</code></pre>
<p>But this actually does not work because 'quaternion_from_euler' returns a four float array, instead of an object with {x, y, z, w} fields.</p>
<p>I have searched for some common function to do this easy transform, but I have not found it within the ros libraries. What I have seen is that many people make his own implementation:</p>
<pre><code>def quaternion_to_msg(q):
msg = Quaternion()
msg.x = q[0]
msg.y = q[1]
msg.z = q[2]
msg.w = q[3]
return msg
goal.pose.orientation = quaternion_to_msg(quat)
</code></pre>
<p>Does ROS provide a common solution for this situation?</p>
https://answers.ros.org/question/12903/quaternions-with-python/?answer=19040#post-id-19040Python has a nifty feature which allows you to expand a tuple into an argument list. Rather than setting all of the quaternion's fields individually, you can use the constructor as follows
q = tf.transformations.quaternion_from_euler(0, 0, th)
msg = Quaternion(*q)
This is equivalent to doing
q = tf.transformations.quaternion_from_euler(0, 0, th)
msg = Quaternion(q[0], q[1], q[2], q[3])
Mon, 06 Feb 2012 02:15:24 -0600https://answers.ros.org/question/12903/quaternions-with-python/?answer=19040#post-id-19040Comment by bit-pirate for <p>Python has a nifty feature which allows you to expand a tuple into an argument list. Rather than setting all of the quaternion's fields individually, you can use the constructor as follows</p>
<pre><code>q = tf.transformations.quaternion_from_euler(0, 0, th)
msg = Quaternion(*q)
</code></pre>
<p>This is equivalent to doing</p>
<pre><code>q = tf.transformations.quaternion_from_euler(0, 0, th)
msg = Quaternion(q[0], q[1], q[2], q[3])
</code></pre>
https://answers.ros.org/question/12903/quaternions-with-python/?comment=114926#post-id-114926Is there documentation for the tf.transformation module?Sun, 05 Jan 2014 03:23:24 -0600https://answers.ros.org/question/12903/quaternions-with-python/?comment=114926#post-id-114926Comment by victorp for <p>Python has a nifty feature which allows you to expand a tuple into an argument list. Rather than setting all of the quaternion's fields individually, you can use the constructor as follows</p>
<pre><code>q = tf.transformations.quaternion_from_euler(0, 0, th)
msg = Quaternion(*q)
</code></pre>
<p>This is equivalent to doing</p>
<pre><code>q = tf.transformations.quaternion_from_euler(0, 0, th)
msg = Quaternion(q[0], q[1], q[2], q[3])
</code></pre>
https://answers.ros.org/question/12903/quaternions-with-python/?comment=58561#post-id-58561Thanks, but I mean about the `tf` python api.Tue, 19 Mar 2013 05:26:10 -0500https://answers.ros.org/question/12903/quaternions-with-python/?comment=58561#post-id-58561Comment by Dan Lazewatsky for <p>Python has a nifty feature which allows you to expand a tuple into an argument list. Rather than setting all of the quaternion's fields individually, you can use the constructor as follows</p>
<pre><code>q = tf.transformations.quaternion_from_euler(0, 0, th)
msg = Quaternion(*q)
</code></pre>
<p>This is equivalent to doing</p>
<pre><code>q = tf.transformations.quaternion_from_euler(0, 0, th)
msg = Quaternion(q[0], q[1], q[2], q[3])
</code></pre>
https://answers.ros.org/question/12903/quaternions-with-python/?comment=58562#post-id-58562What do you mean? The tf api docs look pretty complete to me. Tue, 19 Mar 2013 05:49:34 -0500https://answers.ros.org/question/12903/quaternions-with-python/?comment=58562#post-id-58562Comment by victorp for <p>Python has a nifty feature which allows you to expand a tuple into an argument list. Rather than setting all of the quaternion's fields individually, you can use the constructor as follows</p>
<pre><code>q = tf.transformations.quaternion_from_euler(0, 0, th)
msg = Quaternion(*q)
</code></pre>
<p>This is equivalent to doing</p>
<pre><code>q = tf.transformations.quaternion_from_euler(0, 0, th)
msg = Quaternion(q[0], q[1], q[2], q[3])
</code></pre>
https://answers.ros.org/question/12903/quaternions-with-python/?comment=58517#post-id-58517Where can one find documentation on these mysterious functions ? http://ros.org/doc/groovy/api/tf/html/python/ is quite... primitive.Mon, 18 Mar 2013 22:52:47 -0500https://answers.ros.org/question/12903/quaternions-with-python/?comment=58517#post-id-58517Comment by Dan Lazewatsky for <p>Python has a nifty feature which allows you to expand a tuple into an argument list. Rather than setting all of the quaternion's fields individually, you can use the constructor as follows</p>
<pre><code>q = tf.transformations.quaternion_from_euler(0, 0, th)
msg = Quaternion(*q)
</code></pre>
<p>This is equivalent to doing</p>
<pre><code>q = tf.transformations.quaternion_from_euler(0, 0, th)
msg = Quaternion(q[0], q[1], q[2], q[3])
</code></pre>
https://answers.ros.org/question/12903/quaternions-with-python/?comment=58557#post-id-58557This is a python feature, not ROS. As such, it is documented in the python docs: http://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-listsTue, 19 Mar 2013 04:50:46 -0500https://answers.ros.org/question/12903/quaternions-with-python/?comment=58557#post-id-58557Answer by Wim for <p>I have recently started using rospy after being working with roscpp. I wanted to reproduce this piece of C++ code:</p>
<pre><code>geometry_msgs::Quaternion quat = tf::createQuaternionMsgFromYaw(th);
geometry_msgs::PoseStamped goal;
goal.pose.orientation = quat;
</code></pre>
<p>For what I have found, the closest form would be something like:</p>
<pre><code>quat = quaternion_to_msg(tf.transformations.quaternion_from_euler(0, 0, th))
goal = PoseStamped()
goal.pose.orientation = quat
</code></pre>
<p>But this actually does not work because 'quaternion_from_euler' returns a four float array, instead of an object with {x, y, z, w} fields.</p>
<p>I have searched for some common function to do this easy transform, but I have not found it within the ros libraries. What I have seen is that many people make his own implementation:</p>
<pre><code>def quaternion_to_msg(q):
msg = Quaternion()
msg.x = q[0]
msg.y = q[1]
msg.z = q[2]
msg.w = q[3]
return msg
goal.pose.orientation = quaternion_to_msg(quat)
</code></pre>
<p>Does ROS provide a common solution for this situation?</p>
https://answers.ros.org/question/12903/quaternions-with-python/?answer=19075#post-id-19075I'd highly recommend taking a look at PyKDL, which is part of the KDL library [here](http://orocos.org/kdl) or [here](http://ros.org/wiki/kdl). It has well tested support for working with quaternions, Euler angles, axis-angle, etc. Tue, 07 Feb 2012 05:53:29 -0600https://answers.ros.org/question/12903/quaternions-with-python/?answer=19075#post-id-19075