Narendra Joshi
/
robots
We wrote this for controlling our bots in a game designed for the ARM design challenge
main.cpp@0:2f73cbc3f754, 2013-10-27 (annotated)
- Committer:
- narendraj9
- Date:
- Sun Oct 27 09:19:15 2013 +0000
- Revision:
- 0:2f73cbc3f754
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
narendraj9 | 0:2f73cbc3f754 | 1 | #include "mbed.h" |
narendraj9 | 0:2f73cbc3f754 | 2 | #include "Robo.h" |
narendraj9 | 0:2f73cbc3f754 | 3 | |
narendraj9 | 0:2f73cbc3f754 | 4 | |
narendraj9 | 0:2f73cbc3f754 | 5 | // robot object |
narendraj9 | 0:2f73cbc3f754 | 6 | Robo roger(p20, p19, p18, |
narendraj9 | 0:2f73cbc3f754 | 7 | p21, p22, p23, |
narendraj9 | 0:2f73cbc3f754 | 8 | p24, p25, p27, p28, p29, p30); |
narendraj9 | 0:2f73cbc3f754 | 9 | |
narendraj9 | 0:2f73cbc3f754 | 10 | DigitalOut myled(LED2); |
narendraj9 | 0:2f73cbc3f754 | 11 | DigitalOut left(LED3); // status left LED |
narendraj9 | 0:2f73cbc3f754 | 12 | DigitalOut right(LED4); // status right LED |
narendraj9 | 0:2f73cbc3f754 | 13 | |
narendraj9 | 0:2f73cbc3f754 | 14 | // a buzzer when a life decreses |
narendraj9 | 0:2f73cbc3f754 | 15 | DigitalOut buzzer(p7); |
narendraj9 | 0:2f73cbc3f754 | 16 | |
narendraj9 | 0:2f73cbc3f754 | 17 | //the rf receiver Serial object |
narendraj9 | 0:2f73cbc3f754 | 18 | Serial rx(p9, p10); |
narendraj9 | 0:2f73cbc3f754 | 19 | |
narendraj9 | 0:2f73cbc3f754 | 20 | //a rise on this pin reduces a life of the robot |
narendraj9 | 0:2f73cbc3f754 | 21 | InterruptIn heart(p6); |
narendraj9 | 0:2f73cbc3f754 | 22 | |
narendraj9 | 0:2f73cbc3f754 | 23 | /* a wrapper function for roger.devLives |
narendraj9 | 0:2f73cbc3f754 | 24 | * which can be passed as argument to some other funtion |
narendraj9 | 0:2f73cbc3f754 | 25 | */ |
narendraj9 | 0:2f73cbc3f754 | 26 | void decRoboLives() { |
narendraj9 | 0:2f73cbc3f754 | 27 | roger.decLives(); |
narendraj9 | 0:2f73cbc3f754 | 28 | buzzer = 0; |
narendraj9 | 0:2f73cbc3f754 | 29 | } |
narendraj9 | 0:2f73cbc3f754 | 30 | |
narendraj9 | 0:2f73cbc3f754 | 31 | int main() { |
narendraj9 | 0:2f73cbc3f754 | 32 | char command = 'S'; |
narendraj9 | 0:2f73cbc3f754 | 33 | rx.baud(2400); |
narendraj9 | 0:2f73cbc3f754 | 34 | myled = 1; // test led. test: program loaded successfully |
narendraj9 | 0:2f73cbc3f754 | 35 | heart.rise(decRoboLives); |
narendraj9 | 0:2f73cbc3f754 | 36 | |
narendraj9 | 0:2f73cbc3f754 | 37 | /* main while loop |
narendraj9 | 0:2f73cbc3f754 | 38 | * 1. wait for instructions |
narendraj9 | 0:2f73cbc3f754 | 39 | * 1. read instruction |
narendraj9 | 0:2f73cbc3f754 | 40 | * 2. do the required action |
narendraj9 | 0:2f73cbc3f754 | 41 | * 3. empty the rx object by reading further |
narendraj9 | 0:2f73cbc3f754 | 42 | * 4. go to 1 |
narendraj9 | 0:2f73cbc3f754 | 43 | * we introduced redundancy in sending the instructions so as to make |
narendraj9 | 0:2f73cbc3f754 | 44 | * sure they really end up being received by the robots. that's the raison d'etre |
narendraj9 | 0:2f73cbc3f754 | 45 | * for step 3. |
narendraj9 | 0:2f73cbc3f754 | 46 | */ |
narendraj9 | 0:2f73cbc3f754 | 47 | while(1) { |
narendraj9 | 0:2f73cbc3f754 | 48 | if (rx.readable()) { |
narendraj9 | 0:2f73cbc3f754 | 49 | command = rx.getc(); |
narendraj9 | 0:2f73cbc3f754 | 50 | switch(command) { |
narendraj9 | 0:2f73cbc3f754 | 51 | case 'L' : |
narendraj9 | 0:2f73cbc3f754 | 52 | left = 1; |
narendraj9 | 0:2f73cbc3f754 | 53 | right = 0; |
narendraj9 | 0:2f73cbc3f754 | 54 | roger.goLeft(); |
narendraj9 | 0:2f73cbc3f754 | 55 | break; |
narendraj9 | 0:2f73cbc3f754 | 56 | case 'R': |
narendraj9 | 0:2f73cbc3f754 | 57 | right = 1; |
narendraj9 | 0:2f73cbc3f754 | 58 | left = 0; |
narendraj9 | 0:2f73cbc3f754 | 59 | roger.moveRight(); |
narendraj9 | 0:2f73cbc3f754 | 60 | break; |
narendraj9 | 0:2f73cbc3f754 | 61 | case 'M': |
narendraj9 | 0:2f73cbc3f754 | 62 | roger.goAhead(); |
narendraj9 | 0:2f73cbc3f754 | 63 | break; |
narendraj9 | 0:2f73cbc3f754 | 64 | case 'B': |
narendraj9 | 0:2f73cbc3f754 | 65 | roger.moveBack(); |
narendraj9 | 0:2f73cbc3f754 | 66 | break; |
narendraj9 | 0:2f73cbc3f754 | 67 | case 'S': |
narendraj9 | 0:2f73cbc3f754 | 68 | roger.stop(); |
narendraj9 | 0:2f73cbc3f754 | 69 | break; |
narendraj9 | 0:2f73cbc3f754 | 70 | case 'D': |
narendraj9 | 0:2f73cbc3f754 | 71 | roger.printlcd("Hi"); |
narendraj9 | 0:2f73cbc3f754 | 72 | wait(1); |
narendraj9 | 0:2f73cbc3f754 | 73 | roger.goLeft(); |
narendraj9 | 0:2f73cbc3f754 | 74 | roger.moveRight(); |
narendraj9 | 0:2f73cbc3f754 | 75 | roger.goLeft(); |
narendraj9 | 0:2f73cbc3f754 | 76 | roger.moveRight(); |
narendraj9 | 0:2f73cbc3f754 | 77 | roger.goAhead(); |
narendraj9 | 0:2f73cbc3f754 | 78 | wait(1); |
narendraj9 | 0:2f73cbc3f754 | 79 | roger.moveBack(); |
narendraj9 | 0:2f73cbc3f754 | 80 | wait(1); |
narendraj9 | 0:2f73cbc3f754 | 81 | roger.stop(); |
narendraj9 | 0:2f73cbc3f754 | 82 | default: |
narendraj9 | 0:2f73cbc3f754 | 83 | roger.printlcd("Waiting... "); |
narendraj9 | 0:2f73cbc3f754 | 84 | break; |
narendraj9 | 0:2f73cbc3f754 | 85 | |
narendraj9 | 0:2f73cbc3f754 | 86 | } |
narendraj9 | 0:2f73cbc3f754 | 87 | while (rx.readable()) |
narendraj9 | 0:2f73cbc3f754 | 88 | command = rx.getc(); |
narendraj9 | 0:2f73cbc3f754 | 89 | } |
narendraj9 | 0:2f73cbc3f754 | 90 | } |
narendraj9 | 0:2f73cbc3f754 | 91 | } |