altoslib: Fix 8 to 12 bit conversion for Mega pyro voltage data
[fw/altos] / altoslib / AltosRotation.java
1 /*
2  * Copyright © 2014 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altoslib_12;
20
21 public class AltosRotation extends AltosQuaternion {
22         private AltosQuaternion         rotation;
23
24         /* Compute pitch angle from vertical by taking the pad
25          * orientation vector and rotating it by the current total
26          * rotation value. That will be a unit vector pointing along
27          * the airframe axis. The Z value will be the cosine of the
28          * angle from vertical.
29          *
30          * rot = ao_rotation * vertical * ao_rotation°
31          * rot = ao_rotation * (0,0,0,1) * ao_rotation°
32          *     = ((a.z, a.y, -a.x, a.r) * (a.r, -a.x, -a.y, -a.z)) .z
33          *
34          *     = (-a.z * -a.z) + (a.y * -a.y) - (-a.x * -a.x) + (a.r * a.r)
35          *     = a.z² - a.y² - a.x² + a.r²
36          *
37          * rot = ao_rotation * (0, 0, 0, -1) * ao_rotation°
38          *     = ((-a.z, -a.y, a.x, -a.r) * (a.r, -a.x, -a.y, -a.z)) .z
39          *
40          *     = (a.z * -a.z) + (-a.y * -a.y) - (a.x * -a.x) + (-a.r * a.r)
41          *     = -a.z² + a.y² + a.x² - a.r²
42          *
43          * tilt = acos(rot)  (in radians)
44          */
45
46         public double tilt() {
47                 double  rotz = rotation.z * rotation.z - rotation.y * rotation.y - rotation.x * rotation.x + rotation.r * rotation.r;
48
49                 double tilt = Math.acos(rotz) * 180.0 / Math.PI;
50                 return tilt;
51         }
52
53         /* Given euler rotations in three axes, perform a combined rotation using
54          * quaternions
55          */
56         public void rotate(double x, double y, double z) {
57                 AltosQuaternion rot = AltosQuaternion.euler(x, y, z);
58                 rotation = rot.multiply(rotation).normalize();
59         }
60
61         /* Clone an existing rotation value */
62         public AltosRotation (AltosRotation old) {
63                 this.rotation = new AltosQuaternion(old.rotation);
64         }
65
66         /* Create a new rotation value given an acceleration vector pointing down */
67         public AltosRotation(double x,
68                              double y,
69                              double z,
70                              int pad_orientation) {
71                 AltosQuaternion orient = AltosQuaternion.vector(x, y, z).normalize();
72                 double sky = pad_orientation == 0 ? 1 : -1;
73                 AltosQuaternion up = new AltosQuaternion(0, 0, 0, sky);
74                 rotation = up.vectors_to_rotation(orient);
75         }
76
77         public AltosRotation() {
78                 rotation = new AltosQuaternion();
79         }
80 }