I trying to write an autopilot for the classic Lunar Lander or its clones.

According to this paper, the arrival behavior is exactly what I need for a soft landing. In this example, it accelerates at max_speed until it's near the target and decreases near the target to zero. Adding gravity and a engine to decelerate makes this a lot more complicated, at least for me since I couldn't wrap my head around the concept for the past few hours.

Currently my spaceship moves in the right direction but doesn't counter gravity and just throttles the engine, crashing near the target. I know my current position, velocity and the gravity is constant. I need to output thrust-levels (0-4) with a -90° to 90° angle.

Can you give me some pointers as to how I could incorporate gravity into this steering behavior?


2 Answers 2


You don't need sophisticated methods to accomplish this if you explained the issue correctly. All you need is the following,

  • d - distance from the ground.
  • s - Current speed, going downwards.
  • g - Acceleration downwards resulting from gravity.
  • a - Acceleration upwards from the engine.

When the ship is heading down to the desired landing spot, you can check every frame for the following equality:

var t = 2 * d/s;
if (s ~= (a - g) * t) then slowDown() 

Which you can translate to:

s ~= (a - g) * 2 * (d/s)

Which in turn translates to

s + epsilon > (a - g) * 2 (d/s)

This should do it.

What we are doing here is computing how much time will it take the ship to reach the ground if it could slow down and stop the moment it touches the ground?

If the ship linearly decreases its descent speed and stops completely when it reaches the ground then by average it would travel at half it's current speed, hence t = 2 * d/s which is basically saying -> t * 1/2 s = d.

We use this to check if indeed with the current engine power, it would take the ship this much time to slow down and stop on the ground:

a - g => (how quickly could the ship slow decelerate). s needs to be zero when we touch ground. so we get that this needs to occur (a - g) * t ~= s + epsilon

Please notice that I assume all values are positive scalars.


I think you may want to do some research on PID regulators. Auto pilot is a regulation response. Some regulators even go as far as being able to compensate non linear input to get the desired response. (see [off topic] section for little story*)

PID stands for "Proportional Integral Derivative". In electronics they are implemented with RLC circuits. They are "degree 2" which means they tend to target fast, and if the amortizement is not enough they can oscilate.

Numerically, you could try to make a learning machine, that tries varying ratios of modulated input. Do you know PWM ? Pulse-width modulation of power. It uses "hashing" of a DC source to create an average that can be modulated. Your engine, in a lunar lander, is a On/Off controller, therefore, unodulated it is a DC source. The position of your module is the integral (the I in PID) of your engine thrust. You can decide of a pusle frequency (10Hz, 30Hz?). Like a human would do actually, only faster so you have finer modulation grain. Then your regulator's (black box) input is a float number [0,1] signal that is used to change the width of the pulse. (look it up on wikipedia its going to be immediately clear what I am talking about). Then use a target, your target is a speed curve, the speed is a function of distance to land. Your regulator wants to match this curve. So at the beginning, the speed target is high because you are still far from land. your regulator will not apply thrust until the lander has accelerated near the speed target. then it will approach that target, and the derivative (speed at which the target is being approached), will play a role in the regulator to trigger the decision to apply thrust now, not after the target is overpassed.
Then this is when the actual width of the pulse should be learnt, by some memory (machine learning?) able algorithm. Not much experience with that though. Your regulation is not linear because of gravity, so it makes it slightly unconventional. But it should work more or less even without the learning system. (with some manual tweaking of the magical constants, by empirical testing. physicists could calculate the "charge constant" and be able to determine those factors mathematically, but I am no physicist.)
and this is only unidimensional for now. (vertical)
A simpler goal as a first implementation would be to try to oscilate around the speed target by using ON/OFF regulation (not smooth like PWM-PID) but enough to proof the concept quickly, and familiarize yourself with the reactions of the regulator. Just apply full thrust when over the speed target, to slow down, and release thrust when under speed target, to let it accelerate down naturally. The speed target still is "distance(lander,ground) * magical constant".
Of course mathematically, if this curve was respected perfectly, it would create the snail curve problem. If a snail divides his speed by two everytime he makes half the distance to its target, it will never reach the target. this is a contra-exponential assymptot. but we are in physics, not math, so some inertia and luck will solve this problem. Unfortunately, the same "luck" and "lag" will also result in crashes at times. But going to smooth PD or PID with PWM with some tweaking will get the landing routine perfectly safe at all times, eventually.

good luck, report progress ! :)

* [off topic]
This is actually something that happened as a problem in some industry. Consider this story: One day constructing an helicopter, somebody mounted the throttle mebrane in reverse. Nobody thought it would be possible to do such a thing so no defense existed against that. The membrane is a flexible piece of rubber/textile or fibers, that exists in the gasoline pipes, and is pushed by a piston to regulate gas flow to the engines. The control of the throttle does not go directly from manual lever input, to the piston drivers. It gets through a regulation device that modulates the piston force to get to the exact desired membrane position. The expected lifetime of the normally mounted membrane was 50k hours, but in reverse much less, because the piston had to apply much more force, however it went unnoticed, and the membrane broke quickly, helicopter crashed, people died. (true story)
[/off topic]
intelligent regulators are powerful but could be a risk if this kind of things is not considered :)

in your case, it is just a game, so you're OK though :P hehe


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .