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_13;
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 /* Given euler rotations in three axes, perform a combined rotation using
56 public void rotate(double x, double y, double z) {
57 AltosQuaternion rot = AltosQuaternion.euler(x, y, z);
58 rotation = rot.multiply(rotation).normalize();
61 /* Clone an existing rotation value */
62 public AltosRotation (AltosRotation old) {
63 this.rotation = new AltosQuaternion(old.rotation);
66 /* Create a new rotation value given an acceleration vector pointing down */
67 public AltosRotation(double x,
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);
77 public AltosRotation() {
78 rotation = new AltosQuaternion();