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

From Embedded Systems Learning Academy
Jump to: navigation, search
Line 3: Line 3:
  
 
== State Machine Design ==
 
== State Machine Design ==
State machine design can be easily constructed by considering <i>what</i> 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.
+
State machine design can be constructed by considering <i>what</i> 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, in which you have various states that dictates robot behavior such as :
 
+
*  No Obstacle --> Move forward
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/
+
*  Obstacle to the left --> Turn right
 +
*  Obstacle to the right --> Turn left
  
 
== ENUM ==
 
== ENUM ==
The <b>enum</b> 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 <font size = 3><code>enum</code></font> 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.
+
The <b>enum</b> 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 <font size = 3><code>enum</code></font> to declare the names. The alternative is to declare separate variables set to a different value, but this approach uses more unnecessary memory and is error-prone.
  
<font size = 3>
 
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
// Auto-set the identifiers of the variables
+
// Each enumeration value is unique :
enum{stateSTART, stateEND, stateSENSOR};
+
typedef enum{stateSTART, stateEND, stateSENSOR} myStateType;
  
// Manually set the identifiers of the variables
+
// Manually set the enumeration values :
enum{stateSTART = 1, stateEND = 2, stateSENSOR = 3};
+
typedef enum{stateSTART = 1, stateEND = 2, stateSENSOR = 3} myStateType;
 
</syntaxhighlight>
 
</syntaxhighlight>
</font size>
 
  
 
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.
 
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==
 
==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.
  
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.
+
<BR/>
 
 
<BR>
 
<font size = 3>
 
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
// Test the "currentState" variable with one of the cases given below
 
// Test the "currentState" variable with one of the cases given below
Line 33: Line 30:
 
{   
 
{   
 
   case stateSTART:
 
   case stateSTART:
       i = 0;
+
       // Your code goes here
 
       break;
 
       break;
 
   case stateEND:
 
   case stateEND:
       i = 0;
+
       // Your code goes here
 
       break;
 
       break;
 
   case stateSENSOR:
 
   case stateSENSOR:
       i = 2;
+
       // Your code goes here
 
       break;
 
       break;
 
   default:
 
   default:
       i = 3;
+
       // Your code goes here
}
 
</syntaxhighlight>
 
</font>
 
<BR>
 
 
 
As seen by the previous figure, the <b>switch/case</b> statements look elegant, and are easier to read compared to <b>if/else</b> statements had they been used instead. The variable inside the round brackets <font size = 3><code>(currentState)</code></font> 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 <font size = 3><code>currentState</code></font> was equal to <font size = 3><code>stateSTART</code></font> and there was no <font size = 3><code>break</code></font> statement at the end of the <font size = 3><code>stateSTART</code></font> case, the code would continue without <font size = 3><code>currentState</code></font> comparison with <font size = 3><code>stateEND</code></font> and execute the code inside the <font size = 3><code>stateEND</code></font> 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 <font size = 3><code>default</code></font> case should be the last case, it is for the case if the variable <font size = 3><code>currentState</code></font> does not match any of the case statements, and this case does not have the <font size = 3><code>break statement</code></font>. 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.
 
 
 
<BR>
 
<font size = 3>
 
<syntaxhighlight lang="c">
 
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
 
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
</font>
 
 
<BR>
 
<BR>
  
In the code above, whenever <font size = 3><code>foo()</code></font> is called, it declares a new variable called <font size = 3><code>i</code></font> and sets it to 0. Then it increments by 1 and prints it out. Whenever <font size = 3><code>main()</code></font> calls it, <font size = 3><code>foo()</code></font> does the same thing, and each time it sets <font size = 3><code>i</code></font> as 1 and when it exits, the variable <font size = 3><code>i</code></font> goes out of scope.
+
Lets clarify the syntax of the switch/case code :
 +
*  Variable inside switch() is what is being compared
 +
*:  This variable will be compared with each case.
 +
*  '''case''' is followed by a variable you want to compare, followed by a colon.
 +
*  '''break''' is required at the end of every case
 +
*:    See the next section for more clarification
 +
*  Default case is like an '''else''' statement when neither cases equal the variable being compared. 
 +
*:    See the '''Default case''' section below for details.
  
<BR>
+
=== Why break? ===
<font size = 3>
+
If <font size = 3><code>currentState</code></font> was equal to <font size = 3><code>stateSTART</code></font> and there was no <font size = 3><code>break</code></font> statement at the end of the <font size = 3><code>stateSTART</code></font> case, the code would continue without <font size = 3><code>currentState</code></font> comparison with <font size = 3><code>stateEND</code></font> and execute the code inside the <font size = 3><code>stateEND</code></font> 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.
<syntaxhighlight lang="c">
 
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
 
}
 
</syntaxhighlight>
 
</font>
 
<BR>
 
  
In the code above, the result is different because the variable <font size = 3><code>i</code></font> is declared as <font size = 3><code>static int</code></font>. It should be observed that the variable <font size = 3><code>i</code></font> is only declared once, and the function <font size = 3><code>foo()</code></font> preserves the value of <font size = 3><code>i</code></font> over repeated function calls. For this assignment, if your state machine is enclosed in a function called <font size = 3><code>stateMachine()</code></font>, 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.
+
=== Default case ===
 +
The <font size = 3><code>default</code></font> case should be the last case. It is for the case if the variable <font size = 3><code>currentState</code></font> does not match any of the case statements, and this case does not have the <font size = 3><code>break statement</code></font>. The default case should be used to indicate an error state or unexpected state fault.
  
 
==Assignment==  
 
==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 <font size = 3><code>main()</code></font> 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.  
+
Build the state machine given in the next figure. 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.  
  
 
[[File:stateMachine10.png|600px|center|State Machine Design]]
 
[[File:stateMachine10.png|600px|center|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.
+
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.
  
 
==Sample Code==
 
==Sample Code==
 
<BR>
 
<BR>
<font size = 3>
 
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
void myStateMachine()
+
int main()
 
{
 
{
 
   // Use typedef to create a new variable type called States
 
   // Use typedef to create a new variable type called States
   typedef enum {start, end} States; // Name your states inside { and }
+
   typedef enum {start, end} myStateType; // Name your states inside { and }
   static States currentState = start; // Initialize static variable of States
+
   myStateType currentState = start;     // Initialize static variable of States
  
 
   switch(currentState)
 
   switch(currentState)
Line 125: Line 85:
 
         }
 
         }
 
         break;
 
         break;
 +
 
       case end:
 
       case end:
 
         // Check if button 0 is pressed in this state and transition out of this state
 
         // Check if button 0 is pressed in this state and transition out of this state
Line 133: Line 94:
 
         }
 
         }
 
         break;
 
         break;
 +
 
       default:
 
       default:
 
         printf("State machine ERROR!\n");
 
         printf("State machine ERROR!\n");
Line 138: Line 100:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
</font>
 
 
<BR>
 
<BR>
  
 
==Questions==
 
==Questions==
 
# Describe the behavior you see if you press and hold Button#1 for longer than 100ms in the SENSOR state.
 
# Describe the behavior you see if you press and hold Button#1 for longer than 100ms in the SENSOR state.

Revision as of 22:02, 6 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 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, in which you have various states that dictates robot behavior such as :

  • No Obstacle --> Move forward
  • Obstacle to the left --> Turn right
  • Obstacle to the right --> Turn left

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. The alternative is to declare separate variables set to a different value, but this approach uses more unnecessary memory and is error-prone.

// Each enumeration value is unique :
typedef enum{stateSTART, stateEND, stateSENSOR} myStateType;

// Manually set the enumeration values :
typedef enum{stateSTART = 1, stateEND = 2, stateSENSOR = 3} myStateType;

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.


// Test the "currentState" variable with one of the cases given below
switch(currentState)
{  
   case stateSTART:
      // Your code goes here
      break;
   case stateEND:
      // Your code goes here
      break;
   case stateSENSOR:
      // Your code goes here
      break;
   default:
      // Your code goes here
}


Lets clarify the syntax of the switch/case code :

  • Variable inside switch() is what is being compared
    This variable will be compared with each case.
  • case is followed by a variable you want to compare, followed by a colon.
  • break is required at the end of every case
    See the next section for more clarification
  • Default case is like an else statement when neither cases equal the variable being compared.
    See the Default case section below for details.

Why break?

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.

Default case

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.

Assignment

Build the state machine given in the next figure. 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.

Sample Code


int main()
{
   // Use typedef to create a new variable type called States
   typedef enum {start, end} myStateType; // Name your states inside { and }
   myStateType currentState = start;      // Initialize static variable of States

   switch(currentState)
   {
      case start:
         // Check if button 0 is pressed in this state and transition out of this state
         if( /*button 0 is pressed */)
         {
            currentState = end;
            printf("Current state: END. Press Button 1 to go to START");
         }
         break;

      case end:
         // Check if button 0 is pressed in this state and transition out of this state
         if( /*button 1 is pressed */)
         {
            currentState = start;
            printf("Current state: START. Press Button 0 to go to END");
         }
         break;

       default:
         printf("State machine ERROR!\n");
   }
}


Questions

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