Jump to content

[1.3] kOS Scriptable Autopilot System v1.1.3.0


erendrake

Recommended Posts

Is it possible to have kOS work with mechjeb? For example, I have a rocket which I want to rotate at liftoff, and then after the 1st staging rotate another 90 degrees for the next staging.

Right now I'm doing this manually with Mechjeb by changing the rotate angle after the first staging event.

TIA

Link to comment
Share on other sites

Is it possible to have kOS work with mechjeb? For example, I have a rocket which I want to rotate at liftoff, and then after the 1st staging rotate another 90 degrees for the next staging.

Right now I'm doing this manually with Mechjeb by changing the rotate angle after the first staging event.

TIA

I think you can use kOS's lock steering to heading function to replace that.

Edited by ebigunso
Link to comment
Share on other sites

So I've been playing with kOS and I like it, except for the . used as ending statements - as a C/perl/php guy I'm too used to using ; to end statements.

I've noticed that at least for my ships, using LOCK HEADING at launch causes rotation right after launch, and when I go to LOCK HEADING (90,80) for gravity turn, the ship goes into a seriously corkscrew/wobble. I verified it was from the LOCK HEADING by using a manual turn (no wobble) to compare. My guess here is that LOCK HEADING is forcing tiny changes to pitch/yaw/roll to stay dead on heading, and this is causing the wobble effects. Particularly with gimbal'd engines.

I'm going to have to switch to raw control mode and test the current heading to work around this.... unless I missed something?

Link to comment
Share on other sites

There are some gotchas; a vector is a direction and a velocity but not a roll state, and a rotation doesn't have a magnitude but does have a roll state, and they're sort of interchangeable but sometimes you get undesired effects like this.

Link to comment
Share on other sites

Speaking of vectors and directions, I'm having a bit of trouble with them myself.

Here's the code:


////////////////////////////////////////////////////
//ULV210 Launch script
////////////////////////////////////////////////////


//Initializations and such.
copy ULV210 to archive. //Program self preservation

declare function angleBetween //Returns angle between the two arguments vec1 and vec2 by using the dot product. Returns a value in degrees.
{
parameter dir1.
parameter dir2.

local vec1 is v(1,0,0).
local vec2 is v(1,0,0).

set vec1:direction to dir1.
set vec2:direction to dir2.

return arccos(vdot(vec1,vec2)). //Calculating the angle between vec1 and vec2 using the dot product. Neglected to divide by product of magnitudes because both are unit vectors.
}

declare function abort //Redundant here, would execute an abort and parachute deployment in a manned rocket. THIS FUNCTION MUST BE MODIFIED ON A PER ROCKET BASIS ACCORDING TO ABORT PROCEDURES.
{
lock throttle to 0.
toggle abort.
wait 10.
toggle chutes.
}

set turnStartVel to 80. //SPEED at which the gravity turn begins. m/s
set abortAngle to 10. //Maximum angle allowed between the direction the rocket is facing and its direction of travel.
set turnStartAng to 10. //Initial pithcover amount. Degrees.
set turnStartDir to heading(90,90-turnStartAng). //Actual direction represented by the above. Current heading is compared against this.
set pitchOver to 0.
set compThrot to 0. //Variable facsimile of throttle value.
set throttleUpDuration to 5. //Amount of time allocated for engine thrust buildup.
set ship:control:pilotMainThrottle to 0. //Zeroes out user throttle input before taking control.
lock headingTest to ship:heading. //Stores current durection of the rocket.


//Actual launchy stuff now.
lock throttle to compThrot.
sas on.

stage. //Activate engines.

until compThrot > 1 //Throttle up loop.
{
set compThrot to compThrot + 1/(500 * throttleUpDuration).
}

stage. //Launch clamp release/liftoff.

wait until verticalspeed > turnStartVel. //Vertical climb.
sas off.

until angleBetween(headingTest,turnStartDir) < 0.1 //Initial pitchover loop. Runs until rocket is within 0.1 degrees of the desired pitchover angle.
{
if (angleBetween(headingTest,srfprograde) < abortAngle)
{
until pitchOver > turnStartAng
{
sas off.
set pitchOver to pitchOver + 0.1.
lock steering to heading(90,90-pitchOver).
wait 0.05.
sas on.
wait 0.05.
}
}
else
{
abort().
}
}

The issue is with the angleBetween() function I declare at the beginning of the program. The idea is pretty self explanatory, you pass in two directions, it maps a pair of unit vectors to those directions, and calculates the cross product to determine the angle between those two directions. However, I can't get kOS to recognize the parameters dir1 and dir2 as directions, and every time the angleBetween() function gets called, I get the following error:


Value is not a convertible object: System.Double to kOs.Suffixed.Direction at ULV210 on 1, line 17.

How can I get kOS to actually pass the correct data types into the function?

EDIT: It appears that the issue is somewhere else in the script. After doing a (now obvious) debug test consisting of only the function it seems that the function works just fine, and somehow I'm the one passing the double into it.

EDIT2: Got the issue halfway fixed, though I'm not sure how. Now it's fine with the argument headingTest (now renamed currentDir), it keeps throwing an exception with turnStartDir, although it won't tell me what. It just ends the program and points to that argument.

EDIT3: Changing ship:heading to ship:up fixed it.

EDIT4: So the new code works great. Now I'm having a separate issue controlling the ship via kOS. Whenever I had control over to kOS via lock steering, kOS begins inducing huge oscillations, even with extremely restricted gimbal ranges, causing the rocket to corkscrew, so I can't get through the initial pitchover portion of the gravity turn. The rocket handles like a dream under SAS, however of course kOS can't control the rocket at the same time as SAS. The only upside is that it has proven that the programmed abort function works great. Any ideas? I'm using FAR, if that's at all relevant.

Edited by VFB1210
Link to comment
Share on other sites

Are there any tutorials for using Vectors and Directions? I've tried experimenting with them and logging outputs, but I'm rather lost at trying to understand the results.

FWIW, I took math all the way up through Calc, so it's not the math that's a problem, it's understanding what the hell I'm doing :confused:

Link to comment
Share on other sites

There is a low-level primitive for this:


return arccos(vdot(vec1,vec2)). //Calculating the angle between vec1 and vec2 using the dot product. Neglected to divide by product of magnitudes because both are unit vectors.

Called vang().

vang(vec1,vec2) gives you the same thing as vdot(vec1:unit,vec2:unit), but does it all via Unity's API, which means it might take advantage of the video card's vector processor, if the driver uses it.

- - - Updated - - -

@ibanix:

Are there any tutorials for using Vectors and Directions?

You might find these two old videos helpful:

Link to comment
Share on other sites

^The documentation seems pretty clear to me, but let my try and rephrase it.

ENGINE:MAXTHRUSTAT(PRESSURE) = how much thrust will me engine produce at full throttle and without the in-game thrust limiter engaged when the atmosphere around it is at PRESSURE? As I'm sure you know, the atmospheric pressure is highest at the surface, and gradually reduces as you head to space, when the atmospheric pressure around a rocket engine is high, it produces less thrust, because the exhaust has to work harder to move through the air. This also reduces the specific impulse of the engine. The argument PRESSURE is in terms of Kerbin atmospheres at seal level. PRESSURE = 0 corresponds to an external pressure of 0psi, or a vaccum. ENGINE:MAXTHRUSTAT(0) would return the vaccum thrust of ENGINE. ENGINE:MAXTHRUST(1) would return the sea-level thrust of ENGINE. ENGINE:MAXTHRUST(0.5) would return ENGINE's thrust in an environment with half as much external atmospheric pressure as Kerbin at sea level; quick, pulled-out-of-my-ass guess, this corresponds to an altitude of about 10-15km on Kerbin. PRESSURE can also be greater than 1, ENGINE:MAXTHRUST(2) would return the thrust of the engine at twice Kerbin's sea level pressure, or about the same pressure found at sea level on Eve.

ENGINE:AVAILABLETHRUSTAT(PRESSURE) works the same as the above, but takes into account the thrust limiter tweakable setting.

Link to comment
Share on other sites

I have a line:

SET ThrustPercent TO WantThrust/MAXTHRUSTAT(PRES).

and the console is returning an error:

undefined variable name 'maxthrustat*'.

the use of MAXTHRUSTSTAT requires an a Engine object to reference, it can't be used alone or kOS thinks it's a variable instead of a property

Link to comment
Share on other sites

the use of MAXTHRUSTSTAT requires an a Engine object to reference, it can't be used alone or kOS thinks it's a variable instead of a property

Is there a method to reference the total thrust of currently active or in-stage engines? I had also tried

vessel:maxthrustat(PRES) and the console gave an error:

undefined variable name 'vessel'

Link to comment
Share on other sites

Is there a method to reference the total thrust of currently active or in-stage engines? I had also tried

vessel:maxthrustat(PRES) and the console gave an error:

undefined variable name 'vessel'

"vessel" is a placeholder you're supposed to fill in with whichever vessel you meant.

i.e.


VESSEL("some named vessel"):maxthrust(PRES).

or


SHIP:maxthrust(PRES).

SHIP is a special variable name that means the current ship your program is running on.

Link to comment
Share on other sites

"vessel" is a placeholder you're supposed to fill in with whichever vessel you meant.

i.e.


VESSEL("some named vessel"):maxthrust(PRES).

or


SHIP:maxthrust(PRES).

SHIP is a special variable name that means the current ship your program is running on.

OK, that explains everything! Thank You!

Link to comment
Share on other sites

Is there a replacement for terminal velocity since it was deprecated? If no,is there another direct/indirect way of knowing if I'm going "too fast" and throttling down while ascending?

BTW I'm more interested in avoiding flipping than saving on fuel.

Link to comment
Share on other sites

That's basically what I'm working on now

I'll post that snipett in my morning (gmt-5)

SET MaxAcc TO X. //how much acceleration on top of 'negate gravity' replace X whatever you want. does not take into account air resistance [yet]

LOCK WantThrust TO (SHIP:SENSORS:GRAV:MAG+MaxAcc)*SHIP:MASS. // determiness how much thrust needed to accelerate to that.

LOCK THROTTLE TO MIN(WantThrust/MAX(SHIP:MAXTHRUSTAT(SHIP:SENSORS:PRES),1),1). // throttle to provide that acceleration. if not enough, thrust at max. the MAX prevents division by 0. the MIN prevents throttle from trying to be more than 100%

Edited by ABZB
Link to comment
Share on other sites

Finally I used my kos script to fine tune a Moho injection after long burn lost it.IWiY8D9.png

After main burn the arrows didn't intersect. Then I opened MJ node editor and tweaked little by little until the arrows got close enough that I got an orbit insertion on Moho.

Now step 2: ?

Step 3: profit

Link to comment
Share on other sites

Is there a replacement for terminal velocity since it was deprecated? If no,is there another direct/indirect way of knowing if I'm going "too fast" and throttling down while ascending?

BTW I'm more interested in avoiding flipping than saving on fuel.

Speed will definitely affect how hard it is to hold on target a rocket with unstable equilibrium. However it's possible to build a rocket in such a way that it has stable equilibrium instead and thus the speed does not make it flip over. The key is to put the fins well behind the center of gravity such that to make it deflect from prograde requires effort on your part, making the rocket act like a weathervane. In that configuration you may even have the opposite problem, where the faster you go the *harder* it gets to deflect from prograde and you might have to go slower to get it to turn.

Link to comment
Share on other sites

Hi,

I'm having problems whit this mod.

1) Any commands that i fill in the command line will be executed immediately instead of a full sequence after completing. -eg. when typing 'Print "3". wait 1.immediately a 3 instead of the next command line

2) I cannot asaing a name to a launch script

3) there is no save key :(

TulEDcL.png

YpXxRXO.png

No other mods installed for testing purposis.

Edited by Snark
Link to defunct website removed by moderator
Link to comment
Share on other sites

Speed will definitely affect how hard it is to hold on target a rocket with unstable equilibrium. However it's possible to build a rocket in such a way that it has stable equilibrium instead and thus the speed does not make it flip over. The key is to put the fins well behind the center of gravity such that to make it deflect from prograde requires effort on your part, making the rocket act like a weathervane. In that configuration you may even have the opposite problem, where the faster you go the *harder* it gets to deflect from prograde and you might have to go slower to get it to turn.

Lately my most common problem is when I have a decent TWR like 1.5 or above and MJ starts doing gravity turns. Everything goes fine until I hit 70-90% of the terminal velocity (according to ker) and then it starts facing away from desired direction until it flips. If I throttle manually and never exceed term velocity everything works.

This has happened to lots of rockets so far, big and small. More or less fins will slightly change the speed factor, but almost nothing will work for 200% of term velocity, except brute force aerodynamics. That's why I believe it is the key info to handle all other aspects of ascent, like throttle and speed. My idea is to throttle it so I can always stay under 70% TV no matter altitude, orientation etc

Edit: what I mean is that even a rock is stable if you go slow enough. Now that's a nice name for an enterprise: flying rocks rockets!

I understand that my rockets aren't very stable, it's just that I rather not care if I could get my way with a script.

Edited by Qigon
Link to comment
Share on other sites

Hi,

I'm having problems whit this mod.

1) Any commands that i fill in the command line will be executed immediately instead of a full sequence after completing. -eg. when typing 'Print "3". wait 1.immediately a 3 instead of the next command line

2) I cannot asaing a name to a launch script

3) there is no save key :(

<snip>

<snip>

No other mods installed for testing purposis. Latest version from Kerbal stuff instald ( https://kerbalstuff.com/mod/86/kOS:%20Scriptable%20Autopilot%20System )

You're typing commands into the console, not the program editor. To access the program editor, type "edit <program>." into the console. <program> is the name of the program you want to edit. If a program with the name <program> doesn't exist, a blank program will be created under that name and the program editor will be opened.

Link to comment
Share on other sites

Sorry if this has been asked, but I was looking through the docs and found nothing definite, but am I correct in assuming there is no runtime input? As in I cannot ask the user for input whilst the program is running, as you can do in other languages? Or have I just been missing how to do so?

Link to comment
Share on other sites

Sorry if this has been asked, but I was looking through the docs and found nothing definite, but am I correct in assuming there is no runtime input? As in I cannot ask the user for input whilst the program is running, as you can do in other languages? Or have I just been missing how to do so?

You can detect what the user is doing with the action groups and flight controls. I don't know if there is a way to accept input beyond that.

Link to comment
Share on other sites

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