diff --git a/launch.ks b/launch.ks index 901d72b..6f49322 100755 --- a/launch.ks +++ b/launch.ks @@ -94,8 +94,9 @@ function launch { // 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 orbit at apoapsis altitude and our velocity at apoapsis - set node:PROGRADE to orbital_velocity(APOAPSIS) - VELOCITYAT(SHIP, TIME:SECONDS + ETA:APOAPSIS):ORBIT:MAG. + // 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, target_orbit_altitude, target_orbit_altitude). + set node:PROGRADE to required_velocity - VELOCITYAT(SHIP, TIME:SECONDS + ETA:APOAPSIS):ORBIT:MAG. add node. execute_node(). @@ -117,7 +118,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(target_orbit_altitude). // velocity of target orbit + local target_orbit_velocity is orbital_velocity_from_ap_pe(APOAPSIS, target_orbit_altitude, 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 ea846cf..563bc4e 100644 --- a/lib/equations.ks +++ b/lib/equations.ks @@ -1,10 +1,66 @@ @LAZYGLOBAL OFF. -function orbital_velocity { - // Calculate the required velocity to acheive orbit at the provided altitude. - // https://en.wikipedia.org/wiki/Orbital_speed#Mean_orbital_speed - // http://www.orbiterwiki.org/wiki/Front_Cover_Equations - parameter altitude. + +function semi_major_axis { + // + // Semi-major axis + // Based on: + // https://en.wikipedia.org/wiki/Semi-major_and_semi-minor_axes + // http://www.orbiterwiki.org/wiki/Front_Cover_Equations + // + parameter apoapsis is ORBIT:APOAPSIS. + parameter periapsis is ORBIT:PERIAPSIS. - return SQRT(BODY:MU / (BODY:RADIUS + altitude)). // BODY:MU = CONSTANT:G * BODY:MASS + return (apoapsis + periapsis + 2*BODY:RADIUS) / 2. // Add 2x body radius because KSP measures apoapsis/periapsis from the surface +} + + +function orbital_period { + // + // The orbital period. + // Note that for all ellipses with a given semi-major axis, the orbital period is the same, disregarding their eccentricity. + // Based on: + // https://en.wikipedia.org/wiki/Semi-major_and_semi-minor_axes#Astronomy + // http://www.orbiterwiki.org/wiki/Front_Cover_Equations + // + parameter semi_major_axis is semi_major_axis(). + + return 2 * CONSTANT:PI * SQRT(semi_major_axis^3 / BODY:MU). +} + +function orbital_period_from_ap_pe { + parameter apoapsis, periapsis. + return 2 * CONSTANT:PI * SQRT(semi_major_axis(apoapsis, periapsis)^3 / BODY:MU). +} + + +function orbital_velocity { + // + // The required velocity to maintain orbit at provided altitude. + // Based on: + // https://en.wikipedia.org/wiki/Orbital_speed#Precise_orbital_speed + // http://www.orbiterwiki.org/wiki/Front_Cover_Equations + // https://en.wikipedia.org/wiki/Vis-viva_equation + parameter altitude. + parameter semi_major_axis is semi_major_axis(). + + return SQRT(BODY:MU * ((2 / (altitude+BODY:RADIUS)) - (1 / semi_major_axis))). // add body radius because KSP measures altitude from the surface +} + +function orbital_velocity_from_ap_pe { + parameter altitude, apoapsis, periapsis. + return orbital_velocity(altitude, semi_major_axis(apoapsis, periapsis)). +} + + +function orbital_eccentricity { + // + // The orbital eccentricity, i.e. the ratio by which the orbit deviates from a perfect circle, where e=0 is circular. + // Based on: + // https://en.wikipedia.org/wiki/Orbital_eccentricity#Calculation + // http://www.orbiterwiki.org/wiki/Front_Cover_Equations + parameter apoapsis is ORBIT:APOAPSIS. + parameter periapsis is ORBIT:PERIAPSIS. + + return (apoapsis - periapsis) / (apoapsis + periapsis). } diff --git a/lib/node.ks b/lib/node.ks index a343df3..954a838 100755 --- a/lib/node.ks +++ b/lib/node.ks @@ -61,8 +61,3 @@ function execute_node { wait 1. remove node. } - - -function change_slash_set_orbit { - parameter what. -} diff --git a/orbital_maneuvers.ks b/orbital_maneuvers.ks new file mode 100755 index 0000000..7a43ab1 --- /dev/null +++ b/orbital_maneuvers.ks @@ -0,0 +1,24 @@ +@LAZYGLOBAL OFF. + +run once "lib/equations". +run once "lib/node". + + +function change_orbit { + // + // Change current orbit. Set parameter to -1 to maintain the current value + // + parameter inclination is ORBIT:INCLINATION. // vertical tilt of the orbit with respect to the equator + parameter eccentricity is ORBIT:ECCENTRICITY. // how circular the orbit is (e=0 circular) + parameter periapsis is ORBIT:PERIAPSIS. // lowest point in the orbit + parameter LAN is 0. // longitude of ascending node; the longitude where the orbit crosses the equator northwards + parameter argument_of_periapsis is ORBIT:ARGUMENTOFPERIAPSIS. // the angle from the ascending node to the periapsis, measured in the direction of motion + + print inclination. + print eccentricity. + print periapsis. + print LAN. + print argument_of_periapsis. +} + +change_orbit().