Jump to content

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


Dunbaratu

Recommended Posts

4 hours ago, kcs123 said:

Whenever I try to add R(0,0,RelativeRoll) to direction I also got pitch and yaw too, and that is not desired.

If you go back in this thread 2 or 3 pages, you should find the doubts I had about the R function. It turns out that for legacy reasons the parameters of the R function are called pitch, yaw and roll but are in fact rotations around the x, y and z axes local to your vessel. And of those axes, I think only the y axis is sure to point parallel to south to north of the current SOI body. This is covered in the documentation too; but I had made the mistake of taking the parameters literally because of their names too. I think the same applies to the Q function too. 

Link to comment
Share on other sites

1 hour ago, Pand5461 said:

@kcs123, NORMALIZED suffix is redundant for direction components as they already are unit vectors (saves a few bytes in script and some IPU).

Currently, my docking script is more a help tool than complete docking script, I wrote bunch of comment lines in it, to help me in debuging in case I need to put KSP playing on hiatus and want to resume development after longer period of time. So, memory is not much of issue and I run it from archive at positions around kerbin, mun and minimus where connection to KSC is no longer issue (covered properly by relay network). But thanks for info, something worth to know when time comes for optimization of whole code.

Link to comment
Share on other sites

1 hour ago, scimas said:

If you go back in this thread 2 or 3 pages, you should find the doubts I had about the R function. It turns out that for legacy reasons the parameters of the R function are called pitch, yaw and roll but are in fact rotations around the x, y and z axes local to your vessel. And of those axes, I think only the y axis is sure to point parallel to south to north of the current SOI body. This is covered in the documentation too; but I had made the mistake of taking the parameters literally because of their names too. I think the same applies to the Q function too. 

Yes, I recall that info, I tried to use R() values directly from target too (instead of roll_for() function from lib_navball) without much luck. Probably didn't work either due to loss of info about roll trough converting between vectors and directions as Pellinor explained. I'm aware that yaw,pitch and roll is angle around ship axis, but I was expecting change of angle around only one axis if other two were zero, but I got full direction change instead.

But, since TARGET:PORTFACING:FOREVECTOR is already direction that is aware what is UP I have expected that I will got both axes, forrward and up properly, but inverted. But unfortunately roll value was lost in calculations while starboard vector is preserved. Fortunately, pellinor give even better solution.

Link to comment
Share on other sites

@scimas, @kcs123

Note on rotations.

R(Pt, Yw, Rl) rotates first Pt degrees around global X axis, then Yw degrees around rotated Y axis, then Rl degrees around rotated Z axis. And R(Pt1, Yw1, Rl1) + R(Pt2, Yw2, Rl2) = R(Pt1 + Pt2, Yw1 + Yw2, Rl1 + Rl2).

From this, you can readily see that adding R(0,0,Rl) to any rotation induces additional roll angle.

In case of R(Pt1, Yw1, Rl1) * R(Pt2, Yw2, Rl2), rotations are done right to left. I will try to explain what happens in this case.

Let istar, itop and ifore be the unit vectors of the rotated coordinate system and ix, iy and iz unit vectors of the global coordinate system. Initially, those triplets coincide. After each rotation, starting from rightmost, the whole space rotates to match global (ix, iy, iz) with the unit vectors defining R(Pt, Yw, Rl) rotation. With each rotation, istar, itop and ifore rotate as if they are nailed to the space.

So, SHIP:FACING * R(20,0,0) is the orientation of the ship pitched down 20 degrees, where by "pitch" I mean rotation of the ship around its starboard vector.

R() + R(0,0,roll) must be the same as R() * R(0,0,roll), at least my kOS terminal tells me so.

Link to comment
Share on other sites

@Pand5461, what you have described is exactly what I expected to be. In fact, when I was using this kind of steering:

// variables myPitch,LandHeading,MyRoll were set to some value elsewhere

lock STEERING to NORTH + R(myPitch,LandHeading,MyRoll).
// or this one:
lock STEERING to HEADING(MyHeading,myPitch) + R(myPitch,LandHeading,MyRoll).

I got proper behaviour, craft is steered to desired direction and it is rolled to desired degree.

However, in previous decribed case, when I used inverted direction from target as initial direction and after that either, add or multiply that direction with R(0,0,MyRoll), craft have also shifted with Fore vector and Starboard vector. I was expecting to keep facing toward target with offset only in roll. However, that is not happened. Ship have changed heading and pitching too, while it should not do that.

Link to comment
Share on other sites

I know its late and all, but I can't figure out why this doesn't work.  I've read through the page on flow control a couple times and im not getting it.  

Quote

ON AG1 { 
    print "Action group 1 clicked!".
    return true.
}
UNTIL 1=2 {
}

 

Link to comment
Share on other sites

2 hours ago, Pand5461 said:

@eberkain

ON and WHEN ... THEN triggers only work while the main script is running. Are you sure you activate AG while script is running? To make sure, you can always add WAIT UNTIL FALSE to the end of the main script, effectively putting it into an endless loop.

Thats what the Until 1=2 does, puts it into an endless loop., I am an certain the script is running because I have to CTRL C to break out of it. 

Edited by eberkain
Link to comment
Share on other sites

9 hours ago, eberkain said:

I know its late and all, but I can't figure out why this doesn't work.  I've read through the page on flow control a couple times and im not getting it.  

 

You don't need return command with triggers. Also, if you want for trigger to be repeatable, each time some event happen, not just first time, you need preserve keyword.
Try something like this:

set MyVariable to false.

ON AG1 { 
    toggle MyVariable. // this will change value to oposite from previous one
    print "Action group 1 clicked! MyVariable = "+MyVariable.
    preserve. // this ensure that this event is repeatable
}. // not sure if "." is necessary here, but it does not make any harm when you are in doubt.

UNTIL 1=2 {
  wait 0. // sometimes that was necessary, migth depend on kOS version, not 100% about it
}.

 

Link to comment
Share on other sites

6 minutes ago, kcs123 said:

You don't need return command with triggers. Also, if you want for trigger to be repeatable, each time some event happen, not just first time, you need preserve keyword.

Returning true is meant to be equivalent to using the preserve keyword.

https://ksp-kos.github.io/KOS/language/flow.html#preserving-with-return

Link to comment
Share on other sites

Yeah the docs said that preserve keyword will eventually get removed and return true or false is the preferred.   

I'm also trying to increase the disk size and thats not working for me either, I don't think the mod likes me very much. 

 

Edited by eberkain
Link to comment
Share on other sites

How about a different question, is there any way to optimize this to use a single ON trigger?  

SET CPUID = 1.
ON AG1 { 
	IF CPUDID = 1 {
		print "Action group 1 clicked!".
		return true.
	}
}
ON AG2 { 
	IF CPUDID = 2 {
		print "Action group 2 clicked!".
		return true.
	}
}
ON AG3 { 
	IF CPUDID = 3 {
		print "Action group 3 clicked!".
		return true.
	}
}

 

Link to comment
Share on other sites

39 minutes ago, eberkain said:

How about a different question, is there any way to optimize this to use a single ON trigger?  


SET CPUID = 1.
ON AG1 { 
	IF CPUDID = 1 {
		print "Action group 1 clicked!".
		return true.
	}
}
ON AG2 { 
	IF CPUDID = 2 {
		print "Action group 2 clicked!".
		return true.
	}
}
ON AG3 { 
	IF CPUDID = 3 {
		print "Action group 3 clicked!".
		return true.
	}
}

 

I don't know why you would call it more optimal to do given that you still have to perform all the same checks every time.

But you can do this if you like:
 

WHEN AG1 <> oldAG1 or AG2 <> oldAG2 or AG3 <> oldAG3 THEN {
  set oldAG1 to AG1.
  set oldAG2 to AG2.
  set oldAG2 to AG3.
  ... rest of your code here ...
}

Essentially this is just what ON does anyway - it remembers what the value of the expression was the last time it checked, and sees if it is now different than it used to be.

3 hours ago, eberkain said:

Thats what the Until 1=2 does, puts it into an endless loop., I am an certain the script is running because I have to CTRL C to break out of it. 

until false {

does the same thing and is a bit clearer about your intent.

 

Link to comment
Share on other sites

1 hour ago, Steven Mading said:

I don't know why you would call it more optimal to do given that you still have to perform all the same checks every time.

But you can do this if you like:
 


WHEN AG1 <> oldAG1 or AG2 <> oldAG2 or AG3 <> oldAG3 THEN {
  set oldAG1 to AG1.
  set oldAG2 to AG2.
  set oldAG2 to AG3.
  ... rest of your code here ...
}

Essentially this is just what ON does anyway - it remembers what the value of the expression was the last time it checked, and sees if it is now different than it used to be.

until false {

does the same thing and is a bit clearer about your intent.

 

Let me ammend my question.    Assume CPUID is a static value that will never change during the life of the program.  

Lets say I have the same program running on multiple kOSProcessors and each instance of the scrip has a different CPUID assigned.  Is there a way to reduce the number of ON statements when we know the contents of some of the ON statements will never execute.   For example, will something like this work?

SET CPUID = 1.
IF CPUID = 1 { 	SET AG_ID TO AG1.  }
IF CPUID = 2 { 	SET AG_ID TO AG2.  }
IF CPUID = 3 { 	SET AG_ID TO AG3.  }

ON AG_ID { 
    print "Action group clicked!".
    return true.
}

 

Link to comment
Share on other sites

It does indeed work

Dy8wwKq.png

PARAMETER CPUID IS -1.

IF CPUID = 1 { 	LOCK AG_ID1 TO AG201.  }
IF CPUID = 1 { 	LOCK AG_ID2 TO AG202.  }
IF CPUID = 1 { 	LOCK AG_ID3 TO AG203.  }
IF CPUID = 1 { 	LOCK AG_ID4 TO AG204.  }
IF CPUID = 1 { 	LOCK AG_ID5 TO AG205.  }
IF CPUID = 1 { 	LOCK AG_ID6 TO AG206.  }

IF CPUID = 2 { 	LOCK AG_ID1 TO AG207.  }
IF CPUID = 2 { 	LOCK AG_ID2 TO AG208.  }
IF CPUID = 2 { 	LOCK AG_ID3 TO AG209.  }
IF CPUID = 2 { 	LOCK AG_ID4 TO AG210.  }
IF CPUID = 2 { 	LOCK AG_ID5 TO AG211.  }
IF CPUID = 2 { 	LOCK AG_ID6 TO AG212.  }

ON AG_ID1 { 
    print "MFD " + CPUID + ":1".
    return true.
}
ON AG_ID2 { 
    print "MFD " + CPUID + ":2".
    return true.
}
ON AG_ID3 { 
    print "MFD " + CPUID + ":3".
    return true.
}
ON AG_ID4 { 
    print "MFD " + CPUID + ":4".
    return true.
}
ON AG_ID5 { 
    print "MFD " + CPUID + ":5".
    return true.
}
ON AG_ID6 { 
    print "MFD " + CPUID + ":6".
    return true.
}

In the AHK GUI I can click a button  (to later be replaced with a hardware controller) 

AHK then activates the KSP window and sends a keypress

AGX responds to the keypress and actives an empty action group (201-218)

kOS script sees the Action Group activation and can react

I can run the same script on different CPUs and the script will be able to tell which set of MFD buttons it should react to. 

Edited by eberkain
Link to comment
Share on other sites

Here's an improved version. Pushing first stage of My_ISS  to 400Km, polar orbit.

LAN and Inclination within 0.02 ish. Uses holman transfers for burntime calcs.

 

 

Kos400  (approx 9 Minutes at 4x speed)

I only attend to the code every so often, so have no interest in cleaning it up at the moment. got to much else to do :)

// For K3 Rocket - The first stage of My_ISS
//===========================================

@lazyglobal off.
//******************************************************************************
//								SHIP PARTS
//******************************************************************************
//*** STAGE 1+2 ***
DECLARE	GLOBAL ThisShip is "".
DECLARE	GLOBAL Stg1Thrust is "".
DECLARE	GLOBAL Stg2Thrust is "".
DECLARE	GLOBAL Stg2Wheel is "".

//*** LARGE DOCKING PORTS ***
DECLARE	GLOBAL K3CBM_Top is "".
DECLARE	GLOBAL K3CBM_1200 is "".
DECLARE	GLOBAL K3CBM_0300 is "".
DECLARE	GLOBAL K3CBM_0600 is "".
DECLARE	GLOBAL K3CBM_0900 is "".
DECLARE	GLOBAL K3BPort is "".


//*** NAVIGATION LIGHTS ***
DECLARE	GLOBAL K3NavF_Red is "".
DECLARE	GLOBAL K3NavM_Red is "".
DECLARE	GLOBAL K3NavA_Red is "".

DECLARE	GLOBAL K3NavF_Grn is "".
DECLARE	GLOBAL K3NavM_Grn is "".
DECLARE	GLOBAL K3NavA_Grn is "".

DECLARE	GLOBAL K3NavF_Wht is "".
DECLARE	GLOBAL K3NavM_Wht is "".
DECLARE	GLOBAL K3NavA_Wht is "".


//*** BLUE LIGHTS ***
DECLARE	GLOBAL K3BluF_1030 is "".
DECLARE	GLOBAL K3BluF_0130 is "".
DECLARE	GLOBAL K3BluF_0430 is "".
DECLARE	GLOBAL K3BluF_0730 is "".

DECLARE	GLOBAL K3BluM_1030 is "".
DECLARE	GLOBAL K3BluM_0130 is "".
DECLARE	GLOBAL K3BluM_0430 is "".
DECLARE	GLOBAL K3BluM_0730 is "".

DECLARE	GLOBAL K3BluA_1030 is "".
DECLARE	GLOBAL K3BluA_0130 is "".
DECLARE	GLOBAL K3BluA_0430 is "".
DECLARE	GLOBAL K3BluA_0730 is "".

//*** STAGE-3 ENGINES ***
DECLARE	GLOBAL K3Eng_A1 is "".
DECLARE	GLOBAL K3Eng_A2 is "".
DECLARE	GLOBAL K3Eng_B1 is "".
DECLARE	GLOBAL K3Eng_B2 is "".
DECLARE	GLOBAL K3Eng_C1 is "".
DECLARE	GLOBAL K3Eng_C2 is "".
DECLARE	GLOBAL K3Eng_D1 is "".
DECLARE	GLOBAL K3Eng_D2 is "".
DECLARE	GLOBAL K3Eng_E1 is "".
DECLARE	GLOBAL K3Eng_E2 is "".
DECLARE	GLOBAL K3Eng_F1 is "".
DECLARE	GLOBAL K3Eng_F2 is "".

DECLARE	GLOBAL K3Eng_A is "".
DECLARE	GLOBAL K3Eng_B is "".
DECLARE	GLOBAL K3Eng_C is "".
DECLARE	GLOBAL K3Eng_D is "".
DECLARE	GLOBAL K3Eng_E is "".
DECLARE	GLOBAL K3Eng_F is "".



//*** OXYGEN TANK (UP TOP) ***
DECLARE	GLOBAL K3TankO2_01 is "".

//*** CENTRAL HUB FOR THIS UNIT ***
//K3_HUB_01
DECLARE	GLOBAL K3HUB_01 is "".

//*** PROBE UNIT REQUIRED FOR PROBE-CONTROL-ROOM ***
DECLARE	GLOBAL K3RGU_01 is "".

//*** KOS PROCESSOR UNITS ***
DECLARE	GLOBAL K3KAL9K_1200 is "".
DECLARE	GLOBAL K3KAL9K_0300 is "".
DECLARE	GLOBAL K3KAL9K_0600 is "".
DECLARE	GLOBAL K3KAL9K_0900 is "".


//*** G-SENSORS ***
DECLARE	GLOBAL K3GSens_1200 is "".
DECLARE	GLOBAL K3GSens_0300 is "".
DECLARE	GLOBAL K3GSens_0600 is "".
DECLARE	GLOBAL K3GSens_0900 is "".

//*** WHIP AERIALS ***
DECLARE	GLOBAL K3COM32_1030 is "".
DECLARE	GLOBAL K3COM32_0130 is "".
DECLARE	GLOBAL K3COM32_0430 is "".
DECLARE	GLOBAL K3COM32_0730 is "".


//*** LITHIUM PEROXIDE TANK  ***
DECLARE	GLOBAL K3LitParox_01 is "".

//*** WATER TANK  ***
DECLARE	GLOBAL K3WaterTank_01 is "".

//*** FOOD TANK  ***
DECLARE	GLOBAL K3FoodTank_01 is "".

//*** MMH NTO TANK  ***
DECLARE	GLOBAL K3MMHNTO_01 is "".

//*** BATTERY - 1  ***
DECLARE	GLOBAL K3BATT_01 is "".


//*** HABITAT WHEEL ***
DECLARE	GLOBAL K3WHEEL_01 is "".
DECLARE	GLOBAL K3WHEEL_01 is "".

//*** LITHIUM PREOXIDE SCRUBBER ***
DECLARE	GLOBAL K3Scrubber_01 is "".

//*** WATER SPLITTER ***
DECLARE	GLOBAL K3Splitter_01 is "".

//*** WATER PURIFIER ***
DECLARE	GLOBAL K3Purifier_01 is "".

//*** RADIATOR PANELS ***
DECLARE	GLOBAL K3RadM_0130D is "".
DECLARE	GLOBAL K3RadM_0130A is "".

DECLARE	GLOBAL K3RadM_0430D is "".
DECLARE	GLOBAL K3RadM_0430A is "".

DECLARE	GLOBAL K3RadM_0730D is "".
DECLARE	GLOBAL K3RadM_0730A is "".

DECLARE	GLOBAL K3RadM_1030D is "".
DECLARE	GLOBAL K3RadM_1030A is "".


//*** SOLAR PANELS ***
DECLARE	GLOBAL K3XT4_0130 is "".
DECLARE	GLOBAL K3XT4_0430 is "".
DECLARE	GLOBAL K3XT4_0730 is "".
DECLARE	GLOBAL K3XT4_1030 is "".

DECLARE	GLOBAL K3NIV18_1200 is "".
DECLARE	GLOBAL K3NIV18_0300 is "".
DECLARE	GLOBAL K3NIV18_0600 is "".
DECLARE	GLOBAL K3NIV18_0900 is "".


//*** HABITAT MODULE ***
DECLARE	GLOBAL K3HabM_DOCK is "".
DECLARE	GLOBAL K3HabM_DOCKL is "".



//******************************************************************************
//								OTHER BITS
//******************************************************************************
DECLARE GLOBAL Lunars is "".



//******************************************************************************
//							TARGET PARAMETERS
//******************************************************************************

GLOBAL	TargName is "NONE".
GLOBAL	TargInclination IS 90.
GLOBAL	TargApoapsisHgt IS 415000.
GLOBAL	TargPeriapsisHgt IS 405000.
GLOBAL	TargLan IS 210.
GLOBAL	TargLanLong IS 0.
GLOBAL	TargLatitude IS 0.
GLOBAL	TargLongitude IS 0.
GLOBAL	TargAltitude IS 415000.
GLOBAL	TargStationHgt IS 415000.
GLOBAL	TargMaxHeight IS 0.
GLOBAL	TargMinHeight IS 900000.
GLOBAL	ParkingOrbit IS 200000.


GLOBAL	TargPos_V IS V(0.0,0.0,0.0).  //SHIP:POSITION
GLOBAL	TargBPos_V IS V(0.0,0.0,0.0).
GLOBAL	TargApoapsis_V IS V(0.0,0.0,0.0).
GLOBAL	TargPeriapsis_V IS V(0.0,0.0,0.0).

GLOBAL	LastDistance IS 0.0.
GLOBAL	T_ApoapsisAngle IS 0.0.
GLOBAL	T_PeriapsisAngle IS 0.0.
GLOBAL	D1_Angle IS 0.0.
GLOBAL	D2_Angle IS 0.0.
GLOBAL	D_Wangle IS 0.0.
GLOBAL	M_Wangle IS 0.0.
GLOBAL	DistanceFlag IS 0.

//******************************************************************************
//							SHIP PARAMETERS
//******************************************************************************
GLOBAL	ShipAltitude IS 0.0.
GLOBAL	ShipPitch IS 0.0.
GLOBAL	ShipExitPitch is 10.0.
GLOBAL	ShipPitchRange is 90.
GLOBAL	ShipPosV IS V(0.0,0.0,0.0).
GLOBAL	ShipPos_V IS V(0.0,0.0,0.0).
GLOBAL	ShipBPos_V IS V(0.0,0.0,0.0).
      	
GLOBAL	ShipVelocity_V IS V(0.0,0.0,0.0).
GLOBAL	ShipFore_V IS V(0.0,0.0,0.0).
GLOBAL	ShipTop_V IS V(0.0,0.0,0.0).
GLOBAL	ShipStar_V IS V(0.0,0.0,0.0).
GLOBAL	ShipPos_DV IS V(0.0,0.0,0.0).


GLOBAL	ShipDirection is 0.0.
GLOBAL	ShipLaunchHead is 0.0.
GLOBAL	ShipInclination IS 0.0.
GLOBAL	ShipLan IS 0.0.
GLOBAL	ShipLanLong IS 0.0.
GLOBAL	ShipLaunchLong is 0.0.
GLOBAL	ShipLatitude IS 0.
GLOBAL	ShipLongitude IS 0.

GLOBAL	ShipPeriapisHgt IS 0.0.
GLOBAL	ShipApoapsisHgt IS 0.0.
GLOBAL	ShipOrbitalPeriod IS 0.0.
GLOBAL	ShipEtaApoapsis IS 0.0.
GLOBAL	ShipEtaApoapsisLast IS 0.0.
GLOBAL	ShipEtaPeriapsis IS 0.0.
GLOBAL	ShipEtaPeriapsisLast IS 0.0.
GLOBAL	ShipDApoapsis IS 0.0.
GLOBAL	ShipDPeriapsis IS 0.0.

GLOBAL	i_RollHeight is 300.
GLOBAL	f_StageFuel is 100.0.
GLOBAL	i_PitchAtAlt is 85000.
GLOBAL	i_PitchStartAlt is 500.
GLOBAL	DDINC is 0.0.
GLOBAL	ThisStage is 0.
GLOBAL	ShipBurnTime is 0.0.
GLOBAL	ShipApoVelocity is 0.0.

//*** HABITAT ENGINE SPECS *************
GLOBAL	HabEngine_ISP is 320.824.			// Vacuum ISP
GLOBAL	HabEngine_NEWTONS is 1780.			// Newtons Thrust
GLOBAL	HabEngine_MMH is 0.2137. 			// Consumption rate - kg persec
GLOBAL	HabEngine_NTO is 0.3521.			// Consumption rate - kg persec

//*** HABITAT RCS SPECS ****************
GLOBAL	HabRCS_Aerozine50 is 0.1669.		// Consumption rate - kg?? persec
GLOBAL	HabRCS_NTO is 0.1669.				// Consumption rate - kg?? persec


//******************************************************************************
//						EARTH PARAMETERS
//******************************************************************************
GLOBAL	EarthRotang IS 0.0.
GLOBAL	ShipLatitude IS 0.0.
GLOBAL	ShipLongitude IS 0.0.
GLOBAL	HgtAtmosphere IS 0.
GLOBAL	Hgt01 IS 0.0.
GLOBAL	Hgt02 IS 0.0.
GLOBAL	Hgt03 IS 0.0.
GLOBAL	TimetoLaunch IS 0.
GLOBAL	Count is 0.
GLOBAL	Tangle IS 0.0.
GLOBAL	DVreq is 0.0.
GLOBAL	Vreq is 0.0.

//******************************************************************************
//
//******************************************************************************
//*** GENERAL ***
GLOBAL b_TMP IS FALSE.
GLOBAL b_Targ IS FALSE.
GLOBAL b_Roll IS FALSE.

//*** LAUNCH ***
GLOBAL b_LaunchAscNode IS FALSE.
GLOBAL b_PitchUP IS FALSE.
GLOBAL b_PitchDWN IS FALSE.

//*** ORBIT ***
GLOBAL	b_HaveINC IS FALSE.
GLOBAL	b_HaveLAN IS FALSE.
GLOBAL	b_ShipApoAlt IS FALSE.
GLOBAL	b_ShipPeriAlt IS FALSE.
GLOBAL	b_ProGrade IS FALSE.

GLOBAL b_TangleNDelay IS FALSE.
GLOBAL b_TangleSDelay IS FALSE.
GLOBAL b_RenTime IS TRUE.

GLOBAL b_INC_Reduction IS FALSE.

//******************************************************************************
//							Start of Program
//******************************************************************************
Set ThisShip to VESSEL("K_3").
Set ShipAltitude to ThisShip:ALTITUDE.
//Set ShipPitch to ThisShip:PITCH.
Set ShipPitch to 90.
Set ShipPos_V to SHIP:POSITION.
Set ShipBPos_V to SHIP:BODY:POSITION.

Set ShipVelocity_V to ThisShip:VELOCITY:ORBIT.
Set ShipFore_V to ThisShip:FACING:FOREVECTOR.
Set ShipTop_V to ThisShip:FACING:TOPVECTOR.
Set ShipStar_V to ThisShip:FACING:STARVECTOR.

Set ShipInclination to ThisShip:ORBIT:INCLINATION.
Set ShipLan to ThisShip:ORBIT:LAN.

Set ShipPeriapisHgt to ThisShip:ORBIT:PERIAPSIS.
Set ShipApoapsisHgt to ThisShip:ORBIT:APOAPSIS.
Set ShipOrbitalPeriod to ThisShip:ORBIT:PERIOD.
Set ShipEtaApoapsis to ETA:APOAPSIS.
Set ShipEtaPeriapsis to ETA:PERIAPSIS.

Set EarthRotang to ThisShip:ORBIT:BODY:ROTATIONANGLE.
Set Lunars to ThisShip:BODY.
Set ShipLatitude TO ThisShip:GEOPOSITION:LAT.
Set ShipLongitude TO ThisShip:GEOPOSITION:LNG.

Set Hgt01 to 0.0.
Set Hgt02 to 0.0.
Set Hgt03 to 0.0.

//******************************************************************************
//
//******************************************************************************
// GENERAL FLAG
Set b_TMP to FALSE.

// INCLINATION AND LAN - USED FOR ALIGNMENT
Set b_HaveINC to FALSE.
Set b_HaveLAN to FALSE.

// APSIDE FLAGS - USED FOR ALTITUDES
Set	b_ShipApoAlt to FALSE.
Set	b_ShipPeriAlt to FALSE.

// UP/DOWN FLAGS - USED FOR PITCH
Set b_PitchUP to FALSE.
Set b_PitchDWN to FALSE.

// ORIENTATION LOCK FLAGS
Set b_ProGrade to FALSE.

Set b_TangleNDelay to FALSE.							// Node Delay flags 
Set b_TangleSDelay to FALSE.							//
Set b_RenTime to TRUE.								// TRUE = At next node Run

IF ShipLongitude < 0 {Set ShipLongitude to ShipLongitude + 360.}	// Adjust for [0,360]
//******************************************************************************
//******************************************************************************
//							SCREENS
//******************************************************************************
//******************************************************************************
//------------------------------------------------------------------------------
// 							PRE-LAUNCH SCREEN
//------------------------------------------------------------------------------
Function Screenie_0
{
	Print "---TARGET-------------------------------" AT(1,1).

	Print "Name           : " + TargName AT (1,2).
	Print "Inclination    : " + TargInclination AT (1,3).
	Print "LAN            : " + TargLan AT (1,4).
	Print "Apoapsis       : " + TargApoapsisHgt AT (1,5).
	Print "Periapsis      : " + TargPeriapsisHgt AT (1,6).
	Print "Latitude       : " + TargLatitude AT (1,7).
	Print "Longitude      : " + TargLongitude AT (1,8).
	Print "Altitude       : " + TargAltitude AT (1,9).

	Print "---SHIP---------------------------------" AT (1,11).

	Print "Name           : " + ThisShip:SHIPNAME AT (1,12).
	Print "Inclination    : " + ShipInclination AT (1,13). 
	Print "LAN            : " + ShipLan AT (1,14). 
	Print "Apoapsis       : " + ShipApoapsisHgt AT (1,15).
	Print "Periapsis      : " + ShipPeriapisHgt AT (1,16).
	Print "Latitude       : " + ShipLatitude AT (1,17).
	Print "Longitude      : " + ShipLongitude AT (1,18).
	Print "Altitude       : " + ShipAltitude AT (1,19).

	Print "---STATUS-------------------------------" AT (1,21).
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
Function Screenie_01
{
	Print " WAITING FOR LAUNCH " AT (21,21).
	Print "Launch Time    :                       " AT (1,23).
	Print "Main Engines   : OFF                   " AT (1,24).
	Print "Base Clamps    : SECURED               " AT (1,25).
	Print "Tower Clamps   : SECURED               " AT (1,26).
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
Function Screenie_02
{
	Print "COUNTDOWN COMMENCING" AT (21,21).
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
Function Screenie_1
{
	Print "   <== STAGE-1 ==>  " AT (21,21).
	Print "Thrust Level   :                        " AT (1,23).
	Print "Pitch          :                        " AT (1,24).
	Print "Course         :                        " AT (1,25).
	Print "Apoapsis Time  :                        " AT (1,26).
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
Function Screenie_2
{
	Print "   <== STAGE-2 ==>  " AT (21,21).
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
Function Screenie_3A
{
	Print "   <== STAGE-3 ==>  " AT (21,21).
	Print "Thrust Level   :                        " AT (1,23).
	Print "DeltaV Required:                        " AT (1,24).
	Print "Start Time     :                        " AT (1,25).
	Print "Apoapsis Time  :                        " AT (1,26).
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
Function Screenie_3B
{
	Print "DeltaV Required:                        " AT (1,23).
	Print "Burn Time      :                        " AT (1,24).
	Print "Time Left      :                        " AT (1,25).
	Print "                                        " AT (1,26).
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
Function Screenie_4A
{
	Print "  <== STAGE-4A ==>  " AT (21,21).
	Print "DeltaV Required:                        " AT (1,23).
	Print "Burn Time      :                        " AT (1,24).
	Print "Time Left      :                        " AT (1,25).
	Print "                                        " AT (1,26).
}


//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
Function Screenie_4B
{
	Print "  <== STAGE-4B ==>  " AT (21,21).
	Print "DeltaV Required:                        " AT (1,23).
	Print "Burn Time      :                        " AT (1,24).
	Print "Time Left      :                        " AT (1,25).
	Print "                                        " AT (1,26).
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
Function Screenie_3ADBG
{
	Print "   <==  DEBUG  ==>  " AT (21,21).
	Print "mU             :                        " AT (1,23).
	Print "Earth Radius   :                        " AT (1,24).
	Print "ApoapsisHgt    :                        " AT (1,25).
	Print "ApoVelReq      :                        " AT (1,26).
	Print "Apoapsis Velo  :                        " AT (1,27).
	Print "DeltaV Required:                        " AT (1,28).
	Print "Start Mass     :                        " AT (1,29).
	Print "End Mass       :                        " AT (1,30).
	Print "Avg Mass       :                        " AT (1,31).
	Print "Avg Accel      :                        " AT (1,32).
	Print "Burntime       :                        " AT (1,33).
}
//******************************************************************************
//						PROGRAM START - LAUNCH TO ORBIT
//******************************************************************************
	Set Terminal:WIDTH to 42.
	Set Terminal:HEIGHT to 30.


	ClearScreen.
	Screenie_0().
	Stage_0().
	Screenie_1().
	Stage_1().
	Screenie_2().
	Stage_2().
	Screenie_3A().
	Stage_3().
	IF b_Targ = FALSE
	{
		Stage_4().
	}
	Cruise().


//******************************************************************************
//									Stage_0
//******************************************************************************
FUNCTION Stage_0
{
LOCAL Count is 0.

	Screenie_01().
	RCS OFF.
	SAS OFF.
	MakeShipParts().
	Set_Heights().
	Set_ShipLaunchPosInfo().
	Set_ShipLaunchAngle().
	DO_Params0().
	Until PreLaunch_Longitude() = TRUE
	{
		Print TimetoLaunch at (18,23).
		DO_Params0().
	}

	// We're at 5 seconds to launch - start countdown
	Screenie_02().
	Set Count to 5.
	UNTIL Count = 0
	{
		PRINT "                        "  AT (17,23).
		PRINT Count AT (18,23).
		IF Count = 4
	 	{
			Set THROTTLE to 1.
			STAGE.
			Print " ACTIVATED             " AT (17,24).
		}
		SET Count to Count - 1.
	    WAIT 1.
	}
	wait 0.001.
	Set STEERING to HEADING(ShipDirection,ShipPitch).
	STAGE.
	Print " RELEASED               " AT (17,25).
	Print " RELEASED               " AT (17,26).
	WAIT 1.
}
//******************************************************************************
//									Stage_1
//******************************************************************************
FUNCTION Stage_1
{
LOCAL b_Busy1 is TRUE.
LOCAL b_Pro is FALSE.


	Set ThisStage to 1.
	Until b_Busy1 = FALSE
	{
		DO_Params().
		If Stg1Thrust:thrust < 10  
		{
			STAGE.
			Set b_Busy1 to FALSE.
		}
		ELSE
		{
			DO_PitchProgram().
			DO_SteeringProgram().
			IF b_Pro = FALSE
			{
				IF b_Prograde = TRUE
				{
					SAS ON.
					Set SASMODE to "PROGRADE".
					Set b_Pro to TRUE.
				}
			}
			DO_Screenies().
		}
		IF b_Prograde = FALSE
		{
			Set STEERING to HEADING(ShipDirection,ShipPitch).
		}
	}
}
//******************************************************************************
//									Stage_2
//******************************************************************************
FUNCTION Stage_2
{
LOCAL b_Busy2 is TRUE.
LOCAL b_Pro is FALSE.
LOCAL BCount is 500.
LOCAL CCount is 1000.

	Set ThisStage to 2.
	RCS ON.
	SAS OFF.

	K3NIV18_1200:DOACTION("deploy panels",TRUE).
	K3NIV18_0300:DOACTION("deploy panels",TRUE).
	K3NIV18_0600:DOACTION("deploy panels",TRUE).
	K3NIV18_0900:DOACTION("deploy panels",TRUE).

	Until b_Busy2 = FALSE
	{
		//*** CURRENT SHIP STATE ***
		DO_Params().

		Set BCount to BCount - 1.
		IF BCount < 1
 		{
			Set BCount to 0.
			If Stg2Thrust:thrust < 10 
			{
				STAGE.
				Set b_Busy2 to FALSE.
			}
		}
		IF b_Busy2 = TRUE
		{
			DO_PitchProgram().
			DO_SteeringProgram().
			IF b_Pro = FALSE
			{
				IF b_Prograde = TRUE
				{
					SAS ON.
					Set b_Pro to TRUE.
				}
			}
		}
		DO_Screenies().

		Set CCount to CCount - 1.
		If CCount < 1 {Set CCount to 0.}
		If CCount = 900{COMM32_Antenna_ON().}
		If CCount = 700{NAV_Lights_ON().}
		If CCount = 500{BLU_Lights_ON().}

		IF b_Prograde = FALSE
		{
			Set STEERING to HEADING(ShipDirection,ShipPitch).
		}
	}
}
//******************************************************************************
//									Stage_3
//******************************************************************************
FUNCTION Stage_3
{
LOCAL StartTime is 0.0.
LOCAL b_Burn is FALSE.
LOCAL ThisTime is 0.0.
LOCAL b_OnOff is FALSE.
LOCAL b_Time is FALSE.

	Run_Stg3_Engs(1).
	wait 2.
	Run_Stg3_Engs(0).
	RCS OFF.
	SAS ON.
	SolarPanelsON().
	RadiatorPanelsON().
	Set ThisTime to 7.
	Until ThisTime < 1
	{
		Set Steering to Thisship:VELOCITY:ORBIT.
		Set ThisTime to ThisTime - 1.
		Wait 1.
	}

	CalcBurnTime().
	Set ThisTime to (ShipBurnTime * 0.5).
	Set ShipEtaApoapsis to ETA:APOAPSIS.
	Set kuniverse:timewarp:mode to "PHYSICS".
	Set kuniverse:timewarp:warp to 3.
	Until ShipEtaApoapsis < ThisTime 
	{
		Set Steering to Thisship:VELOCITY:ORBIT.
		Set ShipEtaApoapsis to ETA:APOAPSIS.
		Set StartTime to ShipEtaApoapsis - ThisTime.
		If b_OnOff = FALSE
		{
			If b_Time = FALSE.
			{
				IF StartTime < 25
				{
					Set kuniverse:timewarp:warp to 0.
					Set b_Time to TRUE.
				}
			}
			IF StartTime < 20
			{
				SolarPanelsOFF().
				RadiatorPanelsOFF().
				Set b_OnOff to TRUE.
			}	
		}
		Print StartTime at (18,25).
		Print ShipEtaApoapsis at (18,26).
		DO_Params3().
		CalcDeltaV().
	}
	Screenie_3B().
	RCS ON.
	SAS OFF.
	Run_Stg3_Engs(6).

	Set ThisTime to TIME:SECONDS + ShipBurnTime.
	Set StartTime to TIME:SECONDS.
	Until StartTime < 0.1
	{
		Set Steering to Thisship:VELOCITY:ORBIT.
		DO_Params3().

		Set ShipEtaApoapsis to ETA:APOAPSIS.
		Set StartTime to (ThisTime - TIME:SECONDS).
		PRINT StartTime AT (18,24).
		PRINT (Vreq - ThisShip:VELOCITY:Orbit:MAG) at (18,23).
	}
	Run_Stg3_Engs(0).
	SolarPanelsON().
	RadiatorPanelsON().
}
//******************************************************************************
//									Stage_4
//******************************************************************************
Function Stage_4
{
LOCAL	b_Stage is TRUE.
LOCAL	A is 0.0.
LOCAL	B is 0.0.
LOCAL	Va is 0.0.
LOCAL	Vb is 0.0.
LOCAL	DVa is 0.0.
LOCAL	DVb is 0.0.
LOCAL	Bt is 0.0.

	Screenie_4A().
	RCS OFF.
	SAS ON.
	Set kuniverse:timewarp:mode to "PHYSICS".
	Set kuniverse:timewarp:warp to 3.
	Until b_Stage = FALSE
	{
		DO_Params3().
		Set Steering to Thisship:VELOCITY:ORBIT.
		IF ShipLatitude > 86
		{
			Set kuniverse:timewarp:warp to 0.
		}

		IF ShipLatitude > 87
		{ 
			Set b_Stage to FALSE.
			SolarPanelsOFF().
			RadiatorPanelsOFF().
			Set A to ThisShip:BODY:RADIUS + ((TargAltitude + ShipAltitude)/2).

			Set Va to SQRT(ThisShip:BODY:MU / (ThisShip:BODY:RADIUS + ShipAltitude)).
			Set Vb to SQRT(ThisShip:BODY:MU / (ThisShip:BODY:RADIUS + TargAltitude)).

			Set DVa to ((2 / (ThisShip:BODY:RADIUS + ShipAltitude)) - (1 / A)).
			Set DVa to SQRT(ThisShip:BODY:MU * DVa).
			Set DVa to ABS(DVa - Va).

			Set DVb to ((2 / (ThisShip:BODY:RADIUS + TargAltitude)) - (1 / A)).
			Set DVb to SQRT(ThisShip:BODY:MU * DVb).
			Set DVb to ABS(DVb - Vb).
		}
	}

	Set b_Stage to TRUE.
	Print DVa at (18,23).
	Set Bt to ABS(CalcBurnTime2(DVa)).
	Print Bt at (18,24).
	Until b_Stage = FALSE
	{
		DO_Params3(). 
		Set Steering to Thisship:VELOCITY:ORBIT.
		IF ShipLatitude > 88
		{ 
			Set b_Stage to FALSE.
			Set A to TIME:SECONDS + Bt.
		}
	}
	RCS ON.
	SAS OFF.
	Run_Stg3_Engs(6).
	Set b_Stage to TRUE.
	Until b_Stage = FALSE
	{
		Set Steering to Thisship:VELOCITY:ORBIT.
		DO_Params3().
		Set B to A - TIME:SECONDS.
		Print B at (18,25).
		IF B < 0.1
		{
			Set b_Stage to FALSE.
			Run_Stg3_Engs(0).
		}
	}

	Screenie_4B().
	RCS OFF.
	SAS ON.
	SolarPanelsON().
	RadiatorPanelsON().
	Set b_Stage to TRUE.
	wait 5.

	Set Bt to ABS(CalcBurnTime2(DVb)).
	Print DVb at (18,23).
	Print Bt at (18,24).

	Set kuniverse:timewarp:mode to "PHYSICS".
	Set kuniverse:timewarp:warp to 3.
	Until b_Stage = FALSE
	{
		DO_Params3().
		Set Steering to Thisship:VELOCITY:ORBIT.
		IF ShipLatitude < -86
		{
			Set kuniverse:timewarp:warp to 0.
		}
		IF ShipLatitude < -87
		{ 
			Set b_Stage to FALSE.
			SolarPanelsOFF().
			RadiatorPanelsOFF().
		}
	}
	Set b_Stage to TRUE.
	Until b_Stage = FALSE
	{
		DO_Params3(). 
		Set Steering to Thisship:VELOCITY:ORBIT.
		IF ShipLatitude < -88
		{ 
			Set b_Stage to FALSE.
			Set A to TIME:SECONDS + Bt.
		}
	}
	RCS ON.
	SAS OFF.
	Run_Stg3_Engs(6).
	Set b_Stage to TRUE.
	Until b_Stage = FALSE
	{
		Set Steering to Thisship:VELOCITY:ORBIT.
		DO_Params3().
		Set B to A - TIME:SECONDS.
		Print B at (18,25).
		IF B < 0.1
		{
			Set b_Stage to FALSE.
			Run_Stg3_Engs(0).
		}
	}
	SolarPanelsON().
	RadiatorPanelsON().
}
//******************************************************************************
//
//******************************************************************************
Function Cruise
{
	Unlock Steering.
	RCS OFF.
	SAS ON.
	Set SASMODE to "PROGRADE".
}

//******************************************************************************
//								DO_Params0
//******************************************************************************
FUNCTION DO_Params0
{
	Set EarthRotang to SHIP:ORBIT:BODY:ROTATIONANGLE.
	IF b_Targ = TRUE
	{
		Set TargPos_V to Target:POSITION.
		Set TargBPos_V to Target:BODY:POSITION.
		Set TargLatitude to Target:GEOPOSITION:LAT.
	}
	Set ShipPos_V to ThisShip:POSITION.
	Set ShipBPos_V to ThisShip:BODY:POSITION.
}
//******************************************************************************
//								DO_Params
//******************************************************************************
Function DO_Params
{
	Set ShipInclination to ThisShip:ORBIT:INCLINATION.
	Set ShipLan to ThisShip:ORBIT:LAN.
	Set ShipApoapsisHgt to ThisShip:ORBIT:APOAPSIS.
	Set ShipPeriapisHgt to ThisShip:ORBIT:PERIAPSIS.
	Set ShipLatitude TO ThisShip:GEOPOSITION:LAT.
	Set ShipLongitude TO ThisShip:GEOPOSITION:LNG.
	Set ShipAltitude to ThisShip:ALTITUDE.

	Set ShipEtaApoapsisLast to ShipEtaApoapsis.
	Set ShipEtaApoapsis to ETA:APOAPSIS.
	Set ShipDApoapsis to ShipEtaApoapsis - ShipEtaApoapsisLast.

	Set ShipPos_V to ThisShip:POSITION.
	Set ShipBPos_V to ThisShip:BODY:POSITION.
	Set ShipPos_DV to ShipPos_V - ShipBPos_V.

	IF b_Targ = TRUE
	{
		Set TargPos_V to Target:POSITION.
		Set TargBPos_V to Target:BODY:POSITION.
		Set TargPos_DV to TargPos_V - TargBPos_V.

		Set TargLatitude to Target:GEOPOSITION:LAT.
	}
}
//******************************************************************************
//								DO_Params3
//******************************************************************************
Function DO_Params3
{
	Set ShipInclination to ThisShip:ORBIT:INCLINATION.
	Set ShipLan to ThisShip:ORBIT:LAN.
	Set ShipApoapsisHgt to ThisShip:ORBIT:APOAPSIS.
	Set ShipPeriapisHgt to ThisShip:ORBIT:PERIAPSIS.
	Set ShipLatitude TO ThisShip:GEOPOSITION:LAT.
	Set ShipLongitude TO ThisShip:GEOPOSITION:LNG.
	IF ShipLongitude < 0{Set ShipLongitude to ShipLongitude + 360.}
	IF ShipLongitude > 360 {Set ShipLongitude to ShipLongitude - 360.}
	Set ShipAltitude to ThisShip:ALTITUDE.

	Print ShipInclination AT (18,13).
	Print ShipLan AT (18,14).
	Print ShipApoapsisHgt AT (18,15).
	Print ShipPeriapisHgt AT (18,16).
	Print ShipLatitude AT (18,17).
	Print ShipLongitude AT (18,18).
	Print ShipAltitude AT (18,19).
}
//******************************************************************************
//							DO_PitchProgram
//******************************************************************************
Function DO_PitchProgram
{
LOCAL P is 0.0.

	IF b_ProGrade = FALSE
	{
		Set P to ShipPitch.
		IF ShipAltitude > i_PitchStartAlt
		{
			IF ShipAltitude < Hgt01
			{
				Set P TO ShipPitchRange - ((ShipPitchRange * ShipAltitude) / Hgt01).
				If ABS(P) < ShipExitPitch
				{
					Set P to ShipExitPitch.
				}
			}	
			ELSE
			{
				IF ShipEtaApoapsis < 60
				{
					IF ShipDApoapsis < 0
					{
						Set P to P + 0.01.
					}
				}
				ELSE
				{
					IF ShipDApoapsis > 0
					{
						Set P to P - 0.01.
					}
				}
				If P > ShipExitPitch {Set P to ShipExitPitch.}
				If P < 0 {Set P to 0.}
			}
		}
		ELSE
		{
			Set P to 90.
		}
		Set ShipPitch to P.
	}
}
//******************************************************************************
//							DO_SteeringProgram
//******************************************************************************
Function DO_SteeringProgram
{
LOCAL B is 0.0.
LOCAL d_Inc is 0.0.

	//*** ROLL PROGRAM ***
	IF b_Roll = FALSE
	{
		IF ShipAltitude > i_RollHeight
		{
			Set ShipDirection to ShipLaunchHead.
			Set b_Roll to TRUE.
		}
	}
	ELSE
	{
		IF b_ProGrade = FALSE
		{
			//*** FOLLOW INCLINATION DIFFERENCE ***
			IF ShipAltitude > i_PitchStartAlt
			{
				Set d_Inc to (TargInclination - ShipInclination).
	
				//*** NOT LOCKED TO PROGRADE ? ***
				IF b_ProGrade = FALSE
				{
					//*** WITHIN CONTROL RANGE ? ***
					IF ABS(d_Inc) < DDINC
					{
						Set B to 90 - (TargInclination + (5 * d_Inc)).
						IF B < 0 {Set B to B + 360.}
						IF B > 360 {Set B to B - 360.}
	
						//*** EFFECT COURSE CHANGE ***
						Set ShipDirection to B.
	
						//*** WITHIN PROGRADE LOCK LIMIT ***
						IF ABS(d_Inc) < 0.02
						{
							Set b_ProGrade to TRUE.
						}
					}
				}
			}
		}
	}
}
//******************************************************************************
//
//******************************************************************************
Function MakeShipParts
{
LOCAL PRT is "".
LOCAL PrtMod is "".

	Set ThisShip to Vessel("K_3").

	For Prt in ThisShip:PARTS	
	{

		//*********************************
		// 		 STAGE-1 ENGINES		  *
		//								  *
		// KSPAction is "activate engine" *
		// KSPAction is "shutdown engine" *
		//*********************************
		IF Prt:TAG = "Stg1_Eng_L"
		{
//			For PrtMod in Prt:MODULES
//			{
//				if PrtMod = "ModuleEnginesRF"
//				{
//					Set Stg1Thrust to Prt:GETMODULE(PrtMod).
//				}
//			}
			Set Stg1Thrust to Prt.
		}

		//*********************************
		// 		 STAGE-2 ENGINES		  *
		//								  *
		// KSPAction is "activate engine" *
		// KSPAction is "shutdown engine" *
		//*********************************
		IF Prt:TAG = "Stg2_Eng"
		{
//			For PrtMod in Prt:MODULES
//			{
//				if PrtMod = "ModuleEnginesRF"
//				{
//					Set Stg2Thrust to Prt:GETMODULE(PrtMod).
//				}
//			}
			Set Stg2Thrust to Prt.
		}

		//***********************************
		// 	 	STAGE-2 REACTION WHEEL	  	*
		//								  	*
		// KSPAction is "activate wheel"	*
		// KSPAction is "deactivate wheel" 	*
		//***********************************		
		IF Prt:TAG = "Stg2_Wheel"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleReactionWheel"
				{
					Set Stg2Wheel to Prt:GETMODULE(PrtMod).
				}
			}
		}

		//*********************************
		// 		 STAGE-3 ENGINES		  *
		// 12 Engines operated in 6 pairs *
		//								  *
		// KSPAction is "activate engine" *
		// KSPAction is "shutdown engine" *
		//*********************************
		IF Prt:TAG = "K3_Eng_A1"
		{

			Set K3Eng_A to Prt.
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleEnginesRF"
				{
					Set K3Eng_A1 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_Eng_A2"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleEnginesRF"
				{
					Set K3Eng_A2 to Prt:GETMODULE(PrtMod).
				}
			}
		}

		IF Prt:TAG = "K3_Eng_B1"
		{
			Set K3Eng_B to Prt.
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleEnginesRF"
				{
					Set K3Eng_B1 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_Eng_B2"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleEnginesRF"
				{
					Set K3Eng_B2 to Prt:GETMODULE(PrtMod).
				}
			}
		}

		IF Prt:TAG = "K3_Eng_C1"
		{
			Set K3Eng_C to Prt.
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleEnginesRF"
				{
					Set K3Eng_C1 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_Eng_C2"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleEnginesRF"
				{
					Set K3Eng_C2 to Prt:GETMODULE(PrtMod).
				}
			}
		}

		IF Prt:TAG = "K3_Eng_D1"
		{
			Set K3Eng_D to Prt.
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleEnginesRF"
				{
					Set K3Eng_D1 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_Eng_D2"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleEnginesRF"
				{
					Set K3Eng_D2 to Prt:GETMODULE(PrtMod).
				}
			}
		}

		IF Prt:TAG = "K3_Eng_E1"
		{
			Set K3Eng_E to Prt.
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleEnginesRF"
				{
					Set K3Eng_E1 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_Eng_E2"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleEnginesRF"
				{
					Set K3Eng_E2 to Prt:GETMODULE(PrtMod).
				}
			}
		}

		IF Prt:TAG = "K3_Eng_F1"
		{
			Set K3Eng_F to Prt.
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleEnginesRF"
				{
					Set K3Eng_F1 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_Eng_F2"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleEnginesRF"
				{
					Set K3Eng_F2 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		//***************************************
		// 	 	Probe - probeStackSmall			*
		//(Probe control Room needs a probe)	*
		//								  		*
		// KSPAction is "orbit prograde"		*
		// KSPAction is "orbit retrograde"	 	*
		// KSPAction is "orbit normal"			*
		// KSPAction is "orbit antinormal,"	 	*
		// KSPAction is "orbit radial in"		*
		// KSPAction is "orbit radial out"	 	*
		// KSPAction is "orbit kill rotation"	*
		// KSPAction is "deactivate smartacs"	*
		// KSPAction is "panic!"				*
		// KSPAction is "translatron off"	 	*
		// KSPAction is "translatron keep vert"	*
		// KSPAction is "translatron zero speed"*
		// KSPAction is "translatron +1 speed"	*
		// KSPAction is "translatron -1 speed"	*
		// KSPAction is "translatron toggle h/s"*
		//***************************************
		IF Prt:TAG = "K3_RGU_01"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "MechJebCore"
				{
					Set K3RGU_01 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		//***************************************
		// 	 		Avionics Wheel				*
		//		(Habitat main gyro wheel)		*
		//								  		*
		// KSPAction is "orbit prograde"		*
		// KSPAction is "orbit retrograde"	 	*
		// KSPAction is "orbit normal"			*
		// KSPAction is "orbit antinormal,"	 	*
		// KSPAction is "orbit radial in"		*
		// KSPAction is "orbit radial out"	 	*
		// KSPAction is "orbit kill rotation"	*
		// KSPAction is "deactivate smartacs"	*
		// KSPAction is "panic!"				*
		// KSPAction is "translatron off"	 	*
		// KSPAction is "translatron keep vert"	*
		// KSPAction is "translatron zero speed"*
		// KSPAction is "translatron +1 speed"	*
		// KSPAction is "translatron -1 speed"	*
		// KSPAction is "translatron toggle h/s"*
		//***************************************
		IF Prt:TAG = "K3_WHEEL_01"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "MechJebCore"
				{
					Set K3WHEEL_01 to Prt:GETMODULE(PrtMod).
				}
			}
		}

		//***********************************
		// 	 Antenna - RTLongAntenna2		*
		//			(communotron32)			*
		//								  	*
		// KSPAction is "activate"			*
		// KSPAction is "deactivate"	 	*
		//***********************************
		IF Prt:TAG = "K3_COM32_1030"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleRTAntenna"
				{
					Set K3COM32_1030 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_COM32_0130"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleRTAntenna"
				{
					Set K3COM32_0130 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_COM32_0430"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleRTAntenna"
				{
					Set K3COM32_0430 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_COM32_0730"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleRTAntenna"
				{
					Set K3COM32_0730 to Prt:GETMODULE(PrtMod).
				}
			}
		}

		//***********************************
		// NAV Lights - indicatorLightSmall	*
		//			(Indicator Light)		*
		//								  	*
		// KSPAction is "light toggle"		*
		// KSPAction is "flash toggle"		*
		// KSPAction is "double flash toggle"*
		// KSPAction is "interval toggle"	*
		// KSPAction is "cycle modes"		*
		//***********************************
		IF Prt:TAG = "K3_NavF_Red"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleNavLight"
				{
					Set K3NavF_Red to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_NavM_Red"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleNavLight"
				{
					Set K3NavM_Red to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_NavA_Red"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleNavLight"
				{
					Set K3NavA_Red to Prt:GETMODULE(PrtMod).
				}
			}
		}

		IF Prt:TAG = "K3_NavF_Grn"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleNavLight"
				{
					Set K3NavF_Grn to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_NavM_Grn"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleNavLight"
				{
					Set K3NavM_Grn to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_NavA_Grn"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleNavLight"
				{
					Set K3NavA_Grn to Prt:GETMODULE(PrtMod).
				}
			}
		}

		IF Prt:TAG = "K3_NavF_Wht"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleNavLight"
				{
					Set K3NavF_Wht to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_NavM_Wht"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleNavLight"
				{
					Set K3NavM_Wht to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_NavA_Wht"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleNavLight"
				{
					Set K3NavA_Wht to Prt:GETMODULE(PrtMod).
				}
			}
		}


		//***********************************
		// BLU Lights - indicatorLightSmall	*
		//			(Indicator Light)		*
		//								  	*
		// KSPAction is "light toggle"		*
		// KSPAction is "flash toggle"		*
		// KSPAction is "double flash toggle"*
		// KSPAction is "interval toggle"	*
		// KSPAction is "cycle modes"		*
		//***********************************
		IF Prt:TAG = "K3_BluF_1030"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleNavLight"
				{
					Set K3BluF_1030 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_BluF_0130"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleNavLight"
				{
					Set K3BluF_0130 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_BluF_0430"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleNavLight"
				{
					Set K3BluF_0430 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_BluF_0730"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleNavLight"
				{
					Set K3BluF_0730 to Prt:GETMODULE(PrtMod).
				}
			}
		}

		IF Prt:TAG = "K3_BluM_1030"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleNavLight"
				{
					Set K3BluM_1030 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_BluM_0130"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleNavLight"
				{
					Set K3BluM_0130 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_BluM_0430"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleNavLight"
				{
					Set K3BluM_0430 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_BluM_0730"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleNavLight"
				{
					Set K3BluM_0730 to Prt:GETMODULE(PrtMod).
				}
			}
		}

		IF Prt:TAG = "K3_BluA_1030"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleNavLight"
				{
					Set K3BluA_1030 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_BluA_0130"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleNavLight"
				{
					Set K3BluA_0130 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_BluA_0430"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleNavLight"
				{
					Set K3BluA_0430 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_BluA_0730"
		{
			For PrtMod in Prt:MODULES
			{
				if PrtMod = "ModuleNavLight"
				{
					Set K3BluA_0730 to Prt:GETMODULE(PrtMod).
				}
			}
		}

		//***********************************
		// 		SSTU HABITAT MODULE			*
		//***********************************
		IF Prt:TAG = "K3_HabM_01"
		{
			For PrtMod in Prt:MODULES
			{
				//***************************************
				// KSPAction is "undock node"			*
				// KSPAction is "decouple node"			*
				// KSPAction is "enable crossfeed"		*
				// KSPAction is "disable crossfeed"		*
				// KSPAction is "toggle crossfeed		*
				//***************************************
				if PrtMod = "ModuleDockingNode"
				{
					Set K3HabM_DOCK to Prt:GETMODULE(PrtMod).
				}
				//***********************************
				// KSPAction is "toggle lights"		*
				//***********************************
				if PrtMod = "SSTUAnimateLight"
				{
					Set K3HabM_DOCKL to Prt:GETMODULE(PrtMod).
				}
			}
		}

		//***********************************
		// 			RADIATOR MODULES		*
		//***********************************
		IF Prt:TAG = "K3_RadM_0130"
		{
			For PrtMod in Prt:MODULES
			{
				//***********************************
				// KSPAction is "toggle radiator"	*
				// KSPAction is "extend radiator"	*
				// KSPAction is "retract radiator"	*
				//***********************************
				if PrtMod = "ModuleDeployableRadiator"
				{
					Set K3RadM_0130D to Prt:GETMODULE(PrtMod).
				}
				//***********************************
				// KSPAction is "toggle radiator"	*
				// KSPAction is "activate radiator"	*
				// KSPAction is "shutdown radiator"	*
				//***********************************
				if PrtMod = "ModuleActiveRadiator"
				{
					Set K3RadM_0130A to Prt:GETMODULE(PrtMod).
				}
			}
		}

		IF Prt:TAG = "K3_RadM_0430"
		{
			For PrtMod in Prt:MODULES
			{
				//***********************************
				// KSPAction is "toggle radiator"	*
				// KSPAction is "extend radiator"	*
				// KSPAction is "retract radiator"	*
				//***********************************
				if PrtMod = "ModuleDeployableRadiator"
				{
					Set K3RadM_0430D to Prt:GETMODULE(PrtMod).
				}
				//***********************************
				// KSPAction is "toggle radiator"	*
				// KSPAction is "activate radiator"	*
				// KSPAction is "shutdown radiator"	*
				//***********************************
				if PrtMod = "ModuleActiveRadiator"
				{
					Set K3RadM_0430A to Prt:GETMODULE(PrtMod).
				}
			}
		}

		IF Prt:TAG = "K3_RadM_0730"
		{
			For PrtMod in Prt:MODULES
			{
				//***********************************
				// KSPAction is "toggle radiator"	*
				// KSPAction is "extend radiator"	*
				// KSPAction is "retract radiator"	*
				//***********************************
				if PrtMod = "ModuleDeployableRadiator"
				{
					Set K3RadM_0730D to Prt:GETMODULE(PrtMod).
				}
				//***********************************
				// KSPAction is "toggle radiator"	*
				// KSPAction is "activate radiator"	*
				// KSPAction is "shutdown radiator"	*
				//***********************************
				if PrtMod = "ModuleActiveRadiator"
				{
					Set K3RadM_0730A to Prt:GETMODULE(PrtMod).
				}
			}
		}

		IF Prt:TAG = "K3_RadM_1030"
		{
			For PrtMod in Prt:MODULES
			{
				//***********************************
				// KSPAction is "toggle radiator"	*
				// KSPAction is "extend radiator"	*
				// KSPAction is "retract radiator"	*
				//***********************************
				if PrtMod = "ModuleDeployableRadiator"
				{
					Set K3RadM_1030D to Prt:GETMODULE(PrtMod).
				}
				//***********************************
				// KSPAction is "toggle radiator"	*
				// KSPAction is "activate radiator"	*
				// KSPAction is "shutdown radiator"	*
				//***********************************
				if PrtMod = "ModuleActiveRadiator"
				{
					Set K3RadM_1030A to Prt:GETMODULE(PrtMod).
				}
			}
		}
		//***********************************
		// 			SOLAR PANELS LARGE		*
		//***********************************
		IF Prt:TAG = "K3_XT4_0130"
		{
			For PrtMod in Prt:MODULES
			{
				//***ALLFIELDS************************
				// Single is "sun exposure"			 *
				// Single is "energy flow"			 *
				// String is "status"				 *
				//************************************
				// KSPAction is "toggle solar panel" *
				// KSPAction is "extend solar panel" *
				// KSPAction is "retract solar panel"*
				//************************************
				if PrtMod = "KopernicusSolarPanel"
				{
					Set K3XT4_0130 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_XT4_0430"
		{
			For PrtMod in Prt:MODULES
			{
				//***ALLFIELDS************************
				// Single is "sun exposure"			 *
				// Single is "energy flow"			 *
				// String is "status"				 *
				//************************************
				// KSPAction is "toggle solar panel" *
				// KSPAction is "extend solar panel" *
				// KSPAction is "retract solar panel"*
				//************************************
				if PrtMod = "KopernicusSolarPanel"
				{
					Set K3XT4_0430 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_XT4_0730"
		{
			For PrtMod in Prt:MODULES
			{
				//***ALLFIELDS************************
				// Single is "sun exposure"			 *
				// Single is "energy flow"			 *
				// String is "status"				 *
				//************************************
				// KSPAction is "toggle solar panel" *
				// KSPAction is "extend solar panel" *
				// KSPAction is "retract solar panel"*
				//************************************
				if PrtMod = "KopernicusSolarPanel"
				{
					Set K3XT4_0730 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_XT4_1030"
		{
			For PrtMod in Prt:MODULES
			{
				//***ALLFIELDS************************
				// Single is "sun exposure"			 *
				// Single is "energy flow"			 *
				// String is "status"				 *
				//************************************
				// KSPAction is "toggle solar panel" *
				// KSPAction is "extend solar panel" *
				// KSPAction is "retract solar panel"*
				//************************************
				if PrtMod = "KopernicusSolarPanel"
				{
					Set K3XT4_1030 to Prt:GETMODULE(PrtMod).
				}
			}
		}

		//***********************************
		// 			SOLAR PANELS CURVED		*
		//***********************************
		IF Prt:TAG = "K3_NIV18_1200"
		{
			For PrtMod in Prt:MODULES
			{
				//**ALLFIELDS************************
				// String (RO) is "sun exposure"	*
				// String (RO) is "energy flow"		*
				//***********************************
				// KSPAction is "deploy panels"		*
				// KSPAction is "retract panels"	*
				// KSPAction is "toggle panels"		*
				//***********************************
				if PrtMod = "Curved Solar Panel"
				{
					Set K3NIV18_1200 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_NIV18_0300"
		{
			For PrtMod in Prt:MODULES
			{
				//**ALLFIELDS************************
				// String (RO) is "sun exposure"	*
				// String (RO) is "energy flow"		*
				//***********************************
				// KSPAction is "deploy panels"		*
				// KSPAction is "retract panels"	*
				// KSPAction is "toggle panels"		*
				//***********************************
				if PrtMod = "Curved Solar Panel"
				{
					Set K3NIV18_0300 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_NIV18_0600"
		{
			For PrtMod in Prt:MODULES
			{
				//**ALLFIELDS************************
				// String (RO) is "sun exposure"	*
				// String (RO) is "energy flow"		*
				//***********************************
				// KSPAction is "deploy panels"		*
				// KSPAction is "retract panels"	*
				// KSPAction is "toggle panels"		*
				//***********************************
				if PrtMod = "Curved Solar Panel"
				{
					Set K3NIV18_0600 to Prt:GETMODULE(PrtMod).
				}
			}
		}
		IF Prt:TAG = "K3_NIV18_0900"
		{
			For PrtMod in Prt:MODULES
			{
				//**ALLFIELDS************************
				// String (RO) is "sun exposure"	*
				// String (RO) is "energy flow"		*
				//***********************************
				// KSPAction is "deploy panels"		*
				// KSPAction is "retract panels"	*
				// KSPAction is "toggle panels"		*
				//***********************************
				if PrtMod = "Curved Solar Panel"
				{
					Set K3NIV18_0900 to Prt:GETMODULE(PrtMod).
				}
			}
		}
	}
}
//*************************************************************
// Set_Heights:		Determines if a body has an atmosphere.
//					Calculates parking orbit height.
//					Defaults to 15000m.
//*************************************************************
Function Set_Heights
{
	SET b_TMP TO Lunars:ATM:EXISTS.
	IF b_TMP = TRUE
	{
		SET HgtAtmosphere TO Lunars:ATM:HEIGHT.
	}
	ELSE
	{
		Set HgtAtmosphere to ThisShip:BODY:Raduis.
		Set HgtAtmosphere to HgtAtmosphere * 0.25.
	}
	Set Hgt01 to HgtAtmosphere * 0.65.
	Set Hgt02 to HgtAtmosphere * 1.0.
	Set Hgt03 to HgtAtmosphere * 1.1.
}
//******************************************************************************
//Stg0_ShipLaunchPosInfo:
//
// DETERMINES LAUNCH DIRECTION  - NORTH OR SOUTH.
// 
//******************************************************************************
Function Set_ShipLaunchPosInfo
{
	If ShipLatitude < 0
	{
		Set b_LaunchAscNode to FALSE.
	}
	ELSE
	{
		Set b_LaunchAscNode to TRUE.
	}
	IF ShipLongitude < 0
	{
		Set ShipLongitude to ShipLongitude + 360.
	}
	IF ShipLongitude > 360
	{
		Set ShipLongitude to ShipLongitude - 360.
	}

	// DYNAMIC
	Set ShipLanLong to (ShipLan - EarthRotang).
	IF ShipLanLong < 0 {Set ShipLanLong to 360 + ShipLanLong.}
	IF ShipLanLong > 360 {Set ShipLanLong to ShipLanLong - 360.}
}
//******************************************************************************
//							CALCULATE LAUNCH ANGLE
//
// TARGET INCLINATION HAS TO BE GREATER THAN THE ABSOLUTE LATITUDE OF THE LAUNCH
// BASE, AS MINIMUM INCLINATION, WITHOUT ANY MAJOR INC ADJUSTMENTS IS EQUAL TO
// THE LAUNCH LATITUDE.
// IF THE TARGET INCLINATION IS LOWER THAN THE LATITUDE, WE LAUNCH DUE EAST/WEST
// AND ADJUST INCLINATION AT ASCENDING/DESCENDING NODES.
//******************************************************************************
Function Set_ShipLaunchAngle
{
Local V_Orb is 0.
Local V_xrot is 0.
Local V_yrot is 0.
Local f_TmpA is 0.
Local f_TmpB is 0.

	Set f_TmpA to ABS(ShipLatitude).
	IF TargInclination > f_TmpA
	{
		// DESIRED ANGLE AT BASE LATITUDE
		Set f_TmpB to ARCSIN(COS(TargInclination)/COS(f_TmpA)).

		// VELOCITY AT END OF STAGE-2 (APPROXIMATE)
		Set V_Orb to SQRT(LUNARS:Mu/(LUNARS:Radius + ParkingOrbit)).

		// X-VELOCITY MINUS LATITUDE SURFACE VELOCITY
		Set V_xrot to (V_Orb * SIN(f_TmpB)) - (465.9 * COS(f_TmpA)).

		// Y-VELOCITY COMPONENT
		Set V_yrot to (V_Orb * COS(f_TmpB)).

		// ASCENDING NODE TO BASE LONGITUDES
		Set f_TmpA to ARCCOS(COS(f_TmpB)/SIN(TargInclination)).

		// LAUNCH ANGLE
		Set f_TmpB to ARCTAN(V_xrot/V_yrot).

		// ASCENDING NODE LAUNCH LONGITUDE
		IF b_LaunchAscNode = TRUE
		{
			// CHECK 2PI LIMIT
			IF f_TmpB < 0 {Set f_TmpB to f_TmpB + 360.}
			IF f_TmpB > 360 {Set f_TmpB to f_TmpB - 360.}

			IF ShipLatitude > 0
			{
				Set ShipLaunchLong to (ShipLongitude - f_TmpA).
			}
			ELSE
			{
				Set ShipLaunchLong to (ShipLongitude + f_TmpA).
			}
		}

		// DESCENDING NODE LAUNCH LONGITUDE
		ELSE
		{
			Set f_TmpB to 180 - f_TmpB.

			// CHECK 2PI LIMIT
			IF f_TmpB < 0 {Set f_TmpB to f_TmpB + 360.}
			IF f_TmpB > 360 {Set f_TmpB to f_TmpB - 360.}

			IF ShipLatitude < 0
			{
				Set ShipLaunchLong to (ShipLongitude - f_TmpA).
			}
			ELSE
			{
				Set ShipLaunchLong to (ShipLongitude + f_TmpA).
			}
		}

		// ADJUST LAUNCH HEADING
		Set ShipLaunchHead to f_TmpB - 0.9.

		// ADJUST LAUNCH LONGITUDE RANGE
		IF ShipLaunchLong < 0 { Set ShipLaunchLong to ShipLaunchLong + 360.}
		IF ShipLaunchLong > 360 { Set ShipLaunchLong to ShipLaunchLong - 360.}

		// SET INCLINATION LOCK ANGLE
		Set DDINC to ABS(ShipLaunchHead - (90 - TargInclination)).
		IF DDINC > 10 {Set DDINC to 360 - DDINC.}
		Set DDINC to DDINC/5.

		// IF NO TARGET -> PARKING ORBIT = 400KM
		IF b_Targ = FALSE {Set ParkingOrbit to TargStationHgt.}
	}
	// TARGET INCLINATION < BASE LATITUDE = GO 90!!
	ELSE
	{
		Set b_INC_Reduction to TRUE.
		Set ShipLaunchHead to 90.
	}
}
//******************************************************************************
//							DETERMINES LAUNCH TIME
//******************************************************************************
Function PreLaunch_Longitude
{
local m_Wop is 0.

	// CURRENT TARGET LONGITUDE
	Set TargLanLong to (TargLan - EarthRotang).
	IF TargLanLong < 0 {Set TargLanLong to TargLanLong + 360.}
	IF TargLanLong > 360 {Set TargLanLong to TargLanLong - 360.}	

	// DEGREES TO LAUNCH
	Set TimetoLaunch to TargLanLong - ShipLaunchLong.
	Set TimetoLaunch to TimetoLaunch - 1.
	IF TimetoLaunch < 0 {Set TimetoLaunch to TimetoLaunch + 360.}
	IF TimetoLaunch > 360 {Set TimetoLaunch to TimetoLaunch - 360.}
	
	// DEGREES TO SECONDS
	Set TimetoLaunch to TimetoLaunch * 240.

	// LAUNCH SEQUENCE STARTS AT 5
	If ABS(TimetoLaunch) < 5
	{ 
		Set Tangle to 0.
		
		// IF THERE IS A TARGET DETERMINE LEAD ANGLE
		IF b_Targ = TRUE
		{
			Set ShipPos_V to ShipPos_V - ShipBPos_V.
			Set TargPos_V to TargPos_V - TargBPos_V.
			Set Tangle to Vang(ShipPos_V, TargPos_V).
		}
		
		// IF NO TARGET, LAUNCH ANYWAY
		ELSE
		{
			Return TRUE.
		}

//		// DETERMINE LAUNCH WINDOW 
//		Set m_Wop to TargLatitude - ShipLatitude.
//		IF m_Wop > 30
//		{
//			IF m_Wop < 80
//			{
//				IF Tangle < 80
//				{
//					Return TRUE.
//				}
//			}
//		}
//		PRINT "      LAUNCH ABORTED" AT (20,21).
	}

	// LAUNCH TIME DETERMINES WARP FACTOR
	IF TimetoLaunch < 10 {Set WARP to 0.}
	ELSE IF TimetoLaunch < 50 {Set WARP to 1.}
	ELSE IF TimetoLaunch < 500 {Set WARP to 2.}
	ELSE IF TimetoLaunch < 2000 {Set WARP to 3.}
	ELSE {Set WARP to 4.}
	Return FALSE.
}
//******************************************************************************
//
//******************************************************************************
Function DO_Screenies
{
	Print ShipInclination AT (18,13).
	Print ShipLan AT (18,14).
	Print ShipApoapsisHgt AT (18,15).
	Print ShipPeriapisHgt AT (18,16).
	Print ShipLatitude AT (18,17).
	Print ShipLongitude AT (18,18).
	Print ShipAltitude AT (18,19).

	IF ThisStage = 1 {Print Stg1Thrust:thrust AT (18,23).}
	IF ThisStage = 2 {Print Stg2Thrust:thrust AT (18,23).}
	Print ShipPitch AT (18,24).
	Print ShipDirection AT (18,25).
	Print ShipEtaApoapsis AT (18,26).	
}
//******************************************************************************
// NAV LIGHTS ON
//******************************************************************************
Function NAV_Lights_ON
{
	K3NavF_Red:DOACTION("light toggle",TRUE).
	K3NavM_Red:DOACTION("light toggle",TRUE).
	K3NavA_Red:DOACTION("light toggle",TRUE).

	K3NavF_Grn:DOACTION("light toggle",TRUE).
	K3NavM_Grn:DOACTION("light toggle",TRUE).
	K3NavA_Grn:DOACTION("light toggle",TRUE).

	K3NavF_Wht:DOACTION("light toggle",TRUE).
	K3NavM_Wht:DOACTION("light toggle",TRUE).
	K3NavA_Wht:DOACTION("light toggle",TRUE).

}
//******************************************************************************
// BLUE LIGHTS ON
//******************************************************************************
Function BLU_Lights_ON
{
	K3BluF_1030:DOACTION("light toggle",TRUE).
	K3BluF_0130:DOACTION("light toggle",TRUE).
	K3BluF_0430:DOACTION("light toggle",TRUE).
	K3BluF_0730:DOACTION("light toggle",TRUE).

	K3BluM_1030:DOACTION("light toggle",TRUE).
	K3BluM_0130:DOACTION("light toggle",TRUE).
	K3BluM_0430:DOACTION("light toggle",TRUE).
	K3BluM_0730:DOACTION("light toggle",TRUE).

	K3BluA_1030:DOACTION("light toggle",TRUE).
	K3BluA_0130:DOACTION("light toggle",TRUE).
	K3BluA_0430:DOACTION("light toggle",TRUE).
	K3BluA_0730:DOACTION("light toggle",TRUE).
}
//******************************************************************************
// WHIP AERIALS = ON
//******************************************************************************
Function COMM32_Antenna_ON
{
	K3COM32_1030:DOACTION("activate",TRUE).
	K3COM32_0130:DOACTION("activate",TRUE).
	K3COM32_0430:DOACTION("activate",TRUE).
	K3COM32_0730:DOACTION("activate",TRUE).
}
//******************************************************************************
// WHIP AERIALS = OFF
//******************************************************************************
Function COMM32_Antenna_OFF
{
	K3COM32_1030:DOACTION("deactivate",TRUE).
	K3COM32_0130:DOACTION("deactivate",TRUE).
	K3COM32_0430:DOACTION("deactivate",TRUE).
	K3COM32_0730:DOACTION("deactivate",TRUE).
}

//******************************************************************************
// SOLAR PANELS = ON
//******************************************************************************
Function SolarPanelsON
{
	K3XT4_0130:DOACTION("extend solar panel",TRUE).
	K3XT4_0430:DOACTION("extend solar panel",TRUE).
	K3XT4_0730:DOACTION("extend solar panel",TRUE).
	K3XT4_1030:DOACTION("extend solar panel",TRUE).
}
//******************************************************************************
// SOLAR PANELS = OFF
//******************************************************************************
Function SolarPanelsOFF
{
	K3XT4_0130:DOACTION("retract solar panel",TRUE).
	K3XT4_0430:DOACTION("retract solar panel",TRUE).
	K3XT4_0730:DOACTION("retract solar panel",TRUE).
	K3XT4_1030:DOACTION("retract solar panel",TRUE).
}

//******************************************************************************
// RADIATORS = ON
//******************************************************************************
Function RadiatorPanelsON
{
	K3RadM_0130D:DOACTION("extend radiator",TRUE).
	K3RadM_0430D:DOACTION("extend radiator",TRUE).
	K3RadM_0730D:DOACTION("extend radiator",TRUE).
	K3RadM_1030D:DOACTION("extend radiator",TRUE).

	K3RadM_0130A:DOACTION("activate radiator",TRUE).
	K3RadM_0430A:DOACTION("activate radiator",TRUE).
	K3RadM_0730A:DOACTION("activate radiator",TRUE).
	K3RadM_1030A:DOACTION("activate radiator",TRUE).
}
//******************************************************************************
// RADIATORS = OFF
//******************************************************************************
Function RadiatorPanelsOFF
{
	K3RadM_0130A:DOACTION("shutdown radiator",TRUE).
	K3RadM_0430A:DOACTION("shutdown radiator",TRUE).
	K3RadM_0730A:DOACTION("shutdown radiator",TRUE).
	K3RadM_1030A:DOACTION("shutdown radiator",TRUE).

	K3RadM_0130D:DOACTION("retract radiator",TRUE).
	K3RadM_0430D:DOACTION("retract radiator",TRUE).
	K3RadM_0730D:DOACTION("retract radiator",TRUE).
	K3RadM_1030D:DOACTION("retract radiator",TRUE).
}
//******************************************************************************
// STAGE-3 ENGINE CONTROL
//******************************************************************************
Function	Run_Stg3_Engs
{
Parameter EngCount.

	IF EngCount > 0
	{
		IF K3Eng_A:thrust < 0.1
		{
			K3Eng_A1:DOACTION("activate engine",TRUE).
			K3Eng_A2:DOACTION("activate engine",TRUE).
		}
	}
	ELSE
	{
		IF K3Eng_A:thrust > 0.1
		{
			K3Eng_A1:DOACTION("shutdown engine",TRUE).
			K3Eng_A2:DOACTION("shutdown engine",TRUE).
		}
	}
	IF EngCount > 1
	{
		IF K3Eng_B:thrust < 0.1
		{
			K3Eng_B1:DOACTION("activate engine",TRUE).
			K3Eng_B2:DOACTION("activate engine",TRUE).
		}
	}
	ELSE
	{
		IF K3Eng_B:thrust > 0.1
		{
			K3Eng_B1:DOACTION("shutdown engine",TRUE).
			K3Eng_B2:DOACTION("shutdown engine",TRUE).
		}
	}
	IF EngCount > 2
	{
		IF K3Eng_C:thrust < 0.1
		{
			K3Eng_C1:DOACTION("activate engine",TRUE).
			K3Eng_C2:DOACTION("activate engine",TRUE).
		}
	}
	ELSE
	{
		IF K3Eng_C:thrust > 0.1
		{
			K3Eng_C1:DOACTION("shutdown engine",TRUE).
			K3Eng_C2:DOACTION("shutdown engine",TRUE).
		}
	}
	IF EngCount > 3
	{
		IF K3Eng_D:thrust < 0.1
		{
			K3Eng_D1:DOACTION("activate engine",TRUE).
			K3Eng_D2:DOACTION("activate engine",TRUE).
		}
	}
	ELSE
	{
		IF K3Eng_D:thrust > 0.1
		{
			K3Eng_D1:DOACTION("shutdown engine",TRUE).
			K3Eng_D2:DOACTION("shutdown engine",TRUE).
		}
	}
	IF EngCount > 4
	{
		IF K3Eng_E:thrust < 0.1
		{
			K3Eng_E1:DOACTION("activate engine",TRUE).
			K3Eng_E2:DOACTION("activate engine",TRUE).
		}
	}
	ELSE
	{
		IF K3Eng_E:thrust > 0.1
		{
			K3Eng_E1:DOACTION("shutdown engine",TRUE).
			K3Eng_E2:DOACTION("shutdown engine",TRUE).
		}
	}
	IF EngCount > 5
	{
		IF K3Eng_F:thrust < 0.1
		{
			K3Eng_F1:DOACTION("activate engine",TRUE).
			K3Eng_F2:DOACTION("activate engine",TRUE).
		}
	}
	ELSE
	{
		IF K3Eng_F:thrust > 0.1
		{
			K3Eng_F1:DOACTION("shutdown engine",TRUE).
			K3Eng_F2:DOACTION("shutdown engine",TRUE).
		}
	}
}
//******************************************************************************
//							Calculate Burn Time
//******************************************************************************
Function CalcBurnTime
{
Local VatApo is V(0.0,0.0,0.0).
Local Mend is 0.0.
Local Mavg is 0.0.
Local ShipAccelAvg is 0.0.

	Set Vreq to SQRT(ThisShip:BODY:MU/(ThisShip:BODY:RADIUS + ShipApoapsisHgt)).
	Set	VatApo to VELOCITYAT(ThisShip,(Time:SECONDS + ShipEtaApoapsis)).
	Set DVreq to (Vreq - VatApo:ORBIT:MAG).
	Print DVreq at (18,24).

	Set Mend to (ThisShip:MASS / 2.71828^(DVreq / (9.802 * HabEngine_ISP))).
	Set Mavg to ((ThisShip:MASS + Mend) / 2).
	Set ShipAccelAvg to ((HabEngine_NEWTONS * 12 ) / Mavg).
	Set ShipBurnTime to ((DVreq*1000)/ ShipAccelAvg ).
}
//******************************************************************************
//							Calculate Burn Time 2
//******************************************************************************
Function CalcBurnTime2
{
PARAMETER ThisDV.
LOCAL A is 0.0.

//--- CALCULATE MASS AT END OF DELTA-V ---//
Set A to (ThisShip:MASS / 2.71828^(ThisDV / (9.802 * HabEngine_ISP))).

//--- CALCULATE AVERAGE MASS ---//
Set A to ((ThisShip:MASS + A) / 2).
 	 
		//--- CALCULATE AVERAGE ACCELERATION ---//
	    Set A to ((HabEngine_NEWTONS * 12 ) / A).
 	 
		 //--- CALCULATE BURN TIME ---//
	    Set A to ((ThisDV * 1000)/ A ).
 	 
	//--- SEND IT BACK ---//
	Return A.

}
//******************************************************************************
//							Calculate DeltaV
//******************************************************************************
Function CalcDeltaV
{
Local VatApo is V(0.0,0.0,0.0).

	Set Vreq to SQRT(ThisShip:BODY:MU/(ThisShip:BODY:RADIUS + ShipApoapsisHgt)).
	Set	VatApo to VELOCITYAT(ThisShip,(Time:SECONDS + ShipEtaApoapsis)).
	Set DVreq to (Vreq - VatApo:ORBIT:MAG).
	Print DVreq at (18,24).
}
//******************************************************************************
//							Orpoodleange_DeltaV
//******************************************************************************
//Function Holman
//{
//PARAMETER NewHeight.
//PARAMETER ThisHeight.
//LOCAL A is 0.
//LOCAL V is 0.
//LOCAL B is 0.
//
//	Set A to ThisShip:BODY:RADIUS + ((NewHeight + ThisHeight)/2).
//	Set V to SQRT(ThisShip:BODY:MU / ThisHeight).
//	Set B to ((2 / (ThisShip:BODY:RADIUS + ThisHeight)) - (1 / A)).
//	Set B to SQRT(ThisShip:BODY:MU * B).
//	Set B to B - V.
//	Return B
//}
//******************************************************************************
//							Burn Time Debug
//******************************************************************************
Function BT_DEBUG
{

Local VatApo is V(0.0,0.0,0.0).
Local Mend is 0.0.
Local Mavg is 0.0.
Local ShipAccelAvg is 0.0.

	Set Vreq to SQRT(ThisShip:BODY:MU/(ThisShip:BODY:RADIUS + ShipApoapsisHgt)).
	Set	VatApo to VELOCITYAT(ThisShip,(Time:SECONDS + ShipEtaApoapsis)).
	Set DVreq to (Vreq - VatApo:ORBIT:MAG).

	Set Mend to (ThisShip:MASS / 2.71828^(DVreq / (9.802 * HabEngine_ISP * 0.866))).
	Set Mavg to ((ThisShip:MASS + Mend) / 2).
	Set ShipAccelAvg to ((HabEngine_NEWTONS * 0.866) / Mavg).
	Set ShipBurnTime to (DVreq / ShipAccelAvg).


	Print ThisShip:BODY:MU at (18,23).
	Print ThisShip:BODY:RADIUS at (18,24).
	Print ShipApoapsisHgt at (18,25).
	Print Vreq at (18,26).
	Print VatApo:ORBIT:MAG at (18,27).
	Print DVreq at (18,28).
	Print ThisShip:MASS at (18,29).
	Print Mend at (18,30).
	Print Mavg at (18,31).
	Print ShipAccelAvg at (18,32).
	Print ShipBurnTime at (18,33).
}

 

Link to comment
Share on other sites

Hi, all!  Sorry if my question is too noobish. :wink:

Is it possible to allow KOS terminal at the building time? In VAB or SPH.

Purposes are debug, tuning, specific calculations, method of tests and mistakes ...

KOS should sense ship configuration change, but all active equipment disabled of course.

I do not know how much ship editor is different to ship simulation. How many changes should be made, should it be special mode, is it too Kerbal crazy?

 

And also separate question.

I want to try KOS in official tutorials and scenarios.

There is a "kOS for All!" mod, but it would add kOS to all modules in all games.

Would you add the option to add kOS functionality to all command modules for current game only?

Link to comment
Share on other sites

2 minutes ago, eberkain said:

The terminal is only active in-flight.  You just have to put together something launch it, run your kos tests, revert and make changes, relaunch. 

Thanks for response. I know it. I just want to substitute Kerbal Engineer and MechJeb by KOS program.

Link to comment
Share on other sites

hello folks

I'm having an issue at the moment with my shuttle. It is like the shuttle irl coupled at the side of a big fueltank, so it uses an hydraulic detachement manifold. This is triggered by an actiongroup AG10 together with some ullage motors to push the fueltank away. The shuttle is guided by the program but when the decoupling event triggers the shuttle itself does not steer anymore. i'm almost certain that it has something to do with the decoupling clamp on the side because i tested the program on a craft that does not use this and it works fine. Is this a known bug or am i missing something?

i'm using realism overhaul and al it's recomended mods + principia.

 

Link to comment
Share on other sites

6 hours ago, kerboman25 said:

The shuttle is guided by the program but when the decoupling event triggers the shuttle itself does not steer anymore.

It's a well know problem, that's got to do with KSP itself.

You have to use the Stage command to keep on steering

:)

Edited by ColKlonk2
Link to comment
Share on other sites

19 hours ago, ColKlonk2 said:

You have to use the Stage command to keep on steering

I tried it but it didn't work eighter. After the separation the craft begins to tumble because the craft is not controlled anymore.

//action group staging command:

set ShuttleEngines to 0.5.
 AG10 on. //stage command
 set runmode to 4. //orbital runmode

 

//code with staging command.

set ShuttleEngines to 0.5.
 BoosterEngines:SHUTDOWN.

STAGE. //stage command
 set runmode to 4. //orbital runmode
    

Link to comment
Share on other sites

1 hour ago, ColKlonk2 said:

You might have not set time acceleration correctly ?

If you don't set 'Physics' mode, AFAIK you have no control.

What you have said is true, but OP never said anything about time warping.

5 hours ago, kerboman25 said:

After the separation the craft begins to tumble because the craft is not controlled anymore.

Even if time warp was involved, the craft wouldn't tumble if the mode wasn't PHYSICS, or a mod like PersistentRotation isn't installed.

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