CSCI 5360 JB_MB_BC
/
m3pi_reciever
Executes commands received via xbee.
main.cpp@5:854aea46d7b8, 2015-05-03 (annotated)
- Committer:
- mb4899
- Date:
- Sun May 03 01:20:15 2015 +0000
- Revision:
- 5:854aea46d7b8
- Parent:
- 4:c7a00aca4520
Added "Protocol" header file and updated "main" file accordingly
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bc6599 | 0:98be52da5242 | 1 | #include "mbed.h" |
bc6599 | 0:98be52da5242 | 2 | #include "m3pi.h" |
mb4899 | 5:854aea46d7b8 | 3 | #include "Protocol.h" |
bc6599 | 0:98be52da5242 | 4 | |
bc6599 | 0:98be52da5242 | 5 | Serial xbee(p28, p27); |
bc6599 | 0:98be52da5242 | 6 | m3pi m3pi; |
bc6599 | 0:98be52da5242 | 7 | |
mb4899 | 2:ce95322fe535 | 8 | void set_command(); |
bc6599 | 0:98be52da5242 | 9 | void execute(); |
mb4899 | 2:ce95322fe535 | 10 | |
mb4899 | 5:854aea46d7b8 | 11 | int commands[3]; |
bc6599 | 0:98be52da5242 | 12 | |
mb4899 | 2:ce95322fe535 | 13 | int main() |
mb4899 | 2:ce95322fe535 | 14 | { |
bc6599 | 1:8b83b8a03351 | 15 | m3pi.cls(); |
bc6599 | 4:c7a00aca4520 | 16 | |
mb4899 | 5:854aea46d7b8 | 17 | // Set initial "default" commands for m3pi |
mb4899 | 5:854aea46d7b8 | 18 | // Action (stop), Direction (left), and Duration (zero), respectively |
mb4899 | 5:854aea46d7b8 | 19 | commands[0] = 6; commands[1] = 0; commands[2] = 0; |
bc6599 | 4:c7a00aca4520 | 20 | |
bc6599 | 0:98be52da5242 | 21 | while (1) { |
bc6599 | 4:c7a00aca4520 | 22 | if (xbee.readable()) { |
mb4899 | 5:854aea46d7b8 | 23 | xbee.scanf("|%i,%i,%i@", &commands[0], &commands[1], &commands[2]); |
bc6599 | 4:c7a00aca4520 | 24 | } |
mb4899 | 5:854aea46d7b8 | 25 | |
bc6599 | 4:c7a00aca4520 | 26 | m3pi.locate(0,1); |
mb4899 | 5:854aea46d7b8 | 27 | m3pi.printf("%i,%i,%i", commands[0], commands[1], commands[2]); |
mb4899 | 2:ce95322fe535 | 28 | execute(); |
bc6599 | 0:98be52da5242 | 29 | } |
bc6599 | 0:98be52da5242 | 30 | } |
bc6599 | 0:98be52da5242 | 31 | |
mb4899 | 2:ce95322fe535 | 32 | void execute() |
mb4899 | 2:ce95322fe535 | 33 | { |
mb4899 | 5:854aea46d7b8 | 34 | float speed = 0.25; // Default initial speed |
mb4899 | 5:854aea46d7b8 | 35 | float turn_speed = 0.125; // Default turn speed |
mb4899 | 2:ce95322fe535 | 36 | |
mb4899 | 5:854aea46d7b8 | 37 | // For ease in switch block readability |
mb4899 | 5:854aea46d7b8 | 38 | int action = commands[0]; |
mb4899 | 5:854aea46d7b8 | 39 | int direction = commands[1]; |
mb4899 | 5:854aea46d7b8 | 40 | int duration = commands[2]; |
mb4899 | 5:854aea46d7b8 | 41 | |
mb4899 | 5:854aea46d7b8 | 42 | switch (action) { |
mb4899 | 5:854aea46d7b8 | 43 | case MOVE: |
mb4899 | 5:854aea46d7b8 | 44 | if (direction == FORWARD) { |
bc6599 | 3:042a104c558f | 45 | m3pi.forward(speed); |
mb4899 | 5:854aea46d7b8 | 46 | } else if (direction == LEFT) { |
mb4899 | 5:854aea46d7b8 | 47 | m3pi.left(turn_speed); |
mb4899 | 5:854aea46d7b8 | 48 | m3pi.forward(speed); |
mb4899 | 5:854aea46d7b8 | 49 | } else if (direction == RIGHT) { |
mb4899 | 5:854aea46d7b8 | 50 | m3pi.right(turn_speed); |
mb4899 | 5:854aea46d7b8 | 51 | m3pi.forward(speed); |
mb4899 | 5:854aea46d7b8 | 52 | } else if (direction == BACKWARD) { |
bc6599 | 3:042a104c558f | 53 | m3pi.backward(speed); |
mb4899 | 2:ce95322fe535 | 54 | } break; |
mb4899 | 2:ce95322fe535 | 55 | |
mb4899 | 5:854aea46d7b8 | 56 | case TURN: |
mb4899 | 5:854aea46d7b8 | 57 | if (direction == LEFT) { |
bc6599 | 4:c7a00aca4520 | 58 | m3pi.left(turn_speed); |
mb4899 | 5:854aea46d7b8 | 59 | } else if (direction == RIGHT) { |
bc6599 | 4:c7a00aca4520 | 60 | m3pi.right(turn_speed); |
mb4899 | 2:ce95322fe535 | 61 | } break; |
mb4899 | 2:ce95322fe535 | 62 | |
mb4899 | 5:854aea46d7b8 | 63 | case RUN: |
mb4899 | 5:854aea46d7b8 | 64 | if (direction == FORWARD) { |
mb4899 | 5:854aea46d7b8 | 65 | speed *= 2; |
bc6599 | 3:042a104c558f | 66 | m3pi.forward(speed); |
mb4899 | 5:854aea46d7b8 | 67 | } else if (direction == LEFT) { |
mb4899 | 5:854aea46d7b8 | 68 | m3pi.left(turn_speed); |
mb4899 | 5:854aea46d7b8 | 69 | speed *= 2; |
mb4899 | 5:854aea46d7b8 | 70 | m3pi.forward(speed); |
mb4899 | 5:854aea46d7b8 | 71 | } else if (direction == RIGHT) { |
mb4899 | 5:854aea46d7b8 | 72 | m3pi.right(turn_speed); |
mb4899 | 5:854aea46d7b8 | 73 | speed *= 2; |
mb4899 | 5:854aea46d7b8 | 74 | m3pi.forward(speed); |
mb4899 | 5:854aea46d7b8 | 75 | } else if (direction == BACKWARD) { |
mb4899 | 5:854aea46d7b8 | 76 | speed *= 2; |
bc6599 | 3:042a104c558f | 77 | m3pi.backward(speed); |
mb4899 | 2:ce95322fe535 | 78 | } break; |
mb4899 | 2:ce95322fe535 | 79 | |
mb4899 | 5:854aea46d7b8 | 80 | case STOP: |
bc6599 | 3:042a104c558f | 81 | m3pi.stop(); |
mb4899 | 5:854aea46d7b8 | 82 | break; |
bc6599 | 0:98be52da5242 | 83 | } |
mb4899 | 2:ce95322fe535 | 84 | |
mb4899 | 5:854aea46d7b8 | 85 | wait(duration); |
mb4899 | 2:ce95322fe535 | 86 | m3pi.stop(); |
mb4899 | 5:854aea46d7b8 | 87 | commands[0] = 6; commands[1] = 0; commands[2] = 0; // Reset "defaults" |
bc6599 | 0:98be52da5242 | 88 | } |