![](/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@4:4eebb4de22a7, 2015-11-16 (annotated)
- Committer:
- dreamselec
- Date:
- Mon Nov 16 01:15:45 2015 +0000
- Revision:
- 4:4eebb4de22a7
- Parent:
- 3:843b830ee8bd
- Child:
- 6:98fe30430194
Published
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 | 4:4eebb4de22a7 | 32 | Command *myCommand; |
dreamselec | 3:843b830ee8bd | 33 | bool connectedToPC = false; |
dreamselec | 2:7a55cb10259f | 34 | |
dreamselec | 3:843b830ee8bd | 35 | void detectPC(); |
dreamselec | 3:843b830ee8bd | 36 | |
dreamselec | 4:4eebb4de22a7 | 37 | void initInternal() |
dreamselec | 4:4eebb4de22a7 | 38 | { |
dreamselec | 3:843b830ee8bd | 39 | i2cport = new MCP23017(p9, p10, 0x40); |
dreamselec | 3:843b830ee8bd | 40 | lcd = new WattBob_TextLCD(i2cport); |
dreamselec | 3:843b830ee8bd | 41 | myLED1 = 1; |
dreamselec | 3:843b830ee8bd | 42 | BACKLIGHT_ON(i2cport); |
dreamselec | 4:4eebb4de22a7 | 43 | lcd->cls(); |
dreamselec | 3:843b830ee8bd | 44 | LCDFL(); |
dreamselec | 4:4eebb4de22a7 | 45 | lcd->printf("Initilizing..."); |
dreamselec | 3:843b830ee8bd | 46 | myLED2 = 1; |
dreamselec | 3:843b830ee8bd | 47 | return; |
dreamselec | 3:843b830ee8bd | 48 | } |
dreamselec | 3:843b830ee8bd | 49 | |
dreamselec | 4:4eebb4de22a7 | 50 | void initPort(int baudRate=gBaudRate) |
dreamselec | 4:4eebb4de22a7 | 51 | { |
dreamselec | 2:7a55cb10259f | 52 | myLED3 = 1; |
dreamselec | 3:843b830ee8bd | 53 | pc.baud(baudRate); |
dreamselec | 2:7a55cb10259f | 54 | pc.format(8, SerialBase::None, gStopBits); |
dreamselec | 2:7a55cb10259f | 55 | myLED4 = 1; |
dreamselec | 2:7a55cb10259f | 56 | wait (0.1); |
dreamselec | 3:843b830ee8bd | 57 | return; |
dreamselec | 3:843b830ee8bd | 58 | } |
dreamselec | 3:843b830ee8bd | 59 | |
dreamselec | 4:4eebb4de22a7 | 60 | int readSwitches() |
dreamselec | 4:4eebb4de22a7 | 61 | { |
dreamselec | 3:843b830ee8bd | 62 | do { |
dreamselec | 3:843b830ee8bd | 63 | if(i2cport->read_bit(8)) { |
dreamselec | 3:843b830ee8bd | 64 | return 1; |
dreamselec | 3:843b830ee8bd | 65 | } else if (i2cport->read_bit(9)) { |
dreamselec | 3:843b830ee8bd | 66 | return 2; |
dreamselec | 3:843b830ee8bd | 67 | } else if (i2cport->read_bit(10)) { |
dreamselec | 3:843b830ee8bd | 68 | return 3; |
dreamselec | 3:843b830ee8bd | 69 | } else if (i2cport->read_bit(11)) { |
dreamselec | 3:843b830ee8bd | 70 | return 4; |
dreamselec | 3:843b830ee8bd | 71 | } else { |
dreamselec | 3:843b830ee8bd | 72 | return 0; |
dreamselec | 3:843b830ee8bd | 73 | } |
dreamselec | 3:843b830ee8bd | 74 | } while (1); |
dreamselec | 3:843b830ee8bd | 75 | } |
dreamselec | 3:843b830ee8bd | 76 | |
dreamselec | 4:4eebb4de22a7 | 77 | void detectPC() |
dreamselec | 4:4eebb4de22a7 | 78 | { |
dreamselec | 4:4eebb4de22a7 | 79 | lcd->cls(); |
dreamselec | 3:843b830ee8bd | 80 | LCDFL(); |
dreamselec | 3:843b830ee8bd | 81 | lcd->printf("Detected PC."); |
dreamselec | 3:843b830ee8bd | 82 | LCDSL(); |
dreamselec | 3:843b830ee8bd | 83 | lcd->printf("Connecting"); |
dreamselec | 3:843b830ee8bd | 84 | initPort(); |
dreamselec | 3:843b830ee8bd | 85 | } |
dreamselec | 3:843b830ee8bd | 86 | |
dreamselec | 4:4eebb4de22a7 | 87 | bool displayAboardDialog() |
dreamselec | 4:4eebb4de22a7 | 88 | { |
dreamselec | 4:4eebb4de22a7 | 89 | lcd->cls(); |
dreamselec | 3:843b830ee8bd | 90 | LCDFL(); |
dreamselec | 3:843b830ee8bd | 91 | lcd->printf("Aboard operation?"); |
dreamselec | 3:843b830ee8bd | 92 | LCDSL(); |
dreamselec | 3:843b830ee8bd | 93 | lcd->printf("1: YES, 2,3,4: NO"); |
dreamselec | 3:843b830ee8bd | 94 | int reply = readSwitches(); |
dreamselec | 4:4eebb4de22a7 | 95 | if (reply == 1) { |
dreamselec | 4:4eebb4de22a7 | 96 | return true; |
dreamselec | 4:4eebb4de22a7 | 97 | } else { |
dreamselec | 4:4eebb4de22a7 | 98 | return false; |
dreamselec | 4:4eebb4de22a7 | 99 | } |
dreamselec | 2:7a55cb10259f | 100 | } |
dreamselec | 0:fe5cb0a8fc5a | 101 | |
dreamselec | 4:4eebb4de22a7 | 102 | int main() |
dreamselec | 4:4eebb4de22a7 | 103 | { |
dreamselec | 4:4eebb4de22a7 | 104 | initInternal(); |
dreamselec | 4:4eebb4de22a7 | 105 | initPort(); |
dreamselec | 4:4eebb4de22a7 | 106 | srand((unsigned)time(NULL)); |
dreamselec | 4:4eebb4de22a7 | 107 | U_LEDS_OFF(); |
dreamselec | 4:4eebb4de22a7 | 108 | lcd->cls(); |
dreamselec | 4:4eebb4de22a7 | 109 | myLED1 = 1; |
dreamselec | 4:4eebb4de22a7 | 110 | |
dreamselec | 4:4eebb4de22a7 | 111 | // Create a serial intereput for RxIrq so when PC is connected it sends '$' to tell MBED it's there. |
dreamselec | 4:4eebb4de22a7 | 112 | // https://developer.mbed.org/cookbook/Serial-Interrupts |
dreamselec | 3:843b830ee8bd | 113 | |
dreamselec | 4:4eebb4de22a7 | 114 | for (;;) { |
dreamselec | 4:4eebb4de22a7 | 115 | i2cport->write_bit(1, 12); |
dreamselec | 4:4eebb4de22a7 | 116 | lcd->printf("1: Start sorting."); |
dreamselec | 4:4eebb4de22a7 | 117 | LCDSL(); |
dreamselec | 4:4eebb4de22a7 | 118 | i2cport->write_bit(1,13); |
dreamselec | 4:4eebb4de22a7 | 119 | lcd->printf("2: Connect to PC"); |
dreamselec | 4:4eebb4de22a7 | 120 | |
dreamselec | 4:4eebb4de22a7 | 121 | int selection = 0; |
dreamselec | 4:4eebb4de22a7 | 122 | do { |
dreamselec | 4:4eebb4de22a7 | 123 | selection = readSwitches(); |
dreamselec | 4:4eebb4de22a7 | 124 | } while (selection == 1 || selection == 2); |
dreamselec | 3:843b830ee8bd | 125 | |
dreamselec | 4:4eebb4de22a7 | 126 | if (selection == 1) { |
dreamselec | 4:4eebb4de22a7 | 127 | // User selected op 1: Start sorting autonomaously. |
dreamselec | 4:4eebb4de22a7 | 128 | i2cport->write_bit(0, 13); |
dreamselec | 4:4eebb4de22a7 | 129 | lcd->cls(); |
dreamselec | 4:4eebb4de22a7 | 130 | LCDFL(); |
dreamselec | 4:4eebb4de22a7 | 131 | lcd->printf("Starting sorting operation"); |
dreamselec | 4:4eebb4de22a7 | 132 | wait(1); |
dreamselec | 3:843b830ee8bd | 133 | |
dreamselec | 4:4eebb4de22a7 | 134 | for(;;) { |
dreamselec | 4:4eebb4de22a7 | 135 | lcd->cls(); |
dreamselec | 4:4eebb4de22a7 | 136 | lcd->printf("Waiting for block"); |
dreamselec | 4:4eebb4de22a7 | 137 | i2cport->write_bit(1, 15); |
dreamselec | 4:4eebb4de22a7 | 138 | bool aboardOperation = false; |
dreamselec | 3:843b830ee8bd | 139 | |
dreamselec | 4:4eebb4de22a7 | 140 | int blockInserted = 0; |
dreamselec | 4:4eebb4de22a7 | 141 | // Wait until a block is breaking the beam, or button 4 is pressed to aboard. |
dreamselec | 4:4eebb4de22a7 | 142 | do { |
dreamselec | 4:4eebb4de22a7 | 143 | blockInserted = checkForBlock(); |
dreamselec | 4:4eebb4de22a7 | 144 | if (i2cport->read_bit(11)) { |
dreamselec | 4:4eebb4de22a7 | 145 | aboardOperation = displayAboardDialog(); |
dreamselec | 4:4eebb4de22a7 | 146 | // Cancel the aboard |
dreamselec | 4:4eebb4de22a7 | 147 | if (aboardOperation == false) { |
dreamselec | 4:4eebb4de22a7 | 148 | lcd->cls(); |
dreamselec | 4:4eebb4de22a7 | 149 | LCDFL(); |
dreamselec | 4:4eebb4de22a7 | 150 | lcd->printf("Waiting for block"); |
dreamselec | 3:843b830ee8bd | 151 | } |
dreamselec | 4:4eebb4de22a7 | 152 | } |
dreamselec | 4:4eebb4de22a7 | 153 | } while (aboardOperation == false && blockInserted != 1); |
dreamselec | 4:4eebb4de22a7 | 154 | |
dreamselec | 4:4eebb4de22a7 | 155 | // Break and return to main menu i.e. Start Op, or Connect to PC. |
dreamselec | 4:4eebb4de22a7 | 156 | if (aboardOperation == true) { |
dreamselec | 4:4eebb4de22a7 | 157 | break; |
dreamselec | 4:4eebb4de22a7 | 158 | } |
dreamselec | 4:4eebb4de22a7 | 159 | // Cannot aboard any longer. Block is inserted. |
dreamselec | 4:4eebb4de22a7 | 160 | int largeBlock = blockIsLarge(); |
dreamselec | 4:4eebb4de22a7 | 161 | |
dreamselec | 4:4eebb4de22a7 | 162 | if (largeBlock == hazBlock.blockSizeEnum) { |
dreamselec | 3:843b830ee8bd | 163 | // detectColour() |
dreamselec | 4:4eebb4de22a7 | 164 | lcd->cls(); |
dreamselec | 4:4eebb4de22a7 | 165 | lcd->printf("Detecting Colour"); |
dreamselec | 3:843b830ee8bd | 166 | } |
dreamselec | 3:843b830ee8bd | 167 | } |
dreamselec | 4:4eebb4de22a7 | 168 | |
dreamselec | 4:4eebb4de22a7 | 169 | } |
dreamselec | 4:4eebb4de22a7 | 170 | |
dreamselec | 4:4eebb4de22a7 | 171 | else if (selection == 2) { |
dreamselec | 4:4eebb4de22a7 | 172 | // Wait for PC to send '$' char to make sure it's there. |
dreamselec | 4:4eebb4de22a7 | 173 | lcd->cls(); |
dreamselec | 4:4eebb4de22a7 | 174 | LCDFL(); |
dreamselec | 4:4eebb4de22a7 | 175 | lcd->printf("Waiting for PC..."); |
dreamselec | 4:4eebb4de22a7 | 176 | LCDSL(); |
dreamselec | 4:4eebb4de22a7 | 177 | lcd->printf("4: Main Menu"); |
dreamselec | 4:4eebb4de22a7 | 178 | |
dreamselec | 4:4eebb4de22a7 | 179 | } |
dreamselec | 4:4eebb4de22a7 | 180 | |
dreamselec | 4:4eebb4de22a7 | 181 | } |
dreamselec | 4:4eebb4de22a7 | 182 | |
dreamselec | 4:4eebb4de22a7 | 183 | |
dreamselec | 4:4eebb4de22a7 | 184 | FOREVER { |
dreamselec | 4:4eebb4de22a7 | 185 | pc.printf("In FOREVER\n"); |
dreamselec | 4:4eebb4de22a7 | 186 | Type type = INVALID; |
dreamselec | 4:4eebb4de22a7 | 187 | pc.printf("Type set to: %i\r\n", type); |
dreamselec | 4:4eebb4de22a7 | 188 | do { |
dreamselec | 4:4eebb4de22a7 | 189 | pc.printf("In DO-WHILE loop\r\n"); |
dreamselec | 4:4eebb4de22a7 | 190 | type = listenForCommand(); |
dreamselec | 4:4eebb4de22a7 | 191 | if (i2cport->read_bit(8)) { |
dreamselec | 4:4eebb4de22a7 | 192 | pc.printf("Listening\r\n"); |
dreamselec | 4:4eebb4de22a7 | 193 | } |
dreamselec | 4:4eebb4de22a7 | 194 | } while (type == -1); |
dreamselec | 4:4eebb4de22a7 | 195 | |
dreamselec | 4:4eebb4de22a7 | 196 | pc.printf("Command Inbound\r\n"); |
dreamselec | 4:4eebb4de22a7 | 197 | |
dreamselec | 4:4eebb4de22a7 | 198 | myCommand.typeEnum = type; |
dreamselec | 4:4eebb4de22a7 | 199 | pc.printf("Type: %i\r\n-------------\r\n", myCommand.typeEnum); |
dreamselec | 4:4eebb4de22a7 | 200 | recieveCommand(&myCommand, type); |
dreamselec | 4:4eebb4de22a7 | 201 | |
dreamselec | 4:4eebb4de22a7 | 202 | pc.printf("inputstring:%s\r\n%s\r\n<%s>\r\n%s;\r\n", myCommand.inputString, myCommand.commandType, myCommand.commandObject, myCommand.commandString); |
dreamselec | 3:843b830ee8bd | 203 | // decodeCommand(&myCommand); |
dreamselec | 4:4eebb4de22a7 | 204 | |
dreamselec | 4:4eebb4de22a7 | 205 | |
dreamselec | 4:4eebb4de22a7 | 206 | } |
dreamselec | 0:fe5cb0a8fc5a | 207 | } |