{"id":1862,"date":"2016-12-13T15:35:18","date_gmt":"2016-12-13T07:35:18","guid":{"rendered":"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/?page_id=1862"},"modified":"2023-01-21T05:38:27","modified_gmt":"2023-01-21T05:38:27","slug":"python-code-example-of-converting-rpyeuler-angles-to-rotation-vectorangle-axis-for-universal-robots","status":"publish","type":"page","link":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/python-code-example-of-converting-rpyeuler-angles-to-rotation-vectorangle-axis-for-universal-robots\/","title":{"rendered":"Python code example of converting RPY\/Euler angles to Rotation Vector\/Angle Axis for Universal-Robots."},"content":{"rendered":"<p><a title=\"Visit Zacobria &amp; Webshop &amp; Universal-Robots solutions.\" href=\"https:\/\/www.zacobria.com\/automation\/webshop\/\">Visit Zacobria Webshop<\/a><\/p>\n<p><strong>Python code example of converting RPY\/Euler angles to Rotation Vector\/Angle Axis for Universal-Robots.<\/strong><\/p>\n<p>Python code example of converting RPY\/Euler angles to Rotation Vector\/Angle Axis for Universal-Robots.<\/p>\n<p>Mind you that when all input values are 0, there will be a division by zero error at this line:<br \/>\n# multi = 1 \/ (2 * math.sin(theta))<br \/>\nIn this case theta will be 0 and the sin of 0 is 0, so you\u2019ll probably have to make a special case for this and set the rx, ry and rz to 0 directly.<\/p>\n<pre>import math\nimport numpy as np\n\nroll = 2.6335\npitch = 0.4506\nyaw = 1.1684\n\nprint \"roll = \", roll\nprint \"pitch = \", pitch\nprint \"yaw = \", yaw\nprint \"\"\n\nyawMatrix = np.matrix([\n[math.cos(yaw), -math.sin(yaw), 0],\n[math.sin(yaw), math.cos(yaw), 0],\n[0, 0, 1]\n])\n\npitchMatrix = np.matrix([\n[math.cos(pitch), 0, math.sin(pitch)],\n[0, 1, 0],\n[-math.sin(pitch), 0, math.cos(pitch)]\n])\n\nrollMatrix = np.matrix([\n[1, 0, 0],\n[0, math.cos(roll), -math.sin(roll)],\n[0, math.sin(roll), math.cos(roll)]\n])\n\nR = yawMatrix * pitchMatrix * rollMatrix\n\ntheta = math.acos(((R[0, 0] + R[1, 1] + R[2, 2]) - 1) \/ 2)\nmulti = 1 \/ (2 * math.sin(theta))\n\nrx = multi * (R[2, 1] - R[1, 2]) * theta\nry = multi * (R[0, 2] - R[2, 0]) * theta\nrz = multi * (R[1, 0] - R[0, 1]) * theta\n\nprint rx, ry, rz\n\n<\/pre>\n<p><strong>Testing angle calculation and compare to robot values.<\/strong><\/p>\n<p>RPY values tested<\/p>\n<p>roll = 0<br \/>\npitch = 180<br \/>\nyaw = 20<\/p>\n<pre>import math\nimport numpy as np\n\n#roll = 2.6335\n#pitch = 0.4506\n#yaw = 1.1684\n\nroll = 0\npitch = 180\nyaw = 20\n\nprint \"roll = \", roll\nprint \"pitch = \", pitch\nprint \"yaw = \", yaw\nprint \"\"\n\nyawMatrix = np.matrix([\n[math.cos(yaw), -math.sin(yaw), 0],\n[math.sin(yaw), math.cos(yaw), 0],\n[0, 0, 1]\n])\n\npitchMatrix = np.matrix([\n[math.cos(pitch), 0, math.sin(pitch)],\n[0, 1, 0],\n[-math.sin(pitch), 0, math.cos(pitch)]\n])\n\nrollMatrix = np.matrix([\n[1, 0, 0],\n[0, math.cos(roll), -math.sin(roll)],\n[0, math.sin(roll), math.cos(roll)]\n])\n\nR = yawMatrix * pitchMatrix * rollMatrix\n\ntheta = math.acos(((R[0, 0] + R[1, 1] + R[2, 2]) - 1) \/ 2)\nmulti = 1 \/ (2 * math.sin(theta))\n\nrx = multi * (R[2, 1] - R[1, 2]) * theta\nry = multi * (R[0, 2] - R[2, 0]) * theta\nrz = multi * (R[1, 0] - R[0, 1]) * theta\n\nprint rx, ry, rz<\/pre>\n<p><a href=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2016\/12\/ur_roll_pitch_yaw_0-180-20_result.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1896 size-full\" src=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2016\/12\/ur_roll_pitch_yaw_0-180-20_result.jpg\" alt=\"universal robots roll pitch yaw 0-180-20 result\" width=\"363\" height=\"119\"><\/a><\/p>\n<p><a href=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2016\/12\/ur_roll_pitch_yaw_0-180-20_rpy.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1897 size-full\" src=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2016\/12\/ur_roll_pitch_yaw_0-180-20_rpy.jpg\" alt=\"universal robots roll pitch yaw 0-180-20 data\" width=\"272\" height=\"333\"><\/a><\/p>\n<p>Rx &#8211; Ry and Rz show the same values as calculated by the Python program.<\/p>\n<p><a href=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2016\/12\/ur_roll_pitch_yaw_0-180-20.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1898 size-full\" src=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2016\/12\/ur_roll_pitch_yaw_0-180-20.jpg\" alt=\"universal robots roll pitch yaw 0-180-20 pose\" width=\"537\" height=\"255\"><\/a><\/p>\n<p><strong>Disclaimer:<\/strong> While the Zacobria Pte. Ltd. believes that information and guidance provided is correct, parties must rely upon their skill and judgement when making use of them. Zacobria Pte. Ltd. assumes no liability for loss or damage caused by error or omission, whether such an error or omission is the result of negligence or any other cause. Where reference is made to legislation it is not to be considered as legal advice. Any and all such liability is disclaimed.<\/p>\n<p>If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.<\/p>\n<p>Author:<br \/>\n<a href=\"https:\/\/plus.google.com\/u\/0\/116832821661215606670?rel=author\">By Zacobria Lars Skovsgaard<\/a><br \/>\nAccredited 2015-2018 Universal Robots support Centre and Forum.<\/p>\n<div class=\"fb-share-button\" data-href=\"https:\/\/zacobria.com\" data-layout=\"button_count\"><\/div>\n<p><script type=\"IN\/Share\" data-counter=\"right\" data-onsuccess=\"LinkedInShare\"><\/script><\/p>\n<div class=\"g-follow\" data-annotation=\"bubble\" data-height=\"20\" data-href=\"https:\/\/plus.google.com\/116832821661215606670\" data-rel=\"author\"><\/div>\n<div class=\"g-plusone\" data-size=\"medium\" data-annotation=\"bubble\" data-width=\"300\"><\/div>\n<div class=\"g-plus\" data-action=\"share\" data-annotation=\"bubble\"><\/div>\n<p><a class=\"twitter-share-button\" href=\"https:\/\/twitter.com\/share\" data-url=\"http:\/\/www.zacobria.com\" data-via=\"zacobria\">Tweet<\/a><br \/>\n<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=\/^http:\/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+':\/\/platform.twitter.com\/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');<\/script><\/p>\n<p><a class=\"twitter-follow-button\" href=\"https:\/\/twitter.com\/zacobria\" data-show-count=\"true\">Follow @zacobria<\/a><br \/>\n<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=\/^http:\/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+':\/\/platform.twitter.com\/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Visit Zacobria Webshop Python code example of converting RPY\/Euler angles to Rotation Vector\/Angle Axis for Universal-Robots. Python code example of converting RPY\/Euler angles to Rotation Vector\/Angle Axis for Universal-Robots. Mind you that when all input values are 0, there will&#8230; <\/p>\n<div class=\"more-link-container\"><a class=\"more-link\" href=\"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/python-code-example-of-converting-rpyeuler-angles-to-rotation-vectorangle-axis-for-universal-robots\/\">Read More<\/a><\/div>\n","protected":false},"author":1,"featured_media":2481,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"templates\/full-width-page.php","meta":{"footnotes":""},"class_list":["post-1862","page","type-page","status-publish","has-post-thumbnail","hentry"],"_links":{"self":[{"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/pages\/1862","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/comments?post=1862"}],"version-history":[{"count":14,"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/pages\/1862\/revisions"}],"predecessor-version":[{"id":3115,"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/pages\/1862\/revisions\/3115"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/media\/2481"}],"wp:attachment":[{"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/media?parent=1862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}