Smart car platform driven by Pokitto
Fork of Blinky by
main.cpp@11:524d13eb7d5e, 2018-01-15 (annotated)
- Committer:
- RichardAmes
- Date:
- Mon Jan 15 15:28:26 2018 +0000
- Revision:
- 11:524d13eb7d5e
- Parent:
- 10:f5dfd33d5431
Updated mapping for IN4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pokitto | 0:2d2a3994d55d | 1 | #include "Pokitto.h" |
Pokitto | 0:2d2a3994d55d | 2 | |
RichardAmes | 10:f5dfd33d5431 | 3 | #define STATE_IDLE 0 |
RichardAmes | 10:f5dfd33d5431 | 4 | #define STATE_RUNNING 1 |
RichardAmes | 10:f5dfd33d5431 | 5 | |
Pokitto | 8:1f8f91d671a5 | 6 | // the pokitto core library |
Pokitto | 8:1f8f91d671a5 | 7 | Pokitto::Core pokitto; |
Pokitto | 8:1f8f91d671a5 | 8 | |
RichardAmes | 10:f5dfd33d5431 | 9 | /* Map EXTn outputs to inputs on L298 Full Bridge Driver */ |
RichardAmes | 10:f5dfd33d5431 | 10 | DigitalOut ena = DigitalOut(EXT0); |
RichardAmes | 10:f5dfd33d5431 | 11 | DigitalOut enb = DigitalOut(EXT1); |
RichardAmes | 10:f5dfd33d5431 | 12 | DigitalOut in1 = DigitalOut(EXT2); |
RichardAmes | 10:f5dfd33d5431 | 13 | DigitalOut in2 = DigitalOut(EXT3); |
RichardAmes | 10:f5dfd33d5431 | 14 | DigitalOut in3 = DigitalOut(EXT4); |
RichardAmes | 11:524d13eb7d5e | 15 | DigitalOut in4 = DigitalOut(EXT5); |
RichardAmes | 10:f5dfd33d5431 | 16 | |
RichardAmes | 10:f5dfd33d5431 | 17 | int state; |
RichardAmes | 10:f5dfd33d5431 | 18 | |
RichardAmes | 10:f5dfd33d5431 | 19 | int script[] = {1, 1, 4, -1, -1, 4, 1, -1, 4, -1, 1, 4, 0, 0, 0}; |
RichardAmes | 10:f5dfd33d5431 | 20 | int step; |
RichardAmes | 10:f5dfd33d5431 | 21 | int substep; |
RichardAmes | 10:f5dfd33d5431 | 22 | |
RichardAmes | 10:f5dfd33d5431 | 23 | void init(void) |
RichardAmes | 10:f5dfd33d5431 | 24 | { |
RichardAmes | 10:f5dfd33d5431 | 25 | state = STATE_IDLE; |
RichardAmes | 10:f5dfd33d5431 | 26 | ena.write(0); |
RichardAmes | 10:f5dfd33d5431 | 27 | enb.write(0); |
RichardAmes | 10:f5dfd33d5431 | 28 | in1.write(0); |
RichardAmes | 10:f5dfd33d5431 | 29 | in2.write(0); |
RichardAmes | 10:f5dfd33d5431 | 30 | in3.write(0); |
RichardAmes | 10:f5dfd33d5431 | 31 | in4.write(0); |
RichardAmes | 10:f5dfd33d5431 | 32 | } |
RichardAmes | 10:f5dfd33d5431 | 33 | |
RichardAmes | 10:f5dfd33d5431 | 34 | void set_motors(int a, int b) |
RichardAmes | 10:f5dfd33d5431 | 35 | { |
RichardAmes | 10:f5dfd33d5431 | 36 | ena.write(1); |
RichardAmes | 10:f5dfd33d5431 | 37 | enb.write(1); |
RichardAmes | 10:f5dfd33d5431 | 38 | if (a == -1) |
RichardAmes | 10:f5dfd33d5431 | 39 | { |
RichardAmes | 10:f5dfd33d5431 | 40 | in1.write(0); |
RichardAmes | 10:f5dfd33d5431 | 41 | in2.write(1); |
RichardAmes | 10:f5dfd33d5431 | 42 | } |
RichardAmes | 10:f5dfd33d5431 | 43 | else if (a == 1) |
RichardAmes | 10:f5dfd33d5431 | 44 | { |
RichardAmes | 10:f5dfd33d5431 | 45 | in1.write(1); |
RichardAmes | 10:f5dfd33d5431 | 46 | in2.write(0); |
RichardAmes | 10:f5dfd33d5431 | 47 | } |
RichardAmes | 10:f5dfd33d5431 | 48 | else |
RichardAmes | 10:f5dfd33d5431 | 49 | { |
RichardAmes | 10:f5dfd33d5431 | 50 | in1.write(1); |
RichardAmes | 10:f5dfd33d5431 | 51 | in2.write(1); |
RichardAmes | 10:f5dfd33d5431 | 52 | } |
RichardAmes | 10:f5dfd33d5431 | 53 | if (b == -1) |
RichardAmes | 10:f5dfd33d5431 | 54 | { |
RichardAmes | 10:f5dfd33d5431 | 55 | in3.write(0); |
RichardAmes | 10:f5dfd33d5431 | 56 | in4.write(1); |
RichardAmes | 10:f5dfd33d5431 | 57 | } |
RichardAmes | 10:f5dfd33d5431 | 58 | else if (b == 1) |
RichardAmes | 10:f5dfd33d5431 | 59 | { |
RichardAmes | 10:f5dfd33d5431 | 60 | in3.write(1); |
RichardAmes | 10:f5dfd33d5431 | 61 | in4.write(0); |
RichardAmes | 10:f5dfd33d5431 | 62 | } |
RichardAmes | 10:f5dfd33d5431 | 63 | else |
RichardAmes | 10:f5dfd33d5431 | 64 | { |
RichardAmes | 10:f5dfd33d5431 | 65 | in3.write(1); |
RichardAmes | 10:f5dfd33d5431 | 66 | in4.write(1); |
RichardAmes | 10:f5dfd33d5431 | 67 | } |
RichardAmes | 10:f5dfd33d5431 | 68 | } |
RichardAmes | 10:f5dfd33d5431 | 69 | |
RichardAmes | 10:f5dfd33d5431 | 70 | void run(void) |
RichardAmes | 10:f5dfd33d5431 | 71 | { |
RichardAmes | 10:f5dfd33d5431 | 72 | if (substep == 0) |
RichardAmes | 10:f5dfd33d5431 | 73 | { |
RichardAmes | 10:f5dfd33d5431 | 74 | set_motors(script[step], script[step + 1]); |
RichardAmes | 10:f5dfd33d5431 | 75 | substep = script[step + 2]; |
RichardAmes | 10:f5dfd33d5431 | 76 | } |
RichardAmes | 10:f5dfd33d5431 | 77 | substep--; |
RichardAmes | 10:f5dfd33d5431 | 78 | if (substep == 0) |
RichardAmes | 10:f5dfd33d5431 | 79 | { |
RichardAmes | 10:f5dfd33d5431 | 80 | step += 3; |
RichardAmes | 10:f5dfd33d5431 | 81 | if (script[step] == 0) |
RichardAmes | 10:f5dfd33d5431 | 82 | state = STATE_IDLE; |
RichardAmes | 10:f5dfd33d5431 | 83 | } |
RichardAmes | 10:f5dfd33d5431 | 84 | } |
RichardAmes | 10:f5dfd33d5431 | 85 | |
RichardAmes | 10:f5dfd33d5431 | 86 | void stop(void) |
RichardAmes | 10:f5dfd33d5431 | 87 | { |
RichardAmes | 10:f5dfd33d5431 | 88 | ena.write(0); |
RichardAmes | 10:f5dfd33d5431 | 89 | enb.write(0); |
RichardAmes | 10:f5dfd33d5431 | 90 | in1.write(1); |
RichardAmes | 10:f5dfd33d5431 | 91 | in2.write(1); |
RichardAmes | 10:f5dfd33d5431 | 92 | in3.write(1); |
RichardAmes | 10:f5dfd33d5431 | 93 | in4.write(1); |
RichardAmes | 10:f5dfd33d5431 | 94 | } |
Pokitto | 8:1f8f91d671a5 | 95 | |
Pokitto | 8:1f8f91d671a5 | 96 | int main () |
Pokitto | 8:1f8f91d671a5 | 97 | { |
Pokitto | 8:1f8f91d671a5 | 98 | // initialise the Pokitto |
Pokitto | 8:1f8f91d671a5 | 99 | pokitto.begin(); |
Pokitto | 0:2d2a3994d55d | 100 | |
Pokitto | 8:1f8f91d671a5 | 101 | // the main loop |
Pokitto | 8:1f8f91d671a5 | 102 | while (pokitto.isRunning()) |
Pokitto | 8:1f8f91d671a5 | 103 | { |
Pokitto | 8:1f8f91d671a5 | 104 | // update the Pokitto's state |
Pokitto | 8:1f8f91d671a5 | 105 | if (pokitto.update()) |
Pokitto | 8:1f8f91d671a5 | 106 | { |
RichardAmes | 10:f5dfd33d5431 | 107 | switch (state) |
Pokitto | 8:1f8f91d671a5 | 108 | { |
RichardAmes | 10:f5dfd33d5431 | 109 | case STATE_IDLE: |
RichardAmes | 10:f5dfd33d5431 | 110 | pokitto.display.print("Idle"); |
RichardAmes | 10:f5dfd33d5431 | 111 | stop(); |
RichardAmes | 10:f5dfd33d5431 | 112 | if (pokitto.buttons.aBtn()) |
RichardAmes | 10:f5dfd33d5431 | 113 | { |
RichardAmes | 10:f5dfd33d5431 | 114 | state = STATE_RUNNING; |
RichardAmes | 10:f5dfd33d5431 | 115 | step = 0; |
RichardAmes | 10:f5dfd33d5431 | 116 | substep = 0; |
RichardAmes | 10:f5dfd33d5431 | 117 | } |
RichardAmes | 10:f5dfd33d5431 | 118 | break; |
RichardAmes | 10:f5dfd33d5431 | 119 | case STATE_RUNNING: |
RichardAmes | 10:f5dfd33d5431 | 120 | pokitto.display.print("Running"); |
RichardAmes | 10:f5dfd33d5431 | 121 | run(); |
RichardAmes | 10:f5dfd33d5431 | 122 | if (pokitto.buttons.aBtn()) |
RichardAmes | 10:f5dfd33d5431 | 123 | state = STATE_IDLE; |
RichardAmes | 10:f5dfd33d5431 | 124 | break; |
Pokitto | 8:1f8f91d671a5 | 125 | } |
Pokitto | 8:1f8f91d671a5 | 126 | } |
RichardAmes | 10:f5dfd33d5431 | 127 | wait_ms(100); |
Pokitto | 8:1f8f91d671a5 | 128 | } |
Pokitto | 8:1f8f91d671a5 | 129 | |
Pokitto | 8:1f8f91d671a5 | 130 | return 1; |
Pokitto | 0:2d2a3994d55d | 131 | } |