ES101 - Lesson 11 : State Machine and Board IO

From Embedded Systems Learning Academy
Revision as of 19:18, 2 November 2012 by Charley (talk | contribs)

Jump to: navigation, search

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


As seen by the previous figure, the switch/case statements look elegant, and are easier to read compared to if/else statements had they been used instead. The variable inside the round brackets (currentState) is what is being compared to the other cases. Each statement has a colon (:) at the end. Also, each case ends with a break. Forgetting to include the break will force the code to go to the next state without comparison. For example, if currentState was equal to stateSTART and there was no break statement at the end of the stateSTART case, the code would continue without currentState comparison with stateEND and execute the code inside the stateEND case as well. This may appear to be a programming flaw, however, the switch/case statements were designed this way and some complex designs omit the break statements on purpose.

The default case should be the last case, it is for the case if the variable currentState does not match any of the case statements, and this case does not have the break statement. The default case should be used to indicate an error state or unexpected state fault.

Static Declaration of Variables

So far, you have learned how to locally declare variables inside a function, but sometimes, you want to declare the local variables as static so their values are preserved across multiple calls to the same function.


void foo()
{
   int i = 0;
   i++;
   printf("Value of i = %i\n", i);
}
int main(void)
{
   foo();  // Prints: Value of i = 1
   foo();  // Prints: Value of i = 1
   foo();  // Prints: Value of i = 1
}


In the code above, whenever foo() is called, it declares a new variable called i and sets it to 0. Then it increments by 1 and prints it out. Whenever main() calls it, foo() does the same thing, and each time it sets i as 1 and when it exits, the variable i goes out of scope.


void foo()
{
   static int i = 0;
   i++;
   printf("Value of i = %i\n", i);
}
int main(void)
{
   foo();  // Prints: Value of i = 1
   foo();  // Prints: Value of i = 1
   foo();  // Prints: Value of i = 1
}


In the code above, the result is different because the variable i is declared as static int. It should be observed that the variable i is only declared once, and the function foo() preserves the value of i over repeated function calls. For this assignment, if your state machine is enclosed in a function called stateMachine(), the please decide which variables should preserve their value, and declare them as static. Declaring a static variable is a better programming practice than declaring a global variable which would be visible to the entire program.

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.

State Machine Design

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.