Difference between revisions of "ES101 - Lesson 11 : State Machine and Board IO"

From Embedded Systems Learning Academy
Jump to: navigation, search
(Created page with "== Objective == The objective of this lab is to learn how to design a state machine. == State Machine Design == State machine design can be easily constructed by considering ...")
 
Line 23: Line 23:
  
 
==Switch Statement==
 
==Switch Statement==
 +
 +
The C/C++ programming languages feature <b>switch/case</b> statements. This is similar to using a series of <b>if/else</b> statements. However, it is easier to read and write code using <b>switch/case</b> statements in some situations such as for a state machine design.
 +
 +
<syntaxhighlight lang="c">
 +
// Test the "currentState" variable with one of the cases given below
 +
switch(currentState)
 +
 +
  case stateSTART:
 +
      i = 0;
 +
      break;
 +
  case stateEND:
 +
      i = 0;
 +
      break;
 +
  case stateSENSOR:
 +
      i = 2;
 +
      break;
 +
  default:
 +
      i = 3;
 +
}
 +
</syntaxhighlight>
  
 
==Static Declaration of Variables==
 
==Static Declaration of Variables==

Revision as of 01:52, 2 November 2012

Objective

The objective of this lab is to learn how to design a state machine.

State Machine Design

State machine design can be easily constructed by considering what you want to do based on a certain system state. For example, you might want consider building a state machine for an obstacle-avoidance robot, which acts differently based on if it detects an obstacle or not. In other words, you might have a state to avoid an obstacle, and another state to follow a light-source if there is an obstacle in the way. This helps separate the functionality of what should happen when an obstacle is in the way, and what should happen if an obstacle is not in the way.

Each state should contain meaningful code that should be executed in that state. Furthermore, state transition code should be put in place that provides a way out of the current state to the next state/


ENUM

The enum provides a way to assign names to a sequence of integer values. For example, if a state machine contains two unique states, you should use an enum to declare the names, and the compiler guarantees that the names have a unique identifier. The alternative is to declare separate variables set to a different value, but this approach uses more unnecessary memory and is error-prone.

// Auto-set the identifiers of the variables
enum{stateSTART, stateEND, stateSENSOR};

// Manually set the identifiers of the variables
enum{stateSTART = 1, stateEND = 2, stateSENSOR = 3};

The enumerator variables can be automatically assigned a value, or the value can be manually chosen. In either case, unique values should be set for enumerator variables. An enumerator does not have to be used just for a state machine design, but it can be used anytime when you need to simply set unique identifiers to certain names.

Switch Statement

The C/C++ programming languages feature switch/case statements. This is similar to using a series of if/else statements. However, it is easier to read and write code using switch/case statements in some situations such as for a state machine design.

// Test the "currentState" variable with one of the cases given below
switch(currentState)
{  
   case stateSTART:
      i = 0;
      break;
   case stateEND:
      i = 0;
      break;
   case stateSENSOR:
      i = 2;
      break;
   default:
      i = 3;
}

Static Declaration of Variables

Assignment

Build the state machine given in the next figure. Enclose your state machine inside a function and call the function every 100ms from your main() function. You will notice that the SENSOR state goes to IDLE state upon Button#1, and using the same button, the IDLE state transitions to SENSOR state. First, answer the question in the "Questions" section of this document.

Add image here

Whenever the state is changed, print on the screen what state the system is in. The statement should only print once - not every time the state machine function is called.


Questions

  1. Describe the behavior you see if you press and hold Button#1 for longer than 100ms in the SENSOR state.