Jump to content

KSP KOS Fly By Wire Script


pizzaboy150

Recommended Posts

Hi All,

So with Chat GPT  launch I though it would be fun to test it on a few scripts to see how it would perform.

I have very basic knowledge of vector math or PID loops but I am a coder so thought it would be fun  to try and write a fly by wire type script for KSP.

The script runs surprisingly well, though it is easy to break particularly if you try and yaw / rudder while it is running.

The code was created in chat GPT but in a pesudo code format so all I did was translate it into KOS code and run it.

There is a lot wrong with it such as exiting cleanly and passing back control to the player again. But as a starting point to send craft in a particular orientation it works pretty well.

It does depend how well the craft fly's but if the craft is stable then it seems to have no problems.  

 

Anyways I have no real use for the code but thought it maybe fun to share for anyone trying to do something similar.

//
// FLY BY WIRE SCRIPT
//

set desiredPitch to 90 - vectorangle(ship:up:forevector, ship:facing:forevector).
set desiredRoll  to 0.0.

set pitch to 0.0. // Pitch value to set the control to
set roll  to 0.0. // Roll  value to set the control to

set speed to 0.25. // Max speed we can set pitch and roll at full value

// Adjustment speeds for pitch
set pitchKp to 0.01.   // Big adjustment speed
set pitchKi to 0.001.  // Medium adjustment speed
set pitchKd to 0.0001. // Fine adjustment speed

// Adjustment speeds for roll
set rollKp to 0.01.     // Big adjustment speed
set rollKi to 0.0001.   // Medium adjustment speed
set rollKd to 0.00001.  // Fine adjustment speed

// Pitch variables
set pitchIntegral to 0.0.
set pitchError to 0.0.
set pitchPreviousError to 0.0.

// Roll variables
set rollIntegral to 0.0.
set rollError to 0.0.
set rollPreviousError to 0.0.

// Just an infiniate loop to get going
set loop to 0.0.
until loop > 0.0 {

    clearScreen.
    print "FLY BY WIRE PROGRAM".
    print "=============================".

    // Get current pitch and roll
    set currentPitch to 90 - vectorangle(ship:up:forevector, ship:facing:forevector).
    set currentRoll  to arctan2(-vdot(ship:facing:starvector, ship:up:forevector), vdot(ship:facing:topvector, ship:up:forevector)).

    // Set desired attitude from pilot input
    if ship:control:pilotpitch > 0 { set desiredPitch to desiredPitch + ship:control:pilotpitch * speed. }
    if ship:control:pilotpitch < 0 { set desiredPitch to desiredPitch + ship:control:pilotpitch * speed. }
    if ship:control:pilotroll  > 0 { set desiredRoll  to desiredRoll  + ship:control:pilotroll  * speed. }
    if ship:control:pilotroll  < 0 { set desiredRoll  to desiredRoll  + ship:control:pilotroll  * speed. }

    // Calculate the difference for all axis
    set pitchError to desiredPitch - currentPitch.
    set rollError  to desiredRoll  - currentRoll.

    // Set the integral and derivative
    set pitchIntegral   to pitchIntegral + pitchError.
    set pitchDerivative to pitchError    - pitchPreviousError.
    set rollIntegral    to rollIntegral  + rollError.
    set rollDerivative  to rollError     - rollPreviousError.
    
    // Calculate control signal
    set pitch to pitchKp * pitchError + pitchKi * pitchIntegral + pitchKd * pitchDerivative.
    set roll  to rollKp  * rollError  + rollKi  * rollIntegral  + rollKd  * rollDerivative.

    // Update controls
    set ship:control:pitch to pitch.
    set ship:control:roll  to roll.

    // Update error for next calculation
    set pitchPreviousError to pitchError.
    set rollPreviousError  to rollError.

    // Debug to see values
    print "DESIRED PITCH  :" + round(desiredPitch, 2).
    print "DESIRED ROLL   :" + round(desiredRoll,  2).
    print "CURRENT PITCH  :" + round(currentPitch, 2).
    print "CURRENT ROLL   :" + round(currentRoll,  2).
    print "PITCH INPUT    :" + round(pitch,        2).
    print "ROLL INPUT     :" + round(roll,         2).

    // Wait for next calculation
    wait 0.02.
}

P.S if you find you are getting a lot of oscillations when using it then play with the adjustment speeds. the current settings seem to work okay for the stock Aeris 3A but other craft may need tweaks. From what I can tell the 3 adjustments speeds limit how fast it adjust the controls. If they are out further then it uses the larger adjustments then gets finer as it get closer to it target pitch or roll.

Have fun!

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...