State machine example from today’s lecture

In today’s lecture, we worked through a state machine example designed to navigate a RoboSumo bot to the centre of the arena. Please note that the term state machine refers to an algorithm rather than to a physical machine. Obviously, in this case the algorithm controls a physical machine (the robot), but a state machine approach can be used in many applications where there is no physical machine at all.

The circuit diagram for this example is basically the same as the one from last week’s lecture except that I’ve added an additional colour sensor, so that there’s one on each of the front corners of the robot.

First we drew up a state table to describe the behaviour of the robot in unambiguous terms. What defines each “state” the specific activity of each actuator (i.e. what each motor is doing) and a number of conditions which can trigger a transition from that state to another one.

State Description Motors Transitions
1 Initial pause LM stop, RM stop 2 seconds elapsed: goto 2
2 Forward to white LM forward, RM forward FL white: goto 3
FR white: goto 4
3 Straighten right LM stop, RM forward FR white: goto 5
4 Straighten left LM forward, RM stop FL white: goto 5
5 Reverse to centre LM back, RM back 2.3 seconds elapsed: goto 6
6 Spin LM back, RM forward 5 seconds elapsed: goto 2

Notes:

  • LM is left motor, RM is right motor
  • FL is front left colour sensor, FR is front right colour sensor
  • The 2.3 second delay in state 5 is the estimated time taken for my fictional robot to reverse from the table edge into the centre. For a real robot, this value should be determined experimentally.

This is the complete C code for the state machine example from today’s lecture:

//
// Sumo example for MSP430G2553 / MSP430G2452
// Written by Ted Burke - Last modified 29-3-2017
//
// P1.1 = LF colour sensor - low on black, high on white
// P1.2 = RF colour sensor - low on black, high on white
// P2.0 = LED
// P2.1 = LM forward
// P2.2 = LM reverse
// P2.3 = RM forward
// P2.4 = RM reverse
//
 
#include <msp430.h>
   
void main( void )
{
    WDTCTL = WDTPW + WDTHOLD; // Disable watchdog timer
       
    P1DIR = 0b00000000; // P1.1 is colour sensor
    P2DIR = 0b00011111; // P2.0 is LED, P2.1-4 are motors
     
    // State machine
    int state = 1;
    int FLS, FRS; // front left and right colour sensors
    
    while(1)
    {
        // Read front left colour sensor
        if (P1IN & BIT1) FLS = 1;
        else FLS = 0;
        
        // Read front right colour sensor
        if (P1IN & BIT2) FRS = 1;
        else FRS = 0;
        
        // Implement current state
        if (state == 1)
        {
            P2OUT = 0b00000000; // LM stop, RM stop
            
            __delay_cycles(2000000);
            state = 2; // move to state 2 after 2 seconds
        }
        else if (state == 2)
        {
            P2OUT = 0b00001010; // LM forward, RM forward
            
            if (FLS == 1) state = 3;
            if (FRS == 1) state = 4;
        }
        else if (state == 3)
        {
            P2OUT = 0b00001000; // LM stop, RM forward
            
            if (FRS == 1) state = 5;
        }
        else if (state == 4)
        {
            P2OUT = 0b00000010; // LM forward, RM stop
            
            if (FLS == 1) state = 5;
        }
        else if (state == 5)
        {
            P2OUT = 0b00010100; // LM back, RM back
            
            __delay_cycles(2300000);
            state = 6;
        }
        else if (state == 6)
        {
            P2OUT = 0b00001100; // LM back, RM forward
            
            __delay_cycles(5000000);
            state = 2;
        }
    }
}

The End

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