Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
main.cpp@15:151e59899221, 2017-05-20 (annotated)
- Committer:
- kolanery
- Date:
- Sat May 20 05:17:47 2017 +0000
- Revision:
- 15:151e59899221
- Parent:
- 14:a646667ac9ea
- Child:
- 16:c26d8e007df5
update working version
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| kolanery | 0:cb667de3a336 | 1 | #include "drivecontrol.h" |
| szh66 | 6:1bcfda49e146 | 2 | //#include "pin_assignment.h" |
| kolanery | 4:73510c7fa316 | 3 | #include "io_modules.h" |
| szh66 | 6:1bcfda49e146 | 4 | #include "mbed.h" |
| szh66 | 6:1bcfda49e146 | 5 | |
| szh66 | 6:1bcfda49e146 | 6 | // Battery Debugger Interface |
| szh66 | 6:1bcfda49e146 | 7 | AnalogIn battery(PA_3); |
| szh66 | 6:1bcfda49e146 | 8 | |
| szh66 | 6:1bcfda49e146 | 9 | |
| szh66 | 6:1bcfda49e146 | 10 | // Led Debugger Interface |
| szh66 | 6:1bcfda49e146 | 11 | DigitalOut led_1(PB_12); |
| szh66 | 6:1bcfda49e146 | 12 | DigitalOut led_2(PB_13); |
| szh66 | 6:1bcfda49e146 | 13 | DigitalOut led_3(PB_14); |
| szh66 | 6:1bcfda49e146 | 14 | DigitalOut led_4(PB_15); |
| szh66 | 6:1bcfda49e146 | 15 | |
| szh66 | 6:1bcfda49e146 | 16 | // System io |
| szh66 | 6:1bcfda49e146 | 17 | Serial serial (PA_9, PA_10); |
| kolanery | 2:619b02232144 | 18 | |
| kolanery | 0:cb667de3a336 | 19 | // Define states for debugging the mouse hardware |
| kolanery | 2:619b02232144 | 20 | // const int DRIVE = 1, TURN = 2, DEBUG = 3, STOP = 4; |
| kolanery | 0:cb667de3a336 | 21 | // Direction of which to turn |
| kolanery | 2:619b02232144 | 22 | // const int LEFT = 0, RIGHT = 1; |
| kolanery | 0:cb667de3a336 | 23 | // Start and End Pos |
| kolanery | 14:a646667ac9ea | 24 | const int STRAIGHT = 0, LEFT = 1, RIGHT = 2; |
| kolanery | 0:cb667de3a336 | 25 | const int START_POS = 0, END_POS = 0; |
| kolanery | 2:619b02232144 | 26 | |
| kolanery | 0:cb667de3a336 | 27 | // Terminating condition for the main control loop |
| kolanery | 0:cb667de3a336 | 28 | bool hasFoundCenter = false; |
| kolanery | 0:cb667de3a336 | 29 | |
| kolanery | 0:cb667de3a336 | 30 | // Battery Consumption Indicator |
| kolanery | 0:cb667de3a336 | 31 | void setup() { |
| szh66 | 6:1bcfda49e146 | 32 | // pc.baud(9600); |
| kolanery | 4:73510c7fa316 | 33 | // using the serial functions will have an impact on timing. |
| kolanery | 15:151e59899221 | 34 | // serial.printf("voltage value is: %3.3f%%\r\n", battery.read()*100.0f); |
| kolanery | 4:73510c7fa316 | 35 | // serial.printf("normalized: 0x%04X \r\n", battery.read_u16()); |
| kolanery | 15:151e59899221 | 36 | if (battery.read() < 0.70f){ |
| kolanery | 0:cb667de3a336 | 37 | // flash led |
| kolanery | 4:73510c7fa316 | 38 | led_1 = 1; |
| kolanery | 4:73510c7fa316 | 39 | led_2 = 1; |
| kolanery | 4:73510c7fa316 | 40 | led_3 = 1; |
| kolanery | 4:73510c7fa316 | 41 | led_4 = 1; |
| kolanery | 15:151e59899221 | 42 | } |
| kolanery | 0:cb667de3a336 | 43 | } |
| kolanery | 0:cb667de3a336 | 44 | |
| kolanery | 0:cb667de3a336 | 45 | // Performs the basic drive control of the mouse |
| kolanery | 0:cb667de3a336 | 46 | int main() { |
| kolanery | 4:73510c7fa316 | 47 | DriveControl * driver = new DriveControl (START_POS, END_POS); |
| kolanery | 14:a646667ac9ea | 48 | driver->resetEncoders(); |
| kolanery | 14:a646667ac9ea | 49 | int state; |
| kolanery | 14:a646667ac9ea | 50 | bool finished_traverse_one_cell = true; |
| kolanery | 2:619b02232144 | 51 | while(1) { |
| kolanery | 2:619b02232144 | 52 | setup(); |
| kolanery | 14:a646667ac9ea | 53 | // switch state |
| kolanery | 15:151e59899221 | 54 | wait_ms(1); |
| kolanery | 14:a646667ac9ea | 55 | if (finished_traverse_one_cell == true) { |
| kolanery | 14:a646667ac9ea | 56 | if (!driver->has_front_wall()) { |
| kolanery | 14:a646667ac9ea | 57 | state = STRAIGHT; |
| kolanery | 15:151e59899221 | 58 | driver->resetEncoders(); |
| kolanery | 15:151e59899221 | 59 | } |
| kolanery | 15:151e59899221 | 60 | else if (!driver->has_left_wall()) { |
| kolanery | 15:151e59899221 | 61 | state = LEFT; |
| kolanery | 15:151e59899221 | 62 | driver->resetEncoders(); |
| kolanery | 15:151e59899221 | 63 | } |
| kolanery | 15:151e59899221 | 64 | else { |
| kolanery | 15:151e59899221 | 65 | state = RIGHT; |
| kolanery | 15:151e59899221 | 66 | driver->resetEncoders(); |
| kolanery | 15:151e59899221 | 67 | } |
| kolanery | 15:151e59899221 | 68 | finished_traverse_one_cell = false; |
| kolanery | 15:151e59899221 | 69 | |
| kolanery | 15:151e59899221 | 70 | /* |
| kolanery | 15:151e59899221 | 71 | if (!driver->has_left_wall()) { |
| kolanery | 15:151e59899221 | 72 | state = LEFT; |
| kolanery | 15:151e59899221 | 73 | driver->resetEncoders(); |
| kolanery | 14:a646667ac9ea | 74 | } |
| kolanery | 14:a646667ac9ea | 75 | else if (!driver->has_right_wall()) { |
| kolanery | 15:151e59899221 | 76 | state = RIGHT; |
| kolanery | 15:151e59899221 | 77 | driver->resetEncoders(); |
| kolanery | 14:a646667ac9ea | 78 | } |
| kolanery | 14:a646667ac9ea | 79 | else { |
| kolanery | 15:151e59899221 | 80 | state = STRAIGHT; |
| kolanery | 15:151e59899221 | 81 | driver->resetEncoders(); |
| kolanery | 14:a646667ac9ea | 82 | } |
| kolanery | 15:151e59899221 | 83 | finished_traverse_one_cell = false;*/ |
| kolanery | 7:7215adbae3da | 84 | } |
| kolanery | 14:a646667ac9ea | 85 | |
| kolanery | 14:a646667ac9ea | 86 | if (state == STRAIGHT) { |
| kolanery | 15:151e59899221 | 87 | if (!driver->has_front_wall()) { |
| kolanery | 14:a646667ac9ea | 88 | driver->drive_forward(); |
| kolanery | 14:a646667ac9ea | 89 | } |
| kolanery | 14:a646667ac9ea | 90 | else { |
| kolanery | 15:151e59899221 | 91 | driver->stop(); |
| kolanery | 15:151e59899221 | 92 | wait(0.5); |
| kolanery | 15:151e59899221 | 93 | led_1 = 1; |
| kolanery | 15:151e59899221 | 94 | led_2 = 1; |
| kolanery | 15:151e59899221 | 95 | led_3 = 0; |
| kolanery | 15:151e59899221 | 96 | led_4 = 0; |
| kolanery | 14:a646667ac9ea | 97 | finished_traverse_one_cell = true; |
| kolanery | 14:a646667ac9ea | 98 | continue; |
| kolanery | 14:a646667ac9ea | 99 | } |
| kolanery | 7:7215adbae3da | 100 | } |
| kolanery | 14:a646667ac9ea | 101 | if (state == RIGHT) { |
| kolanery | 14:a646667ac9ea | 102 | if (!driver->should_finish_turn_right()) { |
| kolanery | 14:a646667ac9ea | 103 | driver->turn_right(); |
| kolanery | 14:a646667ac9ea | 104 | } |
| kolanery | 14:a646667ac9ea | 105 | else { |
| kolanery | 14:a646667ac9ea | 106 | driver->stop(); |
| kolanery | 15:151e59899221 | 107 | wait(0.5); |
| kolanery | 15:151e59899221 | 108 | led_1 = 0; |
| kolanery | 15:151e59899221 | 109 | led_2 = 1; |
| kolanery | 14:a646667ac9ea | 110 | led_3 = 1; |
| kolanery | 15:151e59899221 | 111 | led_4 = 0; |
| kolanery | 14:a646667ac9ea | 112 | finished_traverse_one_cell = true; |
| kolanery | 14:a646667ac9ea | 113 | continue; |
| kolanery | 14:a646667ac9ea | 114 | } |
| kolanery | 14:a646667ac9ea | 115 | } |
| kolanery | 14:a646667ac9ea | 116 | if (state == LEFT) { |
| kolanery | 14:a646667ac9ea | 117 | if (!driver->should_finish_turn_left()) { |
| kolanery | 14:a646667ac9ea | 118 | driver->turn_left(); |
| kolanery | 14:a646667ac9ea | 119 | } |
| kolanery | 14:a646667ac9ea | 120 | else { |
| kolanery | 14:a646667ac9ea | 121 | driver->stop(); |
| kolanery | 15:151e59899221 | 122 | wait(0.5); |
| kolanery | 15:151e59899221 | 123 | led_1 = 0; |
| kolanery | 15:151e59899221 | 124 | led_2 = 0; |
| kolanery | 15:151e59899221 | 125 | led_3 = 1; |
| kolanery | 14:a646667ac9ea | 126 | led_4 = 1; |
| kolanery | 14:a646667ac9ea | 127 | finished_traverse_one_cell = true; |
| kolanery | 14:a646667ac9ea | 128 | continue; |
| kolanery | 14:a646667ac9ea | 129 | } |
| kolanery | 15:151e59899221 | 130 | } |
| kolanery | 15:151e59899221 | 131 | |
| kolanery | 0:cb667de3a336 | 132 | } |
| kolanery | 2:619b02232144 | 133 | } |