Jump to content

[1.3] kOS Scriptable Autopilot System v1.1.3.0


erendrake

Recommended Posts

On 7/19/2017 at 4:34 AM, MattPlaysSimulations said:

I would like to include this mod in my modpack that i am currently making.

Okay.... that's not really a question though.  Are you seeking advice on how to go about doing it?

Link to comment
Share on other sites

I have an issue regarding the engine:ISP structure. It won't show the ISP of the engine I'm using (it gives an error) (i have it nametag:ISP). I need this code to work (tho I do recognize I barrely read about the syntax of the language, please help, I'm too lazy to read all that.)

Quote

SET KWRCS TO SHIP:PARTSTAGGED("RCSFuel").
SET KWRCSEngine TO SHIP:PARTSTAGGED("RCSEngine").
SET VESSEL TO SHIP.
SET EBTS TO ((VESSEL:MASS*9.80665*KWRCSEngine:ISP)/VESSEL:THRUST)*(1-constant:e^(-LN(VESSEL:MASS / (VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS)))). //Estimated burn time of stage


 

Edited by GoodHunter14
Link to comment
Share on other sites

9 minutes ago, GoodHunter14 said:

I have an issue regarding the engine:ISP structure. It won't show the ISP of the engine I'm using (it gives an error) (i have it nametag:ISP). I need this code to work (tho I do recognize I barrely read about the syntax of the language, please help, I'm to lazy to read all that.)


 

Where you have PARTSTAGGED() the return is not a single part, but a LIST of parts, even if there is only one part in the list.

If you will only ever give one part that particular tag, you can stick [0] on the end to get the first object from the list. e.g. SET mypart TO SHIP:PARTSTAGGED("mytag")[0].

Edit: if you have multiple parts sharing a tag, you would need to loop through them (e.g. if you wanted to total their mass).

Edited by ElWanderer
Link to comment
Share on other sites

9 minutes ago, ElWanderer said:

Where you have PARTSTAGGED() the return is not a single part, but a LIST of parts, even if there is only one part in the list.

If you will only ever give one part that particular tag, you can stick [0] on the end to get the first object from the list. e.g. SET mypart TO SHIP:PARTSTAGGED("mytag")[0].

Thanks for the tip :wink: . I'll check and solve the rest of the issues of my code and report back.

Link to comment
Share on other sites

I'm about to throw my keyboard away.

Spoiler

LOCK STEERING TO MYSTEER. // from now on we'll be able to change steering by just assigning a new value to MYSTEER
SET SPEED TO 200.
SET z TO 90.
UNTIL SHIP:APOAPSIS > x { // all altitudes are in meters, not kilometers
    SET MYSTEER TO HEADING(90,z).
    //For the initial ascent, we want our steering to be straight
    //up and rolled due east
    IF SHIP:VELOCITY:SURFACE:MAG > 200 AND z>10 {
        IF SHIP:VELOCITY:SURFACE:MAG = SPEED+100.
        {
          SET z TO z-5.
          SET SPEED TO SHIP:VELOCITY:SURFACE:MAG.
        }.
    }.

    IF KWRCSEngine:THRUST > 0
    {
    SET EBTS TO ((VESSEL:MASS*9.80665*KWRCSEngine:ISP)/KWRCSEngine:AVAILABLETHRUSTAT(SHIP:Q))*(1-constant:e^(-LN(VESSEL:MASS / (VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS)))). //Estimated burn time of stage
    PRINT "Estimated Burn Time of stage (s):" + ROUND(EBTS,2) AT(0,21).
    }.

}.

that pink code doesn't work. the ship just pitches straight to 10 degrees. I hate this language so much.

Link to comment
Share on other sites

I'm throwing my keyboard out the window. WHY DOESN'T THE IF STATEMENT WORK WHYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY. WHY DOES IT IGNORE THE GOD DAMN STATEMENT (condition*) AND JUMPS TO UNTIL LOOP WHY???? TELL ME WHY??!?!?!?!?!

Spoiler

CLEARSCREEN.
SET KWRCS TO SHIP:PARTSTAGGED("RCSFuel")[0].
SET KWRCSEngine TO SHIP:PARTSTAGGED("RCSEngine")[0]. //the rcs engine and fuel are in the second stage. Make sure you have enought deltaV in the second stage to reach orbit
//and make sure the engine in the second stage fires before you reach 100km Apoapsis
SET VESSEL TO SHIP.
//Next, we'll lock our throttle to 100%.
LOCK THROTTLE TO 1.0.   // 1.0 is the max, 0.0 is idle.

//This is our countdown loop, which cycles from 10 to 0
PRINT "Counting down:".
FROM {local countdown is 10.} UNTIL countdown = 0 STEP {SET countdown to countdown - 1.} DO {
    PRINT "..." + countdown.
    WAIT 1. // pauses the script here for 1 second.
}


//This is a trigger that constantly checks to see if our thrust is zero.
//If it is, it will attempt to stage and then return to where the script
//left off. The PRESERVE keyword keeps the trigger active even after it
//has been triggered.
WHEN MAXTHRUST = 0 THEN {
    STAGE.
    PRESERVE.
}.


//This will be our main control loop for the ascent. It will
//cycle through continuously until our apoapsis is greater
//than 100km. Each cycle, it will check each of the IF
//statements inside and perform them if their conditions
//are met
SET MYSTEER TO HEADING(90,90).
SET x TO 100000. //your desired altitude at which you want to orbit
SET u TO 3.5316*10^12.
SET r TO 600000 + x.
SET a TO 600000 + x.
SET Vel TO SQRT(u*((2/r) - (1/a))).

LOCK STEERING TO MYSTEER. // from now on we'll be able to change steering by just assigning a new value to MYSTEER
SET SPEED TO 200.
SET z TO 90.
UNTIL SHIP:APOAPSIS > x { //Remember, all altitudes will be in meters, not kilometers
  IF SHIP:VELOCITY:SURFACE:MAG < 100 {
  //This sets our steering 90 degrees up and yawed to the compass
  //heading of 90 degrees (east)
  SET MYSTEER TO HEADING(90,90).

//Once we pass 100m/s, we want to pitch down ten degrees
} ELSE IF SHIP:VELOCITY:SURFACE:MAG >= 100 AND SHIP:VELOCITY:SURFACE:MAG < 200 {
  SET MYSTEER TO HEADING(90,80).
  PRINT "Pitching to 80 degrees" AT(0,15).
  PRINT ROUND(SHIP:APOAPSIS,0) AT (0,16).

//Each successive IF statement checks to see if our velocity
//is within a 100m/s block and adjusts our heading down another
//ten degrees if so
} ELSE IF SHIP:VELOCITY:SURFACE:MAG >= 200 AND SHIP:VELOCITY:SURFACE:MAG < 300 {
  SET MYSTEER TO HEADING(90,70).
  PRINT "Pitching to 70 degrees" AT(0,15).
  PRINT ROUND(SHIP:APOAPSIS,0) AT (0,16).

} ELSE IF SHIP:VELOCITY:SURFACE:MAG >= 300 AND SHIP:VELOCITY:SURFACE:MAG < 400 {
  SET MYSTEER TO HEADING(90,60).
  PRINT "Pitching to 60 degrees" AT(0,15).
  PRINT ROUND(SHIP:APOAPSIS,0) AT (0,16).

} ELSE IF SHIP:VELOCITY:SURFACE:MAG >= 400 AND SHIP:VELOCITY:SURFACE:MAG < 700 {
  SET MYSTEER TO HEADING(90,50).
  PRINT "Pitching to 50 degrees" AT(0,15).
  PRINT ROUND(SHIP:APOAPSIS,0) AT (0,16).

} ELSE IF SHIP:VELOCITY:SURFACE:MAG >= 700 AND SHIP:VELOCITY:SURFACE:MAG < 800 {
  SET MYSTEER TO HEADING(90,40).
  PRINT "Pitching to 40 degrees" AT(0,15).
  PRINT ROUND(SHIP:APOAPSIS,0) AT (0,16).

} ELSE IF SHIP:VELOCITY:SURFACE:MAG >= 800 AND SHIP:VELOCITY:SURFACE:MAG < 900 {
  SET MYSTEER TO HEADING(90,30).
  PRINT "Pitching to 30 degrees" AT(0,15).
  PRINT ROUND(SHIP:APOAPSIS,0) AT (0,16).

} ELSE IF SHIP:VELOCITY:SURFACE:MAG >= 900 AND SHIP:VELOCITY:SURFACE:MAG < 1300 {
  SET MYSTEER TO HEADING(90,11).
  PRINT "Pitching to 20 degrees" AT(0,15).
  PRINT ROUND(SHIP:APOAPSIS,0) AT (0,16).

//Beyond 800m/s, we can keep facing towards 10 degrees above the horizon and wait
//for the main loop to recognize that our apoapsis is above 100km
} ELSE IF SHIP:VELOCITY:SURFACE:MAG >= 1300 {
  SET MYSTEER TO HEADING(90,10).
  PRINT "Pitching to 10 degrees" AT(0,15).
  PRINT ROUND(SHIP:APOAPSIS,0) AT (0,16).

}.
    IF KWRCSEngine:THRUST > 0
    {
    SET EBTS TO ((VESSEL:MASS*9.80665*KWRCSEngine:ISP)/KWRCSEngine:AVAILABLETHRUSTAT(SHIP:Q))*(1-constant:e^(-LN(VESSEL:MASS / (VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS)))). //Estimated burn time of stage
    PRINT "Estimated Burn Time of stage (s):" + ROUND(EBTS,2) AT(0,21).
    }.

}.
PRINT "Velocity necessary to stay in orbit at 100km is:" + ROUND(Vel,2).
SET EBTS TO ((VESSEL:MASS*9.80665*KWRCSEngine:ISP)/KWRCSEngine:AVAILABLETHRUSTAT(SHIP:Q))*(1-constant:e^(-LN(VESSEL:MASS / (VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS)))). //Estimated burn time of stage
PRINT "Estimated Burn Time of stage (s):" + ROUND(EBTS,2) AT(0,21).
SET SVel TO SQRT(u * ((2/(SHIP:APOAPSIS+600000)) - (1/(600000+((SHIP:PERIAPSIS+SHIP:APOAPSIS)/2))))).
SET dV TO Vel - Svel. // estimated delta V to complete orbit
PRINT "DeltaV: " + dV.
SET R TO KWRCS:WETMASS/KWRCS:DRYMASS.
SET y TO (constant:e^(dV/(KWRCSEngine:VISP*9.80665))*(VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS) - (VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS))/(R-constant:e^(dV/(KWRCSEngine:VISP*9.80665))).
SET FM TO y*R. //this is the estimated fuel mass needed to complete the manuever
SET eM TO VESSEL:MASS - FM. // this is the mass after the manuever has completed but I'm using it in the same formula as in EBT for a better burn time estimation
SET EBT TO ((VESSEL:MASS*9.80665*KWRCSEngine:VISP)/KWRCSEngine:AVAILABLETHRUST)*(1-constant:e^(-dV/(9.80665*KWRCSEngine:VISP))).
PRINT "EBT: " + EBT.
SET EBTeM TO ((eM*9.80665*KWRCSEngine:VISP)/KWRCSEngine:AVAILABLETHRUST)*(1-constant:e^(-dV/(9.80665*KWRCSEngine:VISP))).
PRINT "EBTem: " + EBTeM.
SET EMBT TO (EBT + EBTeM)/2. //Estimated burn time to complete DeltaV Manuever
SET APO TO EMBT/2.
PRINT "APO: " + APO.
PRINT "100km apoapsis reached, cutting throttle".
LOCK THROTTLE TO 0.
SET MYSTEER TO HEADING(90,5).
//SET EBTS TO ((VESSEL:MASS*9.80665*KWRCSEngine:ISP)*KWRCSEngine:THRUST)*(1-constant:e^(-LN(VESSEL:MASS / (VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS)))).


//At this point, our apoapsis is above 100km and our main loop has ended.
//time to reach orbit
IF ETA:APOAPSIS = APO. //if the ETA until apoapsis is half of the estimated burn time
{
  PRINT "Burning sequence to circularize orbit engaged".
  UNTIL SHIP:PERIAPSIS > x
  {
    SET MYSTEER TO HEADING(90,0).
    LOCK THROTTLE TO 1.
    IF KWRCSEngine:THRUST > 0
    {
    SET EBTS TO ((VESSEL:MASS*9.80665*KWRCSEngine:ISP)/KWRCSEngine:AVAILABLETHRUSTAT(SHIP:Q))*(1-constant:e^(-LN(VESSEL:MASS / (VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS)))). //Estimated burn time of stage
    PRINT "Estimated Burn Time of stage (s):" + ROUND(EBTS,2) AT(0,21).
    }.
    SET SVel TO SQRT(u * ((2/(SHIP:APOAPSIS+600000)) - (1/(600000+((SHIP:PERIAPSIS+SHIP:APOAPSIS)/2))))).
    SET dV TO Vel - Svel.
    PRINT "DeltaV remaining to complete burn (m/s):" + ROUND(dV,2) AT(0,22).
  }.
}.

//This sets the user's throttle setting to zero to prevent the throttle
//from returning to the position it was at before the script was run.
PRINT "100km periapsis reached, cutting throttle".
LOCK THROTTLE TO 0.
SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.
 

 

Edited by GoodHunter14
Link to comment
Share on other sites

6 minutes ago, GoodHunter14 said:

I'm throwing my keyboard out the window. WHY DOESN'T THE IF STATEMENT WORK WHYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY. WHY DOES IT IGNORE THE GOD DAMN STATEMENT (condition*) AND JUMPS TO UNTIL LOOP WHY???? TELL ME WHY??!?!?!?!?!

  Reveal hidden contents

CLEARSCREEN.
SET KWRCS TO SHIP:PARTSTAGGED("RCSFuel")[0].
SET KWRCSEngine TO SHIP:PARTSTAGGED("RCSEngine")[0]. //the rcs engine and fuel are in the second stage. Make sure you have enought deltaV in the second stage to reach orbit
//and make sure the engine in the second stage fires before you reach 100km Apoapsis
SET VESSEL TO SHIP.
//Next, we'll lock our throttle to 100%.
LOCK THROTTLE TO 1.0.   // 1.0 is the max, 0.0 is idle.

//This is our countdown loop, which cycles from 10 to 0
PRINT "Counting down:".
FROM {local countdown is 10.} UNTIL countdown = 0 STEP {SET countdown to countdown - 1.} DO {
    PRINT "..." + countdown.
    WAIT 1. // pauses the script here for 1 second.
}


//This is a trigger that constantly checks to see if our thrust is zero.
//If it is, it will attempt to stage and then return to where the script
//left off. The PRESERVE keyword keeps the trigger active even after it
//has been triggered.
WHEN MAXTHRUST = 0 THEN {
    STAGE.
    PRESERVE.
}.


//This will be our main control loop for the ascent. It will
//cycle through continuously until our apoapsis is greater
//than 100km. Each cycle, it will check each of the IF
//statements inside and perform them if their conditions
//are met
SET MYSTEER TO HEADING(90,90).
SET x TO 100000. //your desired altitude at which you want to orbit
SET u TO 3.5316*10^12.
SET r TO 600000 + x.
SET a TO 600000 + x.
SET Vel TO SQRT(u*((2/r) - (1/a))).

LOCK STEERING TO MYSTEER. // from now on we'll be able to change steering by just assigning a new value to MYSTEER
SET SPEED TO 200.
SET z TO 90.
UNTIL SHIP:APOAPSIS > x { //Remember, all altitudes will be in meters, not kilometers
  IF SHIP:VELOCITY:SURFACE:MAG < 100 {
  //This sets our steering 90 degrees up and yawed to the compass
  //heading of 90 degrees (east)
  SET MYSTEER TO HEADING(90,90).

//Once we pass 100m/s, we want to pitch down ten degrees
} ELSE IF SHIP:VELOCITY:SURFACE:MAG >= 100 AND SHIP:VELOCITY:SURFACE:MAG < 200 {
  SET MYSTEER TO HEADING(90,80).
  PRINT "Pitching to 80 degrees" AT(0,15).
  PRINT ROUND(SHIP:APOAPSIS,0) AT (0,16).

//Each successive IF statement checks to see if our velocity
//is within a 100m/s block and adjusts our heading down another
//ten degrees if so
} ELSE IF SHIP:VELOCITY:SURFACE:MAG >= 200 AND SHIP:VELOCITY:SURFACE:MAG < 300 {
  SET MYSTEER TO HEADING(90,70).
  PRINT "Pitching to 70 degrees" AT(0,15).
  PRINT ROUND(SHIP:APOAPSIS,0) AT (0,16).

} ELSE IF SHIP:VELOCITY:SURFACE:MAG >= 300 AND SHIP:VELOCITY:SURFACE:MAG < 400 {
  SET MYSTEER TO HEADING(90,60).
  PRINT "Pitching to 60 degrees" AT(0,15).
  PRINT ROUND(SHIP:APOAPSIS,0) AT (0,16).

} ELSE IF SHIP:VELOCITY:SURFACE:MAG >= 400 AND SHIP:VELOCITY:SURFACE:MAG < 700 {
  SET MYSTEER TO HEADING(90,50).
  PRINT "Pitching to 50 degrees" AT(0,15).
  PRINT ROUND(SHIP:APOAPSIS,0) AT (0,16).

} ELSE IF SHIP:VELOCITY:SURFACE:MAG >= 700 AND SHIP:VELOCITY:SURFACE:MAG < 800 {
  SET MYSTEER TO HEADING(90,40).
  PRINT "Pitching to 40 degrees" AT(0,15).
  PRINT ROUND(SHIP:APOAPSIS,0) AT (0,16).

} ELSE IF SHIP:VELOCITY:SURFACE:MAG >= 800 AND SHIP:VELOCITY:SURFACE:MAG < 900 {
  SET MYSTEER TO HEADING(90,30).
  PRINT "Pitching to 30 degrees" AT(0,15).
  PRINT ROUND(SHIP:APOAPSIS,0) AT (0,16).

} ELSE IF SHIP:VELOCITY:SURFACE:MAG >= 900 AND SHIP:VELOCITY:SURFACE:MAG < 1300 {
  SET MYSTEER TO HEADING(90,11).
  PRINT "Pitching to 20 degrees" AT(0,15).
  PRINT ROUND(SHIP:APOAPSIS,0) AT (0,16).

//Beyond 800m/s, we can keep facing towards 10 degrees above the horizon and wait
//for the main loop to recognize that our apoapsis is above 100km
} ELSE IF SHIP:VELOCITY:SURFACE:MAG >= 1300 {
  SET MYSTEER TO HEADING(90,10).
  PRINT "Pitching to 10 degrees" AT(0,15).
  PRINT ROUND(SHIP:APOAPSIS,0) AT (0,16).

}.
    IF KWRCSEngine:THRUST > 0
    {
    SET EBTS TO ((VESSEL:MASS*9.80665*KWRCSEngine:ISP)/KWRCSEngine:AVAILABLETHRUSTAT(SHIP:Q))*(1-constant:e^(-LN(VESSEL:MASS / (VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS)))). //Estimated burn time of stage
    PRINT "Estimated Burn Time of stage (s):" + ROUND(EBTS,2) AT(0,21).
    }.

}.
PRINT "Velocity necessary to stay in orbit at 100km is:" + ROUND(Vel,2).
SET EBTS TO ((VESSEL:MASS*9.80665*KWRCSEngine:ISP)/KWRCSEngine:AVAILABLETHRUSTAT(SHIP:Q))*(1-constant:e^(-LN(VESSEL:MASS / (VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS)))). //Estimated burn time of stage
PRINT "Estimated Burn Time of stage (s):" + ROUND(EBTS,2) AT(0,21).
SET SVel TO SQRT(u * ((2/(SHIP:APOAPSIS+600000)) - (1/(600000+((SHIP:PERIAPSIS+SHIP:APOAPSIS)/2))))).
SET dV TO Vel - Svel. // estimated delta V to complete orbit
PRINT "DeltaV: " + dV.
SET R TO KWRCS:WETMASS/KWRCS:DRYMASS.
SET y TO (constant:e^(dV/(KWRCSEngine:VISP*9.80665))*(VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS) - (VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS))/(R-constant:e^(dV/(KWRCSEngine:VISP*9.80665))).
SET FM TO y*R. //this is the estimated fuel mass needed to complete the manuever
SET eM TO VESSEL:MASS - FM. // this is the mass after the manuever has completed but I'm using it in the same formula as in EBT for a better burn time estimation
SET EBT TO ((VESSEL:MASS*9.80665*KWRCSEngine:VISP)/KWRCSEngine:AVAILABLETHRUST)*(1-constant:e^(-dV/(9.80665*KWRCSEngine:VISP))).
PRINT "EBT: " + EBT.
SET EBTeM TO ((eM*9.80665*KWRCSEngine:VISP)/KWRCSEngine:AVAILABLETHRUST)*(1-constant:e^(-dV/(9.80665*KWRCSEngine:VISP))).
PRINT "EBTem: " + EBTeM.
SET EMBT TO (EBT + EBTeM)/2. //Estimated burn time to complete DeltaV Manuever
SET APO TO EMBT/2.
PRINT "APO: " + APO.
PRINT "100km apoapsis reached, cutting throttle".
LOCK THROTTLE TO 0.
SET MYSTEER TO HEADING(90,5).
//SET EBTS TO ((VESSEL:MASS*9.80665*KWRCSEngine:ISP)*KWRCSEngine:THRUST)*(1-constant:e^(-LN(VESSEL:MASS / (VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS)))).


//At this point, our apoapsis is above 100km and our main loop has ended.
//time to reach orbit
IF ETA:APOAPSIS = APO. //if the ETA until apoapsis is half of the estimated burn time
{
  PRINT "Burning sequence to circularize orbit engaged".
  UNTIL SHIP:PERIAPSIS > x
  {
    SET MYSTEER TO HEADING(90,0).
    LOCK THROTTLE TO 1.
    IF KWRCSEngine:THRUST > 0
    {
    SET EBTS TO ((VESSEL:MASS*9.80665*KWRCSEngine:ISP)/KWRCSEngine:AVAILABLETHRUSTAT(SHIP:Q))*(1-constant:e^(-LN(VESSEL:MASS / (VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS)))). //Estimated burn time of stage
    PRINT "Estimated Burn Time of stage (s):" + ROUND(EBTS,2) AT(0,21).
    }.
    SET SVel TO SQRT(u * ((2/(SHIP:APOAPSIS+600000)) - (1/(600000+((SHIP:PERIAPSIS+SHIP:APOAPSIS)/2))))).
    SET dV TO Vel - Svel.
    PRINT "DeltaV remaining to complete burn (m/s):" + ROUND(dV,2) AT(0,22).
  }.
}.

//This sets the user's throttle setting to zero to prevent the throttle
//from returning to the position it was at before the script was run.
PRINT "100km periapsis reached, cutting throttle".
LOCK THROTTLE TO 0.
SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.
 

 

You appear to be checking for the equality of a floating point number (ETA:APOAPSIS) with either another float (or an integer, it doesn't really matter). This will almost never work. Instead, you need to check to see if the floating point value falls within a range.

Note: if an IF check doesn't seem to be working, it can help to debug it by printing out the values being compared.

Link to comment
Share on other sites

6 minutes ago, ElWanderer said:

You appear to be checking for the equality of a floating point number (ETA:APOAPSIS) with either another float (or an integer, it doesn't really matter). This will almost never work. Instead, you need to check to see if the floating point value falls within a range.

Note: if an IF check doesn't seem to be working, it can help to debug it by printing out the values being compared.

God damn it I should've thinked about this, but at this point I was desperate. I spent the whole day figuring out how to fix this code and make it more efficient. Thanks for telling me why it didn't work.

Edited by GoodHunter14
Link to comment
Share on other sites

IF (APO - 0.1) < ETA:APOAPSIS < (APO + 0.1).

"Tries to compile" ...

"Error: Cannot ordinate BooleanValue < ScalarDoubleValue"

I give up. This language right now, i cant even... I don't know how to fix this. Please give me a solution.

EDIT: Scrap that. Would this work for converting ETA:APOAPSIS value to BooleanValue without giving issues

?

Spoiler

SET Value TO 0

UNTIL (APO - 0.1) < Value < (APO + 0.1) 

{

SET Value TO ETA:APOAPSIS

}

 

Edited by GoodHunter14
Link to comment
Share on other sites

37 minutes ago, GoodHunter14 said:

IF (APO - 0.1) < ETA:APOAPSIS < (APO + 0.1).

"Tries to compile" ...

"Error: Cannot ordinate BooleanValue < ScalarDoubleValue"

I give up. This language right now, i cant even... I don't know how to fix this. Please give me a solution.

This behaviour is true of almost all languages. You can't test for X < Y < Z, you have to test for (X < Y) AND (Y < Z).

Link to comment
Share on other sites

5 minutes ago, ElWanderer said:

This behaviour is true of almost all languages. You can't test for X < Y < Z, you have to test for (X < Y) AND (Y < Z).

Oh yeah i forgot... now that makes me look dumb... should've put an AND...

EDIT: Sorry for being like this. Not being able to find the issue with my code drives me crazy and sometimes I forget things. Thanks for taking the time to help me. I usually expect hate when I do mistakes like this.

Edited by GoodHunter14
Link to comment
Share on other sites

Can anyone tell me why those if conditions don't work? It straight up jumps to z=10 for some reason.  

Spoiler

CLEARSCREEN.
SET KWRCS TO SHIP:PARTSTAGGED("RCSFuel")[0].
SET KWRCSEngine TO SHIP:PARTSTAGGED("RCSEngine")[0].
SET SRBs TO SHIP:PARTSTAGGED("SRB"). //the rcs engine and fuel are in the second stage. Make sure you have enought deltaV in the second stage to reach orbit
SET cooldowntime to 0.
//and make sure the engine in the second stage fires before you reach 100km Apoapsis
SET VESSEL TO SHIP.
//Next, we'll lock our throttle to 100%.
LOCK THROTTLE TO 1.0.   // 1.0 is the max, 0.0 is idle.

//This is our countdown loop, which cycles from 10 to 0
PRINT "Counting down:".
FROM {local countdown is 10.} UNTIL countdown = 0 STEP {SET countdown to countdown - 1.} DO {
    PRINT "..." + countdown.
    WAIT 1. // pauses the script here for 1 second.
}


//This is a trigger that constantly checks to see if our thrust is zero.
//If it is, it will attempt to stage and then return to where the script
//left off. The PRESERVE keyword keeps the trigger active even after it
//has been triggered.
WHEN MAXTHRUST = 0 THEN {
    STAGE.
    PRESERVE.
}.
FOR SRB in SRBs {
  WHEN SRB:MAXTHRUST = 0 THEN
  {
    STAGE.
    if cooldowntime = 0
    {
    PRESERVE.
      if SRB:MAXTHRUST = 0
      {
        set cooldowntime to 1.
      }.
    }.
    
  }.
}.


//This will be our main control loop for the ascent. It will
//cycle through continuously until our apoapsis is greater
//than 100km. Each cycle, it will check each of the IF
//statements inside and perform them if their conditions
//are met
SET MYSTEER TO HEADING(90,90).
SET x TO 100000. //your desired altitude at which you want to orbit
SET u TO 3.5316*10^12.
SET r TO 600000 + x.
SET a TO 600000 + x.
SET Vel TO SQRT(u*((2/r) - (1/a))).

LOCK STEERING TO MYSTEER. // from now on we'll be able to change steering by just assigning a new value to MYSTEER
SET SPEED TO 200.
SET z TO 90.
UNTIL SHIP:APOAPSIS > x { //Remember, all altitudes will be in meters, not kilometers
  SET MYSTEER TO HEADING(90,z).
    //For the initial ascent, we want our steering to be straight
    //up and rolled due east
    IF SHIP:VELOCITY:SURFACE:MAG > 200 AND z>10 {
        IF SPEED+19 < SHIP:VELOCITY:SURFACE:MAG AND SHIP:VELOCITY:SURFACE:MAG < SPEED+21.
        {
          SET z TO z-1.
          SET SPEED TO SPEED+20.
        }.
    }.
    IF KWRCSEngine:THRUST > 0
    {
    SET EBTS TO ((VESSEL:MASS*9.80665*KWRCSEngine:ISP)/KWRCSEngine:AVAILABLETHRUSTAT(SHIP:Q))*(1-constant:e^(-LN(VESSEL:MASS / (VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS)))). //Estimated burn time of stage
    PRINT "Estimated Burn Time of stage (s):" + ROUND(EBTS,2) AT(0,21).
    }.

}.
PRINT "Velocity necessary to stay in orbit at 100km is:" + ROUND(Vel,2).
SET EBTS TO ((VESSEL:MASS*9.80665*KWRCSEngine:ISP)/KWRCSEngine:AVAILABLETHRUSTAT(SHIP:Q))*(1-constant:e^(-LN(VESSEL:MASS / (VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS)))). //Estimated burn time of stage
PRINT "Estimated Burn Time of stage (s):" + ROUND(EBTS,2) AT(0,21).
SET SVel TO SQRT(u * ((2/(SHIP:APOAPSIS+600000)) - (1/(600000+((SHIP:PERIAPSIS+SHIP:APOAPSIS)/2))))).
SET dV TO Vel - Svel. // estimated delta V to complete orbit
PRINT "DeltaV: " + dV.
SET R TO KWRCS:WETMASS/KWRCS:DRYMASS.
SET y TO (constant:e^(dV/(KWRCSEngine:VISP*9.80665))*(VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS) - (VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS))/(R-constant:e^(dV/(KWRCSEngine:VISP*9.80665))).
SET FM TO y*R. //this is the estimated fuel mass needed to complete the manuever
SET eM TO VESSEL:MASS - FM. // this is the mass after the manuever has completed but I'm using it in the same formula as in EBT for a better burn time estimation
SET EBT TO ((VESSEL:MASS*9.80665*KWRCSEngine:VISP)/KWRCSEngine:AVAILABLETHRUST)*(1-constant:e^(-dV/(9.80665*KWRCSEngine:VISP))).
PRINT "EBT: " + EBT.
SET EBTeM TO ((eM*9.80665*KWRCSEngine:VISP)/KWRCSEngine:AVAILABLETHRUST)*(1-constant:e^(-dV/(9.80665*KWRCSEngine:VISP))).
PRINT "EBTem: " + EBTeM.
SET EMBT TO (EBT + EBTeM)/2. //Estimated burn time to complete DeltaV Manuever
SET APO TO EMBT/2.
PRINT "APO: " + APO.

PRINT "100km apoapsis reached, cutting throttle".
LOCK THROTTLE TO 0.
SET MYSTEER TO HEADING(90,5).
//SET EBTS TO ((VESSEL:MASS*9.80665*KWRCSEngine:ISP)*KWRCSEngine:THRUST)*(1-constant:e^(-LN(VESSEL:MASS / (VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS)))).


//At this point, our apoapsis is above 100km and our main loop has ended.
//time to reach orbit
IF (APO - 0.1) < ETA:APOAPSIS AND ETA:APOAPSIS < (APO + 0.1). //if the ETA until apoapsis is half of the estimated burn time
{
  PRINT "Burning sequence to circularize orbit engaged".
  UNTIL SHIP:PERIAPSIS > x
  {
    SET MYSTEER TO HEADING(90,0).
    LOCK THROTTLE TO 1.
    IF KWRCSEngine:THRUST > 0
    {
    SET EBTS TO ((VESSEL:MASS*9.80665*KWRCSEngine:ISP)/KWRCSEngine:AVAILABLETHRUSTAT(SHIP:Q))*(1-constant:e^(-LN(VESSEL:MASS / (VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS)))). //Estimated burn time of stage
    PRINT "Estimated Burn Time of stage (s):" + ROUND(EBTS,2) AT(0,21).
    }.
    SET SVel TO SQRT(u * ((2/(SHIP:APOAPSIS+600000)) - (1/(600000+((SHIP:PERIAPSIS+SHIP:APOAPSIS)/2))))).
    SET dV TO Vel - Svel.
    PRINT "DeltaV remaining to complete burn (m/s):" + ROUND(dV,2) AT(0,22).
  }.
}.

//This sets the user's throttle setting to zero to prevent the throttle
//from returning to the position it was at before the script was run.
PRINT "100km periapsis reached, cutting throttle".
LOCK THROTTLE TO 0.
SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.
 

 

Link to comment
Share on other sites

11 minutes ago, GoodHunter14 said:

Can anyone tell me why those if conditions don't work? It straight up jumps to z=10 for some reason.  

  Reveal hidden contents

CLEARSCREEN.
SET KWRCS TO SHIP:PARTSTAGGED("RCSFuel")[0].
SET KWRCSEngine TO SHIP:PARTSTAGGED("RCSEngine")[0].
SET SRBs TO SHIP:PARTSTAGGED("SRB"). //the rcs engine and fuel are in the second stage. Make sure you have enought deltaV in the second stage to reach orbit
SET cooldowntime to 0.
//and make sure the engine in the second stage fires before you reach 100km Apoapsis
SET VESSEL TO SHIP.
//Next, we'll lock our throttle to 100%.
LOCK THROTTLE TO 1.0.   // 1.0 is the max, 0.0 is idle.

//This is our countdown loop, which cycles from 10 to 0
PRINT "Counting down:".
FROM {local countdown is 10.} UNTIL countdown = 0 STEP {SET countdown to countdown - 1.} DO {
    PRINT "..." + countdown.
    WAIT 1. // pauses the script here for 1 second.
}


//This is a trigger that constantly checks to see if our thrust is zero.
//If it is, it will attempt to stage and then return to where the script
//left off. The PRESERVE keyword keeps the trigger active even after it
//has been triggered.
WHEN MAXTHRUST = 0 THEN {
    STAGE.
    PRESERVE.
}.
FOR SRB in SRBs {
  WHEN SRB:MAXTHRUST = 0 THEN
  {
    STAGE.
    if cooldowntime = 0
    {
    PRESERVE.
      if SRB:MAXTHRUST = 0
      {
        set cooldowntime to 1.
      }.
    }.
    
  }.
}.


//This will be our main control loop for the ascent. It will
//cycle through continuously until our apoapsis is greater
//than 100km. Each cycle, it will check each of the IF
//statements inside and perform them if their conditions
//are met
SET MYSTEER TO HEADING(90,90).
SET x TO 100000. //your desired altitude at which you want to orbit
SET u TO 3.5316*10^12.
SET r TO 600000 + x.
SET a TO 600000 + x.
SET Vel TO SQRT(u*((2/r) - (1/a))).

LOCK STEERING TO MYSTEER. // from now on we'll be able to change steering by just assigning a new value to MYSTEER
SET SPEED TO 200.
SET z TO 90.
UNTIL SHIP:APOAPSIS > x { //Remember, all altitudes will be in meters, not kilometers
  SET MYSTEER TO HEADING(90,z).
    //For the initial ascent, we want our steering to be straight
    //up and rolled due east
    IF SHIP:VELOCITY:SURFACE:MAG > 200 AND z>10 {
        IF SPEED+19 < SHIP:VELOCITY:SURFACE:MAG AND SHIP:VELOCITY:SURFACE:MAG < SPEED+21.
        {
          SET z TO z-1.
          SET SPEED TO SPEED+20.
        }.
    }.
    IF KWRCSEngine:THRUST > 0
    {
    SET EBTS TO ((VESSEL:MASS*9.80665*KWRCSEngine:ISP)/KWRCSEngine:AVAILABLETHRUSTAT(SHIP:Q))*(1-constant:e^(-LN(VESSEL:MASS / (VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS)))). //Estimated burn time of stage
    PRINT "Estimated Burn Time of stage (s):" + ROUND(EBTS,2) AT(0,21).
    }.

}.
PRINT "Velocity necessary to stay in orbit at 100km is:" + ROUND(Vel,2).
SET EBTS TO ((VESSEL:MASS*9.80665*KWRCSEngine:ISP)/KWRCSEngine:AVAILABLETHRUSTAT(SHIP:Q))*(1-constant:e^(-LN(VESSEL:MASS / (VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS)))). //Estimated burn time of stage
PRINT "Estimated Burn Time of stage (s):" + ROUND(EBTS,2) AT(0,21).
SET SVel TO SQRT(u * ((2/(SHIP:APOAPSIS+600000)) - (1/(600000+((SHIP:PERIAPSIS+SHIP:APOAPSIS)/2))))).
SET dV TO Vel - Svel. // estimated delta V to complete orbit
PRINT "DeltaV: " + dV.
SET R TO KWRCS:WETMASS/KWRCS:DRYMASS.
SET y TO (constant:e^(dV/(KWRCSEngine:VISP*9.80665))*(VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS) - (VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS))/(R-constant:e^(dV/(KWRCSEngine:VISP*9.80665))).
SET FM TO y*R. //this is the estimated fuel mass needed to complete the manuever
SET eM TO VESSEL:MASS - FM. // this is the mass after the manuever has completed but I'm using it in the same formula as in EBT for a better burn time estimation
SET EBT TO ((VESSEL:MASS*9.80665*KWRCSEngine:VISP)/KWRCSEngine:AVAILABLETHRUST)*(1-constant:e^(-dV/(9.80665*KWRCSEngine:VISP))).
PRINT "EBT: " + EBT.
SET EBTeM TO ((eM*9.80665*KWRCSEngine:VISP)/KWRCSEngine:AVAILABLETHRUST)*(1-constant:e^(-dV/(9.80665*KWRCSEngine:VISP))).
PRINT "EBTem: " + EBTeM.
SET EMBT TO (EBT + EBTeM)/2. //Estimated burn time to complete DeltaV Manuever
SET APO TO EMBT/2.
PRINT "APO: " + APO.

PRINT "100km apoapsis reached, cutting throttle".
LOCK THROTTLE TO 0.
SET MYSTEER TO HEADING(90,5).
//SET EBTS TO ((VESSEL:MASS*9.80665*KWRCSEngine:ISP)*KWRCSEngine:THRUST)*(1-constant:e^(-LN(VESSEL:MASS / (VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS)))).


//At this point, our apoapsis is above 100km and our main loop has ended.
//time to reach orbit
IF (APO - 0.1) < ETA:APOAPSIS AND ETA:APOAPSIS < (APO + 0.1). //if the ETA until apoapsis is half of the estimated burn time
{
  PRINT "Burning sequence to circularize orbit engaged".
  UNTIL SHIP:PERIAPSIS > x
  {
    SET MYSTEER TO HEADING(90,0).
    LOCK THROTTLE TO 1.
    IF KWRCSEngine:THRUST > 0
    {
    SET EBTS TO ((VESSEL:MASS*9.80665*KWRCSEngine:ISP)/KWRCSEngine:AVAILABLETHRUSTAT(SHIP:Q))*(1-constant:e^(-LN(VESSEL:MASS / (VESSEL:MASS - KWRCS:MASS + KWRCS:DRYMASS)))). //Estimated burn time of stage
    PRINT "Estimated Burn Time of stage (s):" + ROUND(EBTS,2) AT(0,21).
    }.
    SET SVel TO SQRT(u * ((2/(SHIP:APOAPSIS+600000)) - (1/(600000+((SHIP:PERIAPSIS+SHIP:APOAPSIS)/2))))).
    SET dV TO Vel - Svel.
    PRINT "DeltaV remaining to complete burn (m/s):" + ROUND(dV,2) AT(0,22).
  }.
}.

//This sets the user's throttle setting to zero to prevent the throttle
//from returning to the position it was at before the script was run.
PRINT "100km periapsis reached, cutting throttle".
LOCK THROTTLE TO 0.
SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.
 

 

I can't see anything obvious, though there are a couple of things to say that I can see:

1. you could do with a "WAIT 0." inside that UNTIL loop, so that it doesn't go through the contents more than once per tick.

2. You're giving your velocity magnitude a 2m/s window to hit each time. That would probably work, but towards the end of a stage you might have such high TWR (>5) that it could skip right past it (i.e. go from speed plus 18.9 to speed plus 21.1 in one tick)

Those two points wouldn't explain why your rocket is immediately trying to pivot over to 10 degrees pitch angle. Can you add a bunch of display lines to print out the values for surface velocity magnitude, "speed" and z, and to indicate whether it's going inside the IF blocks or not? That might help track down what's going on.

Link to comment
Share on other sites

24 minutes ago, ElWanderer said:

I can't see anything obvious, though there are a couple of things to say that I can see:

1. you could do with a "WAIT 0." inside that UNTIL loop, so that it doesn't go through the contents more than once per tick.

2. You're giving your velocity magnitude a 2m/s window to hit each time. That would probably work, but towards the end of a stage you might have such high TWR (>5) that it could skip right past it (i.e. go from speed plus 18.9 to speed plus 21.1 in one tick)

Those two points wouldn't explain why your rocket is immediately trying to pivot over to 10 degrees pitch angle. Can you add a bunch of display lines to print out the values for surface velocity magnitude, "speed" and z, and to indicate whether it's going inside the IF blocks or not? That might help track down what's going on.

Here is what happens when the if conditions are met.

Spoiler

1z3xInX.jpg

EDIT: Omg I just found out what it was. It was a god damn period at the second if condition. God damn it lmao. That "." is what caused the problem.

Spoiler

  IF SHIP:VELOCITY:SURFACE:MAG > 200 AND z>10 {
        IF SPEED+19 < SHIP:VELOCITY:SURFACE:MAG AND SHIP:VELOCITY:SURFACE:MAG < SPEED+21.
        {
          SET z TO z-1.
          SET SPEED TO SPEED+20.
        }.
    }.

 

Edited by GoodHunter14
Link to comment
Share on other sites

Ah, the smallest things can be so destructive...

That particular one was hard to spot as it is valid syntax to end the IF statement with a . and you can surround blocks of code with curly brackets as much as you like (and occasionally this is useful for limiting the scope of local variables).

Link to comment
Share on other sites

11 hours ago, GoodHunter14 said:

Is there a thread/post about kOS challenges?

I think there is few threads in challanges forum that were dedicated for kOS users. Unfortunately my free time is very limited, it prevents me to join in for some of those chalanges.

Link to comment
Share on other sites

15 hours ago, GoodHunter14 said:

Is there a thread/post about kOS challenges?

EDIT: Never mind I just realised the forum has a special directory for Challenges.

If you do start one for kOS in the Challenges sub-forum, it might be an idea to let people know here and on the kOS Reddit. Most players looking at the Challenges won't be using kOS.

Separately, I did wonder how easy the current "official" KSP challenge of flying through a Mun arch would be to automate with kOS. The official challenge is stock-only, mind, but most people's attempts are very hit and miss as a result. If only I had some spare time!

Link to comment
Share on other sites

Hi!

I have encountered a problem with wheelsteering. My rovers go crazy no matter what I lock on.

See the gif under the spoiler.

Spoiler

video-to-gif output image

Also I noticed that geoposition returns slightly different values. I never noticed it before.

Have anyone stumbled across something similar?

I'm using KSP 1.3 and installed this mod from CKAN.

Link to comment
Share on other sites

2 minutes ago, FumbeiNumbie said:

Hi!

I have encountered a problem with wheelsteering. My rovers go crazy no matter what I lock on.

See the gif under the spoiler.

  Reveal hidden contents

video-to-gif output image

Also I noticed that geoposition returns slightly different values. I never noticed it before.

Have anyone stumbled across something similar?

I'm using KSP 1.3 and installed this mod from CKAN.

Judging from the navball, your control point is facing directly upwards (as opposed to forwards, in-line with the wheels). Have you taken that into account when steering? If not, try putting a probe core or docking port on the front and set it as the "control from here" point (kOS can access that, if you want to do it in a script, but worth doing manually to see if it fixes the issue first).

Link to comment
Share on other sites

1 minute ago, ElWanderer said:

Judging from the navball, your control point is facing directly upwards (as opposed to forwards, in-line with the wheels). Have you taken that into account when steering? If not, try putting a probe core or docking port on the front and set it as the "control from here" point (kOS can access that, if you want to do it in a script, but worth doing manually to see if it fixes the issue first).

Oh, haha. I'm so embarrassed! I totally forgot about it. Now have you noticed the change in geoposition values when the vehicle is still?

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...