State machine example from today’s lecture

In today’s lecture, we worked through a state machine example based on the Race to the Wall.

Remember: A state machine is a type of algorithm – not an actual physical machine! Obviously, in the case of your robot, the algorithm is controlling the behaviour of the machine, but when we say state machine it’s the algorithm we’re talking about.

We began by mapping out the state machine using a state table. If you want to apply a state machine in your RoboSumo robot, this is where you should begin. Constructing a state table similar to the one below provides an excellent opportunity for a focused group discussion about the behaviour of the robot, without getting mired in the details of the C code which will almost certainly be confusing to at least one member of the team.

Once the team has agreed on the contents of the state table, it becomes a lot more practical to assign the task of writing the C code to one member of the team.

That person’s job is then very clearly defined – he or she simply needs to implement exactly what the entire team has agreed. It’s also easier for others to help debug the code, since everyone should have the same understanding of what it’s supposed to be doing.

Here’s the state table for the Race to the Wall which we constructed through our class discussion:

20150318_141828

Let’s see that in slightly clearer form…

State Description Actuators
LM: left motor
RM: right motor
Change state when…
1 2 second delay LM stop
RM stop
2 seconds elapsed → 2
2 forwards to wall LM forward
RM forward
switch input high → 3
3 reverse to line LM reverse
RM reverse
colour sensor low → 4
4 brake on line LM forward
RM forward
0.5 seconds elapsed → 5
5 stay still on line LM stop
RM stop
switch input high → 1

So the state model is very simple in this case. In each state, there is only one possible next state, and it’s simply a matter of deciding when to go there.

state_diagram_RTTW

Now, here’s the above table implemented in C code.

//
// State Machine example for MSP430G2452
// Written by Ted Burke - Last modified 18-3-2015
//
 
#include <msp430.h>
 
int main( void )
{
    WDTCTL = WDTPW + WDTHOLD; // Disable watchdog timer
    
    // 
    // Pins used:
    //   Switch input = P1.3
    //   Colour sensor input = P1.2
    //   Left motor outputs = P2.0-1
    //   Right motor outputs = P2.2-3
    //
    P1DIR = 0b00000000;
    P2DIR = 0b00001111;
     
    // Main loop implements state machine
    int state = 1; // state variable stores current state
    int s1, c1;    // sensor variables
    while(1)
    {
        // Read all sensors and store readings to variables
        
        // Read switch s1 on P1.3
        if ((P1IN & BIT3) > 0) s1 = 1; // pressed
        else s1 = 0;                   // not pressed
        
        // Read colour sensor c1 on P1.2
        if ((P1IN & BIT2) > 0) c1 = 1; // white
        else c1 = 0;                   // black
        
        // Carry out action for current state
        if (state == 1) // 2 second delay at start
        {
            P2OUT = 0b00000000; // both motors stop
            
            // after 2s, go to state 2
            __delay_cycles(2000000);
            state = 2;
        }
        else if (state == 2) // forward to wall
        {
            P2OUT = 0b00000101; // both motors forwards
            
            // if switch pressed, go to state 3
            if (s1 == 1) state = 3;
        }
        else if (state == 3) // reverse to black line
        {
            P2OUT = 0b00001010; // both motors reverse
            
            // if black detected on c1, go to state 4
            if (c1 == 0) state = 4;
        }
        else if (state == 4) // motors forward to brake 
        {
            P2OUT = 0b00000101; // both motors forward
            
            // after 500ms, go to state 5
            __delay_cycles(500000);
            state = 5;
        }
        else if (state == 5) // stop forever
        {
            P2OUT = 0b00000000; // both motors stop
            
            // if switch pressed, go to state 1
            if (s1 == 1) state = 1;
        }
    }
     
    // The program never actually reaches this point
    return 0;
}
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s