AME 3623: Project 9: Finite State Machines

At the end of this project, you should be able to:

Project Outline

Your hovercraft will be placed at a location and orientation on the field. Your craft will take the following steps:
  1. Wait for the switch to be pressed.

  2. Record the current orientation. This will be your goal orientation.

  3. After a 5-second delay, ramp up the middle fan to a point where the craft begins to turn (as measured by the gyro).

  4. Slightly drop the middle fan thrust.

  5. Move forward until a wall is detected.

  6. Stop

  7. Make a 90 degree turn to the left

  8. Move forward until another wall is detected.

  9. Stop, including shutting down all fans.

Component 1: Hardware

Component 2: Finite State Machine

Design your FSM on paper. This FSM must accomplish all of the above steps, including handling the start from the switch press and the stopped state.

Component 3: Software

Add a new variable type to "project.h":

typedef enum {
   STATE_START,
   #LIST YOUR OTHER STATES HERE#
} State;
Modify your main function such that it is structured as follows (you will, of course, need to add other code). Here is an outline for the code ("NEW" lines are explicitly indicated):
int main(void) {
       int16_t counter = 0;
       int16_t heading, heading_goal, heading_error;
       int16_t rotation_rate, distance_left, distance_right;
       State state = STATE_START;   // NEW
       int16_t forward_thrust = 0; 

       #APPROPRIATE VARIABLE DECLARATIONS HERE#

       
       #APPROPRIATE HARDWARE INITIALIZATIONS HERE#

       timer0_config(TIMER0_PRE_1024);   // Prescale by 1024
       sei();  // Enable global interrupts

       #INITIALIZE VARIABLES HERE#

       // NEW: place enable right above while()
       timer0_enable();  // Enable the timer 0 overflow interrupt

       // Loop forever
       while(1) {
           // ////////////////////////////////////////////////
           //  Sensors
           heading = read_rotation();
           heading_error = compute_rotation_error(heading_goal, heading);

           rotation_rate = read_rotation_rate();

           distance_left = read_distance(DISTANCE_LEFT);
           distance_right = read_distance(DISTANCE_RIGHT);

           // Display
           #APPROPRIATE CODE FOR DISPLAYING SENSOR STATES WITH YOUR LEDS#

           // ////////////////////////////////////////////////
           // Finite state machine
           switch(state) {               // NEW
               case STATE_START:
                   :
                  break;
               case STATE_NAVIGATE_1:
                   :
                  break;
                :
                :
               default:
                  // this should never happen: but take safety steps
                  //   if it does
                  #Shut down craft#
                  while(1){};
                  break;
           }

           // ////////////////////////////////////////////////
           // Control
           // NOTE: forward_thrust and heading_goal should be set by your FSM
           // Compute heading_error again in case heading_goal has changed
           heading_error = compute_rotation_error(heading_goal, heading);

           // Note that position_derivative_control() performs the error clipping
           position_derivitive_control(forward_thrust, heading_error, rotation_rate);

           // ////////////////////////////////////////////////
           // Handle loop timing
           // Increment time
           ++counter;

           if(flag_timing) {
               // Error condition: your code body is taking too much
               //  time.
               #Indicate this with an LED display of some form#
           }

           // Wait for the flag to be set (happens once every ~50 ms)
           while(flag_timing == 0) {};

           // Clear the flag for next time.
           flag_timing = 0;
       }
}


Notes


What to Hand In

All components of the project are due by Tuesday, April 28th at 9:00 pm.

Grading

Personal programming credit: Group grade distribution:

Group Grading Rubric

Grades for individuals will be based on the group grade, but weighted by the assessed contributions of the group members to the non-personal programming items.

References


andrewhfagg -- gmail.com

Last modified: Wed Apr 20 23:52:59 2016