![](/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@7:b6e31bfdb2af, 2015-11-16 (annotated)
- Committer:
- dreamselec
- Date:
- Mon Nov 16 18:46:31 2015 +0000
- Revision:
- 7:b6e31bfdb2af
- Parent:
- 6:98fe30430194
- Child:
- 8:e1da2ae62885
Program building with online compiler.
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 | 3:843b830ee8bd | 30 | uint8_t rxBuffer[5 + 1]; |
dreamselec | 2:7a55cb10259f | 31 | |
dreamselec |
6:98fe30430194 | 32 | Commander _commander = Commander(); |
dreamselec |
6:98fe30430194 | 33 | Commander *commander = &_commander; |
dreamselec |
6:98fe30430194 | 34 | FPGA _fpga = FPGA(); |
dreamselec | 7:b6e31bfdb2af | 35 | FPGA *fpga = &_fpga; |
dreamselec |
6:98fe30430194 | 36 | |
dreamselec |
6:98fe30430194 | 37 | extern Block _HazBlock; |
dreamselec |
6:98fe30430194 | 38 | Block *HazBlock = &_HazBlock; |
dreamselec |
6:98fe30430194 | 39 | |
dreamselec | 3:843b830ee8bd | 40 | bool connectedToPC = false; |
dreamselec | 2:7a55cb10259f | 41 | |
dreamselec | 3:843b830ee8bd | 42 | void detectPC(); |
dreamselec |
6:98fe30430194 | 43 | void initInternal(); |
dreamselec | 7:b6e31bfdb2af | 44 | void initPort(int baudRate=kDefaultBaudRate); |
dreamselec |
6:98fe30430194 | 45 | int readSwitches(); |
dreamselec |
6:98fe30430194 | 46 | void printPCDetectedText(); |
dreamselec |
6:98fe30430194 | 47 | bool displayAboardDialog(); |
dreamselec |
6:98fe30430194 | 48 | void Rx_interrupt(); |
dreamselec |
6:98fe30430194 | 49 | |
dreamselec |
6:98fe30430194 | 50 | |
dreamselec |
6:98fe30430194 | 51 | int main() |
dreamselec |
6:98fe30430194 | 52 | { |
dreamselec |
6:98fe30430194 | 53 | initInternal(); |
dreamselec |
6:98fe30430194 | 54 | initPort(); |
dreamselec |
6:98fe30430194 | 55 | srand((unsigned)time(NULL)); |
dreamselec |
6:98fe30430194 | 56 | U_LEDS_OFF(); |
dreamselec |
6:98fe30430194 | 57 | lcd->cls(); |
dreamselec |
6:98fe30430194 | 58 | myLED1 = 1; |
dreamselec |
6:98fe30430194 | 59 | |
dreamselec |
6:98fe30430194 | 60 | // Create a serial intereput for RxIrq so when PC is connected it sends '$' to tell MBED it's there. |
dreamselec |
6:98fe30430194 | 61 | // https://developer.mbed.org/cookbook/Serial-Interrupts |
dreamselec |
6:98fe30430194 | 62 | pc.attach(&Rx_interrupt, Serial::RxIrq); |
dreamselec |
6:98fe30430194 | 63 | |
dreamselec |
6:98fe30430194 | 64 | for (;;) { |
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 |
6:98fe30430194 | 73 | selection = readSwitches(); |
dreamselec |
6:98fe30430194 | 74 | } while (selection == 1 || selection == 2); |
dreamselec |
6:98fe30430194 | 75 | |
dreamselec |
6:98fe30430194 | 76 | if (selection == 1) { |
dreamselec |
6:98fe30430194 | 77 | // User selected op 1: Start sorting autonomously. |
dreamselec |
6:98fe30430194 | 78 | i2cport->write_bit(0, 13); |
dreamselec |
6:98fe30430194 | 79 | lcd->cls(); |
dreamselec |
6:98fe30430194 | 80 | LCDFL(); |
dreamselec |
6:98fe30430194 | 81 | lcd->printf("Starting sorting operation"); |
dreamselec |
6:98fe30430194 | 82 | wait(1); |
dreamselec |
6:98fe30430194 | 83 | |
dreamselec |
6:98fe30430194 | 84 | for(;;) { |
dreamselec |
6:98fe30430194 | 85 | lcd->cls(); |
dreamselec |
6:98fe30430194 | 86 | lcd->printf("Waiting for block"); |
dreamselec |
6:98fe30430194 | 87 | i2cport->write_bit(1, 15); |
dreamselec |
6:98fe30430194 | 88 | bool aboardOperation = false; |
dreamselec |
6:98fe30430194 | 89 | |
dreamselec |
6:98fe30430194 | 90 | int blockInserted = 0; |
dreamselec |
6:98fe30430194 | 91 | // Wait until a block is breaking the beam, or button 4 is pressed to aboard. |
dreamselec |
6:98fe30430194 | 92 | do { |
dreamselec |
6:98fe30430194 | 93 | blockInserted = fpga->checkForBlock(); |
dreamselec |
6:98fe30430194 | 94 | if (i2cport->read_bit(11)) { |
dreamselec |
6:98fe30430194 | 95 | aboardOperation = displayAboardDialog(); |
dreamselec |
6:98fe30430194 | 96 | // Cancel the aboard |
dreamselec |
6:98fe30430194 | 97 | if (aboardOperation == false) { |
dreamselec |
6:98fe30430194 | 98 | lcd->cls(); |
dreamselec |
6:98fe30430194 | 99 | LCDFL(); |
dreamselec |
6:98fe30430194 | 100 | lcd->printf("Waiting for block"); |
dreamselec |
6:98fe30430194 | 101 | } |
dreamselec |
6:98fe30430194 | 102 | } |
dreamselec |
6:98fe30430194 | 103 | } while (aboardOperation == false && blockInserted != 1); |
dreamselec |
6:98fe30430194 | 104 | |
dreamselec |
6:98fe30430194 | 105 | // Break and return to main menu i.e. Start Op, or Connect to PC. |
dreamselec |
6:98fe30430194 | 106 | if (aboardOperation == true) { |
dreamselec |
6:98fe30430194 | 107 | break; |
dreamselec |
6:98fe30430194 | 108 | } |
dreamselec |
6:98fe30430194 | 109 | // Cannot aboard any longer. Block is inserted. |
dreamselec |
6:98fe30430194 | 110 | int blockSize = fpga->checkForSize(); |
dreamselec |
6:98fe30430194 | 111 | |
dreamselec |
6:98fe30430194 | 112 | if (blockSize == HazBlock->size) { |
dreamselec |
6:98fe30430194 | 113 | // detectColour() |
dreamselec |
6:98fe30430194 | 114 | lcd->cls(); |
dreamselec |
6:98fe30430194 | 115 | lcd->printf("Detecting Colour"); |
dreamselec |
6:98fe30430194 | 116 | } |
dreamselec |
6:98fe30430194 | 117 | } |
dreamselec |
6:98fe30430194 | 118 | |
dreamselec |
6:98fe30430194 | 119 | } else if (selection == 2) { |
dreamselec |
6:98fe30430194 | 120 | // Wait for PC to send '!<pc>init;' command. |
dreamselec |
6:98fe30430194 | 121 | lcd->cls(); |
dreamselec |
6:98fe30430194 | 122 | LCDFL(); |
dreamselec |
6:98fe30430194 | 123 | lcd->printf("Waiting for PC..."); |
dreamselec |
6:98fe30430194 | 124 | LCDSL(); |
dreamselec |
6:98fe30430194 | 125 | lcd->printf("4: Main Menu"); |
dreamselec |
6:98fe30430194 | 126 | |
dreamselec |
6:98fe30430194 | 127 | int aboardOperation = false; |
dreamselec |
6:98fe30430194 | 128 | while (connectedToPC == false && aboardOperation == false){ |
dreamselec |
6:98fe30430194 | 129 | aboardOperation = readSwitches() == 4; |
dreamselec |
6:98fe30430194 | 130 | } |
dreamselec |
6:98fe30430194 | 131 | if (aboardOperation == true) { break; } |
dreamselec |
6:98fe30430194 | 132 | else if (connectedToPC == true){ |
dreamselec |
6:98fe30430194 | 133 | lcd->cls(); |
dreamselec |
6:98fe30430194 | 134 | LCDFL(); |
dreamselec |
6:98fe30430194 | 135 | lcd->printf("Connected to PC"); |
dreamselec |
6:98fe30430194 | 136 | LCDSL(); |
dreamselec |
6:98fe30430194 | 137 | lcd->printf("4: Disconnect."); |
dreamselec |
6:98fe30430194 | 138 | } |
dreamselec |
6:98fe30430194 | 139 | |
dreamselec |
6:98fe30430194 | 140 | while (aboardOperation == false){ |
dreamselec |
6:98fe30430194 | 141 | aboardOperation = readSwitches() == 4; |
dreamselec |
6:98fe30430194 | 142 | } |
dreamselec |
6:98fe30430194 | 143 | if (aboardOperation == true ) { break; } |
dreamselec |
6:98fe30430194 | 144 | } |
dreamselec |
6:98fe30430194 | 145 | |
dreamselec |
6:98fe30430194 | 146 | } |
dreamselec |
6:98fe30430194 | 147 | } |
dreamselec |
6:98fe30430194 | 148 | |
dreamselec |
6:98fe30430194 | 149 | /// Called every-time it receives an char from PC. |
dreamselec |
6:98fe30430194 | 150 | void Rx_interrupt(){ |
dreamselec |
6:98fe30430194 | 151 | |
dreamselec |
6:98fe30430194 | 152 | } |
dreamselec | 3:843b830ee8bd | 153 | |
dreamselec | 4:4eebb4de22a7 | 154 | void initInternal() |
dreamselec | 4:4eebb4de22a7 | 155 | { |
dreamselec |
6:98fe30430194 | 156 | i2cport = new MCP23017(p9, p10, 0x40); |
dreamselec |
6:98fe30430194 | 157 | lcd = new WattBob_TextLCD(i2cport); |
dreamselec |
6:98fe30430194 | 158 | myLED1 = 1; |
dreamselec |
6:98fe30430194 | 159 | BACKLIGHT_ON(i2cport); |
dreamselec |
6:98fe30430194 | 160 | lcd->cls(); |
dreamselec |
6:98fe30430194 | 161 | LCDFL(); |
dreamselec |
6:98fe30430194 | 162 | lcd->printf("Initilizing..."); |
dreamselec |
6:98fe30430194 | 163 | myLED2 = 1; |
dreamselec |
6:98fe30430194 | 164 | return; |
dreamselec | 3:843b830ee8bd | 165 | } |
dreamselec | 3:843b830ee8bd | 166 | |
dreamselec | 7:b6e31bfdb2af | 167 | void initPort(int baudRate) |
dreamselec | 4:4eebb4de22a7 | 168 | { |
dreamselec |
6:98fe30430194 | 169 | myLED3 = 1; |
dreamselec |
6:98fe30430194 | 170 | pc.baud(baudRate); |
dreamselec |
6:98fe30430194 | 171 | pc.format(8, SerialBase::None, gStopBits); |
dreamselec |
6:98fe30430194 | 172 | myLED4 = 1; |
dreamselec |
6:98fe30430194 | 173 | wait (0.1); |
dreamselec |
6:98fe30430194 | 174 | return; |
dreamselec | 3:843b830ee8bd | 175 | } |
dreamselec | 3:843b830ee8bd | 176 | |
dreamselec | 4:4eebb4de22a7 | 177 | int readSwitches() |
dreamselec | 4:4eebb4de22a7 | 178 | { |
dreamselec |
6:98fe30430194 | 179 | |
dreamselec |
6:98fe30430194 | 180 | if(i2cport->read_bit(8)) { |
dreamselec |
6:98fe30430194 | 181 | return 1; |
dreamselec |
6:98fe30430194 | 182 | } else if (i2cport->read_bit(9)) { |
dreamselec |
6:98fe30430194 | 183 | return 2; |
dreamselec |
6:98fe30430194 | 184 | } else if (i2cport->read_bit(10)) { |
dreamselec |
6:98fe30430194 | 185 | return 3; |
dreamselec |
6:98fe30430194 | 186 | } else if (i2cport->read_bit(11)) { |
dreamselec |
6:98fe30430194 | 187 | return 4; |
dreamselec |
6:98fe30430194 | 188 | } else { |
dreamselec |
6:98fe30430194 | 189 | return 0; |
dreamselec |
6:98fe30430194 | 190 | } |
dreamselec |
6:98fe30430194 | 191 | |
dreamselec | 3:843b830ee8bd | 192 | } |
dreamselec | 3:843b830ee8bd | 193 | |
dreamselec |
6:98fe30430194 | 194 | void printPCDetectedText() |
dreamselec | 4:4eebb4de22a7 | 195 | { |
dreamselec |
6:98fe30430194 | 196 | lcd->cls(); |
dreamselec |
6:98fe30430194 | 197 | LCDFL(); |
dreamselec |
6:98fe30430194 | 198 | lcd->printf("Detected PC."); |
dreamselec |
6:98fe30430194 | 199 | LCDSL(); |
dreamselec |
6:98fe30430194 | 200 | lcd->printf("Connecting"); |
dreamselec |
6:98fe30430194 | 201 | initPort(); |
dreamselec | 3:843b830ee8bd | 202 | } |
dreamselec | 3:843b830ee8bd | 203 | |
dreamselec | 4:4eebb4de22a7 | 204 | bool displayAboardDialog() |
dreamselec | 4:4eebb4de22a7 | 205 | { |
dreamselec |
6:98fe30430194 | 206 | lcd->cls(); |
dreamselec |
6:98fe30430194 | 207 | LCDFL(); |
dreamselec |
6:98fe30430194 | 208 | lcd->printf("Aboard operation?"); |
dreamselec |
6:98fe30430194 | 209 | LCDSL(); |
dreamselec |
6:98fe30430194 | 210 | lcd->printf("1: YES, 2,3,4: NO"); |
dreamselec |
6:98fe30430194 | 211 | int reply = readSwitches(); |
dreamselec |
6:98fe30430194 | 212 | if (reply == 1) { |
dreamselec |
6:98fe30430194 | 213 | return true; |
dreamselec |
6:98fe30430194 | 214 | } else { |
dreamselec |
6:98fe30430194 | 215 | return false; |
dreamselec |
6:98fe30430194 | 216 | } |
dreamselec | 2:7a55cb10259f | 217 | } |