altoslib: Missed a couple of easy mini voltage API changes
[fw/altos] / src / core / ao_quaternion.h
1 /*
2  * Copyright © 2013 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #ifndef _AO_QUATERNION_H_
19 #define _AO_QUATERNION_H_
20
21 #include <math.h>
22
23 struct ao_quaternion {
24         float   r;              /* real bit */
25         float   x, y, z;        /* imaginary bits */
26 };
27
28 static inline void ao_quaternion_multiply(struct ao_quaternion *r,
29                                           const struct ao_quaternion *a,
30                                           const struct ao_quaternion *b)
31 {
32         struct ao_quaternion    t;
33 #define T(_a,_b)        (((a)->_a) * ((b)->_b))
34
35 /*
36  * Quaternions
37  *
38  *      ii = jj = kk = ijk = -1;
39  *
40  *      kji = 1;
41  *
42  *      ij = k;         ji = -k;
43  *      kj = -i;        jk = i;
44  *      ik = -j;        ki = j;
45  *
46  * Multiplication p * q:
47  *
48  *      (pr + ipx + jpy + kpz) (qr + iqx + jqy + kqz) =
49  *
50  *              ( pr * qr +  pr * iqx +  pr * jqy +  pr * kqz) +
51  *              (ipx * qr + ipx * iqx + ipx * jqy + ipx * kqz) +
52  *              (jpy * qr + jpy * iqx + jpy * jqy + jpy * kqz) +
53  *              (kpz * qr + kpz * iqx + kpz * jqy + kpz * kqz) =
54  *
55  *
56  *               (pr * qr) + i(pr * qx) + j(pr * qy) + k(pr * qz) +
57  *              i(px * qr) -  (px * qx) + k(px * qy) - j(px * qz) +
58  *              j(py * qr) - k(py * qx) -  (py * qy) + i(py * qz) +
59  *              k(pz * qr) + j(pz * qx) - i(pz * qy) -  (pz * qz) =
60  *
61  *              1 * ( (pr * qr) - (px * qx) - (py * qy) - (pz * qz) ) +
62  *              i * ( (pr * qx) + (px * qr) + (py * qz) - (pz * qy) ) +
63  *              j * ( (pr * qy) - (px * qz) + (py * qr) + (pz * qx) ) +
64  *              k * ( (pr * qz) + (px * qy) - (py * qx) + (pz * qr);
65  */
66
67         t.r = T(r,r) - T(x,x) - T(y,y) - T(z,z);
68         t.x = T(r,x) + T(x,r) + T(y,z) - T(z,y);
69         t.y = T(r,y) - T(x,z) + T(y,r) + T(z,x);
70         t.z = T(r,z) + T(x,y) - T(y,x) + T(z,r);
71 #undef T
72         *r = t;
73 }
74
75 static inline void ao_quaternion_conjugate(struct ao_quaternion *r,
76                                            const struct ao_quaternion *a)
77 {
78         r->r = a->r;
79         r->x = -a->x;
80         r->y = -a->y;
81         r->z = -a->z;
82 }
83
84 static inline float ao_quaternion_normal(const struct ao_quaternion *a)
85 {
86 #define S(_a)   (((a)->_a) * ((a)->_a))
87         return S(r) + S(x) + S(y) + S(z);
88 #undef S
89 }
90
91 static inline void ao_quaternion_scale(struct ao_quaternion *r,
92                                        const struct ao_quaternion *a,
93                                        float b)
94 {
95         r->r = a->r * b;
96         r->x = a->x * b;
97         r->y = a->y * b;
98         r->z = a->z * b;
99 }
100
101 static inline void ao_quaternion_normalize(struct ao_quaternion *r,
102                                            const struct ao_quaternion *a)
103 {
104         float   n = ao_quaternion_normal(a);
105
106         if (n > 0)
107                 ao_quaternion_scale(r, a, 1/sqrtf(n));
108         else
109                 *r = *a;
110 }
111
112 static inline float ao_quaternion_dot(const struct ao_quaternion *a,
113                                       const struct ao_quaternion *b)
114 {
115 #define T(_a)   (((a)->_a) * ((b)->_a))
116         return T(r) + T(x) + T(y) + T(z);
117 #undef T
118 }
119                                      
120
121 static inline void ao_quaternion_rotate(struct ao_quaternion *r,
122                                         const struct ao_quaternion *a,
123                                         const struct ao_quaternion *b)
124 {
125         struct ao_quaternion    c;
126         struct ao_quaternion    t;
127
128         ao_quaternion_multiply(&t, b, a);
129         ao_quaternion_conjugate(&c, b);
130         ao_quaternion_multiply(r, &t, &c);
131 }
132
133 /*
134  * Compute a rotation quaternion between two vectors
135  *
136  *      cos(θ) + u * sin(θ)
137  *
138  * where θ is the angle between the two vectors and u
139  * is a unit vector axis of rotation
140  */
141
142 static inline void ao_quaternion_vectors_to_rotation(struct ao_quaternion *r,
143                                                      const struct ao_quaternion *a,
144                                                      const struct ao_quaternion *b)
145 {
146         /*
147          * The cross product will point orthogonally to the two
148          * vectors, forming our rotation axis. The length will be
149          * sin(θ), so these values are already multiplied by that.
150          */
151
152         float x = a->y * b->z - a->z * b->y;
153         float y = a->z * b->x - a->x * b->z;
154         float z = a->x * b->y - a->y * b->x;
155
156         float s_2 = x*x + y*y + z*z;
157         float s = sqrtf(s_2);
158
159         /* cos(θ) = a · b / (|a| |b|).
160          *
161          * a and b are both unit vectors, so the divisor is one
162          */
163         float c = a->x*b->x + a->y*b->y + a->z*b->z;
164
165         float c_half = sqrtf ((1 + c) / 2);
166         float s_half = sqrtf ((1 - c) / 2);
167
168         /*
169          * Divide out the sine factor from the
170          * cross product, then multiply in the
171          * half sine factor needed for the quaternion
172          */
173         float s_scale = s_half / s;
174
175         r->x = x * s_scale;
176         r->y = y * s_scale;
177         r->z = z * s_scale;
178
179         r->r = c_half;
180
181         ao_quaternion_normalize(r, r);
182 }
183
184 static inline void ao_quaternion_init_vector(struct ao_quaternion *r,
185                                              float x, float y, float z)
186 {
187         r->r = 0;
188         r->x = x;
189         r->y = y;
190         r->z = z;
191 }
192
193 static inline void ao_quaternion_init_rotation(struct ao_quaternion *r,
194                                                float x, float y, float z,
195                                                float s, float c)
196 {
197         r->r = c;
198         r->x = s * x;
199         r->y = s * y;
200         r->z = s * z;
201 }
202
203 static inline void ao_quaternion_init_zero_rotation(struct ao_quaternion *r)
204 {
205         r->r = 1;
206         r->x = r->y = r->z = 0;
207 }
208
209 /*
210  * The sincosf from newlib just calls sinf and cosf. This is a bit
211  * faster, if slightly less precise
212  */
213
214 static inline void
215 ao_sincosf(float a, float *s, float *c) {
216         float   _s = sinf(a);
217         *s = _s;
218         *c = sqrtf(1 - _s*_s);
219 }
220
221 /*
222  * Initialize a quaternion from 1/2 euler rotation angles (in radians).
223  *
224  * Yes, it would be nicer if there were a faster way, but because we
225  * sample the gyros at only 100Hz, we end up getting angles too large
226  * to take advantage of sin(x) ≃ x.
227  *
228  * We might be able to use just a couple of elements of the sin taylor
229  * series though, instead of the whole sin function?
230  */
231
232 static inline void ao_quaternion_init_half_euler(struct ao_quaternion *r,
233                                                  float x, float y, float z)
234 {
235         float   s_x, c_x;
236         float   s_y, c_y;
237         float   s_z, c_z;
238
239         ao_sincosf(x, &s_x, &c_x);
240         ao_sincosf(y, &s_y, &c_y);
241         ao_sincosf(z, &s_z, &c_z);
242
243         r->r = c_x * c_y * c_z + s_x * s_y * s_z;
244         r->x = s_x * c_y * c_z - c_x * s_y * s_z;
245         r->y = c_x * s_y * c_z + s_x * c_y * s_z;
246         r->z = c_x * c_y * s_z - s_x * s_y * c_z;
247 }
248
249 #endif /* _AO_QUATERNION_H_ */