Executes commands received via xbee.

Dependencies:   m3pi mbed

Committer:
mb4899
Date:
Thu Apr 30 00:09:53 2015 +0000
Revision:
2:ce95322fe535
Parent:
1:8b83b8a03351
Child:
3:042a104c558f
Code for m3pi_receiver

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
mb4899 2:ce95322fe535 7 void set_command();
bc6599 0:98be52da5242 8 void execute();
mb4899 2:ce95322fe535 9
mb4899 2:ce95322fe535 10 // Once we're finished, these can go in a separate header file:
bc6599 0:98be52da5242 11
mb4899 2:ce95322fe535 12 const int[4] WORDSET1 = {1, 2, 3, 6};
mb4899 2:ce95322fe535 13 const int[4] WORDSET2 = {0, 1, 4, 5};
mb4899 2:ce95322fe535 14 const int[11] WORDSET3 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
bc6599 0:98be52da5242 15
mb4899 2:ce95322fe535 16 const int[3] ARGS = {6, 0, 0}; // Initial arguments for wordsets 1-3
mb4899 2:ce95322fe535 17
bc6599 0:98be52da5242 18 float speed;
bc6599 0:98be52da5242 19
mb4899 2:ce95322fe535 20 int main()
mb4899 2:ce95322fe535 21 {
bc6599 1:8b83b8a03351 22 m3pi.cls();
mb4899 2:ce95322fe535 23
bc6599 0:98be52da5242 24 while (1) {
mb4899 2:ce95322fe535 25 set_command();
mb4899 2:ce95322fe535 26 // m3pi.locate(0,1);
mb4899 2:ce95322fe535 27 // m3pi.printf("%f", m3pi.battery());
mb4899 2:ce95322fe535 28 // m3pi.printf("%c",command);
mb4899 2:ce95322fe535 29 execute();
bc6599 0:98be52da5242 30 }
bc6599 0:98be52da5242 31 }
bc6599 0:98be52da5242 32
mb4899 2:ce95322fe535 33 void set_command()
mb4899 2:ce95322fe535 34 {
mb4899 2:ce95322fe535 35 if (xbee.readable())
mb4899 2:ce95322fe535 36 m3pi.scanf("|%i,%i,%i@", &ARGS[0], &ARGS[1], &ARGS[2]);
bc6599 0:98be52da5242 37 }
bc6599 0:98be52da5242 38
mb4899 2:ce95322fe535 39 void execute()
mb4899 2:ce95322fe535 40 {
mb4899 2:ce95322fe535 41 speed = 0.25; // Default initial speed
mb4899 2:ce95322fe535 42
mb4899 2:ce95322fe535 43 // If first argument...
mb4899 2:ce95322fe535 44 switch (ARGS[0]) {
mb4899 2:ce95322fe535 45 case WORDSET1[0]: // is "move",
mb4899 2:ce95322fe535 46 if (ARGS[1] == WORDSET1[2]) { // only proceed if second argument is "forward",
mb4899 2:ce95322fe535 47 m3pi.forward();
mb4899 2:ce95322fe535 48 } else if (ARGS[1] == WORDSET1[3]) { // or if second argument is "backward"
mb4899 2:ce95322fe535 49 m3pi.backward();
mb4899 2:ce95322fe535 50 } break;
mb4899 2:ce95322fe535 51
mb4899 2:ce95322fe535 52 case WORDSET1[1]: // is "turn",
mb4899 2:ce95322fe535 53 if (ARGS[1] == WORDSET1[0]) { // only proceed if second argument is "left",
mb4899 2:ce95322fe535 54 m3pi.turn();
mb4899 2:ce95322fe535 55 m3pi.left();
mb4899 2:ce95322fe535 56 } else if (ARGS[1] == WORDSET1[1]) { // or if second argument is "right"
mb4899 2:ce95322fe535 57 m3pi.turn();
mb4899 2:ce95322fe535 58 m3pi.right();
mb4899 2:ce95322fe535 59 } break;
mb4899 2:ce95322fe535 60
mb4899 2:ce95322fe535 61 case WORDSET1[2]: // is "run",
mb4899 2:ce95322fe535 62 if (ARGS[1] == WORDSET1[2]) { // only proceed if second argument is "forward",
mb4899 2:ce95322fe535 63 speed *= 2; // (then, double speed)
mb4899 2:ce95322fe535 64 m3pi.forward();
mb4899 2:ce95322fe535 65 } else if (ARGS[1] == WORDSET1[3]) { // or if second argument is "backward"
mb4899 2:ce95322fe535 66 speed *= 2; // (then, double speed)
mb4899 2:ce95322fe535 67 m3pi.backward();
mb4899 2:ce95322fe535 68 } break;
mb4899 2:ce95322fe535 69
mb4899 2:ce95322fe535 70 case WORDSET1[3]: // is "stop",
mb4899 2:ce95322fe535 71 break; // do nothing (robot will stop once out of the switch block)
bc6599 0:98be52da5242 72 }
mb4899 2:ce95322fe535 73
mb4899 2:ce95322fe535 74 wait(ARGS[2]); // Set execution time of command
mb4899 2:ce95322fe535 75 m3pi.stop();
bc6599 0:98be52da5242 76 }