2 * Copyright © 2014 Keith Packard <keithp@keithp.com>
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.
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.
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.
19 package org.altusmetrum.altoslib_14;
21 public class AltosRotation extends AltosQuaternion {
22 private AltosQuaternion rotation;
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.
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
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²
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
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²
43 * tilt = acos(rot) (in radians)
46 public double tilt() {
47 double rotz = rotation.z * rotation.z - rotation.y * rotation.y - rotation.x * rotation.x + rotation.r * rotation.r;
49 double tilt = Math.acos(rotz) * 180.0 / Math.PI;
53 /* Compute azimuth angle from a reference line pointing out the side
56 * rot = ao_rotation * x_axis * ao_rotation°
57 * rot = ao_rotation * (0,1,0,0) * ao_rotation°
58 * = (-a.x, a.r, a.z, -a.y) * (a.r, -a.x, -a.y, -a.z) . x
59 * = (-a.x * -a.x) + (a.r * a.r) + (a.z * -a.z) - (-a.y * -a.y)
60 * = a.x² + a.r² - a.z² - a.y²
62 * = (-a.x, a.r, a.z, -a.y) * (a.r, -a.x, -a.y, -a.z) . y
63 * = (-a.x * -a.y) - (a.r * -a.z) + (a.z * a.r) + (-a.y * -a.x)
64 * = a.x * a.y + a.r * a.z + a.z * a.r + a.y * a.x
66 * The X value will be the cosine of the rotation. The Y value will be the
67 * sine of the rotation; use the sign of that to figure out which direction from
71 public double azimuth() {
72 double rotx = rotation.x * rotation.x + rotation.r * rotation.r - rotation.z * rotation.z - rotation.y * rotation.y;
73 double roty = rotation.x * rotation.y + rotation.r * rotation.z + rotation.z * rotation.r + rotation.y * rotation.x;
75 double az = Math.acos(rotx) * 180.0 / Math.PI;
81 /* Given euler rotations in three axes, perform a combined rotation using
84 public void rotate(double x, double y, double z) {
85 AltosQuaternion rot = AltosQuaternion.euler(x, y, z);
86 rotation = rot.multiply(rotation).normalize();
89 /* Clone an existing rotation value */
90 public AltosRotation (AltosRotation old) {
91 this.rotation = new AltosQuaternion(old.rotation);
94 /* Create a new rotation value given an acceleration vector pointing down */
95 public AltosRotation(double x,
98 int pad_orientation) {
99 AltosQuaternion orient = AltosQuaternion.vector(x, y, z).normalize();
100 double sky = (pad_orientation & 1) == 0 ? 1 : -1;
101 AltosQuaternion up = new AltosQuaternion(0, 0, 0, sky);
102 rotation = up.vectors_to_rotation(orient);
105 public AltosRotation() {
106 rotation = new AltosQuaternion();