Skip to content

Commit 021df8d

Browse files
committed
feat: add quaternion calculations notebook for rotation transformations
1 parent 50bb82d commit 021df8d

File tree

1 file changed

+185
-0
lines changed
  • src/cmp3103m_ros2_code_fragments/cmp3103m_ros2_code_fragments/ipynb

1 file changed

+185
-0
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"kernelspec": {
6+
"display_name": "Python 3",
7+
"language": "python",
8+
"name": "python3"
9+
},
10+
"language_info": {
11+
"codemirror_mode": {
12+
"name": "ipython",
13+
"version": 3
14+
},
15+
"file_extension": ".py",
16+
"mimetype": "text/x-python",
17+
"name": "python",
18+
"nbconvert_exporter": "python",
19+
"pygments_lexer": "ipython3",
20+
"version": "3.6.2"
21+
},
22+
"colab": {
23+
"name": "quaternion.ipynb",
24+
"provenance": []
25+
}
26+
},
27+
"cells": [
28+
{
29+
"cell_type": "code",
30+
"metadata": {
31+
"id": "gcpyFv6whLyc"
32+
},
33+
"source": [
34+
"from math import sin, cos, atan2, pi\n",
35+
"from scipy.spatial.transform import Rotation as R"
36+
],
37+
"execution_count": null,
38+
"outputs": []
39+
},
40+
{
41+
"cell_type": "code",
42+
"metadata": {
43+
"id": "ebPe4SwqhLyg"
44+
},
45+
"source": [
46+
"# some angle:\n",
47+
"\n",
48+
"angle = 30 * pi / 180"
49+
],
50+
"execution_count": 52,
51+
"outputs": []
52+
},
53+
{
54+
"cell_type": "code",
55+
"metadata": {
56+
"id": "ExPk-YxRhLyk"
57+
},
58+
"source": [
59+
"# definition of a quaternion rotation around the z axis alone\n",
60+
"q = {\n",
61+
" 'x': 0.0,\n",
62+
" 'y': 0.0,\n",
63+
" 'z': sin(angle/2),\n",
64+
" 'w': cos(angle/2)\n",
65+
"}"
66+
],
67+
"execution_count": 53,
68+
"outputs": []
69+
},
70+
{
71+
"cell_type": "code",
72+
"metadata": {
73+
"id": "MqACWNyahLym",
74+
"outputId": "6a9d7e41-c10d-4b79-881f-049b71555e9f",
75+
"colab": {
76+
"base_uri": "https://localhost:8080/",
77+
"height": 34
78+
}
79+
},
80+
"source": [
81+
"# show it\n",
82+
"print('quaternion is %s' % q)"
83+
],
84+
"execution_count": 54,
85+
"outputs": [
86+
{
87+
"output_type": "stream",
88+
"text": [
89+
"quaternion is {'x': 0.0, 'y': 0.0, 'z': 0.25881904510252074, 'w': 0.9659258262890683}\n"
90+
],
91+
"name": "stdout"
92+
}
93+
]
94+
},
95+
{
96+
"cell_type": "code",
97+
"metadata": {
98+
"id": "pbvMLBpihLyq",
99+
"outputId": "16b869fe-f8fc-4c2f-a4fc-2802645983bb",
100+
"colab": {
101+
"base_uri": "https://localhost:8080/",
102+
"height": 34
103+
}
104+
},
105+
"source": [
106+
"recomputeangle = atan2(q['z'], q['w'])*2\n",
107+
"print('yaw angle extracted from quaternion is %s' % (recomputeangle * 180 / pi))"
108+
],
109+
"execution_count": 55,
110+
"outputs": [
111+
{
112+
"output_type": "stream",
113+
"text": [
114+
"yaw angle extracted from quaternion is 29.999999999999996\n"
115+
],
116+
"name": "stdout"
117+
}
118+
]
119+
},
120+
{
121+
"cell_type": "code",
122+
"metadata": {
123+
"id": "fRzioAQvhLyt",
124+
"outputId": "f61521de-16a4-4595-9673-db042a97857f",
125+
"colab": {
126+
"base_uri": "https://localhost:8080/",
127+
"height": 68
128+
}
129+
},
130+
"source": [
131+
"r = R.from_quat([q['x'], q['y'], q['z'], q['w']])\n",
132+
"print(r.as_matrix())"
133+
],
134+
"execution_count": 56,
135+
"outputs": [
136+
{
137+
"output_type": "stream",
138+
"text": [
139+
"[[ 0.8660254 -0.5 0. ]\n",
140+
" [ 0.5 0.8660254 0. ]\n",
141+
" [ 0. 0. 1. ]]\n"
142+
],
143+
"name": "stdout"
144+
}
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"metadata": {
150+
"id": "nQKvK6tPhdAw",
151+
"outputId": "ea3e71db-2248-41ee-e583-0ea9cb6a91a0",
152+
"colab": {
153+
"base_uri": "https://localhost:8080/",
154+
"height": 51
155+
}
156+
},
157+
"source": [
158+
"print(r.as_euler('zyx', degrees=True))\n",
159+
"print(r.as_rotvec())"
160+
],
161+
"execution_count": 57,
162+
"outputs": [
163+
{
164+
"output_type": "stream",
165+
"text": [
166+
"[30. 0. 0.]\n",
167+
"[0. 0. 0.52359878]\n"
168+
],
169+
"name": "stdout"
170+
}
171+
]
172+
},
173+
{
174+
"cell_type": "code",
175+
"metadata": {
176+
"id": "9AlK5IbjhvqE"
177+
},
178+
"source": [
179+
""
180+
],
181+
"execution_count": null,
182+
"outputs": []
183+
}
184+
]
185+
}

0 commit comments

Comments
 (0)