![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
3rd year group project. Electronic and Electrical Engineering. Heriot-Watt University. This is the code for the mbed for the Automatic Little Object Organiser (ALOO).
Dependencies: MCP23017 TCS3472_I2C WattBob_TextLCD mbed
main.cpp@28:7e4d29977d72, 2015-12-01 (annotated)
- Committer:
- dreamselec
- Date:
- Tue Dec 01 20:54:26 2015 +0000
- Revision:
- 28:7e4d29977d72
- Parent:
- 27:2cb1bdb7ae3d
- Child:
- 29:9c0339e3c593
Intialization sequence optimised, it will also sync with PC's current state.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dreamselec | 0:fe5cb0a8fc5a | 1 | #include "mbed.h" |
dreamselec | 0:fe5cb0a8fc5a | 2 | #include "WattBob_TextLCD.h" |
dreamselec | 0:fe5cb0a8fc5a | 3 | #include "TCS3472_I2C.h" |
dreamselec | 0:fe5cb0a8fc5a | 4 | #include "MCP23017.h" |
dreamselec | 0:fe5cb0a8fc5a | 5 | #include <string> |
dreamselec | 1:92a2a5ef65a8 | 6 | #include <time.h> |
dreamselec | 2:7a55cb10259f | 7 | //#include <future> |
dreamselec | 2:7a55cb10259f | 8 | #include "globals.h" |
dreamselec | 2:7a55cb10259f | 9 | #include "commander.h" |
dreamselec | 3:843b830ee8bd | 10 | #include "fpga.h" |
dreamselec | 0:fe5cb0a8fc5a | 11 | |
dreamselec | 0:fe5cb0a8fc5a | 12 | #define BACKLIGHT_ON(INTERFACE) INTERFACE->write_bit(1, 4); |
dreamselec | 0:fe5cb0a8fc5a | 13 | #define BACKLIGHT_OFF(INTERFACE) INTERFACE->write_bit(0, 4); |
dreamselec | 0:fe5cb0a8fc5a | 14 | |
dreamselec | 0:fe5cb0a8fc5a | 15 | #define LCDFL() lcd->locate(0,0); |
dreamselec | 0:fe5cb0a8fc5a | 16 | #define LCDSL() lcd->locate(1,0); |
dreamselec | 2:7a55cb10259f | 17 | #define D_LEDS_OFF() i2cport->write_bit(0, 12); i2cport->write_bit(0, 13); i2cport->write_bit(0, 14); i2cport->write_bit(0, 15); |
dreamselec | 2:7a55cb10259f | 18 | #define U_LEDS_OFF() myLED1 = 0; myLED2 = 0; myLED3 = 0; myLED4 = 0; |
dreamselec | 0:fe5cb0a8fc5a | 19 | |
dreamselec | 2:7a55cb10259f | 20 | DigitalOut myLED1(LED1); |
dreamselec | 2:7a55cb10259f | 21 | DigitalOut myLED2(LED2); |
dreamselec | 2:7a55cb10259f | 22 | DigitalOut myLED3(LED3); |
dreamselec | 2:7a55cb10259f | 23 | DigitalOut myLED4(LED4); |
dreamselec | 0:fe5cb0a8fc5a | 24 | |
dreamselec | 0:fe5cb0a8fc5a | 25 | MCP23017 *i2cport; |
dreamselec | 0:fe5cb0a8fc5a | 26 | WattBob_TextLCD *lcd; |
dreamselec | 10:16ba52f8e025 | 27 | TCS3472_I2C rgbSensor(p28, p27); |
dreamselec | 2:7a55cb10259f | 28 | Serial pc(USBTX, USBRX); |
dreamselec |
8:e1da2ae62885 | 29 | uint8_t rxBuffer[kSmallBufferSize + 1]; |
dreamselec |
8:e1da2ae62885 | 30 | int rxIndex = 0; |
dreamselec | 21:60c4fe94d79c | 31 | float percentageError[3]; |
dreamselec | 21:60c4fe94d79c | 32 | float adjustedValues[3]; |
dreamselec | 27:2cb1bdb7ae3d | 33 | int blockCount = 0; |
dreamselec | 27:2cb1bdb7ae3d | 34 | bool lastBlockHaz = false; |
dreamselec | 28:7e4d29977d72 | 35 | bool recievingResponse = false; |
dreamselec | 2:7a55cb10259f | 36 | |
dreamselec |
6:98fe30430194 | 37 | Commander _commander = Commander(); |
dreamselec |
6:98fe30430194 | 38 | Commander *commander = &_commander; |
dreamselec |
6:98fe30430194 | 39 | FPGA _fpga = FPGA(); |
dreamselec | 9:dc8f155b71c8 | 40 | extern FPGA *fpga = &_fpga; |
dreamselec |
6:98fe30430194 | 41 | |
dreamselec | 20:4e0f0944f28f | 42 | Block defualtHazBlock = Block(); |
dreamselec |
6:98fe30430194 | 43 | extern Block _HazBlock; |
dreamselec |
6:98fe30430194 | 44 | Block *HazBlock = &_HazBlock; |
dreamselec |
6:98fe30430194 | 45 | |
dreamselec |
18:44a1c1a30166 | 46 | extern PCModes currentMode; |
dreamselec |
18:44a1c1a30166 | 47 | |
dreamselec |
6:98fe30430194 | 48 | void initInternal(); |
dreamselec | 7:b6e31bfdb2af | 49 | void initPort(int baudRate=kDefaultBaudRate); |
dreamselec |
6:98fe30430194 | 50 | void printPCDetectedText(); |
dreamselec |
6:98fe30430194 | 51 | void Rx_interrupt(); |
dreamselec |
18:44a1c1a30166 | 52 | bool checkColour(int colourValues[]); |
dreamselec | 10:16ba52f8e025 | 53 | void runInServoTestMode(); |
dreamselec | 15:777390eb5afd | 54 | void displayWaitingLine(); |
dreamselec | 15:777390eb5afd | 55 | void displayPCStatus(); |
dreamselec |
18:44a1c1a30166 | 56 | bool waitForBlock(); |
dreamselec |
18:44a1c1a30166 | 57 | void sortBlock(); |
dreamselec |
23:db91aaa43a9e | 58 | void runInBreakBeamTestMode(); |
dreamselec |
23:db91aaa43a9e | 59 | void turnOffTopLEDs(); |
dreamselec |
23:db91aaa43a9e | 60 | void turnOffBottomLEDs(); |
dreamselec | 25:792540d69c49 | 61 | void runInColourSensorTestMode(); |
dreamselec |
6:98fe30430194 | 62 | |
dreamselec |
6:98fe30430194 | 63 | int main() |
dreamselec |
6:98fe30430194 | 64 | { |
dreamselec | 27:2cb1bdb7ae3d | 65 | initInternal(); |
dreamselec | 27:2cb1bdb7ae3d | 66 | initPort(); |
dreamselec | 27:2cb1bdb7ae3d | 67 | |
dreamselec | 27:2cb1bdb7ae3d | 68 | for (;;) { |
dreamselec | 27:2cb1bdb7ae3d | 69 | if (connectedToPC == false) { |
dreamselec | 28:7e4d29977d72 | 70 | pc.printf("DEBUG: PC did not responded.\n"); |
dreamselec | 27:2cb1bdb7ae3d | 71 | lcd->cls(); |
dreamselec | 27:2cb1bdb7ae3d | 72 | i2cport->write_bit(1, 12); |
dreamselec | 27:2cb1bdb7ae3d | 73 | lcd->locate(0,0); |
dreamselec | 27:2cb1bdb7ae3d | 74 | lcd->printf("1: Start sorting."); |
dreamselec | 27:2cb1bdb7ae3d | 75 | lcd->locate(1,0); |
dreamselec | 27:2cb1bdb7ae3d | 76 | i2cport->write_bit(1,13); |
dreamselec | 27:2cb1bdb7ae3d | 77 | lcd->printf("2: Connect to PC"); |
dreamselec | 27:2cb1bdb7ae3d | 78 | } |
dreamselec | 27:2cb1bdb7ae3d | 79 | |
dreamselec | 24:02c61793f90b | 80 | // wait(0.03); |
dreamselec | 10:16ba52f8e025 | 81 | |
dreamselec | 27:2cb1bdb7ae3d | 82 | int selection = 0; |
dreamselec | 27:2cb1bdb7ae3d | 83 | do { |
dreamselec | 27:2cb1bdb7ae3d | 84 | myLED4 = selection; |
dreamselec | 27:2cb1bdb7ae3d | 85 | selection = readSwitches(); |
dreamselec | 27:2cb1bdb7ae3d | 86 | } while (selection != 1 && selection != 2 && connectedToPC == false); |
dreamselec | 27:2cb1bdb7ae3d | 87 | // lcd->cls(); |
dreamselec | 27:2cb1bdb7ae3d | 88 | D_LEDS_OFF(); |
dreamselec | 27:2cb1bdb7ae3d | 89 | if (selection == 1) { |
dreamselec | 27:2cb1bdb7ae3d | 90 | // User selected op 1: Start sorting autonomously. |
dreamselec | 27:2cb1bdb7ae3d | 91 | i2cport->write_bit(1, 12); |
dreamselec | 27:2cb1bdb7ae3d | 92 | lcd->cls(); |
dreamselec | 27:2cb1bdb7ae3d | 93 | LCDFL(); |
dreamselec | 27:2cb1bdb7ae3d | 94 | lcd->printf("Starting sorting"); |
dreamselec | 27:2cb1bdb7ae3d | 95 | wait(0.5); |
dreamselec | 27:2cb1bdb7ae3d | 96 | D_LEDS_OFF(); |
dreamselec | 19:61b21ac4896e | 97 | |
dreamselec | 27:2cb1bdb7ae3d | 98 | for(;;) { |
dreamselec | 27:2cb1bdb7ae3d | 99 | if (blockCount > 0) { |
dreamselec | 27:2cb1bdb7ae3d | 100 | lcd->cls(); |
dreamselec | 27:2cb1bdb7ae3d | 101 | displayWaitingLine(); |
dreamselec | 27:2cb1bdb7ae3d | 102 | lcd->locate(1,0); |
dreamselec | 27:2cb1bdb7ae3d | 103 | if (lastBlockHaz == true) |
dreamselec | 27:2cb1bdb7ae3d | 104 | lcd->printf("Hazardous"); |
dreamselec | 27:2cb1bdb7ae3d | 105 | else if (lastBlockHaz == false) |
dreamselec | 27:2cb1bdb7ae3d | 106 | lcd->printf("Non-Hazardous"); |
dreamselec | 27:2cb1bdb7ae3d | 107 | } else { |
dreamselec | 27:2cb1bdb7ae3d | 108 | displayWaitingLine(); |
dreamselec | 27:2cb1bdb7ae3d | 109 | } |
dreamselec | 10:16ba52f8e025 | 110 | |
dreamselec | 27:2cb1bdb7ae3d | 111 | i2cport->write_bit(1, 15); |
dreamselec |
23:db91aaa43a9e | 112 | |
dreamselec | 27:2cb1bdb7ae3d | 113 | // Break and return to main menu i.e. Start Op, or Connect to PC. |
dreamselec | 27:2cb1bdb7ae3d | 114 | if (waitForBlock() == false) { |
dreamselec | 27:2cb1bdb7ae3d | 115 | D_LEDS_OFF(); |
dreamselec | 27:2cb1bdb7ae3d | 116 | break; |
dreamselec | 27:2cb1bdb7ae3d | 117 | } else { |
dreamselec | 27:2cb1bdb7ae3d | 118 | sortBlock(); |
dreamselec | 27:2cb1bdb7ae3d | 119 | } |
dreamselec | 27:2cb1bdb7ae3d | 120 | } |
dreamselec | 27:2cb1bdb7ae3d | 121 | } |
dreamselec | 10:16ba52f8e025 | 122 | |
dreamselec | 27:2cb1bdb7ae3d | 123 | if (selection == 2 || connectedToPC == true) { |
dreamselec | 27:2cb1bdb7ae3d | 124 | wait(0.1); |
dreamselec | 27:2cb1bdb7ae3d | 125 | for (;;) { |
dreamselec | 27:2cb1bdb7ae3d | 126 | displayPCStatus(); |
dreamselec | 19:61b21ac4896e | 127 | |
dreamselec | 27:2cb1bdb7ae3d | 128 | i2cport->write_bit(1, 15); |
dreamselec | 27:2cb1bdb7ae3d | 129 | int abortOperation = false; |
dreamselec | 27:2cb1bdb7ae3d | 130 | if (connectedToPC == false) |
dreamselec | 27:2cb1bdb7ae3d | 131 | pc.printf(":<pc>connect;"); |
dreamselec | 27:2cb1bdb7ae3d | 132 | while (connectedToPC == false && abortOperation == false) { |
dreamselec | 27:2cb1bdb7ae3d | 133 | abortOperation = readSwitches() == 4; |
dreamselec | 27:2cb1bdb7ae3d | 134 | } |
dreamselec |
18:44a1c1a30166 | 135 | |
dreamselec | 27:2cb1bdb7ae3d | 136 | if (abortOperation == true) { |
dreamselec | 27:2cb1bdb7ae3d | 137 | D_LEDS_OFF(); |
dreamselec | 27:2cb1bdb7ae3d | 138 | break; |
dreamselec | 27:2cb1bdb7ae3d | 139 | } |
dreamselec | 27:2cb1bdb7ae3d | 140 | |
dreamselec | 27:2cb1bdb7ae3d | 141 | displayPCStatus(); |
dreamselec | 15:777390eb5afd | 142 | |
dreamselec | 27:2cb1bdb7ae3d | 143 | while (abortOperation == false && connectedToPC == true) { |
dreamselec | 27:2cb1bdb7ae3d | 144 | if (currentMode == Maintanence) { |
dreamselec | 27:2cb1bdb7ae3d | 145 | displayPCStatus(); |
dreamselec | 27:2cb1bdb7ae3d | 146 | while (currentMode == Maintanence) { |
dreamselec | 27:2cb1bdb7ae3d | 147 | if (runServoTest == true) |
dreamselec | 27:2cb1bdb7ae3d | 148 | runInServoTestMode(); |
dreamselec | 27:2cb1bdb7ae3d | 149 | else if (runBreakBeamTest == true) |
dreamselec | 27:2cb1bdb7ae3d | 150 | runInBreakBeamTestMode(); |
dreamselec | 27:2cb1bdb7ae3d | 151 | else if (runColourSensorTest == true) |
dreamselec | 27:2cb1bdb7ae3d | 152 | runInColourSensorTestMode(); |
dreamselec | 27:2cb1bdb7ae3d | 153 | if (i2cport->read_bit(11) == 1) { |
dreamselec | 27:2cb1bdb7ae3d | 154 | if (displayAbortDialog() == true) { |
dreamselec | 27:2cb1bdb7ae3d | 155 | currentMode = None; |
dreamselec | 27:2cb1bdb7ae3d | 156 | pc.printf(":<mbed>mode=none;"); |
dreamselec | 27:2cb1bdb7ae3d | 157 | } else { |
dreamselec | 27:2cb1bdb7ae3d | 158 | displayPCStatus(); |
dreamselec | 27:2cb1bdb7ae3d | 159 | } |
dreamselec | 27:2cb1bdb7ae3d | 160 | } |
dreamselec | 27:2cb1bdb7ae3d | 161 | } |
dreamselec | 27:2cb1bdb7ae3d | 162 | } else if (currentMode == Normal) { |
dreamselec | 27:2cb1bdb7ae3d | 163 | displayPCStatus(); |
dreamselec | 27:2cb1bdb7ae3d | 164 | while (currentMode == Normal) { |
dreamselec | 27:2cb1bdb7ae3d | 165 | if (currentState == Pause) { |
dreamselec | 27:2cb1bdb7ae3d | 166 | lcd->cls(); |
dreamselec | 27:2cb1bdb7ae3d | 167 | lcd->locate(0,0); |
dreamselec | 27:2cb1bdb7ae3d | 168 | lcd->printf("Sorting Paused."); |
dreamselec | 27:2cb1bdb7ae3d | 169 | lcd->locate(1,0); |
dreamselec | 27:2cb1bdb7ae3d | 170 | lcd->printf("1: Start"); |
dreamselec | 27:2cb1bdb7ae3d | 171 | int button = 0; |
dreamselec | 27:2cb1bdb7ae3d | 172 | i2cport->write_bit(1, 12); |
dreamselec | 27:2cb1bdb7ae3d | 173 | i2cport->write_bit(1, 15); |
dreamselec | 27:2cb1bdb7ae3d | 174 | while (currentState == Pause && currentMode == Normal) { |
dreamselec | 27:2cb1bdb7ae3d | 175 | button = readSwitches(); |
dreamselec | 27:2cb1bdb7ae3d | 176 | if (button == 1) { |
dreamselec | 27:2cb1bdb7ae3d | 177 | pc.printf(":<mbed>sort=start;"); |
dreamselec | 27:2cb1bdb7ae3d | 178 | currentState = Start; |
dreamselec | 27:2cb1bdb7ae3d | 179 | } else if (button == 4) { |
dreamselec | 27:2cb1bdb7ae3d | 180 | currentMode = None; |
dreamselec | 27:2cb1bdb7ae3d | 181 | pc.printf(":<mbed>mode=none;"); |
dreamselec | 27:2cb1bdb7ae3d | 182 | // goto setModeNone; |
dreamselec | 27:2cb1bdb7ae3d | 183 | break; |
dreamselec | 27:2cb1bdb7ae3d | 184 | } |
dreamselec | 27:2cb1bdb7ae3d | 185 | } |
dreamselec | 27:2cb1bdb7ae3d | 186 | } |
dreamselec | 27:2cb1bdb7ae3d | 187 | if (currentState == Start) { |
dreamselec | 27:2cb1bdb7ae3d | 188 | lcd->cls(); |
dreamselec | 27:2cb1bdb7ae3d | 189 | lcd->locate(0,0); |
dreamselec | 27:2cb1bdb7ae3d | 190 | lcd->printf("Sorting mode..."); |
dreamselec | 27:2cb1bdb7ae3d | 191 | while (currentState == Start && currentMode == Normal) { |
dreamselec | 27:2cb1bdb7ae3d | 192 | if (waitForBlock() == false) { |
dreamselec | 28:7e4d29977d72 | 193 | if (connectedToPC == true && currentState != Pause && currentMode == Normal) { |
dreamselec | 27:2cb1bdb7ae3d | 194 | // TODO: Tell PC to update UI if aborted from MBED. |
dreamselec | 27:2cb1bdb7ae3d | 195 | pc.printf(":<mbed>sort=pause;"); |
dreamselec | 27:2cb1bdb7ae3d | 196 | currentState = Pause; |
dreamselec | 27:2cb1bdb7ae3d | 197 | continue; |
dreamselec | 28:7e4d29977d72 | 198 | }else if (connectedToPC == true && currentMode == None) { |
dreamselec | 28:7e4d29977d72 | 199 | goto setModeNone; |
dreamselec | 28:7e4d29977d72 | 200 | }else if (connectedToPC == false) { |
dreamselec | 27:2cb1bdb7ae3d | 201 | currentMode = None; |
dreamselec | 28:7e4d29977d72 | 202 | currentState = Pause; |
dreamselec | 27:2cb1bdb7ae3d | 203 | abortOperation = true; |
dreamselec | 27:2cb1bdb7ae3d | 204 | } |
dreamselec | 27:2cb1bdb7ae3d | 205 | } else { |
dreamselec | 27:2cb1bdb7ae3d | 206 | sortBlock(); |
dreamselec | 27:2cb1bdb7ae3d | 207 | } |
dreamselec | 27:2cb1bdb7ae3d | 208 | } |
dreamselec | 27:2cb1bdb7ae3d | 209 | } |
dreamselec | 27:2cb1bdb7ae3d | 210 | } |
dreamselec | 27:2cb1bdb7ae3d | 211 | } else if (currentMode == None) { |
dreamselec | 27:2cb1bdb7ae3d | 212 | setModeNone: |
dreamselec | 27:2cb1bdb7ae3d | 213 | D_LEDS_OFF(); |
dreamselec | 27:2cb1bdb7ae3d | 214 | i2cport->write_bit(1,15); |
dreamselec | 27:2cb1bdb7ae3d | 215 | displayPCStatus(); |
dreamselec | 27:2cb1bdb7ae3d | 216 | while (currentMode == None && abortOperation == false && connectedToPC == true) { |
dreamselec | 27:2cb1bdb7ae3d | 217 | if (i2cport->read_bit(11)) { |
dreamselec | 27:2cb1bdb7ae3d | 218 | abortOperation = displayAbortDialog(); |
dreamselec | 27:2cb1bdb7ae3d | 219 | // Cancel the Abort |
dreamselec | 27:2cb1bdb7ae3d | 220 | if (abortOperation == false) { |
dreamselec | 27:2cb1bdb7ae3d | 221 | displayPCStatus(); |
dreamselec | 27:2cb1bdb7ae3d | 222 | i2cport->write_bit(1, 15); |
dreamselec | 27:2cb1bdb7ae3d | 223 | } |
dreamselec | 27:2cb1bdb7ae3d | 224 | } |
dreamselec | 27:2cb1bdb7ae3d | 225 | } |
dreamselec | 27:2cb1bdb7ae3d | 226 | } |
dreamselec | 27:2cb1bdb7ae3d | 227 | } |
dreamselec | 15:777390eb5afd | 228 | |
dreamselec | 27:2cb1bdb7ae3d | 229 | if (abortOperation == true ) { |
dreamselec | 27:2cb1bdb7ae3d | 230 | connectedToPC = false; |
dreamselec | 27:2cb1bdb7ae3d | 231 | D_LEDS_OFF(); |
dreamselec | 27:2cb1bdb7ae3d | 232 | break; |
dreamselec | 27:2cb1bdb7ae3d | 233 | } |
dreamselec | 27:2cb1bdb7ae3d | 234 | } |
dreamselec | 27:2cb1bdb7ae3d | 235 | } |
dreamselec | 19:61b21ac4896e | 236 | |
dreamselec | 27:2cb1bdb7ae3d | 237 | } |
dreamselec |
18:44a1c1a30166 | 238 | } |
dreamselec |
8:e1da2ae62885 | 239 | |
dreamselec |
18:44a1c1a30166 | 240 | // Waits until detects block. |
dreamselec |
18:44a1c1a30166 | 241 | // true if block detected, false if cancelled/connected to PC. |
dreamselec | 19:61b21ac4896e | 242 | bool waitForBlock() |
dreamselec | 19:61b21ac4896e | 243 | { |
dreamselec | 27:2cb1bdb7ae3d | 244 | myLED4 = 0; |
dreamselec | 27:2cb1bdb7ae3d | 245 | myLED1 = 1; |
dreamselec | 27:2cb1bdb7ae3d | 246 | if (connectedToPC == false) { |
dreamselec | 27:2cb1bdb7ae3d | 247 | bool abortOperation = false; |
dreamselec | 27:2cb1bdb7ae3d | 248 | int blockInserted = 0; |
dreamselec | 27:2cb1bdb7ae3d | 249 | // Wait until a block is breaking the beam, or button 4 is pressed to abort. |
dreamselec | 27:2cb1bdb7ae3d | 250 | do { |
dreamselec | 27:2cb1bdb7ae3d | 251 | blockInserted = fpga->getBeamValue(Top); |
dreamselec | 27:2cb1bdb7ae3d | 252 | myLED4 = blockInserted; |
dreamselec | 27:2cb1bdb7ae3d | 253 | if (i2cport->read_bit(11)) { |
dreamselec | 27:2cb1bdb7ae3d | 254 | abortOperation = displayAbortDialog(); |
dreamselec | 27:2cb1bdb7ae3d | 255 | // Cancel the Abort |
dreamselec | 27:2cb1bdb7ae3d | 256 | if (abortOperation == false) { |
dreamselec | 27:2cb1bdb7ae3d | 257 | displayWaitingLine(); |
dreamselec | 27:2cb1bdb7ae3d | 258 | } |
dreamselec | 27:2cb1bdb7ae3d | 259 | } |
dreamselec | 27:2cb1bdb7ae3d | 260 | } while (abortOperation == false && blockInserted != 1 && connectedToPC == false); |
dreamselec |
18:44a1c1a30166 | 261 | |
dreamselec | 27:2cb1bdb7ae3d | 262 | if (abortOperation == true || connectedToPC == true) |
dreamselec | 27:2cb1bdb7ae3d | 263 | return false; |
dreamselec | 27:2cb1bdb7ae3d | 264 | else |
dreamselec | 27:2cb1bdb7ae3d | 265 | return true; |
dreamselec | 27:2cb1bdb7ae3d | 266 | } else if (connectedToPC == true) { |
dreamselec | 27:2cb1bdb7ae3d | 267 | bool abortOperation = false; |
dreamselec | 27:2cb1bdb7ae3d | 268 | int blockInserted = 0; |
dreamselec | 27:2cb1bdb7ae3d | 269 | // Wait until a block is breaking the beam, or button 4 is pressed to abort. |
dreamselec | 28:7e4d29977d72 | 270 | pc.printf("INFO: Waiting for block.\n"); |
dreamselec | 27:2cb1bdb7ae3d | 271 | do { |
dreamselec | 27:2cb1bdb7ae3d | 272 | blockInserted = fpga->getBeamValue(Top); |
dreamselec | 27:2cb1bdb7ae3d | 273 | myLED4 = blockInserted; |
dreamselec | 28:7e4d29977d72 | 274 | if (readSwitches() == 4) { |
dreamselec | 27:2cb1bdb7ae3d | 275 | abortOperation = displayAbortDialog(); |
dreamselec | 27:2cb1bdb7ae3d | 276 | // Cancel the Abort |
dreamselec | 27:2cb1bdb7ae3d | 277 | if (abortOperation == false) { |
dreamselec | 27:2cb1bdb7ae3d | 278 | displayWaitingLine(); |
dreamselec | 27:2cb1bdb7ae3d | 279 | lcd->printf("for block"); |
dreamselec | 27:2cb1bdb7ae3d | 280 | i2cport->write_bit(1, 15); |
dreamselec | 27:2cb1bdb7ae3d | 281 | } |
dreamselec | 27:2cb1bdb7ae3d | 282 | } |
dreamselec | 27:2cb1bdb7ae3d | 283 | } while (abortOperation == false && blockInserted != 1 && connectedToPC == true && currentState == Start && currentMode == Normal); |
dreamselec | 19:61b21ac4896e | 284 | |
dreamselec | 28:7e4d29977d72 | 285 | if (abortOperation == true || connectedToPC == false || currentState == Pause || currentMode != Normal) |
dreamselec | 27:2cb1bdb7ae3d | 286 | return false; |
dreamselec | 27:2cb1bdb7ae3d | 287 | else |
dreamselec | 27:2cb1bdb7ae3d | 288 | return true; |
dreamselec | 27:2cb1bdb7ae3d | 289 | } |
dreamselec | 27:2cb1bdb7ae3d | 290 | return false; |
dreamselec |
18:44a1c1a30166 | 291 | } |
dreamselec |
14:cf2f255b5560 | 292 | |
dreamselec | 19:61b21ac4896e | 293 | void sortBlock() |
dreamselec | 19:61b21ac4896e | 294 | { |
dreamselec | 28:7e4d29977d72 | 295 | pc.printf("BLOCK: Sorting block"); |
dreamselec | 27:2cb1bdb7ae3d | 296 | myLED1 = 0; |
dreamselec | 27:2cb1bdb7ae3d | 297 | myLED2 = 1; |
dreamselec | 27:2cb1bdb7ae3d | 298 | // Cannot Abort any longer. Block is inserted. |
dreamselec | 27:2cb1bdb7ae3d | 299 | // Detach rx interrupt until block processed. |
dreamselec | 27:2cb1bdb7ae3d | 300 | NVIC_DisableIRQ(UART1_IRQn); |
dreamselec | 27:2cb1bdb7ae3d | 301 | fpga->moveSortingServo(Haz); |
dreamselec | 27:2cb1bdb7ae3d | 302 | fpga->moveStoppingServo(Go); |
dreamselec | 21:60c4fe94d79c | 303 | |
dreamselec | 27:2cb1bdb7ae3d | 304 | int colourValues[4]; |
dreamselec | 27:2cb1bdb7ae3d | 305 | int averageColourValues[4] = {0, 0, 0, 0}; |
dreamselec | 27:2cb1bdb7ae3d | 306 | int numberOfReadings = 3; |
dreamselec | 27:2cb1bdb7ae3d | 307 | for (int i = 0; i < 4; i++) { |
dreamselec | 27:2cb1bdb7ae3d | 308 | rgbSensor.getAllColors(colourValues); |
dreamselec | 27:2cb1bdb7ae3d | 309 | for (int j = 0; j < 4; j++) { |
dreamselec | 27:2cb1bdb7ae3d | 310 | averageColourValues[j] += colourValues[j]; |
dreamselec | 27:2cb1bdb7ae3d | 311 | } |
dreamselec | 27:2cb1bdb7ae3d | 312 | } |
dreamselec | 27:2cb1bdb7ae3d | 313 | for (int i = 0; i < 4; i++) { |
dreamselec | 27:2cb1bdb7ae3d | 314 | averageColourValues[i] = averageColourValues[i] / numberOfReadings; |
dreamselec | 27:2cb1bdb7ae3d | 315 | } |
dreamselec |
18:44a1c1a30166 | 316 | |
dreamselec | 27:2cb1bdb7ae3d | 317 | lastBlockHaz = false; |
dreamselec | 27:2cb1bdb7ae3d | 318 | lastBlockHaz = checkColour(averageColourValues); |
dreamselec | 28:7e4d29977d72 | 319 | // lastBlockHaz = checkColour(colourValues); |
dreamselec | 27:2cb1bdb7ae3d | 320 | |
dreamselec | 27:2cb1bdb7ae3d | 321 | if (!lastBlockHaz) { |
dreamselec | 27:2cb1bdb7ae3d | 322 | fpga->moveSortingServo(NonHaz); |
dreamselec | 27:2cb1bdb7ae3d | 323 | } |
dreamselec |
23:db91aaa43a9e | 324 | |
dreamselec | 27:2cb1bdb7ae3d | 325 | myLED3 = 1; |
dreamselec | 27:2cb1bdb7ae3d | 326 | int blockSize; |
dreamselec | 27:2cb1bdb7ae3d | 327 | while (fpga->checkForBlock() == 0) { } |
dreamselec | 27:2cb1bdb7ae3d | 328 | blockSize = fpga->checkForSize(); |
dreamselec | 27:2cb1bdb7ae3d | 329 | if (blockSize == HazBlock->size && lastBlockHaz) { |
dreamselec | 27:2cb1bdb7ae3d | 330 | // fpga->moveSortingServo(Haz); |
dreamselec | 27:2cb1bdb7ae3d | 331 | // fpga->moveStoppingServo(Go); |
dreamselec | 27:2cb1bdb7ae3d | 332 | // blockSize = HazBlock->size; |
dreamselec | 27:2cb1bdb7ae3d | 333 | while(fpga->getBeamValue(Bottom) == 1) {} |
dreamselec | 27:2cb1bdb7ae3d | 334 | wait(kServoWait); |
dreamselec | 27:2cb1bdb7ae3d | 335 | fpga->moveStoppingServo(Stop); |
dreamselec | 27:2cb1bdb7ae3d | 336 | } else { |
dreamselec | 27:2cb1bdb7ae3d | 337 | lastBlockHaz = false; |
dreamselec | 27:2cb1bdb7ae3d | 338 | } |
dreamselec | 27:2cb1bdb7ae3d | 339 | fpga->moveSortingServo(NonHaz); |
dreamselec | 27:2cb1bdb7ae3d | 340 | while(fpga->checkForSize()) {} |
dreamselec | 20:4e0f0944f28f | 341 | |
dreamselec | 27:2cb1bdb7ae3d | 342 | if (connectedToPC) { |
dreamselec | 27:2cb1bdb7ae3d | 343 | for (int i = 0; i < 3; i++) { |
dreamselec | 27:2cb1bdb7ae3d | 344 | pc.printf("DEBUG:Percentage Error: %.5f.\n", percentageError[i]); |
dreamselec | 27:2cb1bdb7ae3d | 345 | if ((percentageError[i] < 0 && std::abs(percentageError[i]) < kMinError[i] * errorMultiplier) || percentageError[i] == 0 || (percentageError[i] > 0 && percentageError[i] < kMaxError[i] * errorMultiplier)) |
dreamselec | 27:2cb1bdb7ae3d | 346 | pc.printf("DEBUG:%i Pass.\n", i); |
dreamselec | 27:2cb1bdb7ae3d | 347 | } |
dreamselec | 27:2cb1bdb7ae3d | 348 | pc.printf("BLOCK:Size:%i,Red:%.3f,Green:%.3f,Blue:%.3f,Haz:%i, Offsetred:%.3f, Offsetgreen:%.3f, Offsetblue:%.3f;", blockSize, adjustedValues[0], adjustedValues[1], adjustedValues[2], lastBlockHaz, percentageError[0], percentageError[1], percentageError[2]); |
dreamselec | 27:2cb1bdb7ae3d | 349 | // pc.printf("VALUE:Size:%i,Red:%i,Green:%i,Blue:%i,Clear:%i\n:VALUE", blockSize, colourValues[0], colourValues[1], colourValues[2], colourValues[3], lastBlockHaz); |
dreamselec | 27:2cb1bdb7ae3d | 350 | } |
dreamselec |
18:44a1c1a30166 | 351 | |
dreamselec | 27:2cb1bdb7ae3d | 352 | // Re-Attach rx interrupt |
dreamselec | 27:2cb1bdb7ae3d | 353 | NVIC_EnableIRQ(UART1_IRQn); |
dreamselec | 27:2cb1bdb7ae3d | 354 | |
dreamselec | 27:2cb1bdb7ae3d | 355 | blockCount++; |
dreamselec | 27:2cb1bdb7ae3d | 356 | myLED3 = 0; |
dreamselec | 27:2cb1bdb7ae3d | 357 | myLED4 = 1; |
dreamselec | 27:2cb1bdb7ae3d | 358 | return; |
dreamselec |
6:98fe30430194 | 359 | } |
dreamselec |
6:98fe30430194 | 360 | |
dreamselec |
6:98fe30430194 | 361 | /// Called every-time it receives an char from PC. |
dreamselec | 10:16ba52f8e025 | 362 | void Rx_interrupt() |
dreamselec | 10:16ba52f8e025 | 363 | { |
dreamselec | 28:7e4d29977d72 | 364 | recievingResponse = true; |
dreamselec | 27:2cb1bdb7ae3d | 365 | char interruptChar = pc.getc(); |
dreamselec | 27:2cb1bdb7ae3d | 366 | // Uncomment to Echo to USB serial to watch data flow |
dreamselec | 27:2cb1bdb7ae3d | 367 | // pc.putc(interruptChar); |
dreamselec |
6:98fe30430194 | 368 | |
dreamselec | 27:2cb1bdb7ae3d | 369 | NVIC_DisableIRQ(UART1_IRQn); |
dreamselec |
8:e1da2ae62885 | 370 | |
dreamselec | 27:2cb1bdb7ae3d | 371 | if (interruptChar == CommandTypeValue[Query]) { |
dreamselec | 27:2cb1bdb7ae3d | 372 | commander->decodeCommand(Query); |
dreamselec | 27:2cb1bdb7ae3d | 373 | } else if (interruptChar == CommandTypeValue[Set]) { |
dreamselec | 27:2cb1bdb7ae3d | 374 | commander->decodeCommand(Set); |
dreamselec | 27:2cb1bdb7ae3d | 375 | } else if (interruptChar== CommandTypeValue[Reply]) { |
dreamselec | 27:2cb1bdb7ae3d | 376 | commander->decodeCommand(Reply); |
dreamselec | 27:2cb1bdb7ae3d | 377 | } |
dreamselec |
8:e1da2ae62885 | 378 | |
dreamselec | 27:2cb1bdb7ae3d | 379 | NVIC_EnableIRQ(UART1_IRQn); |
dreamselec | 28:7e4d29977d72 | 380 | recievingResponse = false; |
dreamselec |
6:98fe30430194 | 381 | } |
dreamselec | 3:843b830ee8bd | 382 | |
dreamselec | 4:4eebb4de22a7 | 383 | void initInternal() |
dreamselec | 4:4eebb4de22a7 | 384 | { |
dreamselec | 27:2cb1bdb7ae3d | 385 | myLED1 = 1; |
dreamselec | 27:2cb1bdb7ae3d | 386 | i2cport = new MCP23017(p9, p10, 0x40); |
dreamselec | 27:2cb1bdb7ae3d | 387 | lcd = new WattBob_TextLCD(i2cport); |
dreamselec | 27:2cb1bdb7ae3d | 388 | BACKLIGHT_ON(i2cport); |
dreamselec | 27:2cb1bdb7ae3d | 389 | lcd->cls(); |
dreamselec | 27:2cb1bdb7ae3d | 390 | lcd->locate(0,0); |
dreamselec | 27:2cb1bdb7ae3d | 391 | lcd->printf("Initilizing..."); |
dreamselec | 27:2cb1bdb7ae3d | 392 | DefaultHazBlock(); |
dreamselec | 28:7e4d29977d72 | 393 | rgbSensor.enablePowerAndRGBC(); |
dreamselec | 28:7e4d29977d72 | 394 | rgbSensor.setIntegrationTime(gIntegrationTime); |
dreamselec | 28:7e4d29977d72 | 395 | srand((unsigned)time(NULL)); |
dreamselec | 27:2cb1bdb7ae3d | 396 | myLED2 = 1; |
dreamselec | 28:7e4d29977d72 | 397 | fpga->moveStoppingServo(Stop); |
dreamselec | 28:7e4d29977d72 | 398 | fpga->moveSortingServo(NonHaz); |
dreamselec | 27:2cb1bdb7ae3d | 399 | return; |
dreamselec | 3:843b830ee8bd | 400 | } |
dreamselec | 3:843b830ee8bd | 401 | |
dreamselec | 7:b6e31bfdb2af | 402 | void initPort(int baudRate) |
dreamselec | 4:4eebb4de22a7 | 403 | { |
dreamselec | 27:2cb1bdb7ae3d | 404 | myLED3 = 1; |
dreamselec | 27:2cb1bdb7ae3d | 405 | pc.baud(baudRate); |
dreamselec | 27:2cb1bdb7ae3d | 406 | pc.format(8, SerialBase::None, gStopBits); |
dreamselec | 28:7e4d29977d72 | 407 | pc.attach(&Rx_interrupt, Serial::RxIrq); |
dreamselec | 28:7e4d29977d72 | 408 | myLED4 = 1; |
dreamselec | 28:7e4d29977d72 | 409 | pc.printf(":<pc>connect;"); |
dreamselec | 28:7e4d29977d72 | 410 | // wait (0.3); |
dreamselec | 28:7e4d29977d72 | 411 | // while(recievingResponse == true) {} |
dreamselec | 28:7e4d29977d72 | 412 | turnOffTopLEDs(); |
dreamselec | 27:2cb1bdb7ae3d | 413 | return; |
dreamselec | 3:843b830ee8bd | 414 | } |
dreamselec | 3:843b830ee8bd | 415 | |
dreamselec |
18:44a1c1a30166 | 416 | bool checkColour(int colourValues[]) |
dreamselec | 15:777390eb5afd | 417 | { |
dreamselec | 27:2cb1bdb7ae3d | 418 | bool isHazColour[3] = {false, false, false }; |
dreamselec | 27:2cb1bdb7ae3d | 419 | memset(adjustedValues, 0, sizeof(adjustedValues)); |
dreamselec | 27:2cb1bdb7ae3d | 420 | memset(percentageError, 0, sizeof(percentageError)); |
dreamselec | 27:2cb1bdb7ae3d | 421 | |
dreamselec | 27:2cb1bdb7ae3d | 422 | /* Harcoded working |
dreamselec | 27:2cb1bdb7ae3d | 423 | for (int i = 0; i < 3; i++) { |
dreamselec | 27:2cb1bdb7ae3d | 424 | adjustedValues[i] = (float)colourValues[i]/(float)colourValues[3]; |
dreamselec | 27:2cb1bdb7ae3d | 425 | percentageError[i] = (adjustedValues[i] - kAverageRedBlock[i]) / kAverageRedBlock[i]; |
dreamselec | 15:777390eb5afd | 426 | |
dreamselec | 27:2cb1bdb7ae3d | 427 | if ((percentageError[i] < 0 && std::abs(percentageError[i]) < kMinError[i] * 2) || percentageError[i] == 0 || (percentageError[i] > 0 && percentageError[i] < kMaxError[i] * 2)) { |
dreamselec | 27:2cb1bdb7ae3d | 428 | isHazColour[i] = true; |
dreamselec | 27:2cb1bdb7ae3d | 429 | } |
dreamselec | 27:2cb1bdb7ae3d | 430 | } |
dreamselec | 27:2cb1bdb7ae3d | 431 | */ |
dreamselec | 27:2cb1bdb7ae3d | 432 | for (int i = 0; i < 3; i++) { |
dreamselec | 27:2cb1bdb7ae3d | 433 | adjustedValues[i] = (float)colourValues[i]/(float)colourValues[3]; |
dreamselec | 27:2cb1bdb7ae3d | 434 | percentageError[i] = (adjustedValues[i] - kAverageRedBlock[i]) / kAverageRedBlock[i]; |
dreamselec | 15:777390eb5afd | 435 | |
dreamselec | 27:2cb1bdb7ae3d | 436 | if ((percentageError[i] < 0 && std::abs(percentageError[i]) < currentMinError[i] * errorMultiplier) || percentageError[i] == 0 || (percentageError[i] > 0 && percentageError[i] < currentMaxError[i] * errorMultiplier)) { |
dreamselec | 27:2cb1bdb7ae3d | 437 | isHazColour[i] = true; |
dreamselec | 27:2cb1bdb7ae3d | 438 | } |
dreamselec | 27:2cb1bdb7ae3d | 439 | } |
dreamselec | 15:777390eb5afd | 440 | |
dreamselec | 27:2cb1bdb7ae3d | 441 | // for (int i = 0; i < 3; i++) { |
dreamselec | 27:2cb1bdb7ae3d | 442 | // if (colourValues[i] < HazBlock->maxColour.components[i] && colourValues[i] > HazBlock->minColour.components[i]) { |
dreamselec | 27:2cb1bdb7ae3d | 443 | // isHazColour[i] = true; |
dreamselec | 27:2cb1bdb7ae3d | 444 | // } |
dreamselec | 27:2cb1bdb7ae3d | 445 | // } |
dreamselec | 27:2cb1bdb7ae3d | 446 | |
dreamselec | 27:2cb1bdb7ae3d | 447 | bool isHazBlock = false; |
dreamselec | 15:777390eb5afd | 448 | |
dreamselec | 27:2cb1bdb7ae3d | 449 | if (isHazColour[0] && isHazColour[1] && isHazColour[2]) { |
dreamselec | 27:2cb1bdb7ae3d | 450 | isHazBlock = true; |
dreamselec | 27:2cb1bdb7ae3d | 451 | } else { |
dreamselec | 27:2cb1bdb7ae3d | 452 | isHazBlock = false; |
dreamselec | 27:2cb1bdb7ae3d | 453 | } |
dreamselec | 27:2cb1bdb7ae3d | 454 | myLED2 = 0; |
dreamselec | 27:2cb1bdb7ae3d | 455 | return isHazBlock; |
dreamselec | 3:843b830ee8bd | 456 | } |
dreamselec | 3:843b830ee8bd | 457 | |
dreamselec |
6:98fe30430194 | 458 | void printPCDetectedText() |
dreamselec | 4:4eebb4de22a7 | 459 | { |
dreamselec | 27:2cb1bdb7ae3d | 460 | lcd->cls(); |
dreamselec | 27:2cb1bdb7ae3d | 461 | LCDFL(); |
dreamselec | 27:2cb1bdb7ae3d | 462 | lcd->printf("Detected PC."); |
dreamselec | 27:2cb1bdb7ae3d | 463 | LCDSL(); |
dreamselec | 27:2cb1bdb7ae3d | 464 | lcd->printf("Connecting"); |
dreamselec | 27:2cb1bdb7ae3d | 465 | initPort(); |
dreamselec | 10:16ba52f8e025 | 466 | } |
dreamselec | 10:16ba52f8e025 | 467 | |
dreamselec | 10:16ba52f8e025 | 468 | bool displayAbortDialog() |
dreamselec | 10:16ba52f8e025 | 469 | { |
dreamselec | 27:2cb1bdb7ae3d | 470 | while (i2cport->read_bit(11) == 1) {} |
dreamselec | 27:2cb1bdb7ae3d | 471 | i2cport->write_bit(1, 12); |
dreamselec | 10:16ba52f8e025 | 472 | |
dreamselec | 27:2cb1bdb7ae3d | 473 | lcd->cls(); |
dreamselec | 27:2cb1bdb7ae3d | 474 | LCDFL(); |
dreamselec | 27:2cb1bdb7ae3d | 475 | lcd->printf("Abort?"); |
dreamselec | 27:2cb1bdb7ae3d | 476 | LCDSL(); |
dreamselec | 27:2cb1bdb7ae3d | 477 | lcd->printf("1:Yes, 2,3,4:No"); |
dreamselec | 27:2cb1bdb7ae3d | 478 | int reply = 0; |
dreamselec | 27:2cb1bdb7ae3d | 479 | do { |
dreamselec | 27:2cb1bdb7ae3d | 480 | reply = readSwitches(); |
dreamselec | 27:2cb1bdb7ae3d | 481 | } while(reply == 0); |
dreamselec | 10:16ba52f8e025 | 482 | |
dreamselec | 27:2cb1bdb7ae3d | 483 | D_LEDS_OFF(); |
dreamselec | 27:2cb1bdb7ae3d | 484 | if (reply == 1) { |
dreamselec | 27:2cb1bdb7ae3d | 485 | // while (i2cport->read_bit(8)) { } |
dreamselec | 27:2cb1bdb7ae3d | 486 | return true; |
dreamselec | 27:2cb1bdb7ae3d | 487 | } else { |
dreamselec | 27:2cb1bdb7ae3d | 488 | // while (i2cport->read_bit(9) || i2cport->read_bit(10) || i2cport->read_bit(11)) { } |
dreamselec | 27:2cb1bdb7ae3d | 489 | return false; |
dreamselec | 27:2cb1bdb7ae3d | 490 | } |
dreamselec | 3:843b830ee8bd | 491 | } |
dreamselec | 3:843b830ee8bd | 492 | |
dreamselec | 15:777390eb5afd | 493 | void printServoInfoOnLCD() |
dreamselec | 15:777390eb5afd | 494 | { |
dreamselec | 27:2cb1bdb7ae3d | 495 | lcd->cls(); |
dreamselec | 27:2cb1bdb7ae3d | 496 | lcd->locate(0,0); |
dreamselec | 27:2cb1bdb7ae3d | 497 | if (fpga->stoppingServoPosition == Stop) |
dreamselec | 27:2cb1bdb7ae3d | 498 | lcd->printf("1:Top: Go"); |
dreamselec | 27:2cb1bdb7ae3d | 499 | else if (fpga->stoppingServoPosition == Go) |
dreamselec | 27:2cb1bdb7ae3d | 500 | lcd->printf("1:Top: Stop"); |
dreamselec |
23:db91aaa43a9e | 501 | |
dreamselec | 27:2cb1bdb7ae3d | 502 | lcd->locate(1,0); |
dreamselec | 27:2cb1bdb7ae3d | 503 | if (fpga->sortingServoPosition == NonHaz) |
dreamselec | 27:2cb1bdb7ae3d | 504 | lcd->printf("2:Bottom: Haz"); |
dreamselec | 27:2cb1bdb7ae3d | 505 | else if (fpga->sortingServoPosition == Haz) |
dreamselec | 27:2cb1bdb7ae3d | 506 | lcd->printf("2:Bottom: NonHaz"); |
dreamselec |
23:db91aaa43a9e | 507 | } |
dreamselec |
23:db91aaa43a9e | 508 | |
dreamselec | 27:2cb1bdb7ae3d | 509 | void printServoInfoOnPC() |
dreamselec | 27:2cb1bdb7ae3d | 510 | { |
dreamselec | 27:2cb1bdb7ae3d | 511 | if (fpga->stoppingServoPosition == Stop) |
dreamselec | 27:2cb1bdb7ae3d | 512 | pc.printf(":<servos>1=Stop;"); |
dreamselec | 27:2cb1bdb7ae3d | 513 | else if (fpga->stoppingServoPosition == Go) |
dreamselec | 27:2cb1bdb7ae3d | 514 | pc.printf(":<servos>1=Go;"); |
dreamselec |
23:db91aaa43a9e | 515 | |
dreamselec | 27:2cb1bdb7ae3d | 516 | if (fpga->sortingServoPosition == NonHaz) |
dreamselec | 27:2cb1bdb7ae3d | 517 | pc.printf(":<servos>2=NonHaz;"); |
dreamselec | 27:2cb1bdb7ae3d | 518 | else if (fpga->sortingServoPosition == Haz) |
dreamselec | 27:2cb1bdb7ae3d | 519 | pc.printf(":<servos>2=Haz;"); |
dreamselec | 10:16ba52f8e025 | 520 | } |
dreamselec | 10:16ba52f8e025 | 521 | |
dreamselec | 15:777390eb5afd | 522 | void runInServoTestMode() |
dreamselec | 15:777390eb5afd | 523 | { |
dreamselec | 27:2cb1bdb7ae3d | 524 | pc.printf("VALUES:Testing servos.\n Stopping servo:\n\tStop:%i, Go: %i\n Sorting servo:\n\tHaz:%i, NonHaz:%i\n:VALUES", Stop, Go, Haz, NonHaz); |
dreamselec | 27:2cb1bdb7ae3d | 525 | printServoInfoOnPC(); |
dreamselec | 27:2cb1bdb7ae3d | 526 | printServoInfoOnLCD(); |
dreamselec | 27:2cb1bdb7ae3d | 527 | |
dreamselec | 27:2cb1bdb7ae3d | 528 | i2cport->write_bit(1, 12); |
dreamselec | 27:2cb1bdb7ae3d | 529 | i2cport->write_bit(1, 13); |
dreamselec | 27:2cb1bdb7ae3d | 530 | i2cport->write_bit(1, 15); |
dreamselec | 27:2cb1bdb7ae3d | 531 | int button = 0; |
dreamselec | 27:2cb1bdb7ae3d | 532 | bool finished = false; |
dreamselec | 27:2cb1bdb7ae3d | 533 | do { |
dreamselec | 27:2cb1bdb7ae3d | 534 | button = readSwitches(); |
dreamselec | 27:2cb1bdb7ae3d | 535 | |
dreamselec | 27:2cb1bdb7ae3d | 536 | // gToggleServoNumber: 1 = Toggle top servo, 2 = Toggle bottom servo, 3 = Toggle both servos |
dreamselec | 27:2cb1bdb7ae3d | 537 | if (gToggleServoNumber == 1) { |
dreamselec | 27:2cb1bdb7ae3d | 538 | button = 1; |
dreamselec | 27:2cb1bdb7ae3d | 539 | gToggleServoNumber = 0; |
dreamselec | 27:2cb1bdb7ae3d | 540 | } else if (gToggleServoNumber == 2) { |
dreamselec | 27:2cb1bdb7ae3d | 541 | button = 2; |
dreamselec | 27:2cb1bdb7ae3d | 542 | gToggleServoNumber = 0; |
dreamselec | 27:2cb1bdb7ae3d | 543 | } |
dreamselec |
23:db91aaa43a9e | 544 | |
dreamselec | 27:2cb1bdb7ae3d | 545 | if (button == 1) { |
dreamselec | 27:2cb1bdb7ae3d | 546 | fpga->toggleStoppingServo(); |
dreamselec | 27:2cb1bdb7ae3d | 547 | printServoInfoOnLCD(); |
dreamselec | 27:2cb1bdb7ae3d | 548 | printServoInfoOnPC(); |
dreamselec | 27:2cb1bdb7ae3d | 549 | wait(kServoWait); |
dreamselec | 27:2cb1bdb7ae3d | 550 | } else if (button == 2) { |
dreamselec | 27:2cb1bdb7ae3d | 551 | fpga->toggleSortingServo(); |
dreamselec | 27:2cb1bdb7ae3d | 552 | printServoInfoOnLCD(); |
dreamselec | 27:2cb1bdb7ae3d | 553 | printServoInfoOnPC(); |
dreamselec | 27:2cb1bdb7ae3d | 554 | wait(kServoWait); |
dreamselec | 27:2cb1bdb7ae3d | 555 | } |
dreamselec |
23:db91aaa43a9e | 556 | |
dreamselec | 27:2cb1bdb7ae3d | 557 | finished = button == 4; |
dreamselec | 27:2cb1bdb7ae3d | 558 | button = 0; |
dreamselec | 27:2cb1bdb7ae3d | 559 | } while (finished == false && runServoTest == true); |
dreamselec | 27:2cb1bdb7ae3d | 560 | |
dreamselec | 27:2cb1bdb7ae3d | 561 | D_LEDS_OFF(); |
dreamselec | 27:2cb1bdb7ae3d | 562 | lcd->cls(); |
dreamselec | 27:2cb1bdb7ae3d | 563 | lcd->locate(0,0); |
dreamselec | 27:2cb1bdb7ae3d | 564 | lcd->printf("Done servo test"); |
dreamselec | 28:7e4d29977d72 | 565 | fpga->moveSortingServo(NonHaz); |
dreamselec | 27:2cb1bdb7ae3d | 566 | if (runServoTest == true) { |
dreamselec | 27:2cb1bdb7ae3d | 567 | pc.printf(":<servos>test=pause;"); |
dreamselec | 27:2cb1bdb7ae3d | 568 | runServoTest = false; |
dreamselec | 27:2cb1bdb7ae3d | 569 | } |
dreamselec | 27:2cb1bdb7ae3d | 570 | wait(0.5); |
dreamselec | 27:2cb1bdb7ae3d | 571 | lcd->cls(); |
dreamselec | 27:2cb1bdb7ae3d | 572 | return; |
dreamselec |
23:db91aaa43a9e | 573 | } |
dreamselec | 15:777390eb5afd | 574 | |
dreamselec | 27:2cb1bdb7ae3d | 575 | void printBeamInfoOnLCD() |
dreamselec | 27:2cb1bdb7ae3d | 576 | { |
dreamselec | 27:2cb1bdb7ae3d | 577 | lcd->cls(); |
dreamselec | 27:2cb1bdb7ae3d | 578 | lcd->locate(0,0); |
dreamselec | 27:2cb1bdb7ae3d | 579 | lcd->printf("Top:L1 Btm: L2"); |
dreamselec | 27:2cb1bdb7ae3d | 580 | lcd->locate(1,0); |
dreamselec | 27:2cb1bdb7ae3d | 581 | lcd->printf("On:High, Off:Low"); |
dreamselec |
23:db91aaa43a9e | 582 | } |
dreamselec |
23:db91aaa43a9e | 583 | |
dreamselec | 27:2cb1bdb7ae3d | 584 | void printBeamInfoOnPC(int topBeam, int bottomBeam) |
dreamselec | 27:2cb1bdb7ae3d | 585 | { |
dreamselec | 27:2cb1bdb7ae3d | 586 | pc.printf(":<break_beam>2=%i,1=%i;", topBeam, bottomBeam); |
dreamselec |
23:db91aaa43a9e | 587 | } |
dreamselec |
23:db91aaa43a9e | 588 | |
dreamselec | 27:2cb1bdb7ae3d | 589 | void runInBreakBeamTestMode() |
dreamselec | 27:2cb1bdb7ae3d | 590 | { |
dreamselec | 27:2cb1bdb7ae3d | 591 | turnOffTopLEDs(); |
dreamselec | 27:2cb1bdb7ae3d | 592 | i2cport->write_bit(1, 15); |
dreamselec |
23:db91aaa43a9e | 593 | |
dreamselec | 27:2cb1bdb7ae3d | 594 | int topBeamValue = fpga->getBeamValue(1); |
dreamselec | 27:2cb1bdb7ae3d | 595 | int bottomBeamValue = fpga->getBeamValue(2); |
dreamselec | 27:2cb1bdb7ae3d | 596 | printBeamInfoOnPC(topBeamValue, bottomBeamValue); |
dreamselec | 27:2cb1bdb7ae3d | 597 | printBeamInfoOnLCD(); |
dreamselec | 27:2cb1bdb7ae3d | 598 | |
dreamselec | 27:2cb1bdb7ae3d | 599 | int button = 0; |
dreamselec | 27:2cb1bdb7ae3d | 600 | bool finished = false; |
dreamselec | 27:2cb1bdb7ae3d | 601 | do { |
dreamselec | 27:2cb1bdb7ae3d | 602 | button = readSwitches(); |
dreamselec | 27:2cb1bdb7ae3d | 603 | int currentTopBeamValue = fpga->getBeamValue(1); |
dreamselec | 27:2cb1bdb7ae3d | 604 | int currentBottomBeamValue = fpga->getBeamValue(2); |
dreamselec |
18:44a1c1a30166 | 605 | |
dreamselec | 27:2cb1bdb7ae3d | 606 | // For debugging, hold down both 1 and 3 or 2 and 3 |
dreamselec | 27:2cb1bdb7ae3d | 607 | if (i2cport->read_bit(10) == 1) { |
dreamselec | 27:2cb1bdb7ae3d | 608 | currentTopBeamValue = i2cport->read_bit(8) && i2cport->read_bit(10); |
dreamselec | 27:2cb1bdb7ae3d | 609 | currentBottomBeamValue = i2cport->read_bit(9) && i2cport->read_bit(10); |
dreamselec | 27:2cb1bdb7ae3d | 610 | } |
dreamselec | 27:2cb1bdb7ae3d | 611 | myLED1 = currentTopBeamValue; |
dreamselec | 27:2cb1bdb7ae3d | 612 | myLED2 = currentBottomBeamValue; |
dreamselec |
23:db91aaa43a9e | 613 | |
dreamselec | 27:2cb1bdb7ae3d | 614 | if (currentTopBeamValue != topBeamValue || currentBottomBeamValue != bottomBeamValue ) { |
dreamselec | 27:2cb1bdb7ae3d | 615 | topBeamValue = currentTopBeamValue; |
dreamselec | 27:2cb1bdb7ae3d | 616 | bottomBeamValue = currentBottomBeamValue; |
dreamselec | 27:2cb1bdb7ae3d | 617 | printBeamInfoOnPC(topBeamValue, bottomBeamValue); |
dreamselec | 27:2cb1bdb7ae3d | 618 | } |
dreamselec | 27:2cb1bdb7ae3d | 619 | finished = button == 4; |
dreamselec | 27:2cb1bdb7ae3d | 620 | } while (runBreakBeamTest == true && finished == false); |
dreamselec |
23:db91aaa43a9e | 621 | |
dreamselec | 27:2cb1bdb7ae3d | 622 | turnOffBottomLEDs(); |
dreamselec | 27:2cb1bdb7ae3d | 623 | lcd->cls(); |
dreamselec | 27:2cb1bdb7ae3d | 624 | lcd->locate(0,0); |
dreamselec | 27:2cb1bdb7ae3d | 625 | lcd->printf("Finished test"); |
dreamselec | 27:2cb1bdb7ae3d | 626 | if (runBreakBeamTest == true) { |
dreamselec | 27:2cb1bdb7ae3d | 627 | pc.printf(":<break_beam>test=pause;"); |
dreamselec | 27:2cb1bdb7ae3d | 628 | runBreakBeamTest = false; |
dreamselec | 27:2cb1bdb7ae3d | 629 | } |
dreamselec | 27:2cb1bdb7ae3d | 630 | wait(0.5); |
dreamselec | 27:2cb1bdb7ae3d | 631 | return; |
dreamselec | 2:7a55cb10259f | 632 | } |
dreamselec | 15:777390eb5afd | 633 | |
dreamselec | 27:2cb1bdb7ae3d | 634 | void printColourSensorInfoOnLCD(int colourValues[]) |
dreamselec | 27:2cb1bdb7ae3d | 635 | { |
dreamselec | 27:2cb1bdb7ae3d | 636 | float weightedValues[4]; |
dreamselec | 27:2cb1bdb7ae3d | 637 | |
dreamselec | 27:2cb1bdb7ae3d | 638 | for (int i = 0; i < 3; i++) { |
dreamselec | 27:2cb1bdb7ae3d | 639 | weightedValues[i] = (float)colourValues[i] / (float)colourValues[3]; |
dreamselec | 27:2cb1bdb7ae3d | 640 | } |
dreamselec | 27:2cb1bdb7ae3d | 641 | //TODO: Print values on LCD |
dreamselec | 27:2cb1bdb7ae3d | 642 | lcd->cls(); |
dreamselec | 27:2cb1bdb7ae3d | 643 | lcd->locate(0,0); |
dreamselec | 27:2cb1bdb7ae3d | 644 | lcd->printf("Colour sensor"); |
dreamselec | 27:2cb1bdb7ae3d | 645 | lcd->locate(1,0); |
dreamselec | 27:2cb1bdb7ae3d | 646 | lcd->printf("Test mode"); |
dreamselec | 25:792540d69c49 | 647 | } |
dreamselec | 25:792540d69c49 | 648 | |
dreamselec | 27:2cb1bdb7ae3d | 649 | void printColourSensorInfoOnPC(int colourValues[]) |
dreamselec | 27:2cb1bdb7ae3d | 650 | { |
dreamselec | 27:2cb1bdb7ae3d | 651 | pc.printf(":<colour_sensor>red=%i,green=%i,blue=%i,clear=%i;", colourValues[0], colourValues[1], colourValues[2], colourValues[3]); |
dreamselec | 25:792540d69c49 | 652 | } |
dreamselec | 25:792540d69c49 | 653 | |
dreamselec | 27:2cb1bdb7ae3d | 654 | void runInColourSensorTestMode() |
dreamselec | 27:2cb1bdb7ae3d | 655 | { |
dreamselec | 27:2cb1bdb7ae3d | 656 | turnOffTopLEDs(); |
dreamselec | 27:2cb1bdb7ae3d | 657 | i2cport->write_bit(1, 15); |
dreamselec | 27:2cb1bdb7ae3d | 658 | i2cport->write_bit(1, 12); |
dreamselec | 27:2cb1bdb7ae3d | 659 | lcd->cls(); |
dreamselec | 27:2cb1bdb7ae3d | 660 | lcd->locate(0,0); |
dreamselec | 27:2cb1bdb7ae3d | 661 | lcd->printf("Colour Sensor"); |
dreamselec | 27:2cb1bdb7ae3d | 662 | lcd->locate(1,0); |
dreamselec | 27:2cb1bdb7ae3d | 663 | lcd->printf("Test Mode"); |
dreamselec | 27:2cb1bdb7ae3d | 664 | pc.printf("!<colour_sensor>i-time=%.3f", gIntegrationTime); |
dreamselec | 27:2cb1bdb7ae3d | 665 | |
dreamselec | 27:2cb1bdb7ae3d | 666 | int button = 0; |
dreamselec | 27:2cb1bdb7ae3d | 667 | bool finished = false; |
dreamselec | 27:2cb1bdb7ae3d | 668 | |
dreamselec | 27:2cb1bdb7ae3d | 669 | do { |
dreamselec | 27:2cb1bdb7ae3d | 670 | button = readSwitches(); |
dreamselec | 27:2cb1bdb7ae3d | 671 | |
dreamselec | 27:2cb1bdb7ae3d | 672 | if (getColourSensorValue == true) { |
dreamselec | 27:2cb1bdb7ae3d | 673 | int colourValues[4]; |
dreamselec | 27:2cb1bdb7ae3d | 674 | rgbSensor.getAllColors(colourValues); |
dreamselec | 27:2cb1bdb7ae3d | 675 | printColourSensorInfoOnPC(colourValues); |
dreamselec | 27:2cb1bdb7ae3d | 676 | printColourSensorInfoOnLCD(colourValues); |
dreamselec | 27:2cb1bdb7ae3d | 677 | getColourSensorValue = false; |
dreamselec | 27:2cb1bdb7ae3d | 678 | } else if (getBlockColourValue == true) { |
dreamselec | 27:2cb1bdb7ae3d | 679 | lcd->cls(); |
dreamselec | 27:2cb1bdb7ae3d | 680 | lcd->locate(0,0); |
dreamselec | 27:2cb1bdb7ae3d | 681 | lcd->printf("Drop block"); |
dreamselec | 27:2cb1bdb7ae3d | 682 | |
dreamselec | 27:2cb1bdb7ae3d | 683 | |
dreamselec | 27:2cb1bdb7ae3d | 684 | turnOffTopLEDs(); |
dreamselec | 27:2cb1bdb7ae3d | 685 | |
dreamselec | 27:2cb1bdb7ae3d | 686 | while(fpga->getBeamValue(Top) == 0) { |
dreamselec | 27:2cb1bdb7ae3d | 687 | myLED1 = fpga->getBeamValue(Top); |
dreamselec | 27:2cb1bdb7ae3d | 688 | myLED2 = fpga->getBeamValue(Bottom); |
dreamselec | 27:2cb1bdb7ae3d | 689 | } |
dreamselec | 27:2cb1bdb7ae3d | 690 | |
dreamselec | 27:2cb1bdb7ae3d | 691 | myLED3 = 1; |
dreamselec | 27:2cb1bdb7ae3d | 692 | int colourValues[3][4]; |
dreamselec | 27:2cb1bdb7ae3d | 693 | for (int i = 0; i < 3; i++) { |
dreamselec | 27:2cb1bdb7ae3d | 694 | memset(colourValues[i], 0, sizeof(colourValues)); |
dreamselec | 27:2cb1bdb7ae3d | 695 | } |
dreamselec | 27:2cb1bdb7ae3d | 696 | int valueCount = 0; |
dreamselec | 27:2cb1bdb7ae3d | 697 | |
dreamselec | 27:2cb1bdb7ae3d | 698 | do { |
dreamselec | 27:2cb1bdb7ae3d | 699 | rgbSensor.getAllColors(colourValues[valueCount]); |
dreamselec | 28:7e4d29977d72 | 700 | valueCount++; |
dreamselec | 27:2cb1bdb7ae3d | 701 | } while (fpga->getBeamValue(Top) == 1 && valueCount < 3); |
dreamselec | 27:2cb1bdb7ae3d | 702 | |
dreamselec | 27:2cb1bdb7ae3d | 703 | turnOffTopLEDs(); |
dreamselec | 27:2cb1bdb7ae3d | 704 | myLED4 = 1; |
dreamselec | 27:2cb1bdb7ae3d | 705 | |
dreamselec | 27:2cb1bdb7ae3d | 706 | int averageValues[4] = {0, 0, 0, 0}; |
dreamselec | 27:2cb1bdb7ae3d | 707 | for (int i = 0; i < 3; i++) { |
dreamselec | 27:2cb1bdb7ae3d | 708 | averageValues[0] += colourValues[i][0]; |
dreamselec | 27:2cb1bdb7ae3d | 709 | averageValues[1] += colourValues[i][1]; |
dreamselec | 27:2cb1bdb7ae3d | 710 | averageValues[2] += colourValues[i][2]; |
dreamselec | 27:2cb1bdb7ae3d | 711 | averageValues[3] += colourValues[i][3]; |
dreamselec | 27:2cb1bdb7ae3d | 712 | } |
dreamselec | 27:2cb1bdb7ae3d | 713 | averageValues[0] = averageValues[0] / valueCount; |
dreamselec | 27:2cb1bdb7ae3d | 714 | averageValues[1] = averageValues[1] / valueCount; |
dreamselec | 27:2cb1bdb7ae3d | 715 | averageValues[2] = averageValues[2] / valueCount; |
dreamselec | 27:2cb1bdb7ae3d | 716 | averageValues[3] = averageValues[3] / valueCount; |
dreamselec | 27:2cb1bdb7ae3d | 717 | myLED4 = 0; |
dreamselec | 27:2cb1bdb7ae3d | 718 | |
dreamselec | 27:2cb1bdb7ae3d | 719 | printColourSensorInfoOnPC(averageValues); |
dreamselec | 27:2cb1bdb7ae3d | 720 | printColourSensorInfoOnLCD(averageValues); |
dreamselec | 27:2cb1bdb7ae3d | 721 | |
dreamselec | 27:2cb1bdb7ae3d | 722 | getBlockColourValue = false; |
dreamselec | 27:2cb1bdb7ae3d | 723 | } else if (button == 1) { |
dreamselec | 27:2cb1bdb7ae3d | 724 | getColourSensorValue = true; |
dreamselec | 27:2cb1bdb7ae3d | 725 | button = 0; |
dreamselec | 27:2cb1bdb7ae3d | 726 | } |
dreamselec | 27:2cb1bdb7ae3d | 727 | |
dreamselec | 27:2cb1bdb7ae3d | 728 | finished = button == 4; |
dreamselec | 25:792540d69c49 | 729 | // button = 0; |
dreamselec | 27:2cb1bdb7ae3d | 730 | } while (finished == false && runColourSensorTest == true); |
dreamselec | 27:2cb1bdb7ae3d | 731 | |
dreamselec | 27:2cb1bdb7ae3d | 732 | turnOffBottomLEDs(); |
dreamselec | 27:2cb1bdb7ae3d | 733 | lcd->cls(); |
dreamselec | 27:2cb1bdb7ae3d | 734 | lcd->locate(0,0); |
dreamselec | 27:2cb1bdb7ae3d | 735 | lcd->printf("Finished test"); |
dreamselec | 27:2cb1bdb7ae3d | 736 | if (runColourSensorTest == true) { |
dreamselec | 27:2cb1bdb7ae3d | 737 | pc.printf(":<colour_sensor>test=pause;"); |
dreamselec | 27:2cb1bdb7ae3d | 738 | runColourSensorTest = false; |
dreamselec | 27:2cb1bdb7ae3d | 739 | } |
dreamselec | 27:2cb1bdb7ae3d | 740 | wait(0.5); |
dreamselec | 27:2cb1bdb7ae3d | 741 | return; |
dreamselec | 25:792540d69c49 | 742 | } |
dreamselec | 25:792540d69c49 | 743 | |
dreamselec | 15:777390eb5afd | 744 | void displayWaitingLine() |
dreamselec | 15:777390eb5afd | 745 | { |
dreamselec | 27:2cb1bdb7ae3d | 746 | lcd->cls(); |
dreamselec | 27:2cb1bdb7ae3d | 747 | lcd->locate(0,0); |
dreamselec | 27:2cb1bdb7ae3d | 748 | lcd->printf("Waiting..."); |
dreamselec | 27:2cb1bdb7ae3d | 749 | lcd->locate(1,0); |
dreamselec | 15:777390eb5afd | 750 | } |
dreamselec | 15:777390eb5afd | 751 | |
dreamselec | 15:777390eb5afd | 752 | void displayPCStatus() |
dreamselec | 15:777390eb5afd | 753 | { |
dreamselec | 27:2cb1bdb7ae3d | 754 | lcd->cls(); |
dreamselec | 27:2cb1bdb7ae3d | 755 | lcd->locate(0,0); |
dreamselec | 27:2cb1bdb7ae3d | 756 | if (connectedToPC == true) { |
dreamselec | 27:2cb1bdb7ae3d | 757 | if (currentMode == Normal) |
dreamselec | 27:2cb1bdb7ae3d | 758 | lcd->printf("Normal mode."); |
dreamselec | 27:2cb1bdb7ae3d | 759 | else if (currentMode == Maintanence) |
dreamselec | 27:2cb1bdb7ae3d | 760 | lcd->printf("Maintanence."); |
dreamselec | 27:2cb1bdb7ae3d | 761 | else if (currentMode == None) |
dreamselec | 27:2cb1bdb7ae3d | 762 | lcd->printf("Connected to PC"); |
dreamselec | 27:2cb1bdb7ae3d | 763 | } else |
dreamselec | 27:2cb1bdb7ae3d | 764 | lcd->printf("Waiting for PC.."); |
dreamselec | 15:777390eb5afd | 765 | |
dreamselec | 27:2cb1bdb7ae3d | 766 | lcd->locate(1,0); |
dreamselec | 27:2cb1bdb7ae3d | 767 | lcd->printf("4:Disconnect"); |
dreamselec | 27:2cb1bdb7ae3d | 768 | D_LEDS_OFF(); |
dreamselec | 27:2cb1bdb7ae3d | 769 | i2cport->write_bit(1,15); |
dreamselec |
18:44a1c1a30166 | 770 | } |
dreamselec |
23:db91aaa43a9e | 771 | |
dreamselec | 27:2cb1bdb7ae3d | 772 | void turnOffTopLEDs() |
dreamselec | 27:2cb1bdb7ae3d | 773 | { |
dreamselec | 27:2cb1bdb7ae3d | 774 | myLED1 = 0; |
dreamselec | 27:2cb1bdb7ae3d | 775 | myLED2 = 0; |
dreamselec | 27:2cb1bdb7ae3d | 776 | myLED3 = 0; |
dreamselec | 27:2cb1bdb7ae3d | 777 | myLED4 = 0; |
dreamselec |
23:db91aaa43a9e | 778 | } |
dreamselec |
23:db91aaa43a9e | 779 | |
dreamselec | 27:2cb1bdb7ae3d | 780 | void turnOffBottomLEDs() |
dreamselec | 27:2cb1bdb7ae3d | 781 | { |
dreamselec | 27:2cb1bdb7ae3d | 782 | i2cport->write_bit(0, 12); |
dreamselec | 27:2cb1bdb7ae3d | 783 | i2cport->write_bit(0, 13); |
dreamselec | 27:2cb1bdb7ae3d | 784 | i2cport->write_bit(0, 14); |
dreamselec | 27:2cb1bdb7ae3d | 785 | i2cport->write_bit(0, 15); |
dreamselec |
23:db91aaa43a9e | 786 | } |