State Machine example code from class (two versions)

These are the two versions of the (incomplete) state machine example we looked at in today’s class. The first uses am if-else-if structure within the main state machine loop, whereas the second uses a separate while loop for each state, all enclosed within the main state machine while loop.

We’ll continue adding to this example in tomorrow’s class.

Firstly, here’s the if-else-if version:

//
// State machine example
//

// Function prototypes
void setup();
void set_motor_directions(int lmd, int rmd); // left and right motor directions
int read_analog_channel(int channel); // read colour sensor (FLS:0, FRS:1, BLS:2, BRS:3)

int main()
{
    setup(); // Configure pins etc.
    
    int fls, frs, bls, brs;
    int state = 1;
    
    // State machine loop
    while(1)
    {
        // Read all sensors into variables
        fls = read_analog_channel(0);
        frs = read_analog_channel(1);
        bls = read_analog_channel(2);
        brs = read_analog_channel(3);
        
        // Implement current state
        if (state == 1)
        {
            // Set motors
            set_motor_directions(1, 1);
            
            // Change state?
            if (fls > 450 && fls < 760) state = 2; // FLS is green if (frs > 450 && frs < 760) state = 3; // FRS is green if (fls > 450 && fls < 760 && frs > 450 && frs < 760) state = 8; if (frs > 760) state = 4; // FRS is white
            if (fls > 760) state = 5; // FLS is white
        }
        else if (state == 2)
        {
            // Set motors
            set_motor_directions(1, 0);
            
            // Change state?
            if (fls < 450 || fls > 760 ) state = 1; // FLS not green
        }
        else if (state == 3)
        {
            // Set motors
            set_motor_directions(0, 1);
            
            // Change state?
            if (frs < 450 || frs > 760 ) state = 1; // FLS not green
        }
        // etc etc
    }
    
    return 0;
}

Secondly, here’s the while-while-while version:

//
// State machine example
//

// Function prototypes
void setup();
void set_motor_directions(int lmd, int rmd); // left and right motor directions
int read_analog_channel(int channel); // read colour sensor (FLS:0, FRS:1, BLS:2, BRS:3)

int main()
{
    setup(); // Configure pins etc.
    
    int fls, frs, bls, brs;
    int state = 1;
    
    // State machine loop
    while(1)
    {
        // Implement current state
        while (state == 1)
        {
            // Set motors
            set_motor_directions(1, 1);
            
            // Change state?
            fls = read_analog_channel(0);
            frs = read_analog_channel(1);
            if (fls > 450 && fls < 760) state = 2; // FLS is green if (frs > 450 && frs < 760) state = 3; // FRS is green if (fls > 450 && fls < 760 && frs > 450 && frs < 760) state = 8; if (frs > 760) state = 4; // FRS is white
            if (fls > 760) state = 5; // FLS is white
        }
        
        while (state == 2)
        {
            // Set motors
            set_motor_directions(1, 0);
            
            // Change state?
            fls = read_analog_channel(0);
            if (fls < 450 || fls > 760 ) state = 1; // FLS not green
        }
        
        while (state == 3)
        {
            // Set motors
            set_motor_directions(0, 1);
            
            // Change state?
            frs = read_analog_channel(1);
            if (frs < 450 || frs > 760 ) state = 1; // FLS not green
        }
        
        // etc etc
    }
    
    return 0;
}
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

1 Response to State Machine example code from class (two versions)

  1. Pingback: The PROJECT(s) | modestasjblog

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s