untoldwind / KontrolSystem2

Autopilot scripting system for KSP2
Other
54 stars 14 forks source link

match_inclination() suggests delta_v that makes the maneuver node escape the solar system #94

Closed lsedberg closed 3 months ago

lsedberg commented 1 year ago

To get my vessel to match the inclianation of my target vessel, I made the following script:

let (delta_v, UT) = match_inclination(vessel.orbit, vessel.target.ok_or("No Target")?.orbit)

vessel.maneuver.add_burn_vector(UT, delta_v)

I tested it, and it made an almost perfect maneuver node (off by 0.5 degrees). But since you can correct the inclination from two different points along the orbit (both at the ascending and the descending node), I warped past my original maneuver node, deleted it, and ran the script again. It made a new maneuver node at the other "inclination spot". However, this time it made a maneuver node that pointed the wrong way (and in reverse, in such a way that is impossible to recreate by manually tuning a maneuver node), and had enough delta_v to escape Kerbin, perhaps the entire Kerbol system as well.

Digging in the std files, I went to the public match_inclination function in std/maneuvers.to2, and changed the line from:

-target.orbit_normal.cross(start.relative_position(dn_UT).normalized)

to:

target.orbit_normal.cross(start.relative_position(dn_UT).normalized)

Just removing the negative sign "-" seemed to have done the trick. Now it creates predictable maneuver nodes. However this has not been tested at different orbits, but at my vessel orbit of 180km, and the target orbit at an higher 300km, with ~20deg inclination.

Final function for the std/maneuver:

pub sync fn match_inclination(start : Orbit, target : Orbit) -> (delta_v: Vec3, UT: float) = {
    const an_UT = start.time_of_ascending_node(target, current_time() + 2)
    const dn_UT = start.time_of_descending_node(target, current_time() + 2)

    const v_dir = if(an_UT < dn_UT)         
        target.orbit_normal.cross(start.relative_position(an_UT).normalized)
    else
        target.orbit_normal.cross(start.relative_position(dn_UT).normalized)
    const UT = math::min(an_UT, dn_UT)

    const v = start.orbital_velocity(UT)
    const target_v = v_dir * v.magnitude

    (delta_v: target_v - v, UT: UT)
}

BEFORE "FIX": image

AFTER "FIX": image

untoldwind commented 1 year ago

Should be fixed in 0.4.2.1 release

github-actions[bot] commented 3 months ago

This issue is stale because it has been open for 60 days with no activity.

github-actions[bot] commented 3 months ago

This issue was closed because it has been inactive for 14 days since being marked as stale.