Jump to content

kOS Scriptable Autopilot System 0.9


KevinLaity

Recommended Posts

If anyone ever wondered, you CAN check against booleans!!!

just fugued it out :D

we all know that his dose no work, it will return an error and stop:



if INLIGHT = 0 {print "It's dark out there".} // Returns true if not blocked by celestial body, always false without solar panel.

if you print a boolean. You get True or False... but it is not 0.

Well they are just STRINGS lol..

So you can do this:



if INLIGHT = [B][COLOR="#FF0000"]"False"[/COLOR][/B] {print "It's dark out there".} // Returns true if not blocked by celestial body, always false without solar panel.

Was this common knowledge? :D

Link to comment
Share on other sites

I'm going to have to try that. I grabbed parts of your code from far2 to try for later. I'm trying to get a launcher with 3 komm sats for RT2 into kerbosync. I've got my launcher above 286...blah blah...and have tried getting a node set to the exact altitude needed with not much luck. It just keeps bouncing around the altitude needed. Maybe it'll work for me. :) Once I get my AP where i want, I have to try and see if I can get the PE to where I need it for a 4hr orbital period...but I'll cross that bridge once I get the AP settled. :)

Just worked out how to calculate deltav for changing orbit altitudes. So if your issue is calculating deltav from an existing orbit try this:

// find deltav to move apoapsis target altitude
// use energy invariance to calculate deltav
// by changing vp to vp' the semi major axis changes from a to a'
// 0.5*vp^2 - mu/r + 0.5*mu/a = 0.5*vp'^2 - mu/r + 0.5*mu/a'
// vp'^2 = vp^2 + mu( 1/a - 1/a' )
set mu to 3.5316000*10^12. // mu = G mk, mk: mass of Kerbin
set rk to 600000. // radius of Kerbin
set rm to 200000. // radius of Mun
set r to rk + periapsis.
set a to rk + apoapsis. // semi major axis current orbit
set a2 to (r + rk + mun:altitude)/2. // semi major axis target orbit
set vom to velocity:orbit:mag.
set v2 to sqrt( vom^2 + (mu * (1/a - 1/a2) ) ).
set deltav to v2 - vom.
print "v2: " + round(v2).
print "deltav: " + round(deltav).
set x to node(time:seconds + eta:periapsis, 0, 0, deltav).
add x.
// workaround for scientic numbers bug on load
set mu to 0.

Replace mun:altitude by your target altitude. When using a 100km orbit, 90km target altitude I get a -11.8m/s burn, and the maneuver node calculator estimates an 87.5km periapsis. That is 3% error in altitude, much smaller when looking at the semi axis (~687km instead of 690km).

Not sure at what accuracy you want to hit your altitude.

Btw, my node chaser script has not been tested (i.e. will not work) with changes of deltav < 2 maxthrust/mass.

Edited by baloan
Link to comment
Share on other sites



if INLIGHT = 0 {print "It's dark out there".} // Returns true if not blocked by celestial body, always false without solar panel.

Is inlight a thing? Cool....did not know that. Could be good for turning things off that use a lot of power, like kethane drills/convertors.....Didn't think about using kOS for kethane drills....you could automate the conversion process... :)

Just worked out how to calculate deltav for changing orbit altitudes. So if your issue is calculating deltav from an existing orbit try this:

***CODE HERE***

Replace mun:altitude by your target altitude. When using a 100km orbit, 90km target altitude I get a -11.8m/s burn, and the maneuver node calculator estimates an 87.5km periapsis. That is 3% error in altitude, much smaller when looking at the semi axis (~687km instead of 690km).

Not sure at what accuracy you want to hit your altitude.

Btw, my node chaser script has not been tested (i.e. will not work) with changes of deltav < 2 maxthrust/mass.

Cool, I'll give this one a try when I get a chance then. As far as accuracy goes, I know it likely won't be too accurate just because of the time lag in parsing the if statements.

**Update** Maybe I'm not doing something right...I replaced mun:altitude with 2868750 and when it runs all I get is NaN for both v2 and deltav. I didn't do anything with the vp part of the code, should I have used that some how?

I took the round function out of the code/formula and NaN went away(though looking at it again that was just for the print statements lol), but still not getting a number that'll get me what I want. hmmm

Edited by Sma
Link to comment
Share on other sites

If someone could help me out here. I'm trying to run this very simple demo script: https://dl.dropboxusercontent.com/u/5470754/simple.txt

It runs fine at first but it never quits the final circularization burn for some reason. It does eventually stop, but I have no idea what it relates to. My periapsis by that time is somewhere 8Gm from the sun.

Link to comment
Share on other sites

@Cilph The problem with checking periapsis is that when it flips over (apoapsis becomes periapsis and conversely), its value won't raise anymore. I don't know if you understand what I'm trying to tell, but basically, it's a better idea to check on semi-major axis rather than periapsis as you can't really tell if it's going to increase to the value you want or not. When you burn prograde, you know your semi-major axis will raise, but if you are not precise enough (this script is basic so it is not precise at all), the periapsis won't raise the way you want it. (Ever missed a maneuver node a bit ? Saw how much your orbit went far away from predicted ? ^^ ) Anyway, glad you took time to see what kOS is about, this means you will understand how important is a RT/kOS compatibility for the depth of the game. :P

To anyone who answered my "when .. then .. " problem, thank you ! Basically I already figured out how to determine if the vessel needs a staging, what I needed was a way to ensure that even if 1000 stages are needed, they still would fire during ascent. I like the until {} method but there will be repeated portions of code as I would need several of them, that's why I chose when .. then .. in the first place. I don't know if the recursion could work in another file (basically if a when then statement survives the end of the program), I will test that, and if it works I will use it ! Getting back to you with the results. :wink:

Link to comment
Share on other sites

**Update** Maybe I'm not doing something right...I replaced mun:altitude with 2868750 and when it runs all I get is NaN for both v2 and deltav. I didn't do anything with the vp part of the code, should I have used that some how?

Is the value of this:

vom^2 + (mu * (1/a - 1/a2) )

coming out as a negative number? Taking the square root of a negative number would result in NaN, as floats and doubles can't do complex numbers.

All the variables you're using are positive so the only chance for that expression to end up negative is if 1/a is smaller than 1/a2.

Link to comment
Share on other sites

Is inlight a thing? Cool....did not know that. Could be good for turning things off that use a lot of power, like kethane drills/convertors.....Didn't think about using kOS for kethane drills....you could automate the conversion process... :)

Cool, I'll give this one a try when I get a chance then. As far as accuracy goes, I know it likely won't be too accurate just because of the time lag in parsing the if statements.

**Update** Maybe I'm not doing something right...I replaced mun:altitude with 2868750 and when it runs all I get is NaN for both v2 and deltav. I didn't do anything with the vp part of the code, should I have used that some how?

I took the round function out of the code/formula and NaN went away(though looking at it again that was just for the print statements lol), but still not getting a number that'll get me what I want. hmmm

Works for me:


print "Currently in sphere of " + body.
// find deltav to move apoapsis target altitude
// use energy invariance to calculate deltav
// by changing vp to vp' the semi major axis changes from a to a'
// 0.5*vp^2 - mu/r + 0.5*mu/a = 0.5*vp'^2 - mu/r + 0.5*mu/a'
// vp'^2 = vp^2 + mu( 1/a - 1/a' )
set mu to 3.5316000*10^12. // mu = G mk, mk: mass of Kerbin
set rk to 600000. // radius of Kerbin
set rm to 200000. // radius of Mun
set r to rk + periapsis.
set a to rk + apoapsis. // semi major axis current orbit
// set a2 to (r + rk + mun:altitude)/2. // semi major axis target orbit
set a2 to (r + rk + 2868750)/2. // semi major axis target orbit
set vom to velocity:orbit:mag.
set v2 to sqrt( vom^2 + (mu * (1/a - 1/a2) ) ).
set deltav to v2 - vom.
print "v2: " + round(v2).
print "deltav: " + round(deltav).
set x to node(time:seconds + eta:periapsis, 0, 0, deltav).
add x.
// workaround for scientic numbers bug on load
set mu to 0.

There is a 1% error in altitude compared to the inputs. This might be caused by the script or the way maneuver node system works.

kOS%20orbit.png

Btw, vom^2 + (mu * (1/a - 1/a2) ) should only ever become small when a2 is close to 0. In physics terms a2 does never get smaller than (rk+altitude)/2 that is when periapsis gets close to the center of earth and your radial velocity vp' gets close to 0 (no more kinetic energy). The major axis then extends from your ship to the center of kerbin then, i.e. rk+altitude.

Edited by baloan
Link to comment
Share on other sites

Works for me:


print "Currently in sphere of " + body.
// find deltav to move apoapsis target altitude
// use energy invariance to calculate deltav
// by changing vp to vp' the semi major axis changes from a to a'
// 0.5*vp^2 - mu/r + 0.5*mu/a = 0.5*vp'^2 - mu/r + 0.5*mu/a'
// vp'^2 = vp^2 + mu( 1/a - 1/a' )
set mu to 3.5316000*10^12. // mu = G mk, mk: mass of Kerbin
set rk to 600000. // radius of Kerbin
set rm to 200000. // radius of Mun
set r to rk + periapsis.
set a to rk + apoapsis. // semi major axis current orbit
// set a2 to (r + rk + mun:altitude)/2. // semi major axis target orbit
set a2 to (r + rk + 2868750)/2. // semi major axis target orbit
set vom to velocity:orbit:mag.
set v2 to sqrt( vom^2 + (mu * (1/a - 1/a2) ) ).
set deltav to v2 - vom.
print "v2: " + round(v2).
print "deltav: " + round(deltav).
set x to node(time:seconds + eta:periapsis, 0, 0, deltav).
add x.
// workaround for scientic numbers bug on load
set mu to 0.

There is a 1% error in altitude compared to the inputs. This might be caused by the script or the way maneuver node system works.

**Image**

Btw, vom^2 + (mu * (1/a - 1/a2) ) should only ever become small when a2 is close to 0. In physics terms a2 does never get smaller than (rk+altitude)/2 that is when periapsis gets close to the center of earth and your radial velocity vp' gets close to 0 (no more kinetic energy). The major axis then extends from your ship to the center of kerbin then, i.e. rk+altitude.

Like Steven said vom^2 + (mu * (1/a - 1/a2) ) is negative because A ends up being 2 and A2 ends up being 4 (not counting the decimals). Below is a screenshot from my attempt, I included extra prints to see what some of the variables are doing. Part of the problem, at least with vom^2.... is 1/a is less than 1/a2. Likely because I'm starting from a higher orbit trying to get to a lower one.

The only change I did was to add print statements. Although I tried a few other things like switching a and a2, which got rid of Nan, but totally doesn't get what we're going for.

screenshot396.png

...Just tried making a2 higher than the current Ap, at first I tried something like 3568750, and the node AP ended up as less than the current Ap. SO then I added a 0 to the end of 2,868,750, (so 28,687,500) and the resultant node ap ended up as 19,364,656. I'm guessing the error margin gets larger with larger numbers....maybe?...Sorry I'm being a pest about this lol

Edited by Sma
Link to comment
Share on other sites

Just found out the script only works when starting from a circular orbit. I didn't notice because I circularized for orbit insertion. Let me check whether I can generalize it for arbitrary orbits. The problem is that r and velocity:orbit:mag must correspond to calculate energy. In a circular orbit that is true throughout - and I used that in the formula.

Bear with me. Apologies for the inconvenience caused.

EDIT: Try this apoapsis maneuver node creator. Need yet to clone for periapsis maneuvers. I've also created a warping node chaser which I will upload to kos wiki shortly.

Run it with the desire altitude, for example to create a circular orbit node:

run apo(apoapsis).

declare parameter alt.
// create apoapsis maneuver node
print "T+" + round(missiontime) + " Apoapsis maneuver, orbiting " + body.
print "T+" + round(missiontime) + " Apoapsis: " + round(apoapsis/1000) + "km.".
print "T+" + round(missiontime) + " Periapsis: " + round(periapsis/1000) + "km" + " -> " + round(alt/1000) + "km".
// constants
set mu to 3.5316000*10^12. // mu = G mk, mk: mass of Kerbin
set rk to 600000. // radius of Kerbin
set rm to 200000. // radius of Mun
// present orbit properties
set vom to velocity:orbit:mag. // actual velocity
set r to rk + altitude. // actual distance to body
set ra to rk + apoapsis. // radius in apoapsis
set va to sqrt( vom^2 + 2*mu*(1/ra - 1/r) ). // velocity in apoapsis
set a to (periapsis + 2*rk + apoapsis)/2. // semi major axis present orbit
// future orbit properties
set r2 to rk + apoapsis. // distance after burn at apoapsis
set a2 to (alt + 2*rk + apoapsis)/2. // semi major axis target orbit
set v2 to sqrt( vom^2 + (mu * (2/r2 - 2/r + 1/a - 1/a2 ) ) ).
// setup node
set deltav to v2 - va.
print "T+" + round(missiontime) + " Apoapsis burn: " + round(va) + ", dv:" + round(deltav) + " -> " + round(v2) + "m/s".
set x to node(time:seconds + eta:apoapsis, 0, 0, deltav).
add x.
print "T+" + round(missiontime) + " Node created.".
// workaround for scientic numbers bug on load
set mu to 0.

kOS_node_creator.png

Edited by baloan
Link to comment
Share on other sites

Just found out the script only works when starting from a circular orbit. I didn't notice because I circularized for orbit insertion. Let me check whether I can generalize it for arbitrary orbits. The problem is that r and velocity:orbit:mag must correspond to calculate energy. In a circular orbit that is true throughout - and I used that in the formula.

Bear with me. Apologies for the inconvenience caused.

EDIT: Try this apoapsis maneuver node creator. Need yet to clone for periapsis maneuvers. I've also created a warping node chaser which I will upload to kos wiki shortly.

Run it with the desire altitude, for example to create a circular orbit node:

**Code**

**Image**

Awesome, I'll give it a go later today when I have a moment. Thanks for your help. As much as I "dislike" math, it is interesting how useful it can be, and once it's broken down into its parts it's not actually that hard to understand....sort of ;).

Edited by Sma
Removed code/image to save space...Bahah....
Link to comment
Share on other sites

Is that possible to get the current acceleration of the rocket? I want to create a G-limiting routine that would run in the background through the entire program. If the G load exceeds 4, the throttle variable would be incrementally reduced. This check needs to be continually repeated, and only cease when the payload has separated from the rocket (which happens to end the program, this being an ascent program for the LV).

Link to comment
Share on other sites

I've uploaded the maneuvering scripts to the kOS wiki. Features:

1. add maneuver node at periapsis for changing apopsis altitude (more fuel efficient)

run per1(mun:altitude). 

2. add maneuver node at apoapsis for changing perapsis altitude, for example to cirularize an orbit:

run apo1(apoapsis). 

3. node execution script. The script will

â—¾ turn the ship to node:deltav (and it has a nice workaround to detect when done)

â—¾ warp to the maneuver node

â—¾ burn full throttle, then

â—¾ for accurate deltav throttle down nearing burn end.

run nod1. 

Uses these helper subroutines:

4. Wait for dt seconds. Warps automatically. Covering warp levels up to 5 (3000 seconds).

run warp(dt). 

5. Sets a body's properties global variables:

â—¾ Gravitational parameter mu

â—¾ body radius rb

currently works for Kerbin, Mun and Minmus. The constants are used in the apo1 and per1 scripts.

run bdp(Kerbin). 

Edited by baloan
Link to comment
Share on other sites

Cool, I'll check these out soon. Heading to my in-laws for dinner and a movie. I just tried the code you posted earlier and it seems to work rather well. I think the one time I ran it as it was it got around 2868435, or something like that. Though at first I thought it should help to bring my AP down to the desired altitude, like I said though I'll give the others a try.

Thanks for taking the time to work on these. Wish I understood it more so I could figure it out myself. Maybe some day, I've never really "needed" anything like this before but I think it's pretty cool the potential it has.

**Update** I'm starting to check it out now. I got an error though, saying unrecognized term rb. Upon checking it looks like the bdp.txt sets the radius of the bodies to rk not rb. Trying that now to see if that fixes it. :)

Yep, that seemed to do the trick: :D

screenshot397.png

Now I just have to piece together the rest of it together. Once I start the burn at Pe, and get my Ap where I want it, I'm going to have to place a node at Ap, and adjust until the orbital period is 4hrs. I THINK I have that part figured out, maybe, I'll let you know how it goes.

Was just looking at the time warp program


if dt > 300 {
when time:seconds > t1 - 300 then {
print "T+" + round(missiontime) + " Warp 3.".
set warp to 3.
}
}
if dt > 10 and dt < 300 {
print "T+" + round(missiontime) + " Warp 3.".
set warp to 3.
}
if dt > 60 {
when time:seconds > t1 - 60 then {
print "T+" + round(missiontime) + " Warp 2.".
set warp to 2.
}
}

Should "if dt > 10 and dt < 300" be "if dt > 100 and dt < 300" or maybe "if dt > 60 and dt < 300"? I haven't tried it yet though so for all I know it may be fine.

I've uploaded the maneuvering scripts to the kOS wiki. Features:

1. add maneuver node at periapsis for changing apopsis altitude (more fuel efficient)

run per1(mun:altitude). 

2. add maneuver node at apoapsis for changing perapsis altitude, for example to cirularize an orbit:

run apo1(apoapsis). 

3. node execution script. The script will

â—¾ turn the ship to node:deltav (and it has a nice workaround to detect when done)

â—¾ warp to the maneuver node

â—¾ burn full throttle, then

â—¾ for accurate deltav throttle down nearing burn end.

run nod1. 

Uses these helper subroutines:

4. Wait for dt seconds. Warps automatically. Covering warp levels up to 5 (3000 seconds).

run warp(dt). 

5. Sets a body's properties global variables:

â—¾ Gravitational parameter mu

â—¾ body radius rb

currently works for Kerbin, Mun and Minmus. The constants are used in the apo1 and per1 scripts.

run bdp(Kerbin). 

Edited by Sma
Link to comment
Share on other sites

**Update** I'm starting to check it out now. I got an error though, saying unrecognized term rb. Upon checking it looks like the bdp.txt sets the radius of the bodies to rk not rb. Trying that now to see if that fixes it. :)

Should "if dt > 10 and dt < 300" be "if dt > 100 and dt < 300" or maybe "if dt > 60 and dt < 300"? I haven't tried it yet though so for all I know it may be fine.

Updated bdp.txt on the wiki to correct rb. The original naming was for Kerbin (rk) but I extended the script to work with other bodies as well. Consequently it is no longer radius of Kerbin rk but radius of body rb.

The warp script is not fully symmetric but it should work. I saved the effort of cloning the initial warp factor setting for warp 1 to 3.

Link to comment
Share on other sites

I am trying to run a master script for my mission.

// mission control script
// prepare kOS storage
switch to 1.
copy bdp.txt from 0.
copy far4.txt from 0.
copy per1.txt from 0.
// switch to 2.
copy apo1.txt from 0.
copy nod1.txt from 0.
copy warp.txt from 0.

// conduct mission
run far4.
run per1(mun:altitude).
run nod1.

but I get:

Proceed.

Switch to 0.

run mc1.

Error on line 2: Syntax error. // commenting out "switch to 0." in mc1.txt

run mc1.

Error on line 3: Syntax error.

Missing feature or am I missing something?

Link to comment
Share on other sites

I am trying to run a master script for my mission.

// mission control script
// prepare kOS storage
switch to 1.
copy bdp.txt from 0.
copy far4.txt from 0.
copy per1.txt from 0.
// switch to 2.
copy apo1.txt from 0.
copy nod1.txt from 0.
copy warp.txt from 0.

// conduct mission
run far4.
run per1(mun:altitude).
run nod1.

but I get:

Missing feature or am I missing something?

Try it without .txt? As far as I know it doesn't need the extension.

After trying to deploy some "komm" sats (had a failed attempt that I left in a 10mm by 100km orbit, one of the sats release got triggered on accident, and was destroyed, launcher and other 2 were still ok), I decided to go ahead and deploy those 2 in a 8mm orbit, temporarily at least. In the process though I discovered I need more than 2 dishes so I'm going to have to add another dish or 2 to my sats. Using the radial mount one on the sides to connect each satellite to its neighbor and i think the one that can reach duna as the dish to connect to KSC. Once I get my program(s) finalized I think I'll add to it copying the program needed for circularizing to each of the sats. Would be cool if there was a way to rename things programmatically. Also if we could have more than one kOS window open at a time....;)

Edited by Sma
Link to comment
Share on other sites

there is one problem in the current version of kOS (0.92): whenever I use a variable in a node declaration, I get the error ‘the parameter xyz is not a number’, even though the variable contains a number. Has anyone else experienced this?

Link to comment
Share on other sites

// commenting out "switch to 0." in mc1.txt

I can't see any "switch to 0." anywhere in the code you posted - neither commented nor uncommented. Is what you posted what you're really running?

At any rate the problem probably is, as previously noted, the ".txt", which is always implied and not stated explicitly.

Saying "run prog.txt ." Gets interpreted as two separate statements. "run prog." followed by "txt.". And the syntax error is on not understanding what the command "txt." means.

Edited by Steven Mading
Link to comment
Share on other sites

Like Steven said vom^2 + (mu * (1/a - 1/a2) ) is negative because A ends up being 2 and A2 ends up being 4

Actually, since a and a2 are in denominators of fractions it would have to be the other way around. "A" would have to be bigger than "A2" in order for (1/A) to be smaller than (1/A2).

Link to comment
Share on other sites

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