##Introduction
The Automated Guidance Computer (AGC) is a part that you can attach to your vessel. The AGC lets you run flight control programs that you code in the LISP programming language. For example, you could write a program to automatically lower the landing gear when you get below a set altitude, or a program to damp manual control inputs as speed increases, or even a program that implements a three-axis PID controller to follow a specific ascent path.
##Installation
Copy these folders to your KSP folder, merging them.
##How to use
- Place the AGC (Automated Guidance Computer) on your ship. In career mode, you need to unlock the Flight Control research node. The AGC lets you run programs that you code in the LISP programming language. Put your programs in the Plugins/PluginData/AGC folder with a txt extension. Look at autoabort.txt for an example. The stdlib.txt (stdlib is short for Standard Library) code gets executed when the computer boots up and allows you to implement common library functions to share functionality between your programs.
- To run a program, put the AGC on a ship in KSP, right click on the AGC and Toggle UI. Put autoabort in for the program name, then click Toggle Computer. The computer will start executing the autoabort program, which activates the Abort action group when the vertical speed is significantly negative and the altitude is still low. Take a look at the autoabort.txt file and examine the source code. Note that it uses the library function makeDebounceOneshot to set up an interlock that will fire when the condition is consecutively true for a given number of seconds.
- I also provided a speedlimit.txt example, which will reduce the maximum throttle allowed to try and keep the speed from exceeding the limit set in the input field. If you run this program on a spaceplane, for example, and set the limit to 150, the throttle will automatically reduce to try and keep your speed from going too much over 150 m/s.
- Multiple programs can be run on the same AGC part at the same time. These programs share the same environment, except for the AGC.Input and AGC.Status variables.
- Press the Debug button to dump out the computer's current state (LISP environment) to Plugins/PluginData/AGC/debug.txt.
##LISP Dialect Notes
- The quotation notation
'is not supported, but you can use the built-in quote function to do the same thing, though not as succinctly. - Lisp dot notation for dotted lists is not supported.
- The
condfunction is not supported because if no predicates match, the result is undefined. - The empty list
()is equivalent to(quote ()). - If you pass
cara non-list argument, it will simply return it. So,(car 5)evaluates to 5. - If you pass
cdra non-list argument, it will return the empty list(). - Your program gets evaluated each computer tick. To carry state between ticks, define variables. To make this easier, the
initializebuilt-in function is the same asdefine, except it does nothing if the symbol is already defined. - In Lisp, function arguments are evaluated prior to applying them to the function, except for some special cases like
ifandlambda. If you are new to Lisp, I recommend The Structure and Interpretation of Computer Programs, a free ebook published by MIT.
##Built-in Functions
ifquoteset!defineinitializelambdabegin+-*/notandor><>=<==equal?eq?lengthconscarcdrappendlistlist?null?symbol?defined?moduloabsfloorceilingminmaxapplyidsqrtletpowlnsincostanasinacosatan2
##Interface functions
Inside your programs you can use special functions to interface with your vessel. Note that the Staging functions only work when the vessel is active.
Staging.ActivateNextStageStaging.ActivateStage- expects a single numeric argument
Functions expecting a single boolean argument:
ActionGroups.SetGroupStageActionGroups.SetGroupGearActionGroups.SetGroupLightActionGroups.SetGroupRCSActionGroups.SetGroupSASActionGroups.SetGroupBrakesActionGroups.SetGroupAbortActionGroups.SetGroupCustom01ActionGroups.SetGroupCustom02ActionGroups.SetGroupCustom03ActionGroups.SetGroupCustom04ActionGroups.SetGroupCustom05ActionGroups.SetGroupCustom06ActionGroups.SetGroupCustom07ActionGroups.SetGroupCustom08ActionGroups.SetGroupCustom09ActionGroups.SetGroupCustom10
For the functions starting with vessel below, you can use target instead for the corresponding data about your target. Use vessel.hasTarget to check if you have a target.
Functions taking no arguments, returning number:
vessel.verticalSpeedvessel.staticPressurevessel.geeForcevessel.currentStagevessel.specificAccelerationvessel.heightFromTerrainvessel.pqsAltitudevessel.terrainAltitudevessel.heightFromSurfacevessel.Landedvessel.missionTimevessel.longitudevessel.latitudevessel.altitudevessel.GetTotalMassvessel.obt_speedvessel.srfSpeedvessel.horizontalSrfSpeed
Functions taking no arguments, returning 3-dimensional lists of numbers:
vessel.accelerationvessel.angularMomentumvessel.angularVelocityvessel.CoMvessel.MOIvessel.obt_velocityvessel.srf_velocityvessel.upAxisvessel.vesselTransform.eulerAnglesvessel.vesselTransform.forward
Functions taking 3-element list, returning number:
vessel.orbit.getOrbitalSpeedAtPosvessel.orbit.getOrbitalSpeedAtRelativePosvessel.orbit.GetTrueAnomalyOfZupVector
Functions taking a number, returning number:
vessel.orbit.getObtAtUTvessel.orbit.getObTAtMeanAnomalyvessel.orbit.GetEccentricAnomalyvessel.orbit.RadiusAtTrueAnomalyvessel.orbit.TrueAnomalyAtRadiusvessel.orbit.TrueAnomalyAtTvessel.orbit.getOrbitalSpeedAtvessel.orbit.getOrbitalSpeedAtDistancevessel.orbit.getTrueAnomaly
Functions taking a number, returning 3-element list:
vessel.orbit.getPositionAtTvessel.orbit.getPositionFromEccAnomalyvessel.orbit.getPositionFromMeanAnomalyvessel.orbit.getPositionFromTrueAnomalyvessel.orbit.getRelativePositionAtTvessel.orbit.GetFrameVelAtUTvessel.orbit.getOrbitalVelocityAtObTvessel.orbit.getTruePositionAtUT
Functions taking a number, returning number:
vessel.orbit.GetDTforTrueAnomalyvessel.orbit.GetUTforTrueAnomalyvessel.orbit.GetMeanAnomaly
Functions returning a number:
vessel.orbit.ApAvessel.orbit.ApRvessel.orbit.PeAvessel.orbit.PeRvessel.orbit.semiLatusRectumvessel.orbit.semiMinorAxisvessel.orbit.altitudevessel.orbit.argumentOfPeriapsisvessel.orbit.ClApprvessel.orbit.ClEctr1vessel.orbit.ClEctr2vessel.orbit.closestTgtApprUTvessel.orbit.CrApprvessel.orbit.Evessel.orbit.eccentricAnomalyvessel.orbit.eccentricityvessel.orbit.EndUTvessel.orbit.epochvessel.orbit.FEVpvessel.orbit.FEVsvessel.orbit.fromEvessel.orbit.fromVvessel.orbit.inclinationvessel.orbit.LANvessel.orbit.magvessel.orbit.meanAnomalyvessel.orbit.meanAnomalyAtEpochvessel.orbit.nearestTTvessel.orbit.nextTTvessel.orbit.ObTvessel.orbit.ObTAtEpochvessel.orbit.orbitalEnergyvessel.orbit.orbitalSpeedvessel.orbit.orbitPercentvessel.orbit.periodvessel.orbit.radiusvessel.orbit.sampleIntervalvessel.orbit.semiMajorAxisvessel.orbit.SEVpvessel.orbit.SEVsvessel.orbit.StartUTvessel.orbit.timeToApvessel.orbit.timeToPevessel.orbit.timeToTransition1vessel.orbit.timeToTransition2vessel.orbit.toEvessel.orbit.toVvessel.orbit.trueAnomalyvessel.orbit.UTapprvessel.orbit.UTsoivessel.orbit.V
Functions returning a 3-element list:
vessel.orbit.anvessel.orbit.eccVecvessel.orbit.hvessel.orbit.posvessel.orbit.secondaryPosAtTransition1vessel.orbit.secondaryPosAtTransition2vessel.orbit.velvessel.orbit.GetANVectorvessel.orbit.GetEccVectorvessel.orbit.GetFrameVelvessel.orbit.GetOrbitNormalvessel.orbit.GetRelativeVelvessel.orbit.GetVelvessel.orbit.GetWorldSpaceVel
These functions can be supplied with an argument to set the value, or used with no argument to simply return the currently set value:
ctrlState.mainThrottle (0..1)ctrlState.pitch (-1..1)ctrlState.rollctrlState.yawctrlState.X (RCS translation)ctrlState.YctrlState.ZctrlState.killRot (SAS enable)
##AGC Variables
Unlike above, these AGC variables are not functions, they are variables.
Input variables:
AGC.Input- the user-entered AGC UI value for your program, as a numberAGC.dt- tick delta-time in secondsAGC.TickCount- number of AGC computer ticks since bootupAGC.Runtime- the number of seconds since bootup
Output variables:
AGC.Status- define this variable to output values in the AGC UI