untoldwind / KontrolSystem2

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

premade accent guidance doesn't work #28

Closed lsamman closed 7 months ago

lsamman commented 1 year ago

vehicle goes straight up while running

untoldwind commented 1 year ago

I am sorry, but you have to be a bit more specific than that. The launch script certainly needs a lot of improvements, but launching one of the stock vessels has become something like a daily routine now to see if everything is still working.

There is an issue, that after a reload or revert it might be necessary to manually press the Reboot-Button to reconnect to the vessel correctly, but if it is in that state the script does not do anything including triggering staging.

lsamman commented 1 year ago

not really sure how to add on to this, i am using my own vehicle though, a 2 stage vehicle with no strap-ons (srbs)

untoldwind commented 1 year ago

Can you make the workspace file of the vessel somehow available (the json file in the Workspaces directory of your save).

Also it might be worth taking a look at the log files for errors:

Silvia-Dragoness commented 1 year ago

I just tried using the included "launch rocket" script, with the default apoapsis setting of 100km. The gravity turn is not nearly as aggressive as the ascent path I use when I'm flying by hand (it was going nearly straight up, turning very slowly).

untoldwind commented 1 year ago

The gravity turn is on the todo list, the current launch script is something of a placeholder.

schlosrat commented 1 year ago

FYI, the launch script is fairly consistently putting an SSTO rocket of mine into a 191x41km orbit with gas left in the tank. The gravity turn is doing this odd bobble thing where it pitches east a bit, then back up a bit, then east again. This is a pretty clean rocket with very little exposed for drag.

schlosrat commented 1 year ago

Changing the setting for launch_gt1 enabled me to realize a much better gravity turn, a much shorter circularization burn and a nearly perfect 101x100 orbit!

const launch_gt1 = vessel.main_body.atmosphere_depth * 0.5

I seem to recall that in MJ the end of the gravity turn was configurable by the user and that quite often good values were set a lot lower than the edge of the atmosphere. The correct setting has something to do with the TWR of the rocket.

Perhaps it would be a good idea to elevate launch_gt0 and launch_gt1 to be input parameters?

lsamman commented 1 year ago

sweet ill try it now

lsamman commented 1 year ago

little problem i have no idea how to do that, if thats related to code then im not really going to be able to do it, no experience with any coder other the mm configs

schlosrat commented 1 year ago

Super easy. You can do it in the mod itself using the built-in editor. In the mod's main window click Modules, then scroll down to std::atmo. With std:atmo highlighted, click the edit button. scroll down a bit until you see the line of code like what I showed above except it will be set to "* 0.7" at the end. I find I can't scroll with the mouse wheel, but I can click in the window and use the arrow keys to move down. In the shot below you'll see where I've edited it to be 0.5, change it from 0.7 to 0.5 and then click Save and Reboot. The reboot is essential as only that will recompile the script so it takes effect.

KS2_editor

I find it easier to use VSCode as the mod author has produced a VSIX file that can be used with MS VSCode to give you syntax highlighting, but honestly, any text editor will do. The file you need to edit is called atmo.to2. Don't let the file suffix throw you, it's just a text file so you could edit it in Notepad if you wanted to. If you go to your mod folder ..\BepInEx\plugins\KontrolSystem2\t02\std folder you'll find atmo.to2 there.

schlosrat commented 1 year ago

Alternatively, copy the text below into a file called launch_rocket_gt.to2 (this can be in your to2local folder if you like). This will give you a version of launch_rocket that allows up to three parameters to be specified and will default to the gravity turn parameters I've found useful (with minimal testing - YMMV)

use { Vessel, MODE_AUTOPILOT, MODE_PROGRADE, MODE_STABILITYASSIST } from ksp::vessel
use { trigger_staging } from std::staging
use { sleep, wait_until } from ksp::game
use { acos_deg, max, clamp } from core::math
use { estimate_burn_time, exec_next_node } from std::vac
use { circularize_orbit } from std::maneuvers

pub fn main_flight(vessel: Vessel, target_apoapsis: int = 100000, gt0: float = 0.007, gt1: float = 0.5) -> Result<Unit, string> = {
    atmo_launch(vessel, target_apoapsis, 90, gt0, gt1)?
}

/// Automatically launch a rocket from an atmosphere to a circular orbit.
pub fn atmo_launch(vessel: Vessel, target_apoapsis: float, heading: float, gt0: float, gt1: float) -> Result<Unit, string> = {
    vessel.actions.light = true

    vessel.autopilot.enabled = true
    vessel.autopilot.mode = MODE_AUTOPILOT

    atmo_launch_ascent(vessel, target_apoapsis, heading, gt0, gt1)

    const (delta_v, UT) = circularize_orbit(vessel.orbit)?

    let (burn_time, half_burn_time) = estimate_burn_time(vessel, delta_v.magnitude, 0.5, 1.0)

    vessel.maneuver.add_burn_vector(UT - half_burn_time, delta_v)?

    exec_next_node(vessel)?
}

/// Perform a rocket launch ascent from an atmosphere.
///
/// Note: The rocket will not end up in a stable orbit and most likely crash if no further action
/// is taken.
pub fn atmo_launch_ascent(vessel: Vessel, target_apoapsis: float, heading: float, gt0: float, gt1: float) -> Unit = {
    CONSOLE.print_line("=== Start: atmo_launch_ascent ===")
    const console_row = CONSOLE.cursor_row
    CONSOLE.move_cursor(console_row + 2, 0)

    // Starting/ending height of gravity turn
    const launch_gt0 = vessel.main_body.atmosphere_depth * gt0
    const launch_gt1 = vessel.main_body.atmosphere_depth * gt1

    const throttle_manager = vessel.manage_throttle(fn(deltaT) -> {
        const atmPct = vessel.altitude_sealevel / (vessel.main_body.atmosphere_depth + 1)
        const spd = vessel.surface_velocity.magnitude
        const cutoff = 200 + (400 * max(0, atmPct * 3))

        const throttle = if(spd > cutoff) {
            1.0 - max(0.1, ((spd - cutoff) / cutoff))
        } else {
            const apoPercent = vessel.orbit.apoapsis / target_apoapsis
            const apoCompensation = if(apoPercent > 0.9) (apoPercent - 0.9) * 10 else 0.0
            1.0 - clamp(apoCompensation, 0, 0.5)
        }
        CONSOLE.print_at(console_row + 1, 0, "Target throttle: " + throttle.to_string() )

        throttle
    })

    sleep(1.0)

    CONSOLE.print_line("Launch: Trigger initial stage")
    vessel.staging.next()

    while(vessel.orbit.apoapsis < target_apoapsis) {
        const gtPct = clamp((vessel.altitude_sealevel - launch_gt0) / (launch_gt1 - launch_gt0), 0, 1)
        const pitch = acos_deg(gtPct)

        CONSOLE.print_at(console_row, 0, "Target pitch: " + pitch.to_string())

        vessel.autopilot.target_orientation = vessel.heading_direction(heading, pitch, 0).vector

        if(trigger_staging(vessel)) {
            CONSOLE.print_line("Next stage triggered")
        }

        sleep(0.05)
    }

    throttle_manager.release()

    vessel.autopilot.mode = MODE_PROGRADE

    wait_until(fn() -> vessel.altitude_sealevel > vessel.main_body.atmosphere_depth * 0.9)

    CONSOLE.print_line("=== Done: atmo_launch_ascent ===")
}
untoldwind commented 1 year ago

We could add these as parameters to the function. The values I used were some adhoc decision based on KSP1 that worked for most of my rockets there.

I am somewhat hesitant to put much effort into this right now considering that the thermal systems are not active in the game yet. And overheating was always a problem with the more aggressive gravity turns

... also a have to do a refresher on my math ;)

schlosrat commented 1 year ago

KSP1 had heating during ascent, but what I recall was it always looked much worse than it was. I don't recall ever seeing any parts explode due to heating during ascent. That said, MJ did give the user to option to override defaults and plug in their own GT parameters.

Bringing the GT parameters out to the user with some set of defaults will just give them more flexibility. The defaults could remain 0.007 and 0.7 if you like, but then users could edit those defaults, either in the launch_rocket.to2 script or by clicking on the "3" for configuration on a particular run.

untoldwind commented 1 year ago

Added low- and high-turn parameters to the launch-script in 0.2.2

lsamman commented 1 year ago

Added low- and high-turn parameters to the launch-script in 0.2.2

ah thanks also if possible can we get inclination, also i think mechjeb had a 2d curve you could edit for the accent profile, that would be awesome to have

lsamman commented 1 year ago

Super easy. You can do it in the mod itself using the built-in editor. In the mod's main window click Modules, then scroll down to std::atmo. With std:atmo highlighted, click the edit button. scroll down a bit until you see the line of code like what I showed above except it will be set to "* 0.7" at the end. I find I can't scroll with the mouse wheel, but I can click in the window and use the arrow keys to move down. In the shot below you'll see where I've edited it to be 0.5, change it from 0.7 to 0.5 and then click Save and Reboot. The reboot is essential as only that will recompile the script so it takes effect.

KS2_editor

I find it easier to use VSCode as the mod author has produced a VSIX file that can be used with MS VSCode to give you syntax highlighting, but honestly, any text editor will do. The file you need to edit is called atmo.to2. Don't let the file suffix throw you, it's just a text file so you could edit it in Notepad if you wanted to. If you go to your mod folder ..\BepInEx\plugins\KontrolSystem2\t02\std folder you'll find atmo.to2 there.

tried this, no luck, now the thing is still auto aborting the script and is still staging right after liftoff if turn it back on

untoldwind commented 1 year ago

Eventually I would like to have scripts being able to create their own UI windows/dialogs and adding something of a "Spline-Widget" is certainly a possibility. There is a loot of internal stuff to be handled first though. Especially the issue how to handle window-focus vs. game-input in a reliable way.

As for why the script is aborting: There should be some sort of error message in the console. If not: Please check the logfiles Ksp2.log and BepInEx/LogOutput.log as there might be an underlying problem with the whole system.

lsamman commented 1 year ago

got it ill check in a bit

RobertoBiundo commented 1 year ago

I have also provided a new PR that forces the rocket to dynamically adjust its heading based on a predefined logarithmic curve path. https://github.com/untoldwind/KontrolSystem2/pull/75

You can adjust how aggresive is the log in the code, but generally speaking anything with proper TWR and launch target > 100Km has worked just fine for me.

PyroSA commented 1 year ago

I lost my (local) launch script in a recent update. Should still have it somewhere... In the meantime - I have run in to a few situations where the control does not seem to act on the vehicle. A reboot was normally enough to fix this, but it was not a fault in the script itself.

EDIT: Ah - had a gist of an older version https://gist.github.com/PyroSA/30f3b1c061981e092626fd69ecbbdc94

github-actions[bot] commented 7 months ago

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

github-actions[bot] commented 7 months ago

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