| 1 |
# Copyright (C) 2015 onox |
| 2 |
# |
| 3 |
# This program is free software: you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License as published by |
| 5 |
# the Free Software Foundation; either version 2 of the License, or |
| 6 |
# (at your option) any later version. |
| 7 |
# |
| 8 |
# This program is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU General Public License |
| 14 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
# Sources: |
| 17 |
# |
| 18 |
# [1] https://www.chrobotics.com/library/understanding-euler-angles |
| 19 |
# [2] https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix |
| 20 |
|
| 21 |
var sin = func(a) { math.sin(a * globals.D2R) } |
| 22 |
var cos = func(a) { math.cos(a * globals.D2R) } |
| 23 |
|
| 24 |
var atan = func(a, b) { math.atan2(a, b) * globals.R2D } |
| 25 |
var asin = func(a) { math.asin(a) * globals.R2D } |
| 26 |
var acos = func(a) { math.acos(a) * globals.R2D } |
| 27 |
|
| 28 |
var rotate_from_body_xyz = func (x, y, z, phi, theta, psi) { |
| 29 |
# Rotate point (x, y, z) around the origin from body to inertial |
| 30 |
# frame [1] using a counterclockwise rotation of phi around the x-axis, |
| 31 |
# of theta around the y-axis, and then of psi around the z-axis. |
| 32 |
# |
| 33 |
# This conversion uses the Tait-Bryan Z1Y2X3 matrix [2]. |
| 34 |
|
| 35 |
var cos_psi = cos(psi); |
| 36 |
var cos_theta = cos(theta); |
| 37 |
var cos_phi = cos(phi); |
| 38 |
|
| 39 |
var sin_psi = sin(psi); |
| 40 |
var sin_theta = sin(theta); |
| 41 |
var sin_phi = sin(phi); |
| 42 |
|
| 43 |
var matrix = [ |
| 44 |
[ |
| 45 |
cos_psi*cos_theta, |
| 46 |
sin_psi*cos_theta, |
| 47 |
-sin_theta |
| 48 |
], |
| 49 |
|
| 50 |
[ |
| 51 |
cos_psi*sin_theta*sin_phi - sin_psi*cos_phi, |
| 52 |
sin_psi*sin_theta*sin_phi + cos_psi*cos_phi, |
| 53 |
cos_theta*sin_phi |
| 54 |
], |
| 55 |
|
| 56 |
[ |
| 57 |
cos_psi*sin_theta*cos_phi + sin_psi*sin_phi, |
| 58 |
sin_psi*sin_theta*cos_phi - cos_psi*sin_phi, |
| 59 |
cos_theta*cos_phi |
| 60 |
] |
| 61 |
]; |
| 62 |
|
| 63 |
var x2 = x * matrix[0][0] + y * matrix[1][0] + z * matrix[2][0]; |
| 64 |
var y2 = x * matrix[0][1] + y * matrix[1][1] + z * matrix[2][1]; |
| 65 |
var z2 = x * matrix[0][2] + y * matrix[1][2] + z * matrix[2][2]; |
| 66 |
|
| 67 |
return [x2, y2, z2]; |
| 68 |
}; |
| 69 |
|
| 70 |
var rotate_to_body_zyx = func (x, y, z, phi, theta, psi) { |
| 71 |
# Rotate point (x, y, z) around the origin from the inertial to the |
| 72 |
# body frame [1] using a counterclockwise rotation of psi around the z-axis, |
| 73 |
# of theta around the y-axis, and then of phi around the x-axis. |
| 74 |
# |
| 75 |
# This conversion uses the transposed Tait-Bryan Z1Y2X3 matrix [2]. |
| 76 |
|
| 77 |
var cos_psi = cos(psi); |
| 78 |
var cos_theta = cos(theta); |
| 79 |
var cos_phi = cos(phi); |
| 80 |
|
| 81 |
var sin_psi = sin(psi); |
| 82 |
var sin_theta = sin(theta); |
| 83 |
var sin_phi = sin(phi); |
| 84 |
|
| 85 |
var matrix = [ |
| 86 |
[ |
| 87 |
cos_psi*cos_theta, |
| 88 |
cos_psi*sin_theta*sin_phi - sin_psi*cos_phi, |
| 89 |
cos_psi*sin_theta*cos_phi + sin_psi*sin_phi |
| 90 |
], |
| 91 |
|
| 92 |
[ |
| 93 |
sin_psi*cos_theta, |
| 94 |
sin_psi*sin_theta*sin_phi + cos_psi*cos_phi, |
| 95 |
sin_psi*sin_theta*cos_phi - cos_psi*sin_phi |
| 96 |
], |
| 97 |
|
| 98 |
[ |
| 99 |
-sin_theta, |
| 100 |
cos_theta*sin_phi, |
| 101 |
cos_theta*cos_phi |
| 102 |
] |
| 103 |
]; |
| 104 |
|
| 105 |
var x2 = x * matrix[0][0] + y * matrix[1][0] + z * matrix[2][0]; |
| 106 |
var y2 = x * matrix[0][1] + y * matrix[1][1] + z * matrix[2][1]; |
| 107 |
var z2 = x * matrix[0][2] + y * matrix[1][2] + z * matrix[2][2]; |
| 108 |
|
| 109 |
return [x2, y2, z2]; |
| 110 |
}; |
| 111 |
|
| 112 |
var get_point = func (x, y, z, roll_deg, pitch_deg, heading_deg, point=nil) { |
| 113 |
# Return a tuple of two geo.Coord points (in the inertial frame) of |
| 114 |
# the current aircraft's position with the (x, y, z) offset (in body |
| 115 |
# frame) applied. The first point has the same altitude as the |
| 116 |
# aircraft and can be used for 2D calculations. The second point |
| 117 |
# has the actual new altitude and can be used for 3D calculations. |
| 118 |
|
| 119 |
if (point == nil) { |
| 120 |
var point = geo.aircraft_position(); |
| 121 |
} |
| 122 |
|
| 123 |
(x, y, z) = rotate_from_body_xyz(x, y, z, -roll_deg, pitch_deg, -heading_deg); |
| 124 |
|
| 125 |
# Modify the lateral and longitudinal position |
| 126 |
var distance = math.sqrt(math.pow(x, 2) + math.pow(y, 2)); |
| 127 |
var course = geo.normdeg(atan(y, -x)); |
| 128 |
point.apply_course_distance(course, distance); |
| 129 |
|
| 130 |
var point_2d = geo.Coord.new(point); |
| 131 |
|
| 132 |
# Modify the altitude of the position |
| 133 |
point.set_alt(point.alt() + z); |
| 134 |
|
| 135 |
return [point_2d, point]; |
| 136 |
}; |
| 137 |
|
| 138 |
var get_yaw_pitch_body = func (roll_deg, pitch_deg, object_yaw_deg, object_pitch_deg, yaw_offset=0) { |
| 139 |
# Return a tuple containing the yaw, pitch |
| 140 |
|
| 141 |
var z = math_ext.sin(object_pitch_deg); |
| 142 |
var a = math_ext.cos(object_pitch_deg); |
| 143 |
|
| 144 |
var x = -a * math_ext.cos(object_yaw_deg); |
| 145 |
var y = a * math_ext.sin(object_yaw_deg); |
| 146 |
|
| 147 |
# Convert the position in the inertial frame to the body frame |
| 148 |
(x, y, z) = math_ext.rotate_to_body_zyx(x, y, z, -roll_deg, pitch_deg, 0.0); |
| 149 |
|
| 150 |
var result_distance_2d = math.sqrt(math.pow(x, 2) + math.pow(y, 2)); |
| 151 |
|
| 152 |
# Calculate heading and pitch of object in the body frame |
| 153 |
var result_heading = -math_ext.atan(y, x); |
| 154 |
var result_pitch = math_ext.atan(-z, result_distance_2d); |
| 155 |
|
| 156 |
return [geo.normdeg(result_heading) - yaw_offset, -result_pitch]; |
| 157 |
}; |
| 158 |
|
| 159 |
var get_yaw_pitch_distance_inert = func (position_2d, position, target_position, heading, f=nil) { |
| 160 |
# Return a tuple containing the relative heading, pitch, and distance |
| 161 |
# from the given source to the target position in the inertial |
| 162 |
# frame (world). The relative heading and pitch do not depend on the |
| 163 |
# current roll and pitch angles of the aircraft. |
| 164 |
|
| 165 |
var target_position_alt = target_position.alt(); |
| 166 |
target_position.set_alt(position_2d.alt()); |
| 167 |
|
| 168 |
# Calculate heading in the inertial frame |
| 169 |
var heading_deg = positioned.courseAndDistance(position_2d, target_position)[0] - heading; |
| 170 |
var distance_2d = position_2d.direct_distance_to(target_position); |
| 171 |
|
| 172 |
target_position.set_alt(target_position_alt); |
| 173 |
|
| 174 |
# Calculate pitch and distance in the inertial frame |
| 175 |
if (f == nil) |
| 176 |
f = math_ext.atan; |
| 177 |
var pitch_deg = f(target_position.alt() - position.alt(), distance_2d); |
| 178 |
var distance_m = position.direct_distance_to(target_position); |
| 179 |
|
| 180 |
return [heading_deg, pitch_deg, distance_m]; |
| 181 |
}; |