Executes commands received via xbee.

Dependencies:   m3pi mbed

Committer:
bc6599
Date:
Thu Apr 30 00:26:33 2015 +0000
Revision:
3:042a104c558f
Parent:
2:ce95322fe535
Child:
4:c7a00aca4520
Compiles now

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