Project 4: Finite State Machines

Project Goals

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

Project Outline

The goal for our hovercraft is to navigate through the environment shown below. The thick lines correspond to visible walls.
  1. At the beginning of a test run, your hovercraft will be placed in either the 1, 2 or 3 configuration shown. Your program will not be given any information about which configuration it is in. In addition, switch zero will be set to some state.

  2. On start up, your program should first record the state of the switch.

  3. Phase 1: your hovercraft must navigate from its initial configuration to a subgoal:

  4. Phase 2: depending on the state of the switch, the craft must navigate to the second subgoal:


Project Components

All components are required to receive full credit for the project.

Part 1: Finite State Machine Design

Design a complete FSM in diagram form:

Part 2: Finite State Machine Implementation

Note: this part will count for two personal programming credits (and can be split between two people)

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

typedef enum {
   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_last, heading_goal, heading_error;
       int16_t rotation_rate, distance_left, distance_right;
       State state = START;   // NEW
       int16_t forward_thrust = 0;  // NEW

       #APPROPRIATE VARIABLE DECLARATIONS HERE#

       
       #APPROPRIATE INITIALIZATIONS HERE#

       timer0_config(TIMER0_PRE_1024);   // Prescale by 1024
       timer0_enable();  // Enable the timer 0 overflow interrupt
       sei();  // Enable global interrupts

       // Initialize variables
       heading_goal = get_orientation();

       distance_left = get_distance(LEFT);
       distance_right = get_distance(RIGHT);

       // Begin to lift off the ground
       set_middle_direction(HOVER);

       #RAMP UP MIDDLE THRUST TO HOVER#

       // Loop forever
       while(1) {
       
           heading = get_orientation();
           heading_error = compute_orientation_error(heading_goal, heading);
       
           rotation_rate = get_rotation_rate();

           distance_left = get_distance(LEFT);
           distance_right = get_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){};
           }

           // NEW
           // NOTE: forward_thrust should be set by your FSM
           pd_control(forward_thrust, heading_error, rotation_rate);

           // 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;
       }
}


References

Hints


What to Hand In

The project checkpoint is due by Tuesday, April 23rd at 5:00pm. All components of the project are due by Tuesday, April 30th at 5:00pm.


Grading

Group grade distribution:

Grades for individuals will be based on the group grade, but weighted by the assessed contributions of the group members.


fagg [[at]] cs.ou.edu

Last modified: Fri Apr 22 17:47:45 2016