Executes commands received via xbee.

Dependencies:   m3pi mbed

main.cpp

Committer:
bc6599
Date:
2015-04-30
Revision:
3:042a104c558f
Parent:
2:ce95322fe535
Child:
4:c7a00aca4520

File content as of revision 3:042a104c558f:

#include "mbed.h"
#include "m3pi.h"

Serial xbee(p28, p27);
m3pi m3pi;

void set_command();
void execute();

// Once we're finished, these can go in a separate header file:

const int WORDSET1[4] = {1, 2, 3, 6};
const int WORDSET2[4] = {0, 1, 4, 5};

int args[3] = {6, 0, 0}; // Initial arguments for wordsets 1-3

float speed;

int main()
{
    m3pi.cls();

    while (1) {
        set_command();
        // m3pi.locate(0,1);
        // m3pi.printf("%f", m3pi.battery());
        // m3pi.printf("%c",command);
        execute();
    }
}

void set_command()
{
    if (xbee.readable())
        m3pi.scanf("|%i,%i,%i@", &args[0], &args[1], &args[2]);
}

void execute()
{
    speed = 0.25; // Default initial speed
    
    // If first argument...
    switch (args[0]) {
        case 1:                           // is "move",
            if (args[1] == WORDSET2[2]) {           // only proceed if second argument is "forward",
                m3pi.forward(speed);
            } else if (args[1] == WORDSET2[3]) {    // or if second argument is "backward"
                m3pi.backward(speed);
            } break;
            
        case 2:                           // is "turn",
            if (args[1] == WORDSET2[0]) {           // only proceed if second argument is "left",
                m3pi.left(speed);
            } else if (args[1] == WORDSET2[1]) {    // or if second argument is "right"
                m3pi.right(speed);
            } break;
            
        case 3:                           // is "run",
            if (args[1] == WORDSET2[2]) {           // only proceed if second argument is "forward",
                speed *= 2;                         // (then, double speed)
                m3pi.forward(speed);
            } else if (args[1] == WORDSET2[3]) {    // or if second argument is "backward"
                speed *= 2;                         // (then, double speed)
                m3pi.backward(speed);
            } break;
            
        case 6:                           // is "stop",
            m3pi.stop();
            break;                                  // do nothing (robot will stop once out of the switch block)
    }
    
    wait(args[2]);                                  // Set execution time of command
    m3pi.stop();
}