Martin Werluschnig
/
BertlDrive_V2
BertlDrive_V2
main.cpp@4:ae6da6b26fc9, 2018-11-15 (annotated)
- Committer:
- martwerl
- Date:
- Thu Nov 15 17:28:37 2018 +0000
- Revision:
- 4:ae6da6b26fc9
- Parent:
- 2:1115b3fa7fde
BertlDrive_V2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DoTTi | 0:6d28be2dcb7c | 1 | #include "mbed.h" |
DoTTi | 0:6d28be2dcb7c | 2 | #include "const.h" |
DoTTi | 0:6d28be2dcb7c | 3 | #include "ur_Bertl.h" |
DoTTi | 0:6d28be2dcb7c | 4 | |
bulmecisco | 2:1115b3fa7fde | 5 | Serial bt(p9, p10); |
bulmecisco | 2:1115b3fa7fde | 6 | |
bulmecisco | 2:1115b3fa7fde | 7 | const int blinkTime = 200; // ms |
bulmecisco | 2:1115b3fa7fde | 8 | |
bulmecisco | 2:1115b3fa7fde | 9 | class BlinkBertl : public Bertl |
bulmecisco | 2:1115b3fa7fde | 10 | { |
bulmecisco | 2:1115b3fa7fde | 11 | public: |
bulmecisco | 2:1115b3fa7fde | 12 | void BlinkLeftLed(); |
bulmecisco | 2:1115b3fa7fde | 13 | void BlinkRigthLed(); |
bulmecisco | 2:1115b3fa7fde | 14 | }; |
bulmecisco | 2:1115b3fa7fde | 15 | |
bulmecisco | 2:1115b3fa7fde | 16 | void BlinkBertl::BlinkLeftLed() |
bulmecisco | 2:1115b3fa7fde | 17 | { |
bulmecisco | 2:1115b3fa7fde | 18 | for(int i = 0; i < 3; i++) { |
bulmecisco | 2:1115b3fa7fde | 19 | TurnLedOn(LED_FL2 | LED_BL1); |
bulmecisco | 2:1115b3fa7fde | 20 | wait_ms(blinkTime); |
bulmecisco | 2:1115b3fa7fde | 21 | TurnLedOff(LED_ALL); |
bulmecisco | 2:1115b3fa7fde | 22 | wait_ms(blinkTime); |
bulmecisco | 2:1115b3fa7fde | 23 | } |
bulmecisco | 2:1115b3fa7fde | 24 | } |
bulmecisco | 2:1115b3fa7fde | 25 | |
bulmecisco | 2:1115b3fa7fde | 26 | void BlinkBertl::BlinkRigthLed() |
bulmecisco | 2:1115b3fa7fde | 27 | { |
bulmecisco | 2:1115b3fa7fde | 28 | for(int i = 0; i < 3; i++) { |
bulmecisco | 2:1115b3fa7fde | 29 | TurnLedOn(LED_FR2 | LED_BR1); |
bulmecisco | 2:1115b3fa7fde | 30 | wait_ms(blinkTime); |
bulmecisco | 2:1115b3fa7fde | 31 | TurnLedOff(LED_ALL); |
bulmecisco | 2:1115b3fa7fde | 32 | wait_ms(blinkTime); |
bulmecisco | 2:1115b3fa7fde | 33 | } |
bulmecisco | 2:1115b3fa7fde | 34 | } |
bulmecisco | 2:1115b3fa7fde | 35 | |
DoTTi | 0:6d28be2dcb7c | 36 | int main() |
DoTTi | 0:6d28be2dcb7c | 37 | { |
bulmecisco | 2:1115b3fa7fde | 38 | char cmd, b1, b3; |
bulmecisco | 2:1115b3fa7fde | 39 | bool turn = false; |
bulmecisco | 2:1115b3fa7fde | 40 | |
bulmecisco | 2:1115b3fa7fde | 41 | BlinkBertl karel; |
bulmecisco | 2:1115b3fa7fde | 42 | |
bulmecisco | 2:1115b3fa7fde | 43 | bt.printf("Kommands 1:Move, 3:TurnLeft, 5:TurnRight, 7:MoveBack, 6 and 8 stepwise turns \n"); |
martwerl | 4:ae6da6b26fc9 | 44 | karel.Move(); |
bulmecisco | 2:1115b3fa7fde | 45 | karel.TurnLeft(); |
bulmecisco | 2:1115b3fa7fde | 46 | karel.TurnRigth(); |
bulmecisco | 2:1115b3fa7fde | 47 | karel.MoveBackwards(); |
martwerl | 4:ae6da6b26fc9 | 48 | |
bulmecisco | 2:1115b3fa7fde | 49 | karel.TurnLedOn(LED_ALL); |
bulmecisco | 2:1115b3fa7fde | 50 | wait_ms(200); |
bulmecisco | 2:1115b3fa7fde | 51 | karel.TurnLedOff(LED_ALL); |
bulmecisco | 2:1115b3fa7fde | 52 | wait_ms(200); |
bulmecisco | 2:1115b3fa7fde | 53 | while(karel.WaitUntilButtonPressed()) {} |
bulmecisco | 2:1115b3fa7fde | 54 | wait(1); |
bulmecisco | 2:1115b3fa7fde | 55 | while(karel.WaitUntilButtonPressed()) |
bulmecisco | 2:1115b3fa7fde | 56 | { |
bulmecisco | 2:1115b3fa7fde | 57 | karel.NibbleLeds(karel.GetLineValues()); |
bulmecisco | 2:1115b3fa7fde | 58 | switch(karel.GetLineValues()) |
bulmecisco | 2:1115b3fa7fde | 59 | { |
bulmecisco | 2:1115b3fa7fde | 60 | case 0x00: |
bulmecisco | 2:1115b3fa7fde | 61 | karel.TurnLeftStep(50); |
bulmecisco | 2:1115b3fa7fde | 62 | break; |
bulmecisco | 2:1115b3fa7fde | 63 | case 0x03: case 0x01: case 0x07: |
bulmecisco | 2:1115b3fa7fde | 64 | karel.TurnLeftStep(20); |
bulmecisco | 2:1115b3fa7fde | 65 | break; |
bulmecisco | 2:1115b3fa7fde | 66 | case 0x0E: case 0x0A: case 0x08: |
bulmecisco | 2:1115b3fa7fde | 67 | karel.TurnRigthStep(20); |
bulmecisco | 2:1115b3fa7fde | 68 | break; |
bulmecisco | 2:1115b3fa7fde | 69 | default: |
bulmecisco | 2:1115b3fa7fde | 70 | karel.Move(20); |
bulmecisco | 2:1115b3fa7fde | 71 | break; |
bulmecisco | 2:1115b3fa7fde | 72 | } |
bulmecisco | 2:1115b3fa7fde | 73 | if(!karel.FrontIsClear()) |
bulmecisco | 2:1115b3fa7fde | 74 | karel.TurnLeftStep(1000); |
bulmecisco | 2:1115b3fa7fde | 75 | } |
bulmecisco | 2:1115b3fa7fde | 76 | while(true) { |
bulmecisco | 2:1115b3fa7fde | 77 | if (bt.readable()) { |
bulmecisco | 2:1115b3fa7fde | 78 | b1 = bt.getc(); |
bulmecisco | 2:1115b3fa7fde | 79 | cmd = bt.getc(); |
bulmecisco | 2:1115b3fa7fde | 80 | b3 = bt.getc(); |
bulmecisco | 2:1115b3fa7fde | 81 | //bt.printf("3 Getchar: %x %x %x\n", b1, cmd ,b3); |
bulmecisco | 2:1115b3fa7fde | 82 | karel.NibbleLeds(karel.GetLineValues()); |
bulmecisco | 2:1115b3fa7fde | 83 | switch (cmd) { |
bulmecisco | 2:1115b3fa7fde | 84 | case '1': |
bulmecisco | 2:1115b3fa7fde | 85 | karel.Move(); |
bulmecisco | 2:1115b3fa7fde | 86 | break; |
bulmecisco | 2:1115b3fa7fde | 87 | case '3': |
bulmecisco | 2:1115b3fa7fde | 88 | karel.TurnLeft(); |
bulmecisco | 2:1115b3fa7fde | 89 | break; |
bulmecisco | 2:1115b3fa7fde | 90 | case '5': |
bulmecisco | 2:1115b3fa7fde | 91 | karel.TurnRigth(); |
bulmecisco | 2:1115b3fa7fde | 92 | break; |
bulmecisco | 2:1115b3fa7fde | 93 | case '7': |
bulmecisco | 2:1115b3fa7fde | 94 | karel.MoveBackwards(); |
bulmecisco | 2:1115b3fa7fde | 95 | break; |
bulmecisco | 2:1115b3fa7fde | 96 | case '6': |
bulmecisco | 2:1115b3fa7fde | 97 | karel.TurnLeftStep(); |
bulmecisco | 2:1115b3fa7fde | 98 | break; |
bulmecisco | 2:1115b3fa7fde | 99 | case '8': |
bulmecisco | 2:1115b3fa7fde | 100 | karel.TurnRigthStep(); |
bulmecisco | 2:1115b3fa7fde | 101 | break; |
bulmecisco | 2:1115b3fa7fde | 102 | case '0': |
bulmecisco | 2:1115b3fa7fde | 103 | karel.BlinkLeftLed(); |
bulmecisco | 2:1115b3fa7fde | 104 | break; |
bulmecisco | 2:1115b3fa7fde | 105 | case '2': |
bulmecisco | 2:1115b3fa7fde | 106 | karel.BlinkRigthLed(); |
bulmecisco | 2:1115b3fa7fde | 107 | break; |
bulmecisco | 2:1115b3fa7fde | 108 | default: |
bulmecisco | 2:1115b3fa7fde | 109 | break; |
bulmecisco | 2:1115b3fa7fde | 110 | } |
bulmecisco | 2:1115b3fa7fde | 111 | if(cmd == '9' && turn) |
bulmecisco | 2:1115b3fa7fde | 112 | { |
bulmecisco | 2:1115b3fa7fde | 113 | turn = !turn; |
bulmecisco | 2:1115b3fa7fde | 114 | karel.TurnLedOff(LED_ALL); |
bulmecisco | 2:1115b3fa7fde | 115 | } else if(cmd == '9' && !turn) |
bulmecisco | 2:1115b3fa7fde | 116 | { |
bulmecisco | 2:1115b3fa7fde | 117 | turn = !turn; |
bulmecisco | 2:1115b3fa7fde | 118 | karel.TurnLedOn(LED_ALL); |
bulmecisco | 2:1115b3fa7fde | 119 | } |
bulmecisco | 2:1115b3fa7fde | 120 | |
bulmecisco | 2:1115b3fa7fde | 121 | } |
DoTTi | 0:6d28be2dcb7c | 122 | } |
DoTTi | 0:6d28be2dcb7c | 123 | } |