2016-05-16 07:56:13 +00:00
|
|
|
###########################################################################
|
|
|
|
# simulation of a faraway orbital target (needs handover to spacecraft-specific
|
|
|
|
# code for close range)
|
2020-06-08 13:07:10 +00:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
#
|
|
|
|
# NOTE! This copyright does *not* cover user models that use these Nasal
|
|
|
|
# services by normal function calls - this is merely considered normal use
|
|
|
|
# of the code, and does *not* fall under the heading of "derived work."
|
|
|
|
#
|
2019-03-11 10:55:35 +00:00
|
|
|
# Thorsten Renk 2016-2019
|
2016-05-16 07:56:13 +00:00
|
|
|
###########################################################################
|
|
|
|
|
2020-06-08 13:07:10 +00:00
|
|
|
|
2016-05-16 07:56:13 +00:00
|
|
|
var orbitalTarget = {
|
|
|
|
new: func(altitude, inclination, node_longitude, anomaly) {
|
|
|
|
var t = { parents: [orbitalTarget] };
|
|
|
|
t.altitude = altitude;
|
|
|
|
t.radius = 20908323.0 * 0.3048 + t.altitude;
|
|
|
|
t.GM = 398759391386476.0;
|
2016-06-02 08:36:17 +00:00
|
|
|
#t.GM = 398600441800000.0;
|
2016-05-16 07:56:13 +00:00
|
|
|
t.period = 2.0 * math.pi * math.sqrt(math.pow(t.radius, 3.0)/ t.GM);
|
|
|
|
t.inclination = inclination;
|
|
|
|
t.inc_rad = t.inclination * math.pi/180.0;
|
|
|
|
t.l_vec = [math.sin(t.inc_rad), 0.0, math.cos(t.inc_rad)];
|
|
|
|
t.node_longitude = node_longitude;
|
|
|
|
t.nl_rad = t.node_longitude * math.pi/180.0;
|
2019-01-14 11:47:29 +00:00
|
|
|
t.initial_nl_rad = t.nl_rad;
|
2016-05-16 07:56:13 +00:00
|
|
|
var l_tmp = t.l_vec[0];
|
2019-03-11 10:55:35 +00:00
|
|
|
t.l_vec[0] = math.sin(t.nl_rad) * l_tmp;
|
|
|
|
t.l_vec[1] = -math.cos(t.nl_rad) * l_tmp;
|
2016-05-16 07:56:13 +00:00
|
|
|
t.anomaly = anomaly;
|
|
|
|
t.anomaly_rad = t.anomaly * math.pi/180.0;
|
2019-01-14 11:47:29 +00:00
|
|
|
t.initial_anomaly_rad = t.anomaly_rad;
|
2016-05-16 07:56:13 +00:00
|
|
|
t.delta_lon = 0.0;
|
2016-06-02 08:36:17 +00:00
|
|
|
t.update_time = 0.1;
|
2016-05-16 07:56:13 +00:00
|
|
|
t.running_flag = 0;
|
2019-01-14 11:47:29 +00:00
|
|
|
t.elapsed_time = 0.0;
|
2019-03-11 10:55:35 +00:00
|
|
|
t.delta_time = 0.0;
|
2019-05-28 09:21:41 +00:00
|
|
|
t.label = "";
|
2019-01-14 11:47:29 +00:00
|
|
|
|
2019-10-06 08:30:07 +00:00
|
|
|
print ("Orbital Period: ", t.period);
|
|
|
|
|
2019-05-28 09:21:41 +00:00
|
|
|
# Coefficients for the J3 altitude variation
|
|
|
|
|
|
|
|
var inc_var = t.inclination/60.0;
|
|
|
|
#print ("inc_var:", inc_var);
|
|
|
|
|
|
|
|
t.coeff1 = (10268. - 0.99579 * (t.altitude / 1000.0)) * inc_var;
|
|
|
|
t.coeff2 = 0.212 * 2.0 * math.pi;
|
|
|
|
|
|
|
|
|
|
|
|
#t.node_drift = -4361.26 * 1./math.pow(t.radius/1000.0 ,2.0) * math.cos(t.inc_rad);
|
|
|
|
|
|
|
|
t.node_drift = -2.16732e+9 /math.pow(t.radius/1000.0, 3.48908) * math.cos(t.inc_rad);
|
|
|
|
|
|
|
|
print ("Drift rate: ", t.node_drift);
|
2016-05-16 07:56:13 +00:00
|
|
|
return t;
|
|
|
|
},
|
|
|
|
|
|
|
|
set_anomaly: func (anomaly) {
|
|
|
|
|
|
|
|
t.anomaly = anomaly;
|
|
|
|
t.anomaly_rad = t.anomaly * math.pi/180.0;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set_delta_lon: func (dl) {
|
|
|
|
t.delta_lon = dl;
|
|
|
|
},
|
|
|
|
|
|
|
|
list: func {
|
|
|
|
|
|
|
|
print("Radius: ", me.radius, " period: ", me.period);
|
|
|
|
print("L_vector: ", me.l_vec[0], " ", me.l_vec[1], " ", me.l_vec[2]);
|
|
|
|
print("L_norm: ", math.sqrt(me.l_vec[0] * me.l_vec[0] + me.l_vec[1] * me.l_vec[1] + me.l_vec[2] * me.l_vec[2]));
|
|
|
|
var pos = me.get_inertial_pos();
|
|
|
|
print("Inertial: ", pos[0], " ", pos[1], " ", pos[2]);
|
|
|
|
print("Rad: ", math.sqrt(pos[0] * pos[0] + pos[1] * pos[1] + pos[2] * pos[2]));
|
|
|
|
var lla = me.get_latlonalt();
|
|
|
|
print("Lat: ", lla[0], " lon: ", lla[1], " alt: ", lla[2]);
|
|
|
|
},
|
|
|
|
|
2016-06-02 08:36:17 +00:00
|
|
|
evolve: func {
|
|
|
|
var dt = getprop("/sim/time/delta-sec");
|
|
|
|
#var speedup = getprop("/sim/speed-up");
|
|
|
|
#dt = dt * speedup;
|
2016-05-16 07:56:13 +00:00
|
|
|
me.anomaly_rad = me.anomaly_rad + dt/me.period * 2.0 * math.pi;
|
|
|
|
if (me.anomaly_rad > 2.0 * math.pi)
|
|
|
|
{
|
|
|
|
me.anomaly_rad = me.anomaly_rad - 2.0 * math.pi;
|
|
|
|
}
|
2016-06-02 08:36:17 +00:00
|
|
|
me.anomaly = me.anomaly_rad * 180.0/math.pi;
|
2016-05-16 07:56:13 +00:00
|
|
|
me.delta_lon = me.delta_lon + dt * 0.00418333333333327;
|
2019-01-14 11:47:29 +00:00
|
|
|
me.node_longitude = me.node_longitude + me.node_drift * dt;
|
|
|
|
me.nl_rad = me.node_longitude * math.pi/180.0;
|
2019-03-11 10:55:35 +00:00
|
|
|
|
|
|
|
me.l_vec = [math.sin(me.inc_rad), 0.0, math.cos(me.inc_rad)];
|
|
|
|
var l_tmp = me.l_vec[0];
|
|
|
|
me.l_vec[0] = math.sin(me.nl_rad) * l_tmp;
|
|
|
|
me.l_vec[1] = -math.cos(me.nl_rad) * l_tmp;
|
|
|
|
|
2019-10-06 08:30:07 +00:00
|
|
|
#print (me.label);
|
|
|
|
|
2019-03-11 10:55:35 +00:00
|
|
|
|
2016-05-16 07:56:13 +00:00
|
|
|
},
|
|
|
|
get_inertial_pos: func {
|
|
|
|
|
2019-01-14 11:47:29 +00:00
|
|
|
return me.compute_inertial_pos(me.anomaly_rad, me.nl_rad);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
get_inertial_pos_at_time: func (time) {
|
|
|
|
|
2019-03-11 10:55:35 +00:00
|
|
|
|
|
|
|
var anomaly_rad = me.initial_anomaly_rad + (time - me.delta_time)/me.period * 2.0 * math.pi;
|
2019-01-14 11:47:29 +00:00
|
|
|
while (anomaly_rad > 2.0 * math.pi)
|
|
|
|
{
|
|
|
|
anomaly_rad = anomaly_rad - 2.0 * math.pi;
|
|
|
|
}
|
|
|
|
|
2019-03-11 10:55:35 +00:00
|
|
|
var nl_rad = me.initial_nl_rad + me.node_drift * (time - me.delta_time) * math.pi/180.0;
|
2019-01-14 11:47:29 +00:00
|
|
|
|
|
|
|
return me.compute_inertial_pos(anomaly_rad, nl_rad);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
get_inertial_speed: func () {
|
|
|
|
|
|
|
|
# obtain via numerical discretization from two points
|
2016-05-16 07:56:13 +00:00
|
|
|
|
2019-01-14 11:47:29 +00:00
|
|
|
var anomaly_rad = me.anomaly_rad;
|
|
|
|
while (anomaly_rad > 2.0 * math.pi)
|
|
|
|
{
|
|
|
|
anomaly_rad = anomaly_rad - 2.0 * math.pi;
|
|
|
|
}
|
2016-05-16 07:56:13 +00:00
|
|
|
|
2019-01-14 11:47:29 +00:00
|
|
|
var pos1 = me.compute_inertial_pos(anomaly_rad, me.nl_rad);
|
2016-05-16 07:56:13 +00:00
|
|
|
|
2019-01-14 11:47:29 +00:00
|
|
|
anomaly_rad = me.anomaly_rad + 0.1/me.period * 2.0 * math.pi;
|
|
|
|
while (anomaly_rad > 2.0 * math.pi)
|
|
|
|
{
|
|
|
|
anomaly_rad = anomaly_rad - 2.0 * math.pi;
|
|
|
|
}
|
2016-06-02 08:36:17 +00:00
|
|
|
|
2019-01-14 11:47:29 +00:00
|
|
|
var pos2 = me.compute_inertial_pos(anomaly_rad, me.nl_rad);
|
2016-06-02 08:36:17 +00:00
|
|
|
|
2019-01-14 11:47:29 +00:00
|
|
|
var vx = (pos2[0] - pos1[0])/0.1;
|
2019-01-18 14:37:35 +00:00
|
|
|
var vy = (pos2[1] - pos1[1])/0.1;
|
|
|
|
var vz = (pos2[2] - pos1[2])/0.1;
|
2019-01-14 11:47:29 +00:00
|
|
|
|
|
|
|
return [vx, vy, vz];
|
2016-06-02 08:36:17 +00:00
|
|
|
},
|
|
|
|
|
2019-01-14 11:47:29 +00:00
|
|
|
get_inertial_speed_at_time: func (time) {
|
|
|
|
|
|
|
|
# obtain via numerical discretization from two points
|
|
|
|
|
2019-03-11 10:55:35 +00:00
|
|
|
var anomaly_rad = me.initial_anomaly_rad + (time- me.delta_time)/me.period * 2.0 * math.pi;
|
2019-01-14 11:47:29 +00:00
|
|
|
while (anomaly_rad > 2.0 * math.pi)
|
|
|
|
{
|
|
|
|
anomaly_rad = anomaly_rad - 2.0 * math.pi;
|
|
|
|
}
|
|
|
|
|
2019-03-11 10:55:35 +00:00
|
|
|
var nl_rad = me.initial_nl_rad + me.node_drift * (time - me.delta_time) * math.pi/180.0;
|
2019-01-14 11:47:29 +00:00
|
|
|
var pos1 = me.compute_inertial_pos(anomaly_rad, nl_rad);
|
|
|
|
|
2019-03-11 10:55:35 +00:00
|
|
|
anomaly_rad = me.initial_anomaly_rad + ((time - me.delta_time) + 0.1)/me.period * 2.0 * math.pi;
|
2016-06-02 08:36:17 +00:00
|
|
|
while (anomaly_rad > 2.0 * math.pi)
|
|
|
|
{
|
|
|
|
anomaly_rad = anomaly_rad - 2.0 * math.pi;
|
|
|
|
}
|
|
|
|
|
2019-03-11 10:55:35 +00:00
|
|
|
nl_rad = me.initial_nl_rad + me.node_drift * ((time - me.delta_time) +0.1) * math.pi/180.0;
|
2019-01-14 11:47:29 +00:00
|
|
|
var pos2 = me.compute_inertial_pos(anomaly_rad, nl_rad);
|
|
|
|
|
|
|
|
var vx = (pos2[0] - pos1[0])/0.1;
|
2019-01-18 14:37:35 +00:00
|
|
|
var vy = (pos2[1] - pos1[1])/0.1;
|
|
|
|
var vz = (pos2[2] - pos1[2])/0.1;
|
2019-01-14 11:47:29 +00:00
|
|
|
|
|
|
|
return [vx, vy, vz];
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
compute_inertial_pos: func (anomaly_rad, nl_rad) {
|
|
|
|
|
2019-05-28 09:21:41 +00:00
|
|
|
# J3 variation around radius
|
|
|
|
|
|
|
|
while (anomaly_rad > 2.0 * math.pi)
|
|
|
|
{
|
|
|
|
anomaly_rad = anomaly_rad - 2.0 * math.pi;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (anomaly_rad < 0.0)
|
|
|
|
{
|
|
|
|
anomaly_rad = anomaly_rad + 2.0 * math.pi;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var r_corr = me.coeff1 * math.exp(- math.pow(((anomaly_rad - math.pi)/ me.coeff2),2.0));
|
|
|
|
|
2019-10-06 08:30:07 +00:00
|
|
|
#r_corr = 0.0;
|
2019-05-28 09:21:41 +00:00
|
|
|
#print (r_corr);
|
|
|
|
|
|
|
|
|
2016-06-02 08:36:17 +00:00
|
|
|
# movement around equatorial orbit
|
2019-05-28 09:21:41 +00:00
|
|
|
var x = (me.radius + r_corr) * math.cos(anomaly_rad);
|
|
|
|
var y = (me.radius + r_corr) * math.sin(anomaly_rad);
|
2016-06-02 08:36:17 +00:00
|
|
|
var z = 0;
|
|
|
|
|
|
|
|
# tilt with inclination
|
|
|
|
z = y * math.sin(me.inc_rad);
|
|
|
|
y = y * math.cos(me.inc_rad);
|
|
|
|
|
2019-01-14 11:47:29 +00:00
|
|
|
|
2016-06-02 08:36:17 +00:00
|
|
|
# rotate with node longitude
|
|
|
|
|
2019-01-14 11:47:29 +00:00
|
|
|
var xp = x * math.cos(nl_rad) - y * math.sin(nl_rad);
|
|
|
|
var yp = x * math.sin(nl_rad) + y * math.cos(nl_rad);
|
|
|
|
|
|
|
|
# this is a good bit of trickery to capture leading J3 dynamics
|
|
|
|
|
|
|
|
var corr_200 = -2.6e-5 * me.inclination + 1.00321;
|
|
|
|
|
|
|
|
var corr = corr_200 * (1.0 + (me.altitude/1000.0-200.0) * 6e-7);
|
|
|
|
|
|
|
|
corr = 1.0 + (0.64 * (corr -1.0));
|
|
|
|
#print ("Corr200 is now:", corr_200);
|
|
|
|
#print ("Corr is now:", corr);
|
|
|
|
#print ("Altitude: ", me.altitude);
|
2019-10-06 08:30:07 +00:00
|
|
|
|
|
|
|
var radius_orig = math.sqrt(xp * xp + yp * yp + z* z);
|
|
|
|
|
|
|
|
|
2019-01-14 11:47:29 +00:00
|
|
|
z /= corr;
|
2016-05-16 07:56:13 +00:00
|
|
|
|
2019-10-06 08:30:07 +00:00
|
|
|
var radius_corr = math.sqrt(xp * xp + yp * yp + z* z);
|
|
|
|
|
|
|
|
xp *= radius_orig/radius_corr;
|
|
|
|
yp *= radius_orig/radius_corr;
|
|
|
|
z *= radius_orig/radius_corr;
|
|
|
|
|
2016-05-16 07:56:13 +00:00
|
|
|
return [xp, yp, z];
|
2019-01-14 11:47:29 +00:00
|
|
|
|
2016-05-16 07:56:13 +00:00
|
|
|
},
|
2019-01-14 11:47:29 +00:00
|
|
|
|
2016-05-16 07:56:13 +00:00
|
|
|
get_latlonalt: func {
|
|
|
|
|
|
|
|
var coordinates = geo.Coord.new();
|
|
|
|
var inertial_pos = me.get_inertial_pos();
|
|
|
|
coordinates.set_xyz(inertial_pos[0], inertial_pos[1], inertial_pos[2]);
|
|
|
|
coordinates.set_lon(coordinates.lon() - me.delta_lon);
|
|
|
|
|
|
|
|
return [coordinates.lat(), coordinates.lon(), coordinates.alt()];
|
|
|
|
},
|
2016-06-02 08:36:17 +00:00
|
|
|
|
2016-05-16 07:56:13 +00:00
|
|
|
start: func {
|
|
|
|
if (me.running_flag == 1) {return;}
|
|
|
|
me.running_flag = 1;
|
|
|
|
me.run();
|
2019-01-14 11:47:29 +00:00
|
|
|
|
2016-05-16 07:56:13 +00:00
|
|
|
},
|
|
|
|
stop: func {
|
|
|
|
me.running_flag = 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
run: func {
|
|
|
|
me.evolve (me.update_time);
|
|
|
|
if (me.running_flag == 1)
|
2016-06-02 08:36:17 +00:00
|
|
|
{settimer(func me.run(), 0);}
|
2016-05-16 07:56:13 +00:00
|
|
|
},
|
|
|
|
|
2019-10-06 08:30:07 +00:00
|
|
|
|
|
|
|
test_suite: func {
|
|
|
|
|
|
|
|
var time = 0;
|
|
|
|
var radius = 0;
|
|
|
|
var pos = [];
|
|
|
|
|
|
|
|
for (var i = 0; i< 300; i=i+1)
|
|
|
|
{
|
|
|
|
time = i * 60;
|
|
|
|
pos = me.get_inertial_pos_at_time(time);
|
|
|
|
|
|
|
|
radius = math.sqrt(pos[0] * pos[0] + pos[1] * pos[1] + pos[2] * pos[2]);
|
|
|
|
|
|
|
|
print (time, " ", radius);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2016-05-16 07:56:13 +00:00
|
|
|
};
|