Executes commands received via xbee.

Dependencies:   m3pi mbed

Committer:
bc6599
Date:
Tue Apr 21 15:48:31 2015 +0000
Revision:
1:8b83b8a03351
Parent:
0:98be52da5242
Child:
2:ce95322fe535
Added capacity for storing previous command

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bc6599 0:98be52da5242 1 #include "mbed.h"
bc6599 0:98be52da5242 2 #include "m3pi.h"
bc6599 0:98be52da5242 3
bc6599 0:98be52da5242 4 Serial xbee(p28, p27);
bc6599 0:98be52da5242 5 m3pi m3pi;
bc6599 0:98be52da5242 6
bc6599 0:98be52da5242 7 void execute();
bc6599 0:98be52da5242 8 void set_command();
bc6599 0:98be52da5242 9
bc6599 1:8b83b8a03351 10 //once we're finished, these can go in a separate header file.
bc6599 0:98be52da5242 11 const char LEFT = 'A';
bc6599 0:98be52da5242 12 const char RIGHT = 'B';
bc6599 0:98be52da5242 13 const char STOP = 'D';
bc6599 0:98be52da5242 14 const char FORWARD = 'E';
bc6599 0:98be52da5242 15 const char BACKWARD = 'F';
bc6599 1:8b83b8a03351 16 const float EX_TIME = 1.0;
bc6599 0:98be52da5242 17
bc6599 0:98be52da5242 18 char command;
bc6599 1:8b83b8a03351 19 char prev_command; //in case we need a command history
bc6599 0:98be52da5242 20 float speed;
bc6599 0:98be52da5242 21
bc6599 0:98be52da5242 22 int main() {
bc6599 1:8b83b8a03351 23 prev_command = STOP;
bc6599 0:98be52da5242 24 command = STOP;
bc6599 0:98be52da5242 25 speed = 0.3;
bc6599 1:8b83b8a03351 26 m3pi.cls();
bc6599 0:98be52da5242 27
bc6599 0:98be52da5242 28 while (1) {
bc6599 1:8b83b8a03351 29 //set_command();
bc6599 0:98be52da5242 30 m3pi.locate(0,1);
bc6599 1:8b83b8a03351 31 m3pi.printf("%f", m3pi.battery());
bc6599 1:8b83b8a03351 32 //m3pi.printf("%c",command);
bc6599 1:8b83b8a03351 33 //execute();
bc6599 0:98be52da5242 34 }
bc6599 0:98be52da5242 35 }
bc6599 0:98be52da5242 36
bc6599 0:98be52da5242 37 void execute() {
bc6599 0:98be52da5242 38 switch (command) {
bc6599 0:98be52da5242 39 case LEFT:
bc6599 0:98be52da5242 40 m3pi.left(speed); break;
bc6599 0:98be52da5242 41 case RIGHT:
bc6599 0:98be52da5242 42 m3pi.right(speed); break;
bc6599 0:98be52da5242 43 case STOP:
bc6599 0:98be52da5242 44 m3pi.stop(); break;
bc6599 0:98be52da5242 45 case FORWARD:
bc6599 0:98be52da5242 46 m3pi.forward(speed); break;
bc6599 0:98be52da5242 47 case BACKWARD:
bc6599 0:98be52da5242 48 m3pi.backward(speed); break;
bc6599 0:98be52da5242 49 }
bc6599 0:98be52da5242 50 }
bc6599 0:98be52da5242 51
bc6599 0:98be52da5242 52 void set_command() {
bc6599 0:98be52da5242 53 if (xbee.readable()) {
bc6599 1:8b83b8a03351 54 prev_command = command;
bc6599 0:98be52da5242 55 command = xbee.getc();
bc6599 1:8b83b8a03351 56 wait(EX_TIME);
bc6599 0:98be52da5242 57 }
bc6599 0:98be52da5242 58 }