forked from JonnyOThan/tpksp-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplan_moon_escape.ks
More file actions
55 lines (41 loc) · 2.12 KB
/
plan_moon_escape.ks
File metadata and controls
55 lines (41 loc) · 2.12 KB
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
parameter desired_reentry_pe is 35000.
run once "util/logging".
run once "util/util".
run once "util/orbit_util".
log_message("=== plan_moon_escape ===").
run "remove_all_nodes".
local desired_kerbin_sma to (body:altitude + desired_reentry_pe + 2 * kerbin:radius)/2.
local desired_kerbin_velocity_at_escape to get_orbital_speed_at_altitude(body:altitude, desired_kerbin_sma, kerbin).
local desired_hyperbolic_excess_velocity to desired_kerbin_velocity_at_escape - body:orbit:velocity:orbit:mag.
log_debug("desired kerbin velocity at escape: " + desired_kerbin_velocity_at_escape).
log_debug("desired hev: " + desired_hyperbolic_excess_velocity).
local necessary_orbital_velocity to sqrt(desired_hyperbolic_excess_velocity^2 + 2 * body:mu / (altitude + body:radius)).
local node_dv to necessary_orbital_velocity - velocity:orbit:mag.
log_debug("necessary orbital velocity: " + necessary_orbital_velocity).
log_debug("node dv: " + node_dv).
// TODO: this assumes a circular orbit; should probably use true anomaly instead
local vec_to_ship to ship:position - body:position.
local phase_angle_to_node to mod(360 + get_phase_angle_to_position(body:position + body:orbit:velocity:orbit), 360).
local phase_angle_per_second to 360 / ship:orbit:period.
local time_until_node to phase_angle_to_node / phase_angle_per_second.
local current_speed to velocityat(ship, time:seconds + time_until_node):orbit:mag.
log_debug("phase angle to node: " + phase_angle_to_node).
log_debug("phase angle per second: " + phase_angle_per_second).
local n to node(time:seconds + time_until_node, 0, 0, node_dv).
add n.
function minimize_periapsis {
parameter n.
if (n:orbit:hasnextpatch and n:orbit:nextpatch:body:name="kerbin") {
return -n:orbit:nextpatch:periapsis.
} else {
return -kerbin:soiradius.
}
}
function adjust_periapsis {
parameter n.
return -abs(n:orbit:nextpatch:periapsis - desired_reentry_pe).
}
local velocity_offsets is list(V(0, 0, 0), V(0, 0, 0)).
local time_offsets is list(1, -1).
runpath("util/optimize_node", minimize_periapsis@, n, velocity_offsets, time_offsets).
runpath("util/optimize_node", adjust_periapsis@).