Lunar Lander Applet Project Report

This Web page report on the instructor's lunar lander applet project is provided as an example of technical writing. Student reports are due in the laboratories on paper. However, there is nothing to prevent a student from writing his report as an HTML document and then printing it on paper. Please note, however, that if a student chooses write his report in HTML, he should not publish it (transfer it to his public_html directory). Student work is to be kept secret from other students, particularly source code.

Introduction

This report documents the lunar lander applet specification, design, implementation, and test results. The lunar lander applet example was written to provide students with a graphical version to better explain what the simulation is supposed to do and to show how the core simulation code can be extended to provide a graphical user interface (GUI).

The applet was developed by first implementing the simulation in C++ using the same requirements as for the CS102 student project number one. Once the C++ version was built, optimized, and tested, the code was ported to Java to provide a GUI. The applet was later enhanced to include an autopilot feature (that was first developed in the C++ version). The resulting applet is embedded here below:

You need a Java-enabled browser to see the applet.

Exhibit 1: Lunar Lander Applet.
Use the up and down arrow keys to adjust the throttle.
Hit the "a" key to turn on autopilot.

Specification

The lunar lander applet shall embody the simulation equations of "project 10" of chapter two of the Main and Savitch text: numerical integration to find velocity, altitude, and remaining fuel as a function of time using an appropriate time discretization (time slice). The applet shall also provide a GUI for user interaction for manual piloting and shall also provide an optional autopilot feature for controlling the throttle (current fuel flow rate) for a soft landing.

Simulation

The applet shall utilize an appropriate time slice size to give as accurate a simulation as possible within the performance capabilities a typical personal computer (200 Mhz Pentium). The simulation shall commence with the default lander object data members given in the assignment with the exception of the throttle, which may be set to an arbitrary value so that the user may explore the constant-throttle behavior of the lander.

Autopilot

The applet shall have an optional autopilot feature that will give a very soft landing from the default starting position.

User Interface

The applet shall present a 2D graphical representation of the lander simulation at an appropriate scale for a typical personal computer (1024 x 768 pixels x 256 color display). User input for increasing and decreasing the throttle both before the simulation is started and during the simulation shall be accommodated. The user shall be able optionally to enable the autopilot before starting the simulation. The user shall be able to start the simulation upon applet startup and shall be provided some way of resetting the simulation after landing and restarting the simulation repeatedly thereafter.

Feedback shall be provided on user input so the user knows his commands are received, and the landing velocity shall be presented to the user so that he knows the result of the simulation. Some criteria for success for a "soft" landing shall be utilized and the success state shall be output after each landing.

Design

Simulation

Single precision (instead of double) floating point values are used throughout the simulation because performance is at a premium in an animated applet of this kind.

The simulation is performed in a while() loop that runs when the user clicks the "Start" button. The loop runs while the altitude is greater than zero. The landing speed is defined as the speed at the time slice prior to the altitude becoming zero or less. The time slice is set as an applet private member value, and was optimized by trial and error. Because of the graphical overhead, a rather coarse time slice of 0.5 second was used in the final version of the applet. This is simulated time, not system realtime. The simulation runs in a thread that wakes up every 50 milliseconds to compute the next time slice. Running the simulation in a thread allows the program to receive user input in the applet foreground thread while the simulation is running.

A "soft landing" speed of less than 2 m/s was arbitrarily chosen because tests with the C++ prototype showed it was easily achievable with a constant throttle and an appropriate time slice, and because 2 m/s seems a reasonable speed for a safe landing based on common experience with lander-sized objects such as automobiles.

Autopilot

The autopilot option is activated with the user-input "a" key. Once activated it cannot be deactivated until the simulation is reset with the "Reset" button. The autopilot seeks to achieve a slower velocity the closer the lander gets to the ground. The autopilot feature was achieved by including the following code in the lander's update() function:
    // Autopilot feature:
    if (_bAutoPilot)
    {
      if (_sfAltitude > 0)
      {
        if (_sfVerticalSpeed < -0.7 * Math.sqrt(_sfAltitude))  // Lucky hack.
        {
          _sfThrottle += sfThrottleAdjustment;
          if (_sfThrottle > 1) _sfThrottle = 1;
        }
        else
        {
          _sfThrottle -= sfThrottleAdjustment;
          if (_sfThrottle < 0) _sfThrottle = 0;
        }
      }
    }
The _bAutoPilot variable is a lander private boolean member that is set to true when the user inputs the "a" key.

The 0.7 factor was arrived at by trial and error. The sfThrottleAdjustment single precision floating point variable is local to the update() function set to 0.2 (also arrived at by trial and error). The square root function was also arrived at by trial and error. It was found to provide better autopilot performance than a linear function. It's labeled a "lucky hack" because, while some insight into a desired lander velocity profile was utilized, it was not derived by a rigorous analysis.

User Interface

The applet frame was utilized as the graphic window and also as a container for the control buttons in order to reduce complexity and minimize development time.

The lander polygon outline has 28 vertices. This number was chosen as a good compromise between performance complexity and graphic realism. The simulated rocket flame polygon has seven vertices. The lander was first sketched on a quad-ruled tablet in pencil and then coordinates were assigned to vertices for hard coding into the lander constructor function.

GUI buttons were chosen for the start and reset function activation because of their ease of use. Keystrokes were chosen (up and down arrow keys) for throttle control because of the naturalness of this type of gamelike interface. The autopilot feature was also enabled with a keystroke because it did not seem worth adding another button for a relatively minor feature. That is, users tend to give equal emphasis to features that have equal prominence in the GUI. The autopilot feature always gives the same lander simulation result and will be little used, while the manual simulation is somewhat challenging to the users and will likely be used much more often. Therefore, the start and reset buttons have much more importance, from the user's viewpoint, than the autopilot activation feature.

The simulated flame varies in length with the throttle setting to give added realism. The flame varies in length from zero to 54 pixels.

Implementation

The program was coded in Java as an applet and compiled with the Sun JDK version 1.2. The program was tested using the JDK appletviewer.exe program, and then embedded in an HTML file (this one, see Exhibit 1, above) for final testing. The source code is available for educational purposes only. Any future use of the code must include proper attribution to the author and must include the copyright notice.

Implementation Metrics

Software metrics are used to document the effort (cost) that goes into software production, and are not normally expected in a lower division course report. However, metrics are expected in software engineering courses, so are included here for completeness.

The prototype simulation algorithm was programmed in Visual C++ in about two hours including optimization and testing. The key parts of the simulation code were then ported to Java and extended with a GUI. This took about four hours, including optimization and testing. The autopilot feature took an additional two hours to implement, including optimization.

Test Results

The program was tested to show it meets its specification (above). With the manual simulation mode (using the arrow keys to control throttle) a user can achieve a soft landing (less than two meters per second) with some practice. A very soft landing (less than half a meter per second) is achieved with the autopilot.


This page established February 12, 2000; last updated February 13, 2000.