Jump to content

[0.24] Automated Guidance Computer


bassgojoe

Recommended Posts

Automated Guidance Computer

version 0.2: initial release

version 0.3 updated 2/6/14: orbit parameters, target parameters

Introduction

This is my first plugin for KSP. I wanted to make a plugin that would allow me to automate some functions on my vessel. I didn't find out about the excellent kOS plugin until after getting pretty far with my plugin, so I just decided to carry on anyway. :)Yes, AGC is a reference to the Apollo Guidance Computer.

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 everyone's favorite programming language, LISP :wink: . 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.

How to use

Place the AGC (Automated Guidance Computer) on your ship. In career mode, you need to unlock the Flight Control research node. 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 "cond" function is not supported because if no predicates match, the result is undefined.

The empty list () is equivalent to (quote ()).

If you pass car a non-list argument, it will simply return it. So, (car 5) evaluates to 5.

If you pass cdr a 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 "initialize" built-in function is the same as "define", 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 "if" and "lambda". If you are new to Lisp, I recommend The Structure and Interpretation of Computer Programs, a free ebook published by MIT:

http://mitpress.mit.edu/sicp/

Download

Plugin source code is included in the zip and is licensed under GPL v3. Source code is also available on github: kerbal-agc source

Download from Curse

Edited by bassgojoe
Link to comment
Share on other sites

I just noticed the installations instructions are slightly wrong: you want to extract the zip into your KSP\GameData folder under an AGC subfolder that you will need to create. Sorry about that, I'll correct it for the next release.

Also, here's a list of known issues:

* I need to expose more variables & functions for orbit

* I need to expose variables for your current target (so you can try writing a sweet docking autopilot)

* At the moment, the model is an RCS thruster block :P

Link to comment
Share on other sites

I do like the idea, and it's nice to have an alternative to kOS (which hasn't gotten much attention lately).

But LISP ? I don't know what the acronym stands for (or if it is an acronym), but back in the day we used to joke that it means "Lost In Sucking Parentheses" :)

To this day when I see the name "LISP" it pops up in my head - "oh, it's that programming language with a lot of parentheses".

Anyway, I still hope this gets more popular and wish you luck.

Link to comment
Share on other sites

Thanks for the encouragement! LISP still has lots of parentheses. :) But it's a simple, elegant language that (maybe most importantly) is easy for me to write a robust parser and interpreter for. I think it's also pretty easy to understand for someone who doesn't have a ton of programming experience. I do recommend a text editor with syntax highlighting so you can understand the crazy nested parentheses, though....

If anyone wants to donate a better 3d model, or give me permission to use one from another mod, that'd be rad!

Link to comment
Share on other sites

  • 4 weeks later...

I found a bug in AGC:


--- SRC.OLD/AGC/AGC/AGC.cs 2014-02-05 01:22:25.614996586 +0100
+++ SRC/AGC/AGC/AGC.cs 2014-02-05 01:23:02.418428494 +0100
@@ -334,7 +334,7 @@
env.Add("ctrlState.X", new MicroLisp.ExternalFunc(args => GetSetNumberDelta(vessel.ctrlState.X, lastDeltaFlightCtrlState.deltaX, ref deltaFlightCtrlState.deltaX, args, "ctrlState.X")));
env.Add("ctrlState.Y", new MicroLisp.ExternalFunc(args => GetSetNumberDelta(vessel.ctrlState.Y, lastDeltaFlightCtrlState.deltaY, ref deltaFlightCtrlState.deltaY, args, "ctrlState.Y")));
env.Add("ctrlState.Z", new MicroLisp.ExternalFunc(args => GetSetNumberDelta(vessel.ctrlState.Z, lastDeltaFlightCtrlState.deltaZ, ref deltaFlightCtrlState.deltaZ, args, "ctrlState.Z")));
- env.Add("ctrlState.Z", new MicroLisp.ExternalFunc(args => GetSetBoolDelta(vessel.ctrlState.killRot, lastDeltaFlightCtrlState.deltaKillRot, ref deltaFlightCtrlState.deltaKillRot, args, "ctrlState.killRot")));
+ env.Add("ctrlState.killRot", new MicroLisp.ExternalFunc(args => GetSetBoolDelta(vessel.ctrlState.killRot, lastDeltaFlightCtrlState.deltaKillRot, ref deltaFlightCtrlState.deltaKillRot, args, "ctrlState.killRot")));
}

public void Boot()

There's also need for some more documentation. Yes, reading the code helps, but ...

I'm willing to try my hand, if the project is still GO. (is it?)

Things I sorta miss coming from kOS:

  • A better way to present data. In kOS I have a "whole screen" to "print at"; while this has quite a few rough corners, I use it to display lots of data and status messages (more so when debugging) and other people even print graphs ...
  • having to be concise (should be optional!)
  • having to make every statement count (kOS is '1 statement from each thread (started using when) per round', and this means you want small, fast loops when feedback driven (PIDs and similar)) (should be optional!)
  • UI and running of the computer toggeable via action groups

Link to comment
Share on other sites

  • 1 month later...
* put the source on github

Maybe I am just stupid, but I can't find it on github.com. Could you add a link? Thanks!

PS:

The "cond" function is not supported because if no predicates match, the result is undefined.

Would either "abort with error" or "return nil" (like Common Lisp) be an acceptable answer? Or is a macro writing facility to define our own syntax planned?

Link to comment
Share on other sites

The source isn't up on github yet, it's still on my TODO list. I'll put it up soon. Right now the source is distributed in the zipfile.

If it's a common convention to return nil on no "cond" predicates matching, I'll go ahead and add that. Thanks for the info, I'm not a very experienced lisp programmer, so I'm relying on people like you to tell me this stuff. :)

Link to comment
Share on other sites

The source isn't up on github yet, it's still on my TODO list. I'll put it up soon.

Hopefully one day I'll learn to read more closely.

If it's a common convention to return nil on no "cond" predicates matching, I'll go ahead and add that. Thanks for the info, I'm not a very experienced lisp programmer, so I'm relying on people like you to tell me this stuff. :)

As if I were a very experienced lisp programmer! :confused:

I simply noticed that cond would be something I like to have, so ... google ... Ah. CL nil, some others "error". So I ask, but --- it's your project and your call what features you want us to have. There may not even be a technical reason --- kOS wants to offer a certain feel and thus gave certain features and decided not to have other features (like an "else" branch in an if).

Link to comment
Share on other sites

  • 3 months later...

Iunno how "dead" this thread is, but I'm relatively new here and noticed that the link on the first post is dead, now connects to Curse, which does not seem to have your mod. I found it instead here:

http://.com/space/modpart-automated-guidance-computer-101.html

I can find this neither on github or (more shocking?) reddit.

Anyrate! I was hoping for a list of game-oriented commands ala kOS (i.e. "stage." etc.)

I'm trying this mod because I'm tired of the limitations of kOS.

edit: found list of commands in the readme (RTFM, right?)

Edited by postalbyke
Link to comment
Share on other sites

Man, I thought kerbalspaceprogram.com was supposed to stay up. Oh well. I re-uploaded the mod to Curse, I'll bump the thread once it gets approved and goes live.

Let me know what you think of the mod.

Link to comment
Share on other sites

It's pretty good so far, mostly struggling with the difference in structure coming from a c/java/python background. Trying to find a setup that allows me to run lisp in a window ala' command line with python.

Also, I have the source code (from within the download) and I can kind of see what I want to alter in it (for my own purposes), how do I turn that into the .dll?

Or should I just bug you for what I want in the mod? (a type-in interface like kOS, the ability to assign the computer to an action group, etc.)

Link to comment
Share on other sites

I just got back from lunch and I'm working on some launching commands, and I'm running into error:symbol not recognized:loop and error:symbol not recognized:while. I also can't use break (apparently because of lack of loop support). I understand that the whole program is technically looping, but I was hoping to take advantage of more programming functions.

note: If I get too uppity, tell me to give it a rest, I created mods for silent hunter 3 and had a few really persistent followers.

Link to comment
Share on other sites

Info about compiling the source into the kerbal dll plugin can be found here:

http://wiki.kerbalspaceprogram.com/wiki/Plugins

I used monodevelop.

Scheme in general doesn't do explicit loops, preferring recursion instead. But I also provide a lot of built-in functions to do things that you might do with a loop in an imperative language; if you're processing a list for example there are functions which run a function on each element and accumulate the result (see map or foldl).

Edited by bassgojoe
Link to comment
Share on other sites

Silly question time! Can you tell me any dead giveaway differences between drRacket and the Scheme you use for your mod?

(if this is too ridiculous a question, just tell me "no.")

Also: are you waiting for 0.24 for your next release, or are you on a schedule of your own?

Link to comment
Share on other sites

Here's the comma separated list of built-in functions:

if, quote, set!, define, initialize, lambda, begin, +, -, *, /, not, and, or, >, <, >=, <=, =, equal?, eq?, length, cons, car, cdr, append, list, list?, null?, symbol?, defined?, modulo, abs, floor, ceiling, min, max, apply, id, sqrt, let

There are also some helper functions defined in stdlib.txt.

Link to comment
Share on other sites

This is excellent! I'm teaching children 'hard sciences' with KSP, and would have wanted to teach basic programming as well using kOS. However, as much as I liked it, kOS is not pedagogically very suitable, whereas LISP is really good!

Besides, it's much better to use language that already has good editor/IDE support. Thank you so much for this!

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