From a38712c5a8d5da54d7e134e835ec8fa56e5cd59f Mon Sep 17 00:00:00 2001 From: "Casper V. Kristensen" Date: Fri, 8 Feb 2019 23:18:34 +0100 Subject: [PATCH] Better cohesion. --- launch.ks | 24 ++++++++++------------- lib/equations.ks | 5 +++++ lib/node.ks | 2 +- orbital_maneuvers.ks => maneuvers.ks | 29 +++++++++++++++++++++++++++- 4 files changed, 44 insertions(+), 16 deletions(-) rename orbital_maneuvers.ks => maneuvers.ks (53%) diff --git a/launch.ks b/launch.ks index 63c5d13..b25a262 100644 --- a/launch.ks +++ b/launch.ks @@ -6,6 +6,7 @@ run once "lib/rocket". run once "lib/util". run once "lib/vectors". run once "lib/warp". +run once "maneuvers". function launch { @@ -92,7 +93,7 @@ function launch { wait until APOAPSIS > target_orbit_altitude. lock THROTTLE TO 0. - + print "------------------ CIRCULARIZE -------------------". // Don't create maneuver node until we are out of the atmosphere; otherwise the apoapsis' altitude and eta will change due to drag print "Waiting for ship to leave the atmosphere". @@ -110,17 +111,9 @@ function launch { if auto_extend_antennas { extend_antennas(). } - - // Create maneuver node that will circularize the orbit. TODO: create circulize()-function in lib/maneuvers.ks instead - // NOTE: Potential errors in the inclination are not fixed, since we are most likely going to change our orbit, which will make the inclination change cheaper later on. - local node is NODE(TIME:SECONDS + ETA:APOAPSIS, 0, 0, 0). - // Burn magnitude in the prograde direction is the difference between the required velocity to acheive circular orbit at apoapsis altitude and our velocity at apoapsis - local required_velocity is orbital_velocity_from_ap_pe(APOAPSIS, APOAPSIS, target_orbit_altitude). - set node:prograde to required_velocity - velocityat(SHIP, TIME:seconds + ETA:apoapsis):orbit:mag. - - add node. - execute_node(). + // NOTE: Potential errors in the inclination are not fixed since we are most likely going to change our orbit, which will make the inclination change cheaper later on. + circularize_at_apoapsis(). print "------------ LAUNCH SEQUENCE COMPLETE ------------". @@ -129,9 +122,12 @@ function launch { function calculate_launch_azimuth { + // // Calculate the launch azimuth; the compass heading we head for when launching to achieve orbit of desired inclination. - // http://www.orbiterwiki.org/wiki/Launch_Azimuth - // https://www.princeton.edu/~stengel/MAE342Lecture4.pdf + // Based on: + // http://www.orbiterwiki.org/wiki/Launch_Azimuth + // https://www.princeton.edu/~stengel/MAE342Lecture4.pdf + // parameter target_orbit_inclination. parameter target_orbit_altitude. // to compensate for the rotation of the body, we need to know the velocity of the target orbit, which is calculated from its altitude parameter launch_site_latitude. @@ -139,7 +135,7 @@ function calculate_launch_azimuth { local inertial_azimuth is arcsin(cos(target_orbit_inclination) / cos(launch_site_latitude)). // azimuth in inertial space, that is, disregarding the rotation of the body local equatorial_rotational_velocity is (2 * CONSTANT:PI * BODY:radius) / BODY:rotationperiod. - local target_orbit_velocity is orbital_velocity_from_ap_pe(target_orbit_altitude, target_orbit_altitude, target_orbit_altitude). + local target_orbit_velocity is orbital_velocity_circular(target_orbit_altitude). local launch_vector_x_component is target_orbit_velocity * sin(inertial_azimuth) - equatorial_rotational_velocity * cos(launch_site_latitude). local launch_vector_y_component is target_orbit_velocity * cos(inertial_azimuth). diff --git a/lib/equations.ks b/lib/equations.ks index a5c8855..73b58eb 100644 --- a/lib/equations.ks +++ b/lib/equations.ks @@ -52,6 +52,11 @@ function orbital_velocity_from_ap_pe { return orbital_velocity(altitude, semi_major_axis(ap, pe)). } +function orbital_velocity_circular { + parameter altitude. + return orbital_velocity_from_ap_pe(altitude, altitude, altitude). +} + function orbital_eccentricity { // diff --git a/lib/node.ks b/lib/node.ks index 7b6261d..5b819d4 100644 --- a/lib/node.ks +++ b/lib/node.ks @@ -29,7 +29,7 @@ function execute_node { print "=== EXECUTE MANEUVER NODE ===". print "Estimated burn duration: " + ROUND(burn_duration, 1) + "s". - print "Aligning ship with burn vector..". + print "Aligning ship with burn vector". SAS off. lock STEERING to node:deltav. wait until vang(SHIP:facing:vector, node:deltav) < 0.5. diff --git a/orbital_maneuvers.ks b/maneuvers.ks similarity index 53% rename from orbital_maneuvers.ks rename to maneuvers.ks index fe530ed..9775b40 100644 --- a/orbital_maneuvers.ks +++ b/maneuvers.ks @@ -21,4 +21,31 @@ function change_orbit { print argument_of_periapsis. } -change_orbit(). + +function circularize { + // + // Circularize orbit at the given target. + // + parameter target. + parameter eta. // eta to target + + local node is NODE(TIME:SECONDS + eta, 0, 0, 0). + // Prograde burn magnitude is the difference between the required velocity to achieve circular orbit at target's altitude and our actual velocity at the target + local required_velocity is orbital_velocity_circular(target). + set node:prograde to required_velocity - velocityat(SHIP, TIME:seconds + eta):orbit:mag. + + add node. + execute_node(node). +} + +function circularize_at_apoapsis { + print "Circularizing at apoapsis". + return circularize(APOAPSIS, ETA:APOAPSIS). +} + +function circularize_at_periapsis { + print "Circularizing at periapsis". + return circularize(PERIAPSIS, ETA:PERIAPSIS). +} + +circularize_at_periapsis(). \ No newline at end of file