untoldwind / KontrolSystem2

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

Documentation: AutopilotModeConstants.Navigation vs AutopilotModeConstants.Autopilot #90

Closed plonk-75 closed 1 year ago

plonk-75 commented 1 year ago

While experimenting, I noticed a useful difference in the usage of AutopilotModeConstants.Navigation and AutopilotModeConstants.Autopilot. If the AP is in Autopilot-mode it tries to align the vessel to the Vec3 vessel.autopilot.target_orientation without roll information, whereas in Navigation-mode it aligns it to the Direction vessel.autopilot.lock_direction, roll included. This can be very useful and convenient.

untoldwind commented 1 year ago

Can you provide a script to demonstrate that? As far as I can tell lock_direction is reset on each game update, i.e. it has to be constantly overwritten for the SAS to "notice". And in that case I do not see much difference between AutopilotMode.Autopilot and AutopilotMode.StabilityAssist.

plonk-75 commented 1 year ago

Yes, I can. BUT... Attached you find a script. It is part of a port of my old kOS launch script which I've used frequently. For brevity, I've stripped it down as much as I could, without loosing the roll control. Perplexingly, I was not able to convince the KSP2 AP/SAS to not freak out when setting up some simple test script. I've tried for several hours, without any success. It does, however, work in this script. This is weird. Also weird: The script explicitly enables the AP. This only works once, while on the launchpad. If interrupted in flight, and restarted no control is taken.

You're correct regarding that lock_direction has to be constantly overwritten. In the script, it is placed in a loop, having a single yield() at the end:

    while(vessel.orbit.apoapsis.value < burnEnd) {
        safeStage(vessel, tm)
        let steerToDir = vessel.heading_direction(launchAzimuth, ppf(), 180)  // 180° roll
        vessel.autopilot.lock_direction = steerToDir   // lock_direction seems to work with AutopilotMode.Navigation only 
        yield()
    }

I've tried using SteeringControl/ler via std::control::steering::control_steering but KSP2 always did a CTD when it's direction has been changed from within the loop. Impatience then made me look for other ways, and at first lock_directionseemed to be a working solution.

AP_test.zip

To run the script, place a rocket on a pad, pointy end up, and start the script. It correctly controls roll, but now I'm confused why it does that at all.

I conclude that it's probably for the better to not use SAS for anything than what can be set using the UI buttons.

Case closed, apologies for the inconvenience.

untoldwind commented 1 year ago

Let me re-open this for a moment, because a CTD peeks my interest ;)

I replaced the ascent with this:

pub fn ascent(vessel: Vessel, launchAzimuth: float, tpf: fn(float) -> float, ppf: fn() -> float) -> bool = {
    const burnEnd = vessel.main_body.atmosphere_depth
    const tm = vessel.manage_throttle(tpf)
    const steering_control = control_steering(vessel)

    vessel.autopilot.enabled = false
    while(vessel.orbit.apoapsis.value < burnEnd) {
        safeStage(vessel, tm)
        let steerToDir = vessel.heading_direction(launchAzimuth, ppf(), 180) // 180° roll
        steering_control.set_direction(steerToDir)
        sleep(0.2)
    }
    steering_control.release()
    tm.release()
    false
}

(with use { control_steering } from std::control::steering) And tested with the stock K1 ... I did not encounter any issues with that.

It would be nice if you could recheck if there is some wired timing issue I can not reproduce on my machine

plonk-75 commented 1 year ago

Nope. It is rather a weird timing issue I cannot reproduce on my machine! Joking aside, this is embarrassing. It is now working as I had hoped in the first place. And pretty much the same changes you made were still in comments in the right place in the original launch script. I've definitely tried it, but there was probably enough of my "creativity" in and around the loop to result in a CTD. I removed most of the creativity as soon as the KSP autopilot "worked", so now I can't reproduce it again. Even downdated all plugins.

Great! Now it works and I wish it didn't. And look like a complete idiot. But hey, at least I didn't file a bug, just an addition to the docs, which might still be a valid point despite all the trouble. Maybe with a hic sunt draconis warning or something. Have you tried changing the autopilot mode in my test script? Maybe there's even more that can't be reproduced. :/

untoldwind commented 1 year ago

Maybe you have forgotten to add a yield or sleep in the loop? The code generator add some hidden code for these cases to force a yield if a loop is running too long. It might be that this requires some fine-tuning.

But I close this for now as it is really hard to do this kind of thing that this early stage in the game.