Project 4: Finite State Machines
- All components of the project are due by Tuesday, May 3rd
at 5:00pm.
- Discussion within groups is fine.
- Discussion across groups may not be about the specifics of the
solution (general programming/circuit issues are fine to
discuss).
Project Goals
At the end of this project, you should be able to:
- design a Finite State Machine (FSM) that performs a specified
high-level task,
- implement the FSM in code,
- connect FSM events to sensor events, and
- connect FSM actions to control actions.
Project Outline
The goal for the hovercraft is to navigate to a specific "corner" of a
room or hallway. Define theta1 to be the initial
orientation of the craft (nominally oriented with the hallway).
Define theta2 to point in a direction orthogonal to
theta1. If the switch is in a low logic state, then
theta2 should be clockwise from theta1.
Otherwise, theta2 should be counter-clockwise from
theta1.
Here are the steps:
- Ramp up the middle fan to a level that enables the craft to
leave the ground.
- Navigate along theta1 while avoiding oblique obstacles
(obstacles such that the two distance sensors give very
different values).
- If 15 seconds has gone by since step #2 has started, then the
craft should brake long enough to stop (this time period should
be represented by its own state). Then, continue with step #2.
- Should an obstacle appear directly in front of the craft during
execution of step #2 (as indicated by the two distance sensors
having similar values),
then the craft should first brake and then turn toward
theta2. If the
new direction is also blocked by an obstacle, then the craft
should power down and stop.
- Navigate along theta2 while avoiding oblique
obstacles.
- If 15 seconds has gone by since step #5 has started, then the
craft should brake long enough to stop. Then, continue with step #5.
- Should an obstacle appear directly in front of the craft while
executing step #5, then the craft should first brake and then turn toward
theta1. If the new direction is also blocked by an
obstacle, then the craft should power down and stop.
- Continue with step #2
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:
- List the actions. How the actions relate to control
signals? For example, an action such as brake
requires the reversal of the middle fan and a large middle fan
duty cycle for a brief period of time.
- List the events. How do the events relate to internal or
external information? For example, an event such as
obstacle requires that the values of the two distance
sensors are similar and within some range.
- Draw the FSM diagram with states, events and actions listed. Include this FSM
diagram as part of your report.
Part 2: Finite State Machine Implementation
Note: this part will count for two personal programming credits
(and can be split between two people)
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:
int main(void) {
int16_t counter = 0;
int16_t heading, heading_last, heading_goal, heading_error;
int16_t rotation_rate, distance_left, distance_right;
int16_t theta1, theta2;
uint8_t state;
#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
theta1 = heading_goal = get_orientation();
theta2 = ## add code to set theta2
state = STATE_START;
distance_left = get_distance(LEFT);
distance_right = get_distance(RIGHT);
// Begin to lift off the ground
middle_thrust_dir(HOVER);
#RAMP UP MIDDLE THRUST TO HOVER#
// Loop forever
while(1) {
heading = get_orientation();
heading_error = compute_error(heading_goal, heading);
rotation_rate = get_rotation_rate();
distance_left = get_distance(LEFT);
distance_right = get_distance(RIGHT);
// Display
if(#SWITCH 0 OPEN#) {
display_distance(distance_left);
}else{
display_rotation_rate(rotation_rate);
}
if(#SWITCH 1 OPEN#) {
display_orient(heading_error);
}else{
display_orient(heading);
}
// Finite state machine
switch(state) {
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){};
}
// Steer away from obstacles
status = obstacle_control(distance_left, distance_right, rotation_rate, #PICK SOME SMALL VALUE#);
if(status == -1) {
// No obstacles: steer to desired heading
pd_control(heading_error, rotation_rate, #PICK THE SAME SMALL VALUE#);
}
// 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
What to Hand In
All components of the project are due by Tuesday, May 3rd at 5:00pm.
- Demonstration/Presentation/Code Review: All group
members must be present. You must reserve one hour with the
instructor for this process
- Demonstrate your final product.
- Present your design and implementation (5 minutes).
The group is responsible for assembling a short presentation
consisting of 3-4 slides (on computer or in printed
form). Describe the design including:
- which team members were responsible for what
components (including the personal programming components),
- the circuit,
- your software solution for transforming the
analog value read from the sensors into the returned
integer,
- your software solution for displaying sensor
values,
- your FSM diagram,
- DO NOT include low-level code (only give
the general logic and equations)
Notes:
- All team members must be able to speak to the hardware
and software solutions.
- At the end of this meeting, you will have your group
grade (individual grades will be assigned at a later time).
- Code: Check your documented code to your
subversion repository.
- Circuit diagram: check your circuit diagram into your
subversion repository. This diagram must be developed
using EagleCad (see the downloads page).
- Presentation: check a copy of your powerpoint
presentation into the subversion repository.
- Personal report: One submission per person through CATME.
Grading
Group grade distribution:
- 35%: Project implementation
- 30%: Demonstration/presentation of working project (to either
of the TA or the instructor)
- 35%: Code documentation and group report
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: Mon May 9 15:40:10 2011