File size: 9,375 Bytes
5ab75a9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | function [KEP] = Cart2Kep(CART,AnomType,UnitType)
% Cart2Kep - Converts an object's Cartesian elements to Keplerian elements
%
% Syntax: [a,e,i,Omega,w,anom] = Cart2Kep(CART,'AnomType','UnitType');
% Example: [a,e,i,Omega,w,M] = Cart2Kep([r_vec,v_vec],'Mean','Rad');
%
% =========================================================================
%
% Copyright (c) 2023 United States Government as represented by the
% Administrator of the National Aeronautics and Space Administration.
% All Rights Reserved.
%
% =========================================================================
%
% Description:
%
% Converts an object's Cartesian elements to Keplerian elements. The
% state vector must be input in units of km and km/s. The user specifies
% which anomaly angle to be output (true, mean, eccentric) as well as the
% units of the output angles (degrees or radians).
% The function does not handle any special case orbits such as
% equatorial circular (True Longitude of Perigee needed), circular
% inclined (Argument of Latitude needed), or equatorial elliptical (True
% Longitude needed).
%
% =========================================================================
%
% Input:
%
% CART - Cartesian state vector [1x6] or [6x1]
% AnomType - Anomaly angle ('True', 'Mean', or 'Eccentric')
% UnitType - Angle output units ('Deg' or 'Rad')
%
% =========================================================================
%
% Output:
%
% a - Semi-major axis (km)
% e - Eccentricity
% i - Inclination (deg [0, 180] or rad [0, pi])
% Omega - Right ascension of the ascending node (RAAN) (deg [0, 360]
% or rad [0, 2pi])
% w - Argument of perigee (deg [0, 360] or rad [0, 2pi])
% anom - Anomaly angle (true, mean, or eccentric) (deg [0, 360] or
% rad [0, 2pi])
%
% =========================================================================
%
% Initial version: Mar 2013; Latest update: Jun 2023
%
% ----------------- BEGIN CODE -----------------
% Ensure that Cartesian elements are valid
if ~all(isa(CART, 'double')) || length(CART) ~= 6
error('Cart2Kep:InvalidCartesianElements', 'Input is not a valid set of Cartesian elements');
end
r = CART(1:3);
v = CART(4:6);
if all(~v)
error('Cart2Kep:ZeroVelocity', 'Satellite cannot have zero velocity');
end
if norm(r) <= 6378.1370
error('Cart2Kep:InvalidPosition', 'Satellite cannot be inside the Earth');
end
% Earth gravitational constant (EGM-96) [km^3/s^2]
mu = 3.986004418e5;
% Angular momentum vector
h = cross(r,v);
if abs(h) <= 1E-5
error('Cart2Kep:RectilinearOrbit', 'Rectilinear orbit, unable to calculate Keplerian elements');
end
% Unit vector in k-direction
k = [0,0,1];
% Line of nodes vector
n = cross(k,h);
% Magnitude of position, velocity, angular momentum, and line of nodes vectors
norm_r = norm(r); norm_v = norm(v); norm_h = norm(h); norm_n = norm(n);
% Eccentricity: Vector and its norm
E = cross(v,h)/mu - r/norm_r;
e = norm(E);
if abs(1 - e) <= 1E-5
error('Cart2Kep:ParabolicOrbit', 'Parabolic orbit, unable to calculate Keplerian elements');
end
if e > 1
error('Cart2Kep:HyperbolicOrbit', 'Hyperbolic orbit, unable to calculate Keplerian elements');
end
% Specific energy
Energy = 0.5 * norm_v^2 - mu/norm_r;
% Semi-major axis
a = -0.5 * mu / Energy;
% Inclination
i = real(acosd(h(3)/norm_h));
if abs(e) <= 1E-5 && abs(norm_n) <= 1E-5
warning('Cart2Kep:EquatorialCircularOrbit', 'Special case orbit: equatorial circular orbit');
e = 0;
% No ascending node
Omega = 0;
% No perigee
w = 0;
% True longitude
anom = real(acosd(r(1)/norm_r));
% True longitude quadrant check
if r(2) < 0
anom = 360 - anom;
end
elseif abs(e) <= 1E-5
warning('Cart2Kep:InclinedCircularOrbit', 'Special case orbit: inclined circular orbit');
e = 0;
% Right ascension of the ascending node (RAAN)
Omega = real(acosd(n(1)/norm_n));
% RAAN quadrant check
if (n(2) < 0)
Omega = 360 - Omega;
end
% No perigee
w = 0;
% Argument of latitude
anom = real(acosd(dot(n,r)/(norm_n*norm_r)));
% Argument of latitude unit check
if r(3) < 0
anom = 360 - anom;
end
elseif abs(norm_n) <= 1E-5
warning('Cart2Kep:GeneralEquatorialOrbit', 'Special case orbit: equatorial elliptical or hyperbolic orbit');
% No ascending node
Omega = 0;
% True longitude of periapsis
w = real(acosd(E(1)/e));
% True longitude of periapsis quadrant check
if (E(2) < 0)
w = 360 - w;
end
% True anomaly
anom = real(acosd(dot(E,r)/(norm_r*e)));
% True anomaly quadrant check
if (dot(r,v) < 0)
anom = 360 - anom;
end
else
% Right ascension of the ascending node (RAAN)
Omega = real(acosd(n(1)/norm_n));
% RAAN quadrant check
if (n(2) < 0)
Omega = 360 - Omega;
end
% Argument of perigee
w = real(acosd(dot(n,E)/(norm_n*e)));
% Argument of perigee quadrant check
if (E(3) < 0)
w = 360 - w;
end
% True anomaly
anom = real(acosd(dot(E,r)/(norm_r*e)));
% True anomaly quadrant check
if (dot(r,v) < 0)
anom = 360 - anom;
end
end
% Compute anomaly angle
switch lower(AnomType)
case 'true'
% Do nothing, already true
case 'eccentric'
% Eccentric anomaly
anom = (2 * atand(sqrt((1-e)/(1+e))*tand(anom/2)));
% Eccentric anomaly quadrant check
if (anom < 0)
anom = anom + 360;
end
case 'mean'
% Eccentric anomaly
anom = 2 * atand(sqrt((1-e)/(1+e))*tand(anom/2));
% Eccentric anomaly quadrant check
if (anom < 0)
anom = anom + 360;
end
% Mean anomaly
% Since eccentricity is unitless, the calculation with sin ends
% up being in radians, so the rad2deg adjustment is necessary
% here
anom = (anom - rad2deg(e * sind(anom)));
otherwise
error('Cart2Kep:InvalidAnomType', [AnomType ' as AnomType is not supported...']);
end
% Convert output to specified units
switch lower(UnitType)
case 'deg'
% Do nothing, already in degrees
case 'rad'
% Convert output to radians
i = i * pi/180;
Omega = Omega * pi/180;
w = w * pi/180;
anom = anom * pi/180;
otherwise
error('Cart2Kep:InvalidUnitType', [UnitType ' as UnitType is not supported...']);
end
KEP = [a,e,i,Omega,w,anom];
return
% ----------------- END OF CODE ------------------
%
% Please record any changes to the software in the change history
% shown below:
%
% ----------------- CHANGE HISTORY ------------------
% Developer | Date | Description
% ---------------------------------------------------
% D. Plakalovic | Mar - 2013 | Initial Development
% D. Plakalovic | 12-11-2014 | Added functionality to include mean and
% eccentric anomaly as an output option as
% well as an option to choose degrees or
% radians as units of output
% T. Lechtenberg | 07-17-2019 | Added more flexibility to options inputs
% E. White | 06-12-2023 | Added support for special case orbits,
% added check for parabolic orbits, added
% compliant header and footer, added input
% verification
% E. White | 06-30-2023 | Fixed bug involving negative positional
% values, updated documentation, added
% checks for rectilinear and hyperbolic
% orbits
% =========================================================================
%
% Copyright (c) 2023 United States Government as represented by the
% Administrator of the National Aeronautics and Space Administration.
% All Rights Reserved.
%
% =========================================================================
|