Jump to content

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


Dunbaratu

Recommended Posts

3 hours ago, Tonas1997 said:

If my boot file has an error and I fix it in Notepad++ during the flight scene, how do I get the terminal to reload the file? When I reboot it doesn't seem to do so :/ 

There are two ways to do this. You can point the core to pick up the bootFile you changed on the archive using this:

set core:bootFileName to "<path to boot file on archive>".

Or, you can copy the updated bootfile from the archive to the '/boot/' folder on the core.

After you've done either of those, you can reboot and it should run the updated file.

Link to comment
Share on other sites

On 6/7/2021 at 2:04 AM, surge said:

Janus92,

Do you plan to release your work? Those images are quite impressive. I've nearly perfected the same thing for bodies which have no atmosphere. Available at http://obtlib.sourceforge.net mentioned above - file landnatm.ks. I won't link to the direct file because it depends on a few other parts of the system.

I've had a go at atmospheric versions, but never really nailed it. A long time ago a guy made a similar thing:

His relied heavily on RCS and B.R.A.K.E.S. (or whatever they're called now) and quite a few fudge factors from memory. but he had some innovative vector calculations in there. I am too dumb to understand them, but seeing your cute little targeting triangles, I figure you might be interested.

 

Actually I did. The most recent piece of code belongs to a new vehicle. This one also performs a boostback for the booster and lands it near the launchpad for 100% recovery.

Here's the link:

kOS is one of the best KSP mods around. Many thanks to @Dunbaratu!

5KpOswV.gif

Link to comment
Share on other sites

  • 3 months later...

Hello. I found the bug that breaks any kOS program in KSP 1.10.1+ and posted it on kOS github issues tracer: https://github.com/KSP-KOS/KOS/issues/3036

Basically it is an Null object reference error that fires every  time you try to change SASMODE when NavBall is hidden: in IVA (for example in Probe IVA mode) and breaks immersion if you want to play without entering flight view.

Is anyone knows any walk-around, please PM me.

Edited by 1greywind
Link to comment
Share on other sites

  • 6 months later...

I'm not sure if kOS is the cause of this or just hit by another problem: When I revert to launch, the log is spammed with this: 

Spoiler

ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
    System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <9577ac7a62ef43179789031239ba8798>:0)
    System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <9577ac7a62ef43179789031239ba8798>:0)
    kOS.Screen.Interpreter.IsWaitingForCommand () (at <3ccb18ebae2a49929054690a1a176760>:0)
    kOS.Screen.TermWindow.ProcessUnconsumedInput () (at <3ccb18ebae2a49929054690a1a176760>:0)
    kOS.Screen.TermWindow.Update () (at <3ccb18ebae2a49929054690a1a176760>:0)
    UnityEngine.DebugLogHandler:LogException(Exception, Object)
    ModuleManager.UnityLogHandle.InterceptLogHandler:LogException(Exception, Object)
    UnityEngine.Debug:CallOverridenDebugHandler(Exception, Object)

And all my staging is lost. When reverting again, half my vessel is missing. 

Link to comment
Share on other sites

17 hours ago, infinite_monkey said:

I'm not sure if kOS is the cause of this or just hit by another problem: When I revert to launch, the log is spammed with this: 

  Hide contents

ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
    System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <9577ac7a62ef43179789031239ba8798>:0)
    System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <9577ac7a62ef43179789031239ba8798>:0)
    kOS.Screen.Interpreter.IsWaitingForCommand () (at <3ccb18ebae2a49929054690a1a176760>:0)
    kOS.Screen.TermWindow.ProcessUnconsumedInput () (at <3ccb18ebae2a49929054690a1a176760>:0)
    kOS.Screen.TermWindow.Update () (at <3ccb18ebae2a49929054690a1a176760>:0)
    UnityEngine.DebugLogHandler:LogException(Exception, Object)
    ModuleManager.UnityLogHandle.InterceptLogHandler:LogException(Exception, Object)
    UnityEngine.Debug:CallOverridenDebugHandler(Exception, Object)

And all my staging is lost. When reverting again, half my vessel is missing. 

This can be a side effect of system load very often.  Are you running out of RAM?  Kos will send that spam until KSP responds to its requests during its booting of kos cores is all I know specifically, so the more overloaded KSP is, the more you see them.  When launching large craft I'll often do it right after firing up KSP and not much else running

Link to comment
Share on other sites

22 hours ago, darthgently said:

This can be a side effect of system load very often.  Are you running out of RAM?  Kos will send that spam until KSP responds to its requests during its booting of kos cores is all I know specifically, so the more overloaded KSP is, the more you see them.  When launching large craft I'll often do it right after firing up KSP and not much else running

Interesting. Indeed, with the last Linux kernel update, RAM handling seems quite different. While only half my RAM (32 GB) is shown as being used, only 3GB or less are available. And some mod in KSP seems to have issues as well, as on every scene switch (even map/flight), I can see RAM usage ramping up to 100%, then falling back to the previous level. This happens something like 8 times, taking more than 10 minutes, and then suddenly everything is fine. That makes KSP barely playable...

Anyway, the weird thing is that the issues with broken staging, missing parts (and apparently almost all the PartModules are gone) etc. only happens when loading a vessel sitting on the launchpad. So far it never happened when loading a scene where my vessel is in flight, and never when launching the vessel for the first time. I guess it's somehow related to parsing the savegame where the vessel sits on the launchpad.

So probably not related to kOS...

Link to comment
Share on other sites

9 hours ago, infinite_monkey said:

Interesting. Indeed, with the last Linux kernel update, RAM handling seems quite different. While only half my RAM (32 GB) is shown as being used, only 3GB or less are available. And some mod in KSP seems to have issues as well, as on every scene switch (even map/flight), I can see RAM usage ramping up to 100%, then falling back to the previous level. This happens something like 8 times, taking more than 10 minutes, and then suddenly everything is fine. That makes KSP barely playable...

Anyway, the weird thing is that the issues with broken staging, missing parts (and apparently almost all the PartModules are gone) etc. only happens when loading a vessel sitting on the launchpad. So far it never happened when loading a scene where my vessel is in flight, and never when launching the vessel for the first time. I guess it's somehow related to parsing the savegame where the vessel sits on the launchpad.

So probably not related to kOS...

I would also not suspect KSP necessarily, but between KSP and  mods memory leaks absolutely exist in my experience so if you run it for days it will eventually consume all memory.  My 64GB machine would last a few days; but I run a lot of mods and craft with 1 to 2 thousand parts at times when that machine is working .  If you have 32GB with only 3GB available  something is happening. 

I know that Linux has for some time "pre-allocated" memory and then gives it to processes as needed via some clever mechanism so the apparent amount of available memory can be deceiving; you likely have more available than it may appear.  But maybe they did change some code that isn't working as smart as it did before.  Idk.   It isn't just a simple first-come first-serve memory allocation system.  I can't remember the rationale, but it made good sense at the time when I read about it.  Processes still get the memory they need in spite of appearances from memory monitors

When pushing memory/swap I always have the worst time on the launchpad, no question about it.  That is the most sensitive scene for memory limitations in my experience; who knows why that might be so.

You could have a loose memory board or a bad one.  That has happened to me more than once.  Chips are just tiny complicated infra-red light bulbs with millions of filaments.  Eventually something burns out just like a light bulb

Link to comment
Share on other sites

5 minutes ago, newzilla7 said:

You're either in a scene which doesn't have an active vessel (like the VAB) or every vessel in the scene is "on rails" (like the tracking station).

But I'm at the launch pad. I downloaded the mod Starship Interface which automates the Starship mods via kos. How do I start that autopilot?

Link to comment
Share on other sites

Humm... did you include a kOS part in your vessel build?

Please excuse me if you already did that, and I am "overexplaining"... The thing is that you need to include a specific part in your build for kOS to run; there are several of them and are unlocked at different science tree nodes (if on Science or Career mode). I can't remember right now the specific names of the parts, so you will have to navigate the available parts (or the science tree) to find them... Hope this helps!

Also, this might help: CheersKevin tutorial on kOS (youtube)

Edit #1: added "overexplanation" paragraph ;O)

Edit #2: added youtube link.

Edited by alartor
Link to comment
Share on other sites

10 hours ago, alartor said:

Humm... did you include a kOS part in your vessel build?

Please excuse me if you already did that, and I am "overexplaining"... The thing is that you need to include a specific part in your build for kOS to run; there are several of them and are unlocked at different science tree nodes (if on Science or Career mode). I can't remember right now the specific names of the parts, so you will have to navigate the available parts (or the science tree) to find them... Hope this helps!

Also, this might help: CheersKevin tutorial on kOS (youtube)

Edit #1: added "overexplanation" paragraph ;O)

Edit #2: added youtube link.

In addition to that, there are some mods that add kOS to every command module, so you don't need additional parts. I use the following mod for that - in CKAN, it's named "MechJeb and Engineer for all!". Despite the name, it does add kOS as well. And it only adds the modules of mods you have actually installed, i.e. I have kOS, but I don't have MechJeb, so only kOS is added.

 

Link to comment
Share on other sites

  • 2 weeks later...
1 hour ago, MAFman said:

I'm writing a script to tune a PID controller for a drone using the Ziegler-Nichols method. How do I detect when the altitude or vertical speed starts oscillating, and figure out the period of oscillation?

Someone on StackOverflow mentioned the fast Fourier transform. How do I implement that?

Link to comment
Share on other sites

Actually, I have a semi-working script already. Just, it hard-codes the kP value instead of finding it experimentally. How do I find the kP value in a PID?

Here's my script:

// Tune a quadcopter using electric motors
function tuneQuad {
  clearscreen.
  core:doEvent("Open Terminal").
  print "Toggle AG1 to start tuning...".

  wait until ag1.
  print "Tuning...".
  sas on.
  wait 1.

  local kU is 6.
  local motors is list(
    ship:partsTagged("Front Left")[0]:getModule("ModuleRoboticServoRotor"),
    ship:partsTagged("Front Right")[0]:getModule("ModuleRoboticServoRotor"),
    ship:partsTagged("Rear Left")[0]:getModule("ModuleRoboticServoRotor"),
    ship:partsTagged("Rear Right")[0]:getModule("ModuleRoboticServoRotor")
  ).

  local pid is pidloop(kU, 0, 0, 0, 1).
  set pid:setpoint to 0.
  for m in motors {
    m:setField("RPM Limit", 460 * pid:update(time:seconds, verticalSpeed)).
  }

  wait until alt:radar > 500.
  local errorSign is -1.
  local flips is list().

  // Measure how many times the vertical speed bounces in 20 seconds
  local startTime is time:seconds.
  until time:seconds > startTime + 20 {
    if pid:error * errorSign < 0 {
      flips:add(time:seconds).
      set errorSign to -errorSign.
    }
    for m in motors {
      m:setField("RPM Limit", 460 * pid:update(time:seconds, verticalSpeed)).
    }
    wait 0.01.
  }

  // Calculate gains
  local total is 0.
  if flips:length > 0 for i in range(flips:length - 1, 1) {
    set total to total + (flips[i] - flips[i - 1]).
  }

  // Formulas taken from https://en.wikipedia.org/wiki/Ziegler%E2%80%93Nichols_method

  local pU is total / (flips:length - 1) * 2.
  local kP is kU * (1/3).
  local tI is 0.5 * pU.
  local tD is (1/3) * pU.
  local kI is ((2/3) * kU) / pU.
  local kD is (1/9) * kU * pU.

  set pid:kP to kP.
  set pid:kI to kI.
  set pid:kD to kD.

  print "Landing...".
  set pid:setpoint to -5.
  until alt:radar < 1 {
    for m in motors {
      m:setField("RPM Limit", 460 * pid:update(time:seconds, verticalSpeed)).
    }
  }
  for m in motors m:setField("RPM Limit", 0).
  wait 1.
  print "Exporting PID gains to file...".
  log "set kP to " + kP + ". set kI to " + kI + ". set kD to " + kD + "." to "0:/" + ship:name + "PidValues.ks".
}

// Tune a drone using jets or rockets for thrust
function tuneJetDrone {
  clearscreen.
  core:doEvent("Open Terminal").
  print "Toggle AG1 to start tuning...".

  wait until ag1.
  print "Tuning...".
  sas on.
  stage.
  wait 1.

  local kU is 6.

  // Full throttle until 500 meters
  lock throttle to 1.
  wait until alt:radar > 500.

  local pid is pidloop(kU, 0, 0, 0, 1).
  set pid:setpoint to 0. // Target vertical speed of 0
  lock throttle to pid:update(time:seconds, verticalSpeed).

  // Wait for vertical speed to go negative
  wait until verticalSpeed < 0.
  local errorSign is -1.
  local flips is list().
  local err is 0.

  // Measure how many times the vertical speed bounces in 20 seconds
  local startTime is time:seconds.
  until time:seconds > startTime + 20 {
    set err to max(err, abs(ship:verticalSpeed)).
    if ship:verticalSpeed * errorSign < 0 {
      flips:add(time:seconds).
      set errorSign to -errorSign.
    }
    lock throttle to pid:update(time:seconds, verticalSpeed).
    wait 0.01.
  }

  // Calculate gains
  local total is 0.
  if flips:length > 0 for i in range(flips:length - 1, 1) {
    set total to total + (flips[i] - flips[i - 1]).
  }

  // Formulas taken from https://en.wikipedia.org/wiki/Ziegler%E2%80%93Nichols_method

  local pU is total / (flips:length - 1) * 2.
  local kP is kU * (1/3).
  local tI is 0.5 * pU.
  local tD is (1/3) * pU.
  local kI is ((2/3) * kU) / pU.
  local kD is (1/9) * kU * pU.

  set pid:kP to kP.
  set pid:kI to kI.
  set pid:kD to kD.

  print "Landing...".
  set pid:setpoint to -5.
  until alt:radar < 2 {
      lock throttle to pid:update(time:seconds, verticalSpeed).
      wait 0.01.
  }
  lock throttle to 0.
  wait 1.
  print "Exporting PID gains to file...".
  log "set kP to " + kP + ". set kI to " + kI + ". set kD to " + kD + "." to "0:/" + ship:name + "PidValues.ks".
}

local type is "".
for p in ship:parts {
  if p:hasModule("ModuleRoboticServoRotor") set type to "quad".
  else if p:hasModule("ModuleEngine") set type to "jet".
}
if type = "quad" tuneQuad().
else tuneJetDrone().

 

Edited by MAFman
Added a detail
Link to comment
Share on other sites

52 minutes ago, Frostiken said:

YT9q1h0.png

 

Guys... what?

The statement terminator for kOS is a period char. So it's complaining that you don't have the terminator. 

Try "clearscreen."

You might find the documentation useful: https://ksp-kos.github.io/KOS/index.html

Edited by Zelda
Adding documentation link
Link to comment
Share on other sites

13 minutes ago, Zelda said:

The statement terminator for kOS is a period char. So it's complaining that you don't have the terminator. 

Try "clearscreen."

You might find the documentation useful: https://ksp-kos.github.io/KOS/index.html

Hahah okay that did it.

 

One must use proper grammar when interfacing with kOS ;)

Link to comment
Share on other sites

How do I tune a PID controller to control a drone's altitude using the Ziegler Nichols method? Specifically, the first step is tripping me up, where the proportional gain starts at zero and is slowly ramped up until the output oscillates. How do I do that with the plant variable being motor throttle and the measured variable being altitude?

Link to comment
Share on other sites

  • 2 weeks later...

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