![](/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@9:dc8f155b71c8, 2015-11-16 (annotated)
- Committer:
- dreamselec
- Date:
- Mon Nov 16 23:44:44 2015 +0000
- Revision:
- 9:dc8f155b71c8
- Parent:
- 8:e1da2ae62885
- Child:
- 10:16ba52f8e025
Testing of command parsing successful. Implementing ability to declare new has block.
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 | 0:fe5cb0a8fc5a | 27 | |
dreamselec | 2:7a55cb10259f | 28 | TCS3472_I2C rgb_sensor(p28, p27); |
dreamselec | 2:7a55cb10259f | 29 | Serial pc(USBTX, USBRX); |
dreamselec |
8:e1da2ae62885 | 30 | uint8_t rxBuffer[kSmallBufferSize + 1]; |
dreamselec |
8:e1da2ae62885 | 31 | int rxIndex = 0; |
dreamselec | 2:7a55cb10259f | 32 | |
dreamselec |
6:98fe30430194 | 33 | Commander _commander = Commander(); |
dreamselec |
6:98fe30430194 | 34 | Commander *commander = &_commander; |
dreamselec |
6:98fe30430194 | 35 | FPGA _fpga = FPGA(); |
dreamselec | 9:dc8f155b71c8 | 36 | extern FPGA *fpga = &_fpga; |
dreamselec |
6:98fe30430194 | 37 | |
dreamselec |
6:98fe30430194 | 38 | extern Block _HazBlock; |
dreamselec |
6:98fe30430194 | 39 | Block *HazBlock = &_HazBlock; |
dreamselec |
6:98fe30430194 | 40 | |
dreamselec | 3:843b830ee8bd | 41 | void detectPC(); |
dreamselec |
6:98fe30430194 | 42 | void initInternal(); |
dreamselec | 7:b6e31bfdb2af | 43 | void initPort(int baudRate=kDefaultBaudRate); |
dreamselec |
6:98fe30430194 | 44 | int readSwitches(); |
dreamselec |
6:98fe30430194 | 45 | void printPCDetectedText(); |
dreamselec |
6:98fe30430194 | 46 | bool displayAboardDialog(); |
dreamselec |
6:98fe30430194 | 47 | void Rx_interrupt(); |
dreamselec |
6:98fe30430194 | 48 | |
dreamselec |
6:98fe30430194 | 49 | |
dreamselec |
6:98fe30430194 | 50 | int main() |
dreamselec |
6:98fe30430194 | 51 | { |
dreamselec |
6:98fe30430194 | 52 | initInternal(); |
dreamselec |
6:98fe30430194 | 53 | initPort(); |
dreamselec |
6:98fe30430194 | 54 | srand((unsigned)time(NULL)); |
dreamselec |
6:98fe30430194 | 55 | U_LEDS_OFF(); |
dreamselec |
6:98fe30430194 | 56 | lcd->cls(); |
dreamselec |
6:98fe30430194 | 57 | myLED1 = 1; |
dreamselec |
6:98fe30430194 | 58 | |
dreamselec |
6:98fe30430194 | 59 | // Create a serial intereput for RxIrq so when PC is connected it sends '$' to tell MBED it's there. |
dreamselec |
6:98fe30430194 | 60 | // https://developer.mbed.org/cookbook/Serial-Interrupts |
dreamselec |
6:98fe30430194 | 61 | pc.attach(&Rx_interrupt, Serial::RxIrq); |
dreamselec |
6:98fe30430194 | 62 | |
dreamselec |
6:98fe30430194 | 63 | for (;;) { |
dreamselec | 9:dc8f155b71c8 | 64 | lcd->cls(); |
dreamselec |
6:98fe30430194 | 65 | i2cport->write_bit(1, 12); |
dreamselec |
6:98fe30430194 | 66 | lcd->printf("1: Start sorting."); |
dreamselec |
6:98fe30430194 | 67 | LCDSL(); |
dreamselec |
6:98fe30430194 | 68 | i2cport->write_bit(1,13); |
dreamselec |
6:98fe30430194 | 69 | lcd->printf("2: Connect to PC"); |
dreamselec |
6:98fe30430194 | 70 | |
dreamselec |
6:98fe30430194 | 71 | int selection = 0; |
dreamselec |
6:98fe30430194 | 72 | do { |
dreamselec | 9:dc8f155b71c8 | 73 | myLED4 = selection; |
dreamselec |
6:98fe30430194 | 74 | selection = readSwitches(); |
dreamselec | 9:dc8f155b71c8 | 75 | } while (selection != 1 && selection != 2 && connectedToPC == false); |
dreamselec | 9:dc8f155b71c8 | 76 | D_LEDS_OFF(); |
dreamselec |
6:98fe30430194 | 77 | if (selection == 1) { |
dreamselec |
6:98fe30430194 | 78 | // User selected op 1: Start sorting autonomously. |
dreamselec | 9:dc8f155b71c8 | 79 | i2cport->write_bit(1, 12); |
dreamselec |
6:98fe30430194 | 80 | lcd->cls(); |
dreamselec |
6:98fe30430194 | 81 | LCDFL(); |
dreamselec | 9:dc8f155b71c8 | 82 | lcd->printf("Starting sorting"); |
dreamselec | 9:dc8f155b71c8 | 83 | wait(0.5); |
dreamselec | 9:dc8f155b71c8 | 84 | D_LEDS_OFF(); |
dreamselec | 9:dc8f155b71c8 | 85 | |
dreamselec |
6:98fe30430194 | 86 | for(;;) { |
dreamselec |
6:98fe30430194 | 87 | lcd->cls(); |
dreamselec | 9:dc8f155b71c8 | 88 | lcd->printf("Waiting..."); |
dreamselec | 9:dc8f155b71c8 | 89 | |
dreamselec |
6:98fe30430194 | 90 | i2cport->write_bit(1, 15); |
dreamselec |
6:98fe30430194 | 91 | bool aboardOperation = false; |
dreamselec |
6:98fe30430194 | 92 | |
dreamselec |
6:98fe30430194 | 93 | int blockInserted = 0; |
dreamselec |
6:98fe30430194 | 94 | // Wait until a block is breaking the beam, or button 4 is pressed to aboard. |
dreamselec |
6:98fe30430194 | 95 | do { |
dreamselec |
6:98fe30430194 | 96 | blockInserted = fpga->checkForBlock(); |
dreamselec |
6:98fe30430194 | 97 | if (i2cport->read_bit(11)) { |
dreamselec |
6:98fe30430194 | 98 | aboardOperation = displayAboardDialog(); |
dreamselec |
6:98fe30430194 | 99 | // Cancel the aboard |
dreamselec |
6:98fe30430194 | 100 | if (aboardOperation == false) { |
dreamselec |
6:98fe30430194 | 101 | lcd->cls(); |
dreamselec |
6:98fe30430194 | 102 | LCDFL(); |
dreamselec | 9:dc8f155b71c8 | 103 | lcd->printf("Waiting..."); |
dreamselec | 9:dc8f155b71c8 | 104 | i2cport->write_bit(1, 15); |
dreamselec |
6:98fe30430194 | 105 | } |
dreamselec |
6:98fe30430194 | 106 | } |
dreamselec |
6:98fe30430194 | 107 | } while (aboardOperation == false && blockInserted != 1); |
dreamselec |
6:98fe30430194 | 108 | |
dreamselec |
6:98fe30430194 | 109 | // Break and return to main menu i.e. Start Op, or Connect to PC. |
dreamselec |
6:98fe30430194 | 110 | if (aboardOperation == true) { |
dreamselec | 9:dc8f155b71c8 | 111 | D_LEDS_OFF(); |
dreamselec |
6:98fe30430194 | 112 | break; |
dreamselec |
6:98fe30430194 | 113 | } |
dreamselec |
8:e1da2ae62885 | 114 | |
dreamselec |
6:98fe30430194 | 115 | // Cannot aboard any longer. Block is inserted. |
dreamselec |
8:e1da2ae62885 | 116 | // Detach rx interrupt until block processed. |
dreamselec |
8:e1da2ae62885 | 117 | NVIC_DisableIRQ(UART1_IRQn); |
dreamselec |
8:e1da2ae62885 | 118 | |
dreamselec |
6:98fe30430194 | 119 | int blockSize = fpga->checkForSize(); |
dreamselec |
6:98fe30430194 | 120 | |
dreamselec |
6:98fe30430194 | 121 | if (blockSize == HazBlock->size) { |
dreamselec |
6:98fe30430194 | 122 | // detectColour() |
dreamselec |
6:98fe30430194 | 123 | lcd->cls(); |
dreamselec |
6:98fe30430194 | 124 | lcd->printf("Detecting Colour"); |
dreamselec |
6:98fe30430194 | 125 | } |
dreamselec |
8:e1da2ae62885 | 126 | |
dreamselec |
8:e1da2ae62885 | 127 | // Re-Attach rx interrupt |
dreamselec |
8:e1da2ae62885 | 128 | NVIC_EnableIRQ(UART1_IRQn); |
dreamselec |
6:98fe30430194 | 129 | } |
dreamselec |
6:98fe30430194 | 130 | |
dreamselec | 9:dc8f155b71c8 | 131 | } else if (selection == 2 || connectedToPC == true) { |
dreamselec |
8:e1da2ae62885 | 132 | for (;;){ |
dreamselec | 9:dc8f155b71c8 | 133 | if (!connectedToPC) { |
dreamselec | 9:dc8f155b71c8 | 134 | i2cport->write_bit(1, 13); |
dreamselec | 9:dc8f155b71c8 | 135 | // Wait for PC to send '!<pc>connect;' command. |
dreamselec | 9:dc8f155b71c8 | 136 | lcd->cls(); |
dreamselec | 9:dc8f155b71c8 | 137 | LCDFL(); |
dreamselec | 9:dc8f155b71c8 | 138 | lcd->printf("Waiting for PC..."); |
dreamselec | 9:dc8f155b71c8 | 139 | wait(0.5); |
dreamselec | 9:dc8f155b71c8 | 140 | D_LEDS_OFF(); |
dreamselec | 9:dc8f155b71c8 | 141 | LCDSL(); |
dreamselec | 9:dc8f155b71c8 | 142 | lcd->printf("4: Main Menu"); |
dreamselec | 9:dc8f155b71c8 | 143 | } |
dreamselec | 9:dc8f155b71c8 | 144 | i2cport->write_bit(1, 15); |
dreamselec |
8:e1da2ae62885 | 145 | int aboardOperation = false; |
dreamselec |
8:e1da2ae62885 | 146 | while (connectedToPC == false && aboardOperation == false){ |
dreamselec |
8:e1da2ae62885 | 147 | aboardOperation = readSwitches() == 4; |
dreamselec |
8:e1da2ae62885 | 148 | } |
dreamselec | 9:dc8f155b71c8 | 149 | if (aboardOperation == true) { D_LEDS_OFF(); break; } |
dreamselec |
8:e1da2ae62885 | 150 | else if (connectedToPC == true){ |
dreamselec |
8:e1da2ae62885 | 151 | lcd->cls(); |
dreamselec |
8:e1da2ae62885 | 152 | LCDFL(); |
dreamselec |
8:e1da2ae62885 | 153 | lcd->printf("Connected to PC"); |
dreamselec |
8:e1da2ae62885 | 154 | LCDSL(); |
dreamselec |
8:e1da2ae62885 | 155 | lcd->printf("4: Disconnect."); |
dreamselec |
8:e1da2ae62885 | 156 | } |
dreamselec |
8:e1da2ae62885 | 157 | |
dreamselec |
8:e1da2ae62885 | 158 | while (aboardOperation == false && connectedToPC == true){ |
dreamselec |
8:e1da2ae62885 | 159 | aboardOperation = readSwitches() == 4; |
dreamselec |
8:e1da2ae62885 | 160 | } |
dreamselec | 9:dc8f155b71c8 | 161 | |
dreamselec | 9:dc8f155b71c8 | 162 | if (aboardOperation == true ) { D_LEDS_OFF(); break; } |
dreamselec |
6:98fe30430194 | 163 | } |
dreamselec |
6:98fe30430194 | 164 | } |
dreamselec |
6:98fe30430194 | 165 | |
dreamselec |
6:98fe30430194 | 166 | } |
dreamselec |
6:98fe30430194 | 167 | } |
dreamselec |
6:98fe30430194 | 168 | |
dreamselec |
6:98fe30430194 | 169 | /// Called every-time it receives an char from PC. |
dreamselec |
6:98fe30430194 | 170 | void Rx_interrupt(){ |
dreamselec |
8:e1da2ae62885 | 171 | char interruptChar = pc.getc(); |
dreamselec | 9:dc8f155b71c8 | 172 | // Uncomment to Echo to USB serial to watch data flow |
dreamselec | 9:dc8f155b71c8 | 173 | pc.putc(interruptChar); |
dreamselec |
6:98fe30430194 | 174 | |
dreamselec |
8:e1da2ae62885 | 175 | NVIC_DisableIRQ(UART1_IRQn); |
dreamselec |
8:e1da2ae62885 | 176 | |
dreamselec |
8:e1da2ae62885 | 177 | if (interruptChar == CommandTypeValue[Query]){ |
dreamselec |
8:e1da2ae62885 | 178 | commander->decodeCommand(Query); |
dreamselec |
8:e1da2ae62885 | 179 | }else if (interruptChar == CommandTypeValue[Set]){ |
dreamselec |
8:e1da2ae62885 | 180 | commander->decodeCommand(Set); |
dreamselec |
8:e1da2ae62885 | 181 | }else if (interruptChar== CommandTypeValue[Reply]){ |
dreamselec |
8:e1da2ae62885 | 182 | commander->decodeCommand(Reply); |
dreamselec |
8:e1da2ae62885 | 183 | } |
dreamselec |
8:e1da2ae62885 | 184 | |
dreamselec |
8:e1da2ae62885 | 185 | NVIC_EnableIRQ(UART1_IRQn); |
dreamselec |
6:98fe30430194 | 186 | } |
dreamselec | 3:843b830ee8bd | 187 | |
dreamselec | 4:4eebb4de22a7 | 188 | void initInternal() |
dreamselec | 4:4eebb4de22a7 | 189 | { |
dreamselec |
6:98fe30430194 | 190 | i2cport = new MCP23017(p9, p10, 0x40); |
dreamselec |
6:98fe30430194 | 191 | lcd = new WattBob_TextLCD(i2cport); |
dreamselec |
6:98fe30430194 | 192 | myLED1 = 1; |
dreamselec |
6:98fe30430194 | 193 | BACKLIGHT_ON(i2cport); |
dreamselec |
6:98fe30430194 | 194 | lcd->cls(); |
dreamselec |
6:98fe30430194 | 195 | LCDFL(); |
dreamselec |
6:98fe30430194 | 196 | lcd->printf("Initilizing..."); |
dreamselec |
6:98fe30430194 | 197 | myLED2 = 1; |
dreamselec |
6:98fe30430194 | 198 | return; |
dreamselec | 3:843b830ee8bd | 199 | } |
dreamselec | 3:843b830ee8bd | 200 | |
dreamselec | 7:b6e31bfdb2af | 201 | void initPort(int baudRate) |
dreamselec | 4:4eebb4de22a7 | 202 | { |
dreamselec |
6:98fe30430194 | 203 | myLED3 = 1; |
dreamselec |
6:98fe30430194 | 204 | pc.baud(baudRate); |
dreamselec |
6:98fe30430194 | 205 | pc.format(8, SerialBase::None, gStopBits); |
dreamselec |
6:98fe30430194 | 206 | myLED4 = 1; |
dreamselec |
6:98fe30430194 | 207 | wait (0.1); |
dreamselec |
6:98fe30430194 | 208 | return; |
dreamselec | 3:843b830ee8bd | 209 | } |
dreamselec | 3:843b830ee8bd | 210 | |
dreamselec | 4:4eebb4de22a7 | 211 | int readSwitches() |
dreamselec | 4:4eebb4de22a7 | 212 | { |
dreamselec |
6:98fe30430194 | 213 | |
dreamselec |
6:98fe30430194 | 214 | if(i2cport->read_bit(8)) { |
dreamselec |
6:98fe30430194 | 215 | return 1; |
dreamselec |
6:98fe30430194 | 216 | } else if (i2cport->read_bit(9)) { |
dreamselec |
6:98fe30430194 | 217 | return 2; |
dreamselec |
6:98fe30430194 | 218 | } else if (i2cport->read_bit(10)) { |
dreamselec |
6:98fe30430194 | 219 | return 3; |
dreamselec |
6:98fe30430194 | 220 | } else if (i2cport->read_bit(11)) { |
dreamselec |
6:98fe30430194 | 221 | return 4; |
dreamselec |
6:98fe30430194 | 222 | } else { |
dreamselec |
6:98fe30430194 | 223 | return 0; |
dreamselec |
6:98fe30430194 | 224 | } |
dreamselec |
6:98fe30430194 | 225 | |
dreamselec | 3:843b830ee8bd | 226 | } |
dreamselec | 3:843b830ee8bd | 227 | |
dreamselec |
6:98fe30430194 | 228 | void printPCDetectedText() |
dreamselec | 4:4eebb4de22a7 | 229 | { |
dreamselec |
6:98fe30430194 | 230 | lcd->cls(); |
dreamselec |
6:98fe30430194 | 231 | LCDFL(); |
dreamselec |
6:98fe30430194 | 232 | lcd->printf("Detected PC."); |
dreamselec |
6:98fe30430194 | 233 | LCDSL(); |
dreamselec |
6:98fe30430194 | 234 | lcd->printf("Connecting"); |
dreamselec |
6:98fe30430194 | 235 | initPort(); |
dreamselec | 3:843b830ee8bd | 236 | } |
dreamselec | 3:843b830ee8bd | 237 | |
dreamselec | 4:4eebb4de22a7 | 238 | bool displayAboardDialog() |
dreamselec | 4:4eebb4de22a7 | 239 | { |
dreamselec | 9:dc8f155b71c8 | 240 | while (i2cport->read_bit(11) == 1){} |
dreamselec | 9:dc8f155b71c8 | 241 | i2cport->write_bit(1, 12); |
dreamselec | 9:dc8f155b71c8 | 242 | |
dreamselec |
6:98fe30430194 | 243 | lcd->cls(); |
dreamselec |
6:98fe30430194 | 244 | LCDFL(); |
dreamselec | 9:dc8f155b71c8 | 245 | lcd->printf("Aboard?"); |
dreamselec |
6:98fe30430194 | 246 | LCDSL(); |
dreamselec | 9:dc8f155b71c8 | 247 | lcd->printf("1:YES, 2,3,4:NO"); |
dreamselec | 9:dc8f155b71c8 | 248 | int reply = 0; |
dreamselec | 9:dc8f155b71c8 | 249 | do { |
dreamselec | 9:dc8f155b71c8 | 250 | reply = readSwitches(); |
dreamselec | 9:dc8f155b71c8 | 251 | } while(reply == 0); |
dreamselec | 9:dc8f155b71c8 | 252 | |
dreamselec | 9:dc8f155b71c8 | 253 | D_LEDS_OFF(); |
dreamselec |
6:98fe30430194 | 254 | if (reply == 1) { |
dreamselec |
6:98fe30430194 | 255 | return true; |
dreamselec |
6:98fe30430194 | 256 | } else { |
dreamselec |
6:98fe30430194 | 257 | return false; |
dreamselec |
6:98fe30430194 | 258 | } |
dreamselec | 2:7a55cb10259f | 259 | } |