PX4 Firmware
PX4 Autopilot Software http://px4.io
rotation.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2013 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file rotation.cpp
36  *
37  * Vector rotation library
38  */
39 
40 #include <px4_platform_common/defines.h>
41 #include "math.h"
42 #include "rotation.h"
43 
46 {
48  math::radians((float)rot_lookup[rot].roll),
49  math::radians((float)rot_lookup[rot].pitch),
50  math::radians((float)rot_lookup[rot].yaw)}};
51 }
52 
55 {
57  math::radians((float)rot_lookup[rot].roll),
58  math::radians((float)rot_lookup[rot].pitch),
59  math::radians((float)rot_lookup[rot].yaw)}};
60 }
61 
62 __EXPORT void
63 rotate_3f(enum Rotation rot, float &x, float &y, float &z)
64 {
65  float tmp;
66 
67  switch (rot) {
68  case ROTATION_NONE:
69  case ROTATION_MAX:
70  return;
71 
72  case ROTATION_YAW_45: {
73  tmp = M_SQRT1_2_F * (x - y);
74  y = M_SQRT1_2_F * (x + y);
75  x = tmp;
76  return;
77  }
78 
79  case ROTATION_YAW_90: {
80  tmp = x; x = -y; y = tmp;
81  return;
82  }
83 
84  case ROTATION_YAW_135: {
85  tmp = -M_SQRT1_2_F * (x + y);
86  y = M_SQRT1_2_F * (x - y);
87  x = tmp;
88  return;
89  }
90 
91  case ROTATION_YAW_180:
92  x = -x; y = -y;
93  return;
94 
95  case ROTATION_YAW_225: {
96  tmp = M_SQRT1_2_F * (y - x);
97  y = -M_SQRT1_2_F * (x + y);
98  x = tmp;
99  return;
100  }
101 
102  case ROTATION_YAW_270: {
103  tmp = x; x = y; y = -tmp;
104  return;
105  }
106 
107  case ROTATION_YAW_315: {
108  tmp = M_SQRT1_2_F * (x + y);
109  y = M_SQRT1_2_F * (y - x);
110  x = tmp;
111  return;
112  }
113 
114  case ROTATION_ROLL_180: {
115  y = -y; z = -z;
116  return;
117  }
118 
120  tmp = M_SQRT1_2_F * (x + y);
121  y = M_SQRT1_2_F * (x - y);
122  x = tmp; z = -z;
123  return;
124  }
125 
127  tmp = x; x = y; y = tmp; z = -z;
128  return;
129  }
130 
132  tmp = M_SQRT1_2_F * (y - x);
133  y = M_SQRT1_2_F * (y + x);
134  x = tmp; z = -z;
135  return;
136  }
137 
138  case ROTATION_PITCH_180: {
139  x = -x; z = -z;
140  return;
141  }
142 
144  tmp = -M_SQRT1_2_F * (x + y);
145  y = M_SQRT1_2_F * (y - x);
146  x = tmp; z = -z;
147  return;
148  }
149 
151  tmp = x; x = -y; y = -tmp; z = -z;
152  return;
153  }
154 
156  tmp = M_SQRT1_2_F * (x - y);
157  y = -M_SQRT1_2_F * (x + y);
158  x = tmp; z = -z;
159  return;
160  }
161 
162  case ROTATION_ROLL_90: {
163  tmp = z; z = y; y = -tmp;
164  return;
165  }
166 
168  tmp = z; z = y; y = -tmp;
169  tmp = M_SQRT1_2_F * (x - y);
170  y = M_SQRT1_2_F * (x + y);
171  x = tmp;
172  return;
173  }
174 
176  tmp = z; z = y; y = -tmp;
177  tmp = x; x = -y; y = tmp;
178  return;
179  }
180 
182  tmp = z; z = y; y = -tmp;
183  tmp = -M_SQRT1_2_F * (x + y);
184  y = M_SQRT1_2_F * (x - y);
185  x = tmp;
186  return;
187  }
188 
189  case ROTATION_ROLL_270: {
190  tmp = z; z = -y; y = tmp;
191  return;
192  }
193 
195  tmp = z; z = -y; y = tmp;
196  tmp = M_SQRT1_2_F * (x - y);
197  y = M_SQRT1_2_F * (x + y);
198  x = tmp;
199  return;
200  }
201 
203  tmp = z; z = -y; y = tmp;
204  tmp = x; x = -y; y = tmp;
205  return;
206  }
207 
209  tmp = z; z = -y; y = tmp;
210  tmp = -M_SQRT1_2_F * (x + y);
211  y = M_SQRT1_2_F * (x - y);
212  x = tmp;
213  return;
214  }
215 
217  tmp = z; z = -y; y = tmp;
218  tmp = x; x = y; y = -tmp;
219  return;
220  }
221 
222  case ROTATION_PITCH_90: {
223  tmp = z; z = -x; x = tmp;
224  return;
225  }
226 
227  case ROTATION_PITCH_270: {
228  tmp = z; z = x; x = -tmp;
229  return;
230  }
231 
233  tmp = z; z = x; x = tmp;
234  y = -y;
235  return;
236  }
237 
239  tmp = x; x = z; z = tmp;
240  y = -y;
241  return;
242  }
243 
245  tmp = x; x = y;
246  y = -z; z = -tmp;
247  return;
248  }
249 
251  float tmpx = x;
252  float tmpy = y;
253  float tmpz = z;
254  x = 0.143039f * tmpx + 0.368776f * tmpy + -0.918446f * tmpz;
255  y = -0.332133f * tmpx + -0.856289f * tmpy + -0.395546f * tmpz;
256  z = -0.932324f * tmpx + 0.361625f * tmpy + 0.000000f * tmpz;
257  return;
258  }
259 
261  tmp = x; x = -y;
262  y = z; z = -tmp;
263  return;
264  }
265 
267  float tmpx = x;
268  float tmpy = y;
269  float tmpz = z;
270  x = -0.987688f * tmpx + 0.000000f * tmpy + -0.156434f * tmpz;
271  y = 0.000000f * tmpx + -1.000000f * tmpy + 0.000000f * tmpz;
272  z = -0.156434f * tmpx + 0.000000f * tmpy + 0.987688f * tmpz;
273  return;
274  }
275 
276  case ROTATION_PITCH_45: {
277  tmp = M_SQRT1_2_F * x + M_SQRT1_2_F * z;
278  z = M_SQRT1_2_F * z - M_SQRT1_2_F * x;
279  x = tmp;
280  return;
281  }
282 
283  case ROTATION_PITCH_315: {
284  tmp = M_SQRT1_2_F * x - M_SQRT1_2_F * z;
285  z = M_SQRT1_2_F * z + M_SQRT1_2_F * x;
286  x = tmp;
287  return;
288  }
289 
291  tmp = x;
292  x = -z;
293  z = y;
294  y = -tmp;
295  return;
296  }
297  }
298 }
__EXPORT void rotate_3f(enum Rotation rot, float &x, float &y, float &z)
rotate a 3 element float vector in-place
Definition: rotation.cpp:63
Definition: I2C.hpp:51
Vector rotation library.
Quaternion class.
Definition: Dcm.hpp:24
Rotation
Enum for board and external compass rotations.
Definition: rotation.h:51
constexpr T radians(T degrees)
Definition: Limits.hpp:85
Euler angles class.
Definition: AxisAngle.hpp:18
const rot_lookup_t rot_lookup[]
Definition: rotation.h:97
__EXPORT matrix::Dcmf get_rot_matrix(enum Rotation rot)
Get the rotation matrix.
Definition: rotation.cpp:45
__EXPORT matrix::Quatf get_rot_quaternion(enum Rotation rot)
Get the rotation quaternion.
Definition: rotation.cpp:54