Jump to content

[1.3] kOS Scriptable Autopilot System v1.1.3.0


erendrake

Recommended Posts

I just use the variable itself; the first time it's used in the program is to start slowing myself down for descent in my Mun lander.

The section is:

Wait until Alt:Radar < 10000.

Until Periapsis <0 {

Lock Throttle to 1.0.

Set Ship:Control:Pilotmainthrottle to 1.0.

}

Now, I'm new to coding so this could very well be a case of the problem existing between chair and keyboard, but the ship just sits there like a lemon until the altimeter reads 10,000.

I know I'm not just above a flat surface because later on, I use it again (the exact same way) to start slowing down when it reaches 2,000. My engines barely cut in before I slam into the ground.

Link to comment
Share on other sites

you only evaluate alt:radar once. it never updates.

try:

lock radarAlt to alt:radar.
wait until radarAlt < 10000 { // do something }

If you use lib_pid.ks you could try my full script.

It's on pastebin at lander.ks

There's a few oddities in in, for example I had to reverse the initial PID controller since I look at absolute velocity without taking direction into account.

However, it's landed a few contracts on the Mun successfully.

Edited by Snarfster
Link to comment
Share on other sites

you only evaluate alt:radar once. it never updates.

try:

lock radarAlt to alt:radar.
wait until radarAlt < 10000 { // do something }

If you use lib_pid.ks you could try my full script.

It's on pastebin at lander.ks

There's a few oddities in in, for example I had to reverse the initial PID controller since I look at absolute velocity without taking direction into account.

However, it's landed a few contracts on the Mun successfully.

Something very peculiar seems to be happening with my copy of the mod.

Not only does your solution yield the same result, but your program isn't working; I took a screenshot.

FE21F8CE051454A72A7A7879FF3AEA19DBC2D331

I'm definitely starting to think I messed up somewhere, although it might be a conflicting mod...

Link to comment
Share on other sites

Having some trouble unlocking throttle. I've got a routine (adapted from tutorial) to execute a maneuver node and then unlock throttle, but no matter what I do it lets out this big 100% throttle belch for what appears to be a single physics tick before shutting down, which completely blows my careful node attempt. I don't know what in here is triggering the 100% throttle.


lock THROTTLE to valThrottle.

...

until done {
SAS on.
set manNodeVec to lookdirup(manNode:DELTAV, SHIP:FACING:TOPVECTOR).
lock STEERING to manNodeVec.
set shipAcc to thrust_avail(engineList)/SHIP:MASS.
set manNodeBurn to manNode:DELTAV:MAG/shipAcc.

set valThrottle to -cos(min(manNodeBurn, 2)*90)/2.06 + 0.51.

if vdot(dvStart, manNode:DELTAV) < 0 {
// starting and current vectors are opposing - we've overshot.
lock THROTTLE to 0.
remove manNode.
set done to True.
break.
}

if manNode:DELTAV:MAG < 0.25 {
// Burn slowly until our node vector starts to drift significantly from initial vector
wait until vdot(dvStart, manNode:DELTAV) < 0.5.
lock THROTTLE to 0.
remove manNode.
set done to True.
break.
}

wait 0.001.
}

unlock STEERING.
unlock THROTTLE.
set SHIP:CONTROL:PILOTMAINTHROTTLE to 0.
wait 1.

Any thoughts on how to avoid that? I have a pretty good launch/circularize script now. My next step is to build up a library of helpers that will allow me to reliably do a direct ascent to synchronous directly above KSC without over/undershoot. If I can work that out, then a set of helpers to move from that orbit to sync above any given longitude. Looking to make a set-it-and-forget-it script for building RT comm networks.

Link to comment
Share on other sites

I just use the variable itself; the first time it's used in the program is to start slowing myself down for descent in my Mun lander.

The section is:

Wait until Alt:Radar < 10000.

Until Periapsis <0 {

Lock Throttle to 1.0.

Set Ship:Control:Pilotmainthrottle to 1.0.

}

Now, I'm new to coding so this could very well be a case of the problem existing between chair and keyboard, but the ship just sits there like a lemon until the altimeter reads 10,000.

I know I'm not just above a flat surface because later on, I use it again (the exact same way) to start slowing down when it reaches 2,000. My engines barely cut in before I slam into the ground.

The stock KSP game itself puts a max limit on reading terrain height with the radar altimeter. It often can't read it as high as 10,000. The exact distance will depend on your game's graphic detail settings because it's affected by how far away you have to be from the ground for it to stop being made of solid (collidable) polygons and instead be a fuzzy hologram.

When kOS gets bogus readings from the stock game's radar altimeter because it's too high to take a reading, it returns the areo altimeter as a fallback, as that's better than returning -1.

Link to comment
Share on other sites

That makes sense, but I don't think that's the problem.

As I said, I have another line for when it gets 2,000 metres up using the same command, and that happens when the altimeter reads 1,999.

Unless it's also less than two kilometers...

But I have kerbal engineer on, and that seems to be giving me a perfectly functional terrain altimeter; is that relevant? Do they cut out at the same height?

Link to comment
Share on other sites

Hi everybody,

Great work on this mod! It's really expanding the types of projects I'm planning on doing in KSP.

I'm trying to learn from

but I can't get them to start. It seems to be complaining when I try to copy exenode.ks to the local volume.

QsvOk6e.png

The above screenshot shows what I've done to troubleshoot this so far.

- exenode.ks exists

- I can copy other files (aponode.ks went over without any problems)

- I tried both with and without the .ks extension. Both seemed to complain.

I'm thinking exenode is a reserved word. Has that been turned into a built in function/command or something like that?

Link to comment
Share on other sites

So, I made up this landing code that should be able to land my anywhere I so please. I just have to set the name of the body I'm around to BodyName, whether it's a specific site I'm trying to land at with TargetLanding (assumes inclination is already perfect) and if I am where. And whether or not I want to ditch the thing that de-orbited me. Largely based on KK4TEE's tutorials, just modified for my own use. Problem is, it doesn't initialize the first mode. I don't know what's causing the error, because it's parallel to my launch code. Again, largely based on this fella's stuff. If someone could tell me why the first runmode doesn't kick off, that'd be great. When I abort the terminal spits out "Argument out of range. Parameter name: index. At interpreter history, line 6.". Can someone help me please, 'cause I don't know the issue. Oh, and Jeb is stuck in orbit until this is fixed. I didn't pack parachutes.

I have two scripts, one to prepare things, and another to actually do functions. The problem is with the second one, but I feel obligated to post both. As a side note, if someone could check my math syntax, that'd be great. It's still pretty foreign to me

landing.ks

//use variable "runmode" to represent different program stages, 0 will represent off

//GOAL: Originally used to pull all the subprogram databases, but those variable weren't kept. Databases were merged with GoLand.

//General constants
set yes to 1.
set no to 0.

//Needed subprograms
copy goland from 0.

set runmode to 1. //start program

until runmode = 0 {

print "Please set BodyName and lock in whether TargetLanding is active.".
print "If TargetLanding is active, then set location.".
print "Lock in whether DitchServMod is active.".
print "Waiting for the the GoLand.".
set runmode to 0.

}

goland.ks

print "Ready for action!".
wait 2.
clearscreen.

//Use variable "runmode" to represent different program stages.

//GOAL: Land craft safely under its own power

//Set ship to known state
SAS off.
RCS off.
lights off.
set throttle to 0.
gear off.

//General constants
set yes to 1.
set no to 0.

//Body database
set Kerbin to 1.
set Mun to 2.
set Minmus to 3.
set Duna to 4.
set Ike to 5.
set Eve to 6.
set Gilly to 7.
set Moho to 8.
set Dres to 9.
set Jool to 10.
set Laythe to 11.
set Vall to 12.
set Tylo to 13.
set Bop to 14.
set Pol to 15.
set Eeloo to 16.

until BodyName = 0 { //BodyName will substitute runmode

if BodyName = 1 {
set ParkOrbAlt to 80000.
set AtmoPresence to yes.
set AtmoAlt to 70000.
set ReEntryPeri to 20000.
set BodyName to 0.
}

if BodyName = 2 {
set ParkOrbAlt to 20000.
set AtmoPresence to no.
set AtmoAlt to no.
set ReEntryPeri to no.
set BodyName to 0.
}
}

//Target database

if TargetLanding = no {
set LandingSite to 0.
set DeOrbBurnLNG to no.
}

//Kerbin
set KSC to 1.

//Mun

//Minmus

//Duna

//Ike

//Eve

//Gilly

//Moho

//Dres

//Laythe

//Vall

//Bop

//Pol

//Eeloo

until LandingSite = 0 {

if LandingSite = 1 {
set DeOrbBurnLNG to -60. //Value is a placeholder
set LandingSite to 0.
}

}

//Needed data, math, and constants
lock ShipLatLng to SHIP:GEOPOSITION. //set shipLatLng to the ship's LatLng
lock SurfElevation to ShipLatLng:TERRAINHEIGHT. //height of ground
lock RadarAlt to (ALTITUDE - SurfElevation). //Mods can make ALT:RADAR glitched, so this unsures that we get our true alt
lock ImpactTime to (RadarAlt / -VERTICALSPEED). //Time until we hit the ground, not including gravity gains or drag
set Gravity to ((constant():G * BODY:MASS) / BODY:RADIUS^2).
lock TWR to max( 0.001, (MAXTHRUST / (SHIP:MASS * Gravity))).

set runmode to 1. //start program
set autostage to no.
set TVAL to 0.
lock throttle to TVAL.

until runmode = 0 {

if runmode = 1 {
if ETA:APOAPSIS > 90 {
set warp to 3.
}
if ETA:APOAPSIS < 90 {
set warp to 0.
set runmode to 2.
}
}

if runmode = 2 {
lock steering to RETROGRADE.
if ETA:APOAPSIS < 5 {
set TVAL to (1.1 - (ParkOrbAlt / SHIP:PERIAPSIS)).
}
if PERIAPSIS < ParkOrbAlt {
set TVAL to 0.
set runmode to 3.
}
}

if runmode = 3 {
if ETA:PERIAPSIS > 90 {
set warp to 3.
}
if ETA:PERIAPSIS < 90 {
set warp to 0.
set runmode to 4.
}
}

if runmode = 4 {
lock steering to RETROGRADE.
if ETA:PERIAPSIS < 5 {
set TVAL to (1.1 - ((SHIP:PERIAPSIS + 500) / SHIP:APOAPSIS)).
}
if APOAPSIS < (PERIAPSIS +500) {
set TVAL to 0.
set runmode to 5.
}
}


if runmode = 5 {
if TargetLanding = yes {
if (ShipLatLng:LNG < (DeOrbBurnLNG - 10)) or (ShipLatLng:LNG > (DeOrbBurnLNG + 2)) {
set warp to 3.
}
if ShipLatLng:LNG > (DeOrbBurnLNG - 10) {
set warp to 0.
}
if ShipLatLng:LNG > (DeOrbBurnLNG - 5) {
set runmode to 6.
}
}
if TargetLanding = no {
set runmode to 6.
}
}

if runmode = 6 {
if AtmoPresence = yes {
lock steering to RETROGRADE.
set TVAL to (1.33 - (ReEntryPeri/ PERIAPSIS)).
if PERIAPSIS < ReEntryPeri {
set TVAL to 0.
if DitchServMod = yes {
stage.
}
set runmode to 7.
}
}
if AtmoPresence = no {
lock steering to (VELOCITY:SURFACE * -1).
set TVAL to (1.05 - (3 / SURFACESPEED)).
if SURFACESPEED < 3 {
set TVAL to 0.
if DitchServMod = yes {
stage.
}
set runmode to 7.
}
}
}

if runmode = 7 {
//INSERT PANELS OFF HERE
lock steering to (VELOCITY:SURFACE * -1).
if AtmoPresence = yes {
if SHIP:ALTITUDE > AtmoAlt {
set warp to 3.
}
if SHIP:ALTITUDE < AtmoAlt {
set warp to 0.
set runmode to 8.
}
}
if AtmoPresence = no {
if ImpactTime > 180 {
set warp to 3.
}
if (ImpactTime < 180) and if (ImpactTime > 120) {
set warp to 2.
}
if ImpactTime < 120 {
set warp to 0.
set runmode to 8.
}
}
}

if runmode = 8 {
lock steering to (VELOCITY:SURFACE * -1).
set LandingRadar to min( ALTITUDE, RadarAlt).
set TVAL to (( 1 / TWR) - ( verticalspeed + max( 5, ((LandingRadar^1.08) / 8))) / 3 / TWR).
if RadarAlt < 20 and ABS(VERTICALSPEED) < 1 {
lock throttle to 0.
lock steering to up.
print "Landed!".
wait 2.
set runmode to 0.
}
}
}

Remember: JEB NEEDS YOU!

Edited by Storywalker4
Link to comment
Share on other sites

I have two scripts, one to prepare things, and another to actually do functions. The problem is with the second one, but I feel obligated to post both. As a side note, if someone could check my math syntax, that'd be great. It's still pretty foreign to me

I assume you are running these both at the command line since it doesn't look like either script runs the other. Could you post a screenshot of the terminal where you run these along with the error message?

Link to comment
Share on other sites

The terminal doesn't output anything other than what I said, and yes. One reminds me to set things, and one actually does things. There is no error that appears, but it doesn't function, it just sits and does nothing. Program is active and running, but runmode 1 never does what runmode 1 should do. The abort spits out the lines I said before "Argument is out of range. Parameter name: index. At interpreter history, line 6." I guess I can do a picture if you really demand it.

So I took 13 screenshots at different points, all with different results. More or less. So I'll just direct you to my photobucket page.

http://s1248.photobucket.com/user/Woz02/library/?view=recent&page=1

If there are any repeats, I apologize.

Edited by Storywalker4
Link to comment
Share on other sites

I think you are getting stuck in the "Until BodyName = 0" loop.*

The problem is BodyName is initialized to the string "Kerbin", not the variable Kerbin. So the IF blocks inside that loop never get executed. Try changing those to use an If/else block where you show an error message if it doesn't meet any of your expected values. To debug this, I'd also suggest adding a print statement into that loop so you know what BodyName is.


if BodyName = 1
{
// do BodyName 1 stuff
} else if BodyName = 2
{
// do BodyName 2 stuff
} else {
// handle unexpected values...
}

but even better yet would be to stop using the numbers

something like


set Space to 0.
Until BodyName = Space
{
if BodyName = Kerbin
{
// do Kerbin stuff
set BodyName to Space.
} else if BodyName = Mun {
// do Mun stuff
set BodyName to Space.
}
}

* - I've only been looking into kOS for a few days so this may all be wrong (but I program for a living)

Link to comment
Share on other sites

Oh, I didn't know Kerbin was a proper string. That would make sense. BodyName never gets set to 0, so it never moves past the until loop. Moreover, that could explain why my subprogram database idea didn't work: the values where never set. Although now that I think about it, the BODY:THINGYMABOB things have to have "body" defined. I haven't had "else if" work, and "if" works just the same, so I don't use "else if." Never tried a normal "else." But thanks! I'll use abbreviations probably. I'm lazy like that. And in case that isn't the problem, the body database won't get in the way.

And I'm new to kOS too. My first script was literally "point up, stage, wait time, stage, at alt turn, wait, stage, at alt turn, wait stage. Now circularize yourself you lazy jerk, I'm done with running this rocket for you."

Edited by Storywalker4
Link to comment
Share on other sites

I can has a possible solution!

tag well known rare part type with the tag name "

find the part, get the tag name, use + to build any string I want with single double quotes. Profit.

Now all I have to do is write enough scripts with out it to fly enough missions to upgrade the VAB so I can set tags and see if my evil plan works.

Found a short cut (no VAB upgrade...) plant flag or name some ship ", search for flag vessel where ">" + avessel:name + avessel:name + "<" = ">""<".

local dblquote to avessel:name. //dbltquote now holds the string "

print (">""<"). // produces this as output   >""<

also something that would be nice to have is ...


// TBMK
function DoSomeFunctionyThingAsSideeffect { {...the useful bit...} return 0.}
lock foo = DoSomeFunctionyThingAsSideeffect().

global event to list (when_i_want_it_to_happen, foo). // foo and side effect currently happens here.

until false {
if time:seconds> event[0] {
set dummy to event[1]. // << it would be nice if event[1] was a reference to the lock and caused the side effect here... it does not.
}
wait 0.1.
}
// basically it would be nice to have any mechanism to store pointer to a function. Then execute it when associate data says it time.
// locks get close but then you cant lump them into lists. {poor man structs/classes} {So I cant have an event /task queue} :{
// plausible new syntax?
// local Thunk to function DoSomeFunctionyThingAsSideEffect.
// later....
// Thunk(). //calls the function is was set to.
// Thunk( parm1, parm2). // would be nice too.

usecase. Not every rocket I build has the same fuel management requirements. Some need bottom up tank by tank fuel use or they flip/explode. Some, once, used fuel from stages I am not jettisoning yet/ever.

Because i actually want the empty tanks in space... So being able to neatly bolt on fuel usage management would be nice. I have ways and means, aka which lib did i run. (dont change the function ptr change the code) but sometimes it can be dynamic?

Its shiny.

Edited by Onchyophora
clarity
Link to comment
Share on other sites

Oh, I didn't know Kerbin was a proper string. That would make sense. BodyName never gets set to 0, so it never moves past the until loop.

I'm not sure that "SET BodyName to Kerbin" gets set to a string but I'm pretty sure it doesn't get set to 1 since you initialize the Kerbin variable in the second script but you do the set before that script gets run.

Why don't you use SHIP:BODY to find out what sphere of influence you are in rather than prompt for it? That should make your script a bit more robust...

Link to comment
Share on other sites

I'm not sure that "SET BodyName to Kerbin" gets set to a string but I'm pretty sure it doesn't get set to 1 since you initialize the Kerbin variable in the second script but you do the set before that script gets run.

Why don't you use SHIP:BODY to find out what sphere of influence you are in rather than prompt for it? That should make your script a bit more robust...

You see, I was thinking about that... but I'm not sure if it'd work with the outer planets mod. And with the distance of those bodies, signal delay would be ages. (Neidon takes over 20 years to intercept.)

Link to comment
Share on other sites

Hi everybody,

Great work on this mod! It's really expanding the types of projects I'm planning on doing in KSP.

I'm trying to learn from

but I can't get them to start. It seems to be complaining when I try to copy exenode.ks to the local volume.

http://i.imgur.com/QsvOk6e.png

The above screenshot shows what I've done to troubleshoot this so far.

- exenode.ks exists

- I can copy other files (aponode.ks went over without any problems)

- I tried both with and without the .ks extension. Both seemed to complain.

I'm thinking exenode is a reserved word. Has that been turned into a built in function/command or something like that?

I bet your disk is full. Try

Switch to 1. List.

And check out how much is left. According to your post you need about 2600 space.

- - - Updated - - -

is there any way to read the new 1.0 thermal properties, as shown in the right click panel after fiddling on the debug menu? Or even read overheat values when things are about to explode?

Link to comment
Share on other sites

Hi everybody,

Great work on this mod! It's really expanding the types of projects I'm planning on doing in KSP.

I'm trying to learn from

but I can't get them to start. It seems to be complaining when I try to copy exenode.ks to the local volume.

http://i.imgur.com/QsvOk6e.png

The above screenshot shows what I've done to troubleshoot this so far.

- exenode.ks exists

- I can copy other files (aponode.ks went over without any problems)

- I tried both with and without the .ks extension. Both seemed to complain.

I'm thinking exenode is a reserved word. Has that been turned into a built in function/command or something like that?

Hi, those are my scripts, not KK4TEE :)

Exenode is a script that executes a maneuver node, you can even find it in examples on KOS website (there is a typo there, but it's easy to fix).

Also, I suggest learning from slightly simpler scripts, but I'll try to help you anyway.

The scripts in the video you linked were written for KOS 0.16.2 and KSP 0.90, and quite a lot was changed since then, for example the ascend script is very different now.

I suggest you take a glance at KSLIB library of scripts found here (https://github.com/KSP-KOS/KSLib) for more up-to-date code examples.

Since that video I've rewritten the code slightly to take advantage of new KOS features and if you want I can show you the parts that are of interest to you.

- - - Updated - - -

is there any way to read the new 1.0 thermal properties, as shown in the right click panel after fiddling on the debug menu? Or even read overheat values when things are about to explode?

Not yet, but I've seen some work being done on that front, so I expect it to be added at some point.

Edited by Ziw
Link to comment
Share on other sites

I bet your disk is full. Try

Switch to 1. List.

And check out how much is left. According to your post you need about 2600 space.

I'm not unable to reproduce this but I've been messing with the scripts so you may be right. Odds are I shortened my test scripts and now I'm not filling the drive up. UPDATE: scratch that. My problem was I was trying to do this from a boot script. It looks like KOS sometimes kicks off the boot scripts before everything is initialized.

Hi, those are my scripts, not KK4TEE :)

Exenode is a script that executes a maneuver node, you can even find it in examples on KOS website (there is a typo there, but it's easy to fix).

Also, I suggest learning from slightly simpler scripts, but I'll try to help you anyway.

The scripts in the video you linked were written for KOS 0.16.2 and KSP 0.90, and quite a lot was changed since then, for example the ascend script is very different now.

I suggest you take a glance at KSLIB library of scripts found here (https://github.com/KSP-KOS/KSLib) for more up-to-date code examples.

Since that video I've rewritten the code slightly to take advantage of new KOS features and if you want I can show you the parts that are of interest to you.

Sorry for the mis-attribution. I found the scripts on a youtube video on a post from KK4TEE so I assumed that KK4TEE was the source of the scripts. The part I was interested in was the getting into orbit part but I'll try to find more basic scripts that do that. Thanks!

Edited by AndreZero
Link to comment
Share on other sites

What is the best way to detect that a solid booster is spent so one could trigger a stage? Here is my attempt at it


SET twr to MAXTHRUST / (MASS * 9.81).
SET firstStage TO 1.

UNTIL firstStage = 0.
{
SET previousTwr to twr.
IF previousTwr > (twr * 1.3) // if TWR drops by 30% assume the booster just stopped
{
PRINT "Solid boosters exhausted. Staging.".
STAGE.
SET firstStage TO 0.
}
}

This works, but I feel like there has to be a more graceful way to do it, especially since the 30% is a totally arbitrary number.

Also, how would you suggest rolling a given amount? I'd like to be able to say "roll 90 degrees" but I'm having a hard time figuring out how to do that. I can use the LOCK STEERING TO R(0,0,90) but that assumes I was going 0,0,0 prior to the roll. I'm thinking I might need to use the "raw" controls, but I don't know how to detect how far I've rolled so I can know when to stop.

Link to comment
Share on other sites

Hi Guys, i have a small question.

Since i played kos, i had the issue, that i have to force sas to off when launching, or otherwise steering won't work.

I am speaking of the first action after hitting the launch button in the vab.

The SAS Button is showing it is deactivated, but steering won't work unless i have a "SAS off." at the beginning.

Seems a little strange to me, that sas initially seems to be floating somewhere, but that is only strange and not a problem.

Playing Career now and here comes the problem.

For the Stayputnik you can't disable SAS because it has none.

So every Steering command just gives me very fast oscilations for the steering but no actual steering.

It is not even working with the simplest "lock steering to north." command.

Flight Computer is also deactivated.

Is this Problem known?

Perhaps a Problem together with another mod?

Link to comment
Share on other sites

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