Jump to content

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


Dunbaratu

Recommended Posts

Thanks Pand,

That might indeed work as well,

I think I have completed the puzzle altough there may be more ways of doing this.

Here's what I got so far:

First I named one of the booster engines "BoosterEngines" through the right click menu.

Then I added this code to my launch script.

set Boosterengine to ship:partstagged("BoosterEngine")[0].

when Boosterengine:availablethrust <0.5 then {
            stage.
            }  

It works but any suggestions welcome!

Link to comment
Share on other sites

1 hour ago, Ricovandijk said:

It works but any suggestions welcome! 

lock BoosterStatus to ship:partstagged("BoosterEngine")[0]:getmodule("ModuleEnginesFX"):getfield("status").
if BoosterStatus = "Flame-Out!" { // do staging stuff }

It's not as generic as what Steven put up but works great for cases where you want to know what engine is out of fuel. You can also use it to see if the booster is running by checking for a "Nominal" string

Link to comment
Share on other sites

FIXED. I broke the equation down into multiple lines of code and that seemed to work. It still doesn't point exactly to perigee but it's close enough. 

 

I'm trying to calculate an eccentricity vector for a rendezvous script. It is a vector that points from the center of the Earth to the perigee with magnitude equal to that of the eccentricity. 

The equation for this (I learned from my space mechanics class) is (1/mu)*((V X H) - (mu*R_unitvector)). The value for H is R X V

When I do this calculation, it results in a vector equal in direction to the ships R vector with magnitude exactly 1. Needless to say, this is incorrect. I have taken into account that the R vector in kOS is actually pointed downward from the ship (just multiplied by -1) and that kOS uses a left handed coordinate system (also multiplied by -1 after cross products were taken). 

 

Am I doing something wrong? Or does the problem lie in kOS itself and I should stop trying (has happened before)? 

 

Edit: I am using Realism Overhaul and RSS

Edited by dafidge9898
Link to comment
Share on other sites

2 hours ago, dafidge9898 said:

The equation for this (I learned from my space mechanics class) is (1/mu)*((V X H) - (mu*R_unitvector)). The value for H is R X V

The bolded bit there should just be the unit position vector, I'm pretty sure (no factor of mu).

latex_dcae3eb77f4c7f760d4958d30969ecd5.p

 

Edited by blorgon
Link to comment
Share on other sites

7 minutes ago, dafidge9898 said:

It's the same thing as what I wrote. The mu would cancel out with the one multiplied by the R unit vector, unless my parentheses are incorrect. 

lol nope I misread. Inline maths is hard to read....

Can you post the relevant code?

Link to comment
Share on other sites

@dafidge9898 yes, the code would help to understand what's wrong.

One thought is that R in KSP is -body:position.

Also, you can use rotations to get the ecc vector:

set ecc_normal to lookdirup(solarprimevector, V(0,1,0)) * R(0, -orbit:LAN, -orbit:inclination) * R(0,-orbit:argumentofperiapsis,0).
set ecc_vector to orbit:eccentricity * ecc_normal:vector.
set normal_vector to ecc_normal:upvector.

 

Link to comment
Share on other sites

I have a rocket with a command pod manned by a pilot, it has an antenna (activated) and it's sitting on the launchpad. Is it expected that ADDONS:RT:HASKSCCONNECTION(SHIP) will return false in this case?

Yes, I do have local control, but I would still expect it to return true. I want to know if I can copy files over from archive or not.

Link to comment
Share on other sites

1 hour ago, scimas said:

I have a rocket with a command pod manned by a pilot, it has an antenna (activated) and it's sitting on the launchpad. Is it expected that ADDONS:RT:HASKSCCONNECTION(SHIP) will return false in this case?

Yes, I do have local control, but I would still expect it to return true. I want to know if I can copy files over from archive or not.

I think that's this issue: https://github.com/KSP-KOS/KOS/issues/2328

Link to comment
Share on other sites

  • 2 weeks later...
4 minutes ago, dakhr said:

Hi

 Function for rotation R(pitch,yaw,roll) is not recognized, my line of code:

set steer to ship:srfretrograde + R(steerPitch, steerYaw, 0).

I got error message :Undefined variable name `R`

KSP version 1.3.1 , KOS version 1.1.5

That doesn't make much sense.  Can you show a log of it?

Link to comment
Share on other sites

3 hours ago, Steven Mading said:

That doesn't make much sense.  Can you show a log of it?

I don't know why, but this does happen sometimes after my scripts are executed. Not specifically with the R function, but with variables that are supposed to be pre-bound in general. For example, KOS will finish executing some script and then I type in the console

    lock STEERING to someDirection. 

And it will throw an error saying undefined variable steering. It complains only once. If I type in the command again, it doesn't complain and executes it properly. I will try to get logs the next time this happens. 

Link to comment
Share on other sites

The problem isn't just "R()", if it's the same problem I've seen before.  If it's the same one I've seen before then you'd get the same effect from trying any of the built-in function names like HEADING(), V(), VDOT(), etc.  There is a step that runs through all the reserved names and sets them up when kOS initializes the computer part.  Something is allowing the kOS code to start running while that step hasn't happened, and I can't figure out what causes that.  It's a timing issue, I think.  What did you do *exactly* just prior to getting this problem, and if you re-ran the same program after getting this problem, does it then work on the second attempt?

Link to comment
Share on other sites

15 hours ago, Steven Mading said:

The problem isn't just "R()", if it's the same problem I've seen before.  If it's the same one I've seen before then you'd get the same effect from trying any of the built-in function names like HEADING(), V(), VDOT(), etc.  There is a step that runs through all the reserved names and sets them up when kOS initializes the computer part.  Something is allowing the kOS code to start running while that step hasn't happened, and I can't figure out what causes that.  It's a timing issue, I think.  What did you do *exactly* just prior to getting this problem, and if you re-ran the same program after getting this problem, does it then work on the second attempt?

1.Rebooting script doesn't change anything.

2.I tested Your theory and replaced R with V code executes without error.

3.Here is the code with function which executes before  troubled function.

This is so annoying issue!

function step_fhBoostersGlide{
    parameter k.
    parameter FinSteeringDist.
    
if (ship:ALTITUDE <75000 ) { 
                RCS ON .
                

                                    }
set  intensity to 10.
LOCK STEERING TO - SHIP:VELOCITY:SURFACE:NORMALIZED *  landingTarget:POSITION:MAG - VXCL(SHIP:VELOCITY:SURFACE, VXCL(SHIP:UP:FOREVECTOR,  landingTarget:POSITION * 1.42 - impactPoint(k)))*intensity. 

if(isShip("fhBooster1") or isShip("fhBooster2"))
        SET sBurnStart TO boosterSburnStart.


if (SHIP:ALTITUDE < sBurnStart+FinSteeringDist OR geoDist<1400 ) {


If NOT BRAKES BRAKES ON.


    
        setHoverPIDLOOPS().
            limitGridFinAuthority(80).    
set startT to time:seconds.
set oldT to startT.
set prevPos to ship:geoposition.
wait 1.


SET step to false.
}


}
function impactPoint {
    DECLARE PARAMETER k.
    DECLARE PARAMETER dv IS V(0,0,0).

    SET ps TO V(0,0,0).
    SET vs TO SHIP:VELOCITY:SURFACE + dV.
    SET as TO V(0,0,0).
    SET t TO 0.
    UNTIL ps:Z < -ALT:RADAR {
        SET t TO t + 1.
        SET as TO (- vs:NORMALIZED * (vs:MAG ^ 2 * k * (0.99997 ^ SHIP:ALTITUDE)) / (SHIP:MASS * 1000)) - SHIP:UP:FOREVECTOR:NORMALIZED * 9.81.
        SET vs TO VS + as.
        SET ps TO ps + vs.
    }
    RETURN ps.
}

function step_entry{

    parameter dummy1 is 0.
    parameter dummy2 is 0.
    //SET thrott TO 0.

If NOT BRAKES BRAKES ON.
set missionT to time:seconds.
    set dt to missionT - oldT.
    set curPos to ship:geoposition.
    set curAlt to ship:altitude.
    set velLat to (prevPos:lat - curPos:lat)/dt.
    set velLng to (prevPos:lng - curPos:lng)/dt.
set landingAltitude to landingTarget:terrainheight + 7.

    set impactTime to timeToAltitude(landingAltitude). // time until impact
        set impactPos to body:geopositionof(positionat(ship, time:seconds + impactTime)). // position of impact
        set landingPos to latlng(impactPos:lat, impactPos:lng - impactTime * 0.01666666). // adding planet rotation
    
// preparing PID loops
// --- landing loops ---

// Altitude control
//local AltVel_PID is pidloop(0.2, 0, 0.04, -200, 200). // calculating desired velocity during landing
//local VelThr_PID is pidloop(0.05, 0.01, 0.005, 0, 1). // adjusting thrust to reach that velocity

// Latitude control
local LatVel_PID is pidloop(6, 0, 40, -0.15, 0.15). // same as above but for steering during landing
local VelPit1_PID is pidloop(700, 0, 200, -5, 5).
local VelPit2_PID is pidloop(700, 0, 200, -15, 15).

// Longditude control
local LngVel_PID is pidloop(6, 0, 40, -0.15, 0.15).
local VelYaw1_PID is pidloop(500, 0, 200, -5, 5).
local VelYaw2_PID is pidloop(500, 0, 200, -15, 15).

// --- END landing loops ---
 //setTorqueFactor("high").
    //setHoverMaxHorizSpeed(200).

    set LatVel_PID:setpoint to landingTarget:lat. // steering
        set VelPit1_PID:setpoint to LatVel_PID:update(missionT, landingTarget:lat).
        set steerPitch to VelPit1_PID:update(missionT, velLat).
        
        set LngVel_PID:setpoint to landingTarget:lng.
        if curAlt < 5000 { // if below 5k, use actual longditude instead of the expected one
            rcs on.
            set VelYaw1_PID:setpoint to LngVel_PID:update(missionT, longitude).
        } else {
            set VelYaw1_PID:setpoint to LngVel_PID:update(missionT, landingPos:lng + posMod2).
        }
        set steerYaw to VelYaw1_PID:update(missionT, velLng).
        
        set steer to ship:srfretrograde + R(steerPitch, steerYaw, 0).  //HERE IS TROUBLE LINE OF CODE
        if (curAlt <= 3000) or ((longitude - 0.03) <= landingTarget:lng) // if below 3k or almost directly over landing pos, it's time to slow down
        {
        
            set step to false.
        }
set oldT to missionT.
    set prevPos to curPos.
    wait 0.025. //sampling time


}

 

Link to comment
Share on other sites

2 hours ago, dakhr said:

 

Nothing you posted looks like it should be a problem, but that's not the entire program.  I can't see the script you called the function *from* so I don't know what was done just before this occurred.  Did anything prior to this use a variable called "R", perhaps a variable for a 'radius' or something like that?

At any rate the first test is to just make a very dumb little script that uses R() and doesn't do much else, to see if it's R() itself that's broken, or if there's something else getting in it's way based on what the script did beforehand.

This would work, for example:

// test_r.ks script
//
// Run on a vessel just hanging in low kerbin orbit.
// Make sure it is a vessel that has run NO OTHER scripts yet.
// (i.e. use the cheat menu to set orbit to just put a
// vessel in low orbit, then try this script.)
//
print "Now trying to lock steering to prograde.".
lock steering to prograde.
wait 15.
print "Okay, now trying to lock steering to prograde + R(0,90,0).".
lock steering to prograde + R(0,90,0).
wait 15.
print "Did it work or did it crash?".

 

Link to comment
Share on other sites

Quote

Did anything prior to this use a variable called "R", perhaps a variable for a 'radius' or something like that?

Bingo! I have found variable R in some dummy function, which I did never called (it just was sitting there and I forget about it).Thanks!.Merry Christmas !

Edited by dakhr
edit
Link to comment
Share on other sites

8 hours ago, Briso said:

Sorry I'm certainly not at the right place, but I have a problem with kos can the problem is so stupid and you are faced with it. How can I solve it ? https://m.imgur.com/gallery/TQrspGz

i think the relies version dos not support the new API of trajectory (at the moment)

ther is a version that dos in this tread

Link to comment
Share on other sites

If it helps, I'll leave you the script. The error is observed in Falcon9.L.ks: line 36, 40, 50 and 412. In The Falcon 9.L.functions.ks: line 6. (ksp 1.4.1)

https://pastebin.com/paXan50U

https://pastebin.com/YFhySkx6

https://imgur.com/3IcczqZ

Edit: When using KSP 1.2.2, kos 1.2.2 and trajectories 1.2.2, everything works. But when using KSP 1.4.1, kos 1.4.1 and trajectories 1.4.1, it does not work. I don't know what this is about.

Edited by Briso
Link to comment
Share on other sites

Hey guys, I'm running into a problem here I can't seem to understand.

I'm writing a gui and since I've got multiple buttons which should all look the same with a custom image as background, I wanted to add a new skin. However, when I assign the skin to the button, the image is quite off and distorted and it's not where it's supposed to be. However, if I don't assign that skin and instead change all of the buttons style attributes, it works just fine and looks how I want it to be.

Any guidance? :/ I really dont want to have 8 times 8 lines of code for these identical buttons.. :/

The skin I want to create is skin_modeToggle and the button I want to assign it to is b_spd. Would be awesome if anyone could help me, thanks!

// GUI FOR AUTOPILOT

clearguis().
print "gui started.".
local exit is false.

local g is gui(500).
set g:x to 20.	// start position
set g:y to 50.
set g:style:padding:h to 5. // padding = Spacing between the outside of the widget and its contents (text, etc.).
set g:style:padding:v to 5.
set g:style:bg to "KK_ap/gui_bg.png".



// SKIN for mode toggles
set skin_modeToggle to g:skin:add("modeToggle",g:skin:toggle).
set skin_modeToggle:textcolor to rgb(0.8,0.8,0.8).
set skin_modeToggle:on:textcolor to rgb(0.1,0.9,0.1).
set skin_modeToggle:hover:textcolor to rgb(1.0,0.8,0.0).
set skin_modeToggle:hover_on:textcolor to rgb(1.0,0.8,0.0).
set skin_modeToggle:active:textcolor to rgb(0.1,0.9,0.1).
set skin_modeToggle:active_on:textcolor to rgb(0.1,0.9,0.1).
set skin_modeToggle:width to 46.
set skin_modeToggle:height to 30.
set skin_modeToggle:bg to "KK_ap/button_empty_std.png".
set skin_modeToggle:on:bg to "KK_ap/button_empty_pushed.png".
set skin_modeToggle:hover:bg to "KK_ap/button_empty_pushed.png".
set skin_modeToggle:hover_on:bg to "KK_ap/button_empty_pushed.png".
set skin_modeToggle:active:bg to "KK_ap/button_empty_pushed.png".
set skin_modeToggle:active_on:bg to "KK_ap/button_empty_pushed.png".


local box_title is g:addhbox().
	local l_logo is box_title:addlabel("").
	set l_logo:image to "KK_ap/logo.png".
	set l_logo:style:margin:bottom to 10. // margin = Spacing between this and other widgets.
	set l_logo:style:margin:right to 20.
	set l_logo:style:width to 160.

	local l_vesselname is box_title:addlabel("<b>" + ship:name + "</b>").
	set l_vesselname:style:width to 280.
	set l_vesselname:style:align to "left".
	set l_vesselname:style:fontsize to 22.
	set l_vesselname:style:margin:top to 10.
	set l_vesselname:style:textcolor to rgb(1.0,0.8,0.0).

	local b_exit is box_title:addbutton("").
	set b_exit:style:bg to "KK_ap/button_close_std.png".
	set b_exit:style:on:bg to "KK_ap/button_close_std.png".
	set b_exit:style:hover:bg to "KK_ap/button_close_hover.png".
	set b_exit:style:hover_on:bg to "KK_ap/button_close_hover.png".
	set b_exit:style:active:bg to "KK_ap/button_close_hover.png".
	set b_exit:style:active_on:bg to "KK_ap/button_close_std.png".
	set b_exit:style:width to 30.
	set b_exit:style:height to 30.	
	set b_exit:style:margin:top to 9.
	set b_exit:style:align to "right".
	set b_exit:onclick to { set exit to true.}.

local box_main is g:addhbox().
	local box_modes is box_main:addhbox(). // want to see a box containing all the mode toggles and parameter fields

		local box_modetoggles is box_modes:addvlayout(). // don't wanna see this

			local b_spd is box_modetoggles:addbutton("SPD").
			set b_spd:style to skin_modeToggle.


g:show().
//wait 2.
wait until exit.
g:dispose().
print "gui stopped.".

Edit: Just realized that I forgot to add the toggle behaviour to the bottom / use checkbox instead, but this doesn't change anything about my issue.. :/

Edited by Kartoffelkuchen
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...