Bluethunder198

Kos script Project

Recommended Posts

Hi to all.

In this Topic, I like to go Create scripts, together whit you all.

From simple to complex scripts.

"1 launche a rocket,  to drive a rover,  And yes,  if we script it it is possible that we do it whit 2  or 10 objects at the same time" 

Its not that I now a lot about the scripting, but when we do it al to gether we now a lot more. 

So script along whit me, and how join the topic, to have fun whit the Kos system.

 

Here is my GitHub for the file's.

off scripts and crafts I use.

If you also have  a GitHub  to share the file's  let it now. 

WE set it all here so we can have fun, to share it..

Edited by Bluethunder198
update, for the file's linked on the topic.

Share this post


Link to post
Share on other sites

For now I'm working on a career script start,  Only whit Kos Mod. 

1. Save rocket to launch.

2. automatic science gathering.

3. save orbit launche and landing. 

Als whit script save and craft save.

 

Share this post


Link to post
Share on other sites

I don't know how to work with kOS yet, but i would love to learn along, since im planning to make kOS scripts too for my launch vehicles!

Share this post


Link to post
Share on other sites
6 minutes ago, NSEP said:

I don't know how to work with kOS yet, but i would love to learn along, since im planning to make kOS scripts too for my launch vehicles!

That just also the thing whit me,  just working on a nice script on the moment,  for science collecting.

Edited by Bluethunder198

Share this post


Link to post
Share on other sites

Just made a update  whit my GitHub  file share.

the  first 4 mission to get to the start off Kos use , I have loadup there.

So have fun whit it,    if you get a better result on the science gettering,  just post it.

Than we can update the craft.. 

 

Share this post


Link to post
Share on other sites
On 1/20/2018 at 5:50 PM, Bluethunder198 said:

Here is my GitHub for the file's.

off scripts and crafts I use.

If you also have  a GitHub  to share the file's  let it now.

I cannot see you github account to look at the launch vehicle.

Can you provide this so we can collaborate?

Share this post


Link to post
Share on other sites
7 hours ago, Space_Kadet said:

I cannot see you github account to look at the launch vehicle.

Can you provide this so we can collaborate?

I will do,  just need to see what did happend whit it...

don't now if this working fine??

on the wiki tab I have the thing how I did do it.

https://github.com/Kos-script-Project/Kos-script-Project-by-bluethunder198

https://github.com/Kos-script-Project/Kos-script-Project-by-bluethunder198/wiki

 

 

Edited by Bluethunder198

Share this post


Link to post
Share on other sites

Right now working on a issue whit the  stage. part when fuel is running out.

Program  run, but it stopping to soon,  before stage. 2e time.

Share this post


Link to post
Share on other sites

Awesome, I got your github now.

just took a look at what you have.

What mods might I need to load your craft files?

 

Also if you don't mind, if you share your .ks I can look at it and hopefully help with the staging issue.

we can learn from each other :)

Share this post


Link to post
Share on other sites
LOCK throttle to 1.
LOCK steering to up + R( 0, 0,180).
wait 1.
stage.
//Staging
WHEN MAXTHRUST = 0 THEN
{
 STAGE.
 SET staged to TRUE.
}
wait until ALTITUDE > 3000.
lock steering to R(0,80,180).
 
 
this I have so far, but if I set it a 2e time. 
it stage. all in 1 time.

Share this post


Link to post
Share on other sites

What .craft file should I be trying to write a script for?

   

sciencemis-1, 2, 3 or 4?

 

None of your craft files have a kOS computer...

Edited by Space_Kadet

Share this post


Link to post
Share on other sites
10 hours ago, Space_Kadet said:

What .craft file should I be trying to write a script for?

   

sciencemis-1, 2, 3 or 4?

 

None of your craft files have a kOS computer...

That's right, those 4 are the  4 mission to get to de KOS-part. in career mode.

to research this.

  1. start.
  2. engineering 101.
  3. survivabillity.
  4. basic science.
  5. Fight control.
  6. basic rocketry.

this about 4 misions, if it going right. All fights are whit SaS, and RCS on.

 

I'm now working on the kos. scripting to do more advanced thing's.

there will be some mods  come whit it.

I will zip them together as mudpack.

later on.

the stage problem is still active, if I have more than 1 stage, whit fuel tanks.

I now there was a part , but it whit  the old kos.

"if ship:liquedfuel < 0.1 {than stage.}  "

but I think I have a idee, 

"WHEN MAXTHRUST = 0 THEN < this say if there no trust, then.....
{
 STAGE.  <  the action it must do.
 SET staged to TRUE.   < this the one I think that staged all the stage's. ( I mean it don't call out just the one stage.)
} "

there must something else there or meby nothing.

Share this post


Link to post
Share on other sites
12 hours ago, Bluethunder198 said:

That's right, those 4 are the  4 mission to get to de KOS-part. in career mode.

to research this.

  1. start.
  2. engineering 101.
  3. survivabillity.
  4. basic science.
  5. Fight control.
  6. basic rocketry.

this about 4 misions, if it going right. All fights are whit SaS, and RCS on.

 

I'm now working on the kos. scripting to do more advanced thing's.

there will be some mods  come whit it.

I will zip them together as mudpack.

later on.

the stage problem is still active, if I have more than 1 stage, whit fuel tanks.

I now there was a part , but it whit  the old kos.

"if ship:liquedfuel < 0.1 {than stage.}  "

but I think I have a idee, 

"WHEN MAXTHRUST = 0 THEN < this say if there no trust, then.....
{
 STAGE.  <  the action it must do.
 SET staged to TRUE.   < this the one I think that staged all the stage's. ( I mean it don't call out just the one stage.)
} "

there must something else there or meby nothing.

13

I am not really understanding what you are trying to say here.

 

are you opposed to using liquid fuel engines? all i see in your .craft files are SRB's.

Share this post


Link to post
Share on other sites
CLEARSCREEN.
//
LOCK throttle to 1.
SET mySteering to STEERING.                  // Setting steering to a variable
LOCK mySteering to UP + R( 0, 0,180).
WAIT 1.
STAGE.
 
WHEN MAXTHRUST = 0 THEN {
    STAGE.
    PRINT "Staging" AT (0,1). // Set to whatever line and collum you like
    //SET staged to TRUE.  // What are you trying to do with setting the stage to true?
}
 
WAIT UNTIL SHIP:ALTITUDE >= 3000. {
    LOCK mySteering to UP + R(0,80,180).
}

Share this post


Link to post
Share on other sites

You may want to put preserve command inside when block, or return true, so that event is triggered again each time your craft runs out of fuel. Like this:

Quote

when SHIP:MAXTHRUST = 0 then {
    STAGE.
    print "Staged".
    DynamicDisplay("Staged                 "). // custom function to write some data on screen. string inside bracket is used as parameter that is printed
    preserve.
    // return true. // same functionality as preserve keyword
}.

I'm still learning kOS, despite using it for some time now. Mostly because of limited time for playing KSP and long time passed between two playing sessions. Same as you, I started with some small simple scripts that could possibly be used in more complex scripts and crafts. I can't say that it is most efficient way to do it, it is more intendend to be small tutorial/reminder how to use kOS commands, functions and libraries than ultimate solution.

Another thing in mind, it is designed to be used with large variety of rockets, so it does not fit perfectly for ascent trajectory of each craft, but it can be easy to adjust. That being said, I found to be to lazy to make adjustment for each craft as it does job well most of times and I can made some small adjustments in flight if something goes wrong.

I tend to break code in functions, so it can be more easy copied and re-used elsewhere when you need it. At first, I used to run launching script from terminal using parameters to set desired heading, apoapsis and time before reaching apoapsis to start final burn to orbit. Most recent version include small GUI for such purpose, so it is even more easy to use.

 

Spoiler

// Arguments for programm must be on top
parameter MyHeading is 90. // initial heading for orbit - influence starting inclination
parameter MyApoapsis is 120000. // Desired APOAPSIS of orbit
parameter ApETA is 27. // time before reaching Apoapsis when circularization burn start
clearscreen.
// load the functions I'm using:
//run once lib_PID. // "once" alows to execute libraray only one time
runoncepath("lib_navball").

// Declaration of PIDs for pitching and velocity
// Velocity change can't work with Solid Fuel Boosters

                            // Kp, Ki, Kd, min, max control  range vals. // Kp - most influence on initial overshoot value
set VelGroundPID to PIDLOOP( 0.15, 0.05, 0.1, 0.0, 1.0 ). // Throttle/Vertical velocity PID controller - some overshoot

// initial info on screen
print "Rocket Launch kOS autopilot". // Row 0.
print "--------------------------------------------".
print "Launching parameters:".
print "Heading =  "+MyHeading.
print "Apoapsis = "+MyApoapsis.
print "                                            ".
print "     Stage once to start kOS autopilot      ".
print "     Press CTRL + C to terminate program    ".
print "--------------------------------------------".
print "Autopilot state:                            ". // Row 9.
print "Velocity:        (m/s)                      ". // Row 10.
print "Altitude:        (m)                        ". // Row 11.
print "                                            ". // Row 12.


// function that will display dynamic data on terminal
function DynamicDisplay{
  parameter output. // dynamic data - mandatory parameter
  parameter startCol is 17.
  parameter startRow is 9. // define where the block of text should be positioned
 
  print MyHeading+"                        " at (10,startRow-6).
  print MyApoapsis+"                       " at (10,startRow-5).

  print output at (startCol,startRow).
  print round(SHIP:AIRSPEED,2) + " "at (10,startRow+1).
  print round(SHIP:ALTITUDE,2) at (10,startRow+2).
}.

function AltitudePitch{
// Define pitching curve up to 10 km
// Goal is 45 degree at 10 km
    local Pitching is 83.
        if (SHIP:ALTITUDE > 2000) or (SHIP:AIRSPEED > 120){
        set Pitching to 85.//89.
        
    }.
    if (SHIP:ALTITUDE > 2000) or (SHIP:AIRSPEED > 100){
        set Pitching to 80.//75.
    }.
    if (SHIP:ALTITUDE > 3000){
        set Pitching to 77.//72.
    }.
    if (SHIP:ALTITUDE > 4000){
        set Pitching to 74.//70.
    }.
    if (SHIP:ALTITUDE > 5000){
        set Pitching to 68.
    }.
    if (SHIP:ALTITUDE > 6000){
        set Pitching to 65.
    }.
    if (SHIP:ALTITUDE > 7000){
        set Pitching to 60.
    }.
    if (SHIP:ALTITUDE > 8000){
        set Pitching to 60.
    }.
    if (SHIP:ALTITUDE > 15000){
        set Pitching to 55.
    }.
    if (SHIP:ALTITUDE > 20000){
        set Pitching to 50.
    }.
    if (SHIP:ALTITUDE > 25000){
        set Pitching to 45.
    }.
    if (SHIP:ALTITUDE > 30000){
        set Pitching to 40.
    }.
    if (SHIP:ALTITUDE > 35000){
        set Pitching to 35.
    }.
    if (SHIP:ALTITUDE > 40000){
        set Pitching to 30.
    }.
    if (SHIP:ALTITUDE > 45000){
        set Pitching to 25.
    }.
    if (SHIP:ALTITUDE > 50000){
        set Pitching to 20.
    }.
    if (SHIP:ALTITUDE > 55000){
        set Pitching to 15.
    }.
    if (SHIP:ALTITUDE > 60000){
        set Pitching to 10.
    }.
    if (SHIP:ALTITUDE > 65000){
        set Pitching to 10.
    }.
    DynamicDisplay("Turning to: "+Pitching).
    return Pitching.
}.

// GUI creation
set Rocket_GUI to GUI(320,400). // (width,height)
set Rocket_GUI:x to 515. // set GUI window position to X
set Rocket_GUI:y to 60. // set GUI window position to Y
// Elements of GUI - title and output status from various functions
set TitleLabel to Rocket_GUI:ADDLABEL("GUI for "+SHIP:NAME).
set OutputLabel to Rocket_GUI:ADDLABEL("Autopilot output").

// New vertical box for velocity and heading
set VerticalBox01 to Rocket_GUI:ADDVBOX.
set HorisontalBox01 to VerticalBox01:ADDHBOX.
// ---
set HeadingInputBox to VerticalBox01:ADDHBOX.
set HeadingLabel to HeadingInputBox:ADDLABEL("Heading:").
set HeadingData to HeadingInputBox:ADDTEXTFIELD(MyHeading:TOSTRING).
set HeadingButton to HeadingInputBox:ADDBUTTON("Set").
set HeadingButton:STYLE:ALIGN to "RIGHT".

set ApoapsisInputBox to VerticalBox01:ADDHBOX.
set ApoapsisLabel to ApoapsisInputBox:ADDLABEL("Apoapsis:").
set ApoapsisData to ApoapsisInputBox:ADDTEXTFIELD(MyApoapsis:TOSTRING).
set ApoapsisButton to ApoapsisInputBox:ADDBUTTON("Set").
set ApoapsisButton:STYLE:ALIGN to "RIGHT".

set BurnETAInputBox to VerticalBox01:ADDHBOX.
set BurnETALabel to BurnETAInputBox:ADDLABEL("BurnETA:").
set BurnETAData to BurnETAInputBox:ADDTEXTFIELD(ApETA:TOSTRING).
set BurnETAButton to BurnETAInputBox:ADDBUTTON("Set").
set BurnETAButton:STYLE:ALIGN to "RIGHT".

// --
set CloseButton to Rocket_GUI:ADDBUTTON("Close").
// -- end of GUI creation

ON HeadingData:CONFIRMED {
    NewHeading().
    preserve.
}.
set HeadingButton:ONCLICK to NewHeading@.


ON ApoapsisData:CONFIRMED {
    NewApoapsis().
    preserve.
}.
set ApoapsisButton:ONCLICK to NewApoapsis@.


ON BurnETAData:CONFIRMED {
    NewETA().
    preserve.
}.
set BurnETAButton:ONCLICK to NewETA@.


function NewHeading{
    parameter FakeStr is "".
    if HeadingData:TEXT:TONUMBER(-9999) <> -9999 {
        set MyHeading to HeadingData:TEXT:TONUMBER(-9999).
    } else {
        set HeadingData:TEXT to MyHeading:TOSTRING.
    }.
    DynamicDisplay("Heading changed              ").
}.

function NewApoapsis{
    parameter FakeStr is "".
    if ApoapsisData:TEXT:TONUMBER(-9999) <> -9999 {
        set myApoapsis to ApoapsisData:TEXT:TONUMBER(-9999).
    } else {
        set ApoapsisData:TEXT to myApoapsis:TOSTRING.
    }.
    DynamicDisplay("Apoapsis changed              ").
}.

function NewETA{
    parameter FakeStr is "".
    if BurnETAData:TEXT:TONUMBER(-9999) <> -9999 {
        set ApETA to BurnETAData:TEXT:TONUMBER(-9999).
    } else {
        set BurnETAData:TEXT to ApETA:TOSTRING.
    }.
    DynamicDisplay("Burn ETA changed              ").
}.

SET CloseButton:ONCLICK to {
    Rocket_GUI:hide().
}.

// showing GUI on demand by pressing action group
on AG4 {
    Rocket_GUI:show().
    preserve. // ensurance that AG is triggered again on demand
}.


SAS on.
// ensurance that there will not be conflict with stock autopilot and we can still use "magic" rotation wheels
SET SASMODE TO "STABILITYASSIST".

wait until SHIP:MAXTHRUST > 0. // waiting idle until staged once for launch
// program will not continue down until staged
SAS off.
DynamicDisplay("Launch started").

// staging whenever ship is out of fuel
// "WHEN" command is trigger or event. Command inside brackets will run when condition is met
// "PRESERVE." command ensure that event will be triggered again when craft run out of fuel again
// kOS CPU is not aware of this until it reached for first time
when SHIP:MAXTHRUST = 0 then {
    STAGE.
    print "Staged".
    DynamicDisplay("Staged                 ").
    preserve.
}.

// Initial throttle and heading
SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 1.
lock STEERING to HEADING(MyHeading,90). // works better than UP direction as UP tend to rotate

wait until SHIP:AIRSPEED > 70. // no stearing until proper velocity gained
set myPitch to AltitudePitch(). // first input from Pitching curve function
// Lock is needed only once, outside loop, steering will be changed whenever myPitch changes
lock STEERING to HEADING(MyHeading, myPitch).

until (ship:APOAPSIS > MyApoapsis-10000) {
    set myPitch to AltitudePitch().
    wait 1.
}.

// for last 10 km switching to PID controled throttle
set VelGroundPID:MINOUTPUT to 0.
set VelGroundPID:MAXOUTPUT to 1.
VelGroundPID:RESET(). // reseting previous ITERM and ERRORSUM
set VelGroundPID:SETPOINT to MyApoapsis.
            
until (ship:APOAPSIS > MyApoapsis) {
    set myPitch to AltitudePitch().
    VelGroundPID:UPDATE(TIME:SECONDS, ship:APOAPSIS).
    set myThrottle to VelGroundPID:OUTPUT.
    SET SHIP:CONTROL:PILOTMAINTHROTTLE TO myThrottle.
    wait 0.
}.
// Desired orbit is reached at this point

SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.
lock STEERING to ship:PROGRADE. // orbital prograde - to minimise drag
print "Apoapsis reached at "+round(SHIP:AIRSPEED,2)  + "(m/s) / "+round(SHIP:ALTITUDE,2)    + "(m)".
DynamicDisplay("Apoapsis reached").

// waiting until craft reach space
wait until (SHIP:ALTITUDE > 70000).
//lock STEERING to HEADING(MyHeading,0). // align with horizont until AP reached
lock STEERING to HEADING(MyHeading,-4). // align slightly below horizont until AP reached
hudtext("Steering set to horizont ",2,4,60,white,false).
print "Point to horizont at "+round(SHIP:AIRSPEED,2)  + "(m/s) / "+round(SHIP:ALTITUDE,2)    + "(m)".
        
// reseting circularization PID
set VelGroundPID:MINOUTPUT to 0.
set VelGroundPID:MAXOUTPUT to 1.
VelGroundPID:RESET(). // reseting previous ITERM and ERRORSUM
print "Velocity PID reset".
        
// Starting maneuver slightly before reaching Ap
//wait until (SHIP:ALTITUDE > ship:APOAPSIS - 3000).
wait until (ETA:APOAPSIS <= ApETA). // waiting until 20 sec before Ap.
set kuniverse:TimeWarp:rate to 1. // set timewarp to 1X, just to be safe
        
SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 1.
print "Maneuver started at "+round(SHIP:AIRSPEED,2)  + "(m/s) / "+round(SHIP:ALTITUDE,2)    + "(m)".
        
wait until (SHIP:ALTITUDE > ship:APOAPSIS - 500).
lock STEERING to ship:PROGRADE. // it is enough to have one single lock
        
print "PID burning for stable orbit".
        
until (ship:PERIAPSIS > 71000) {
    set VelGroundPID:SETPOINT to MyApoapsis-1000.
    VelGroundPID:UPDATE(TIME:SECONDS, ship:PERIAPSIS).
    set myThrottle to VelGroundPID:OUTPUT.
            
    // safety check, to ensure that ship is turned to desired direction
    // ship facing need to be inside 2 degree error margin from orbital prograde vector
    if round(VANG(SHIP:FACING:VECTOR, SHIP:VELOCITY:ORBIT),10) < 2 {
        SET SHIP:CONTROL:PILOTMAINTHROTTLE TO myThrottle.
    }
    else
    {
        SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.
    }.            
    wait 0. // wait for one timeframe
}.
print "PID burning circular orbit".
// to maximise efficiency, allow burning only when craft is close to Apoapsis
// using lower time than the first orbital burn as we are much closer to goal
// turning to horizont instead of prograde, to prevent raising Apoapsis too much
set BurnAllowed to false.
//lock STEERING to HEADING(MyHeading,0). // align with horizont
lock STEERING to HEADING(MyHeading,-4). // align with horizont
when(ETA:APOAPSIS <= ApETA/2 ) and (BurnAllowed=false) then {
    set BurnAllowed to true.
    DynamicDisplay("Fine Burning              ").
    set kuniverse:TimeWarp:rate to 1. // set timewarp to 1X, just to be safe
    preserve.
}.
when(ETA:APOAPSIS >= ApETA )  and (BurnAllowed=true) then {
    set BurnAllowed to false.
    DynamicDisplay("Wait for fine burning     ").
    preserve.
}.
        
until (ship:PERIAPSIS > MyApoapsis) or (ship:APOAPSIS > MyApoapsis + 20000 ) {
    // Circularize
    set VelGroundPID:SETPOINT to MyApoapsis.
    VelGroundPID:UPDATE(TIME:SECONDS, ship:PERIAPSIS).
    set myThrottle to VelGroundPID:OUTPUT.
            
    //if (round(VANG(SHIP:FACING:VECTOR, SHIP:VELOCITY:ORBIT),10) < 2) and (BurnAllowed=true) { // must be higher tolerance than locked steering with -4 degree below horizont
    if (round(VANG(SHIP:FACING:VECTOR, SHIP:VELOCITY:ORBIT),10) < 6) and (BurnAllowed=true) {
        SET SHIP:CONTROL:PILOTMAINTHROTTLE TO myThrottle.
        DynamicDisplay("Burning allowed ETA: "+round(ETA:APOAPSIS,1)+"(s)").
    }
    else
    {
        SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.
        DynamicDisplay("Burning baned ETA: "+round(ETA:APOAPSIS,1)+"(s)  ").
    }.            
    wait 0. // wait for one timeframe
}.
        
print "Finishing launch program ".
        
hudtext("Orbit established. ",2,4,60,white,false).
Rocket_GUI:hide().
        
// stoping program sequence
SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.
SAS on.
wait 0.
SET SASMODE TO "STABILITYASSIST".
UNLOCK all.
SET SHIP:CONTROL:NEUTRALIZE TO TRUE. // returning controls to pilot - pitch/yaw/roll
// ---

 

It require lib_navball.ks from kOS github, to show shup heading, pitch and roll on screen. It probably can be used/adjusted with some other more advanced script that will calculate heading and pitching as well as apoapsis for orbit and just run this one with all parameters set.

You can use it from terminal like this:

Quote

runpath("/rlaunch",90,120000).

One other small script that is used a lot and helps a lot is execute maneuver node script. I breaked it into two ks files. One is library to define function and other is used to call function from library. Have yet to create small GUI for it.

node_lib.ks is in first spoiler:

Spoiler

// executing next maneuver none - derived from kOS tutorial
// exemple how to find maneuver node.
// set MyNode to nextnode.

// Example for on screen messages
// HUDTEXT( Message, delaySeconds,style(4 - low center),Font_size,RGBA_colour, boolean doEcho_toTerminal).
// hudtext("Custom message",2,4,60,white,false).

// default display function for usage within ManeuverBurn function
// include single parameter and print that on terminal
function DefaultDisplay{
    parameter output.
    print "kOS autopilot state: " + output at(1,1) .
}.
          
// Universal Maneuver function
// 12.12.2017 updated with canceling option if node is deleted
function ManeuverBurn {
    parameter nd.
    parameter ErrorMargin is 0.1. // acceptable error margin
    // "@" is used to point on custom function(delegate)
    // so it is possible to use some other function in main script than default one
    parameter DisplayBlock is DefaultDisplay@.
    local output is "".
    
    local max_acc is ship:maxthrust/ship:mass. // current ship acceleration
    //set burn_duration to nd:deltav:mag/max_acc. // Calculating burn time
    local np is nd:deltav. // picking up point from node deltaV vector
    lock steering to np.

    set output to "Turning to maneuver.".
    // calling external "delegate" function - can be anything that accept one mandatory parameter.
    DisplayBlock:call(output).
    
    wait until (round(VANG(SHIP:FACING:VECTOR, nd:deltav),10) < 2) or (hasnode = false).
    set output to "Waiting ...".
     // waiting until node is close + 60s spare time inside llop so display can be updated
    until (nd:eta <= ((nd:deltav:mag/max_acc)/2 + 60)) or (hasnode = false) {
        DisplayBlock:call(output).
        wait 1.
    }.
    // change timewarp to 1X if it is accelerated while waiting
    set kuniverse:TimeWarp:rate to 1.
    set output to "Final precheck.".
    DisplayBlock:call(output).
    
    // picking node direction if something changed
    set np to nd:deltav. // picking up point from node deltaV vector
    lock steering to np.

    // prepare execution trough loop
    local VelGroundPID is PIDLOOP( 0.48, 0.576, 0.01, 0.0, 0.0 ).
    set VelGroundPID:MINOUTPUT to 0.
    set VelGroundPID:MAXOUTPUT to 1.
    VelGroundPID:RESET(). // reseting previous ITERM and ERRORSUM
    local myThrottle is 0. // ensure that is local

    // waiting again until is right time to execute
    until (nd:eta <= ((nd:deltav:mag/max_acc)/2 + 1)) or (hasnode = false){
        DisplayBlock:call(output).
        wait 0.
    }.
    
    set kuniverse:TimeWarp:rate to 1.
    
    set output to "Burning.".
    DisplayBlock:call(output).

    // using PID to determine if we reached goal instead of scary math
    local done is false.
    until done  or (hasnode = false)
    {
        set VelGroundPID:SETPOINT to 0.
        // using vector scalar info for pid (-1)* for oposite magnitude
        VelGroundPID:UPDATE(TIME:SECONDS, (-1)*nd:deltav:mag).
        set myThrottle to VelGroundPID:OUTPUT.
    
        set np to nd:deltav. // picking up point from node deltaV vector
        lock steering to np.
    
        // safety check, to ensure that ship is turned to desired direction
        // ship facing need to be inside 2 degree error margin from maneuver vector
        
        if nd:deltav:mag > 10 {
            if round(VANG(SHIP:FACING:VECTOR, nd:deltav),10) < 2 {
                SET SHIP:CONTROL:PILOTMAINTHROTTLE TO myThrottle.
            }
            else
            {
                SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.
            }.
        } else {
            // new piece of code for precision maneuver
            if round(VANG(SHIP:FACING:VECTOR, nd:deltav),10) < 0.5 {
                SET SHIP:CONTROL:PILOTMAINTHROTTLE TO myThrottle.
            }
            else
            {
                SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.
            }.
        }.
        
        wait 0. // wait for one timeframe
        if nd:deltav:mag < ErrorMargin {
            set done to true.
        }.
        DisplayBlock:call(output).
    }.
    SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.
    unlock steering.
    SAS ON.
    if (hasnode = false){
        set output to "Maneuver canceled.".
    } else {
        set output to "Maneuver done.".
    }.
    
    DisplayBlock:call(output).
}. // end of function

and other "maneuver.ks" script that calls functions from library:

Spoiler

// executing next maneuver none - derived from kOS tutorial
RUNONCEPATH("node_lib"). // declaration of Maneuver functions

SAS off.
clearscreen.

function CustomDisplay{
    parameter output.
    print "                                                     " at (0,1).
    print "                      " at (21,3). // Longest output is 21 char - making sure row is cleared
    // print new stuff
    print "Maneuver Node in: " + round(MyNode:eta) + ", DeltaV: " + round(MyNode:deltav:mag) at (0,1).
    print output at (22,3).
}.

// ability to stage when craft runs out of fuel
when SHIP:MAXTHRUST = 0 then {
    STAGE.
    print "Staged".
    CustomDisplay("Staged                 ").
    preserve.
}.

if hasnode {
    set MyNode to nextnode.
    print "kOS autopilot for maneuver node".
    print "Maneuver Node in: " + round(MyNode:eta) + ", DeltaV: " + round(MyNode:deltav:mag).
    print " ".
    print "kOS autopilot state: ".
    print " ".
    // calling ManeuverBurn function with custom display delegate
    ManeuverBurn(MyNode,0.05,CustomDisplay@). // using function( "delegate") as passing parameter
}
else
{
    print "Ship have no maneuver node to execute.".
    hudtext("Ship have no maneuver node to execute.",2,4,60,white,false).    
}.
unlock all.
SAS on.

 

And final touch, to bind those two together in boot script that you can choose in VAB/SPH screen to execute tedious tasks on launch:

Spoiler

if not exists("/lib_navball")
{
    copypath("0:/lib_navball","").
}.
if not exists("/node_lib")
{
    copypath("0:/node_lib","/node_lib").
}.
if not exists("/maneuver")
{
    copypath("0:/maneuver","").
}.
if not exists("/rlaunch_gui")
{
copypath("0:/rlaunch_gui","").
runpath("/rlaunch_gui",90,120000).
} else
{
runpath("/maneuver").
}.
//print "Command runpath('/rlaunch_gui',MyHeading,MyApoapsis).".
//print "Use double quote and numbers for parameters".
//print "Example: runpath('/rlaunch',90,120000)".

My crafts are still not fully autonomous, as I need to learn a lot about rocket science and math behind it, so I set maneuver nodes manualy with help of PreciseNode mod. Just recently I have put a command that will execute maneuver node, if one exist for craft, each time I revisit craft in flight. Also, I have added ability to cancel burning if I delete node. For example, craft reached desired orbit good enough, but program is still executing because error margin is not within boundaries.

Certainly all of above have a lot of room for improvement, I will let you to discover flaws that I didn't mentioned, so have fun with this, I hope it will provide some boost to allow you to write more complex stuff.

Share this post


Link to post
Share on other sites

I'll see if I can gather the Auto-Rover script I was working on.  It's not totally complete (could use a few more features) but it is functional and can be expanded upon.

 

That is, unless someone already has a script for that :)

Share this post


Link to post
Share on other sites
On 2/1/2018 at 12:36 PM, Slam_Jones said:

I'll see if I can gather the Auto-Rover script I was working on.  It's not totally complete (could use a few more features) but it is functional and can be expanded upon.

 

That is, unless someone already has a script for that :)

I found this a while ago: https://github.com/aidygus/astar-rover

Eventually, I'll steal it and recode it in a compilable language and run with KRPC.

Why is anyone still using KOS? Is it just because it came out first? It's not any easier or less feature-complete. Plus you can code in a real-world language.

Share this post


Link to post
Share on other sites
10 hours ago, FleshJeb said:

Why is anyone still using KOS? Is it just because it came out first? It's not any easier or less feature-complete. Plus you can code in a real-world language.

Personal preference, I guess. KRPC seems too much for me, like hacky way to interrupt with game controls. On the other hand, kOS set some limits how much you can do with parts provided. Have to think about limited storage space, electricity consumption and efficiency of your script, how much CPU power it will require.

It feels much more like additional computer within a game to control stuff, rather than computer beside KSP game world that influence whole game behaviour and experience. Just my personal preference.

Share this post


Link to post
Share on other sites
10 hours ago, kcs123 said:

Personal preference, I guess. KRPC seems too much for me, like hacky way to interrupt with game controls. On the other hand, kOS set some limits how much you can do with parts provided. Have to think about limited storage space, electricity consumption and efficiency of your script, how much CPU power it will require.

It feels much more like additional computer within a game to control stuff, rather than computer beside KSP game world that influence whole game behaviour and experience. Just my personal preference.

That's very fair and understandable; I prefer to self-limit. I like the fact that KRPC is part-free, so you can share craft more easily.

I have a lot of other thoughts on this, but this isn't the thread for it.

Share this post


Link to post
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.