Jump to content

[KSP 1.12.x] kOS v1.4.0.0: kOS Scriptable Autopilot System


Dunbaratu

Recommended Posts

12 hours ago, Steven Mading said:

If you think that X / Y might fail because Y might be zero, and you'd like a bignumber result when Y is zero, try maybe doing this:

x / max(y, 0.0001)

Yes, that's what I have been doing so far. I was just wondering if there was a better way.

12 hours ago, Steven Mading said:

That would still error out when calculating the division by zero, before it got to doing the min().

Oh, I thought the "error when trying to push infinity to stack" only occurred if I actually assigned the value to a variable.

Link to comment
Share on other sites

31 minutes ago, Steven Mading said:

Just get the kOS version for KSP 1.4.1.  It works on KSP 1.4.2 just fine - it's just that CKAN and Spacedock and Curse don't know that.  If you ask them to allow you to install "incompatible" versions so it will list it, it works.

You misunderstand - I have the 1.4.1 version, but it's not working.

I can put the kOS part on my craft, and old designs using it still work, but right-clicking on it doesn't give me the button to open the console. I also don't have a kOS icon on my toolbar, like I used to.

Link to comment
Share on other sites

Just now, Hyperlynx said:

You misunderstand - I have the 1.4.1 version, but it's not working.

I can put the kOS part on my craft, and old designs using it still work, but right-clicking on it doesn't give me the button to open the console. I also don't have a kOS icon on my toolbar, like I used to.

If that was happening to everyone we'd know about it.  Others are able to use it.  There must be something wrong with the installation.  Can you put the output log in a pastebin somewhere and show it?

Link to comment
Share on other sites

1 minute ago, Steven Mading said:

If that was happening to everyone we'd know about it.  Others are able to use it.  There must be something wrong with the installation.  Can you put the output log in a pastebin somewhere and show it?

Oh whoops, I didn't realise you were the OP. Of course, it must be something at my end. I'll track down the logs...

Link to comment
Share on other sites

6 minutes ago, scimas said:

Oh, I thought the "error when trying to push infinity to stack" only occurred if I actually assigned the value to a variable.

The expressions evaluate using a stack, like an RPN calculator, if you've ever used one of those.

set z to x / y + 1.

for example ends up being like this:

push ident string "z".

push x

push y

divide

push 1

add

store  (top of stack now has Z and the value on it, this pops those two things from the stack, storing the value in the variable)

Every intermediate step along the way is temporarily on the stack as it goes through the expression.
 

Link to comment
Share on other sites

Hm. Is there any way to set up different script directories for different saved games? I have my scripts under Git source control, and would rather like to do that with my ship designs as well, in the one repo. I usually have a craft and a script to launch and operate that craft, but the crafts are stored per save and the scripts are stored globally, at least by default.

I guess as a workaround I can have the repo be in ksp/saves/savename/Ships and softlink a scripts directory to the ksp/Ships/Script directory. I just tried putting a subdirectory in boot, with the idea of having each save use a different subdir, but the contents was not recognised. I guess I'll need to append a namespace to my scripts' filenames, if I want to keep them from conflicting across saves. Not the end of the world, though, seeing as I'm probably not going to end up running two saves at the same time.

Link to comment
Share on other sites

11 hours ago, infinite_monkey said:

I'm not sure if it's kOS or something else, but after trying a script a LOT of times, failing and reloading, KSP is getting slower and slower. Could there be scripts running in the background if I don't terminate them before reloading?

I've noticed this too, but haven't tried doing similar steps without kOS to see if it is a plain KSP issue first and foremost. I have read posts where people say there are KSP memory leaks associated with certain actions that reload scenes.

There was a worry about a potential memory leak a year ago, but the Devs couldn't recreate the problem: https://github.com/KSP-KOS/KOS/issues/1960

Link to comment
Share on other sites

9 hours ago, ElWanderer said:

I've noticed this too, but haven't tried doing similar steps without kOS to see if it is a plain KSP issue first and foremost. I have read posts where people say there are KSP memory leaks associated with certain actions that reload scenes.

There was a worry about a potential memory leak a year ago, but the Devs couldn't recreate the problem: https://github.com/KSP-KOS/KOS/issues/1960

There definitely is some problem of memory leaks, either with mods or stock KSP, but it's obscure. Very recently, last week or so, I had my RSS, RO install crash from out of memory problem. I have 16GB of RAM. The game crashed, caused windows display manager or something to crash and I had to hard reboot it. The thing is, it had never happened before that and hasn't happened since then. And I know I haven't changed anything in that install, same mods, nothing more installed, nothing removed.

Link to comment
Share on other sites

Hmm, I didn't notice any memory leaks (I'm playing on Linux), but I wonder why on earth KSP is using 160% CPU (that is, 1.6 cores), even when paused.

Another thing I'm struggling with: Is throttle lock back? I don't know what's causing this yet, sometimes the it works, sometimes it doesn't. Trying to land my boosters SpaceX-style, but it fails to activate the engine for the sudicide burn. Boostback works, reentry burn works, suicide burn doesn't. Even if I quit the script, I can't control the throttle manually :0.0:

Edited by infinite_monkey
Nevermind, MechJeb was interfering.
Link to comment
Share on other sites

Alright I am having a REALLY hard with learning this mod. I am a programmer but I feel like things behind the scenes are changing without my knowledge. I have tested the below script multiple times and every time it has worked until now. I have not added any more mods or changed anything about the game. My problem is that the steering no longer works or even locks. It does not steer the vessel and I can still steer it. It used to be that I could not steer it while the script was running and it did steer itself correctly. Anyone able to see what I am doing wrong?

clearscreen.
sas off.
rcs on.
set ship:control:mainthrottle to 0.7.
print "COUNT DOWN!".
wait 1.
from { local countdown is 3. }
until countdown = 0 step { set countdown to countdown - 1. }
do {
  clearscreen.
  print "COUNT DOWN!".
  print countdown.
  wait 1.
}
clearscreen.
print "LAUNCH!".
stage.
wait 5.
lock steering to ship:up.
print "GEAR UP".
set gear to not gear.
wait 2.
print "SOLAR PANELS DEPLOYED".
ag10 on.
when ship:altitude > 100 then {
  print "CLEARED FOR FULL THROTTLE".
  set ship:control:mainthrottle to 1.0.
  return false.
}
when apoapsis >= 5000 then {
  print "APOAPSIS ABOVE 5km".
  print "TURNING TO 0 DEGREES".
  lock steering to heading(90, 0).
  return false.
}
when apoapsis >= 25000 then {
  print "REACHED APOAPSIS OF 25km".
  print "ENGINES OFF".
  set ship:control:mainthrottle to 0.0.
  return false.
}
wait until ship:apoapsis >= 25000.
sas on.
rcs off.
print "SAS TURNED ON".
print "---------".
print "SET MANEUVERING NODE FOR".
print "ORBITAL INSERTION BURN".
print "---------".

EDIT: I figured it out. It's a bug between kOS and Extraplanetary Launch pads. When I build a ship with EL pads and run a kOS script the steering does not work. But if I build the ship with EL pads THEN load up another ship more then 2.5 km away then go back and load up the ship built with EL pads then run a kOS script EVERYTHING works including the steering.

Edited by GuyOfNew
Link to comment
Share on other sites

I have a launch script that works.

I'm trying to convert it to put the launching part in a function, called by a button click. However, when I click the button my ship stages but doesn't throttle up. What's going on?

@LAZYGLOBAL OFF.

//flight parameters
//Note: these must be global, or the locks won't work
global targetApoapsis to 71000.
global targetPeriapsis to 70000.
global targetTWR to 1.5.
global gravTurnSpeed to 90.
global gravTurnAngle to 4.
global timeBeforeApoapsis to 30.
/////////////////////////////


SAS OFF.

LOCAL preflightGUI IS GUI(200).
LOCAL launchButton TO preflightGUI:ADDBUTTON("Launch").
set launchButton:ONCLICK to launch@.
LOCAL closeButton TO preflightGUI:ADDBUTTON("Close").

preflightGUI:SHOW().

until closeButton:TAKEPRESS
{
  // button still isn't pressed yet, let's keep waiting.
  wait 0.
}

function launch
{
  preflightGUI:HIDE().
  print targetApoapsis.
  print targetTWR.
 
  stage.
  //lock throttle to the target thrust-to-weight ratio:
 
  global lock distance to SHIP:ALTITUDE + BODY("Kerbin"):RADIUS.
  global lock weight to CONSTANT():G * ((SHIP:MASS* BODY("Kerbin"):MASS) / ( distance * distance )).
 
  lock THROTTLE to (targetTWR * weight) / (SHIP:MAXTHRUST + 0.001).
 
  lock steering to up + R(0, 0, 270).
 
  print "Ascending".
  wait until SHIP:VERTICALSPEED > gravTurnSpeed.
  lock steering to up + R(0, -gravTurnAngle, 270).
 
  print "Gravity turn".
 
  wait 10.
 
  // surface prograde
  lock steering to R(0,0,0) + velocity:surface.
 
  // when high enough, go hell-for-leather for orbital prograde.
  when ship:altitude > 27000 then
  {
    lock steering to ship:prograde.
    set targetTWR to 100.
  }
 
  when ship:maxthrust < 0.01 then
  {
    stage.
    when ship:maxthrust < 0.01 then
    {
      //don't thrust into the next stage, or it will explode and knock the craft off course
 
      lock throttle to 0.
      //wait for engine to stop
      declare global waitTime is TIME:SECONDS + 0.25.
      when TIME:SECONDS >= waitTime then
      {
        stage.
        set waitTime to TIME:SECONDS + 1.
        when TIME:SECONDS >= waitTime then
        {
          lock throttle to (targetTWR * weight) / (SHIP:MAXTHRUST + 0.001).
        }
      }
    }
  }
 
  print "Waiting until target apoapsis".
  wait until SHIP:APOAPSIS >= targetApoapsis.
  lock throttle to 0.
  print "Done".
}

 

Link to comment
Share on other sites

2 hours ago, Hyperlynx said:

I'm trying to convert it to put the launching part in a function, called by a button click. However, when I click the button my ship stages but doesn't throttle up. What's going on?

https://github.com/KSP-KOS/KOS/issues/2191

The button callback function is a trigger, so it blocks the triggers that update the steering and throttle whilst it is executing. Unless/until this behaviour gets changed, I would recommend that button callbacks should be short, simple functions.

I'd implement this as a run mode loop and have button prrsses try to change the run mode.

Link to comment
Share on other sites

34 minutes ago, ElWanderer said:

https://github.com/KSP-KOS/KOS/issues/2191

The button callback function is a trigger, so it blocks the triggers that update the steering and throttle whilst it is executing. Unless/until this behaviour gets changed, I would recommend that button callbacks should be short, simple functions.

I'd implement this as a run mode loop and have button prrsses try to change the run mode.

Excellent. Thanks!

Link to comment
Share on other sites

Hi guys and gals.

I've been getting a strange error  "Undefined Variable Ship" in one of my library files, I can't find anywhere that has Set Ship to or Local Ship, and the script runs fine when called on it's own. 

I'm not looking for support, just someone who might give me some other idea as to what to look for so I can debug it myself. 

Thanks

Geh

Link to comment
Share on other sites

1 hour ago, genericeventhandler said:

Hi guys and gals.

I've been getting a strange error  "Undefined Variable Ship" in one of my library files, I can't find anywhere that has Set Ship to or Local Ship, and the script runs fine when called on it's own. 

I'm not looking for support, just someone who might give me some other idea as to what to look for so I can debug it myself. 

Thanks

Geh

Which version of kOS? There was a problem in the past where if you logged out a vessel's object (e.g. to store the current target so that a rendezvous script doesn't break if the target gets unset midway through) it would be recorded as "SHIP('Kerbal X')" instead of "VESSEL('Kerbal X')", which then causes an error on trying to read it back in. I don't know if that error was 'Undefined variable ship', though, but it's the first thing that came to my mind.

https://github.com/KSP-KOS/KOS/issues/1776

Edited by ElWanderer
link to Github
Link to comment
Share on other sites

Can I run a programme on a kos processor that separates from my main ship, and will programmes continue to run if I go out of physics range of the vessel?

I want to launch a 4 satellite communications network, so plan to lift the initial vessel in to a resonant orbit and have each satellite separate and and circularize at successive apoapsis'.  So I'm presumably going to need the satellite's processor to be triggered by the separation, but it may well go out of physics range of the main vessel before finishing it's circularisation burn.

I could simply separate and then remotely type in commands, but it would be nice to try and automate the whole thing.

 

 

Link to comment
Share on other sites

27 minutes ago, RizzoTheRat said:

Can I run a programme on a kos processor that separates from my main ship, and will programmes continue to run if I go out of physics range of the vessel?

Yes (though with caveats - see below) and no respectively. If the vessel gets unloaded, kOS will stop running on it (and even if it kept running somehow, it would have nothing to control).

The caveats are that KSP itself limits certain abilities (e.g. adding a manoeuvre node) to the active vessel under player control.

Edit: one solution would be to make your script switch focus to the satellite, then have the satellite run a circularisation program that switches back to the mothership on completion. The script on the mothership would need to cope with its running being interrupted if it goes out of physics range while you're controlling the satellite.

Edited by ElWanderer
Link to comment
Share on other sites

15 hours ago, ElWanderer said:

one solution would be to make your script switch focus to the satellite, then have the satellite run a circularisation program that switches back to the mothership on completion. The script on the mothership would need to cope with its running being interrupted if it goes out of physics range while you're controlling the satellite.

 

Does a boot script run whenever you switch to a vessel that's got an processor on it that's not running anything, or is it just when loading from beyond physics range?

If the former then presumably I could have code on the satellites to do the circularization, and have the launching vessel finish it's code segment as it switches to the satellite so it starts again when I switch back. 

The only time I've left a vessel that was running a script was to do a rescue while the ship had a command to wait for 10 minutes, so I'm not sure what happens when you switch between kos enabled craft.

Link to comment
Share on other sites

Hi,

I just want to give a big thanks for this mod. I'm using it to make some spaceX-like 1st stage landing, and it's running great. I also use it to make a small plane autopilot to make long range flight in fastforward mode.

Having this working with my piece of code is a great satisfaction ^^

Link to comment
Share on other sites

2 hours ago, RizzoTheRat said:

Does a boot script run whenever you switch to a vessel that's got an processor on it that's not running anything, or is it just when loading from beyond physics range? 

Boot scripts run on load, any load (i.e. on loading a save, on putting a vessel on the launchpad, on coming within physics range or switching to a vessel that had been unloaded), if the processor is powered.

I've not done much with multiple CPUs per craft to separate out launcher code from payload code. One exception is my equivalent of an Apollo lander. That boots up on the launchpad, stores a bit of state (so the next time it boots, it acts differently), then turns its own CPU off, to be reawoken later (manually, but it could be automated) in Mun orbit.

As KSP limits some actions to the active vessel, I try to avoid having more than one vessel actively doing things at once (though this may be excessively cautious). You don't want the mothership time warping to the next satellite release while the last one is still manoeuvring, either. You could use inter-CPU/vessel messaging to pass control back and forth.

Edited by ElWanderer
Link to comment
Share on other sites

On 4/25/2018 at 12:32 PM, ElWanderer said:

Which version of kOS? There was a problem in the past where if you logged out a vessel's object (e.g. to store the current target so that a rendezvous script doesn't break if the target gets unset midway through) it would be recorded as "SHIP('Kerbal X')" instead of "VESSEL('Kerbal X')", which then causes an error on trying to read it back in. I don't know if that error was 'Undefined variable ship', though, but it's the first thing that came to my mind.

https://github.com/KSP-KOS/KOS/issues/1776

Thanks, I'm looking through the scripts and replacing all references to SHIP:  to vesselx: 

I hope it works


Geh

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...