CSCI 5360 JB_MB_BC
/
m3pi_reciever
Executes commands received via xbee.
Revision 5:854aea46d7b8, committed 2015-05-03
- Comitter:
- mb4899
- Date:
- Sun May 03 01:20:15 2015 +0000
- Parent:
- 4:c7a00aca4520
- Commit message:
- Added "Protocol" header file and updated "main" file accordingly
Changed in this revision
Protocol.h | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Protocol.h Sun May 03 01:20:15 2015 +0000 @@ -0,0 +1,16 @@ +#ifndef PROTOCOL_H +#define PROTOCOL_H + +// Commands from Wordset 1 +#define MOVE 1 +#define TURN 2 +#define RUN 3 +#define STOP 6 + +// Commands from Wordset 2 +#define LEFT 0 +#define RIGHT 1 +#define FORWARD 4 +#define BACKWARD 5 + +#endif \ No newline at end of file
--- a/main.cpp Thu Apr 30 01:40:02 2015 +0000 +++ b/main.cpp Sun May 03 01:20:15 2015 +0000 @@ -1,5 +1,6 @@ #include "mbed.h" #include "m3pi.h" +#include "Protocol.h" Serial xbee(p28, p27); m3pi m3pi; @@ -7,70 +8,81 @@ 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 -int command1; -int command2; -int command3; +int commands[3]; int main() { m3pi.cls(); - command1 = 6; - command2 = 0; - command3 = 0; + // Set initial "default" commands for m3pi + // Action (stop), Direction (left), and Duration (zero), respectively + commands[0] = 6; commands[1] = 0; commands[2] = 0; while (1) { if (xbee.readable()) { - xbee.scanf("|%i,%i,%i@", &command1, &command2, &command3); + xbee.scanf("|%i,%i,%i@", &commands[0], &commands[1], &commands[2]); } + m3pi.locate(0,1); - m3pi.printf("%i,%i,%i", command1, command2, command3); + m3pi.printf("%i,%i,%i", commands[0], commands[1], commands[2]); execute(); } } void execute() { - float speed = 0.25; // Default initial speed - float turn_speed = 0.125; // Default turn speed + float speed = 0.25; // Default initial speed + float turn_speed = 0.125; // Default turn speed - // If first argument... - switch (command1) { - case 1: // is "move", - if (command2 == WORDSET2[2]) { // only proceed if second argument is "forward", + // For ease in switch block readability + int action = commands[0]; + int direction = commands[1]; + int duration = commands[2]; + + switch (action) { + case MOVE: + if (direction == FORWARD) { m3pi.forward(speed); - } else if (command2 == WORDSET2[3]) { // or if second argument is "backward" + } else if (direction == LEFT) { + m3pi.left(turn_speed); + m3pi.forward(speed); + } else if (direction == RIGHT) { + m3pi.right(turn_speed); + m3pi.forward(speed); + } else if (direction == BACKWARD) { m3pi.backward(speed); } break; - case 2: // is "turn", - if (command2 == WORDSET2[0]) { // only proceed if second argument is "left", + case TURN: + if (direction == LEFT) { m3pi.left(turn_speed); - } else if (command2 == WORDSET2[1]) { // or if second argument is "right" + } else if (direction == RIGHT) { m3pi.right(turn_speed); } break; - case 3: // is "run", - if (command2 == WORDSET2[2]) { // only proceed if second argument is "forward", - speed *= 2; // (then, double speed) + case RUN: + if (direction == FORWARD) { + speed *= 2; m3pi.forward(speed); - } else if (command2 == WORDSET2[3]) { // or if second argument is "backward" - speed *= 2; // (then, double speed) + } else if (direction == LEFT) { + m3pi.left(turn_speed); + speed *= 2; + m3pi.forward(speed); + } else if (direction == RIGHT) { + m3pi.right(turn_speed); + speed *= 2; + m3pi.forward(speed); + } else if (direction == BACKWARD) { + speed *= 2; m3pi.backward(speed); } break; - case 6: // is "stop", + case STOP: m3pi.stop(); - break; // do nothing (robot will stop once out of the switch block) + break; } - wait(command3); // Set execution time of command + wait(duration); m3pi.stop(); - command1 = 6; command2 = 0; command3 = 0; + commands[0] = 6; commands[1] = 0; commands[2] = 0; // Reset "defaults" } \ No newline at end of file