![](/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@3:843b830ee8bd, 2015-11-11 (annotated)
- Committer:
- dreamselec
- Date:
- Wed Nov 11 03:45:30 2015 +0000
- Revision:
- 3:843b830ee8bd
- Parent:
- 2:7a55cb10259f
- Child:
- 4:4eebb4de22a7
The MBED program is supposed to work both when connected to PC, and on it's own. So making use of Serial (UART) interrupts, to listen for incoming commands when PC is connected. Currently only mbed control works, no commands are sent to PC, or FPGA
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 PRINT_LCD(TEXT) lcd->printf(TEXT); |
dreamselec | 0:fe5cb0a8fc5a | 16 | #define CLEAR_LCD() lcd->cls(); |
dreamselec | 0:fe5cb0a8fc5a | 17 | #define LCDFL() lcd->locate(0,0); |
dreamselec | 0:fe5cb0a8fc5a | 18 | #define LCDSL() lcd->locate(1,0); |
dreamselec | 2:7a55cb10259f | 19 | #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 | 20 | #define U_LEDS_OFF() myLED1 = 0; myLED2 = 0; myLED3 = 0; myLED4 = 0; |
dreamselec | 0:fe5cb0a8fc5a | 21 | |
dreamselec | 2:7a55cb10259f | 22 | DigitalOut myLED1(LED1); |
dreamselec | 2:7a55cb10259f | 23 | DigitalOut myLED2(LED2); |
dreamselec | 2:7a55cb10259f | 24 | DigitalOut myLED3(LED3); |
dreamselec | 2:7a55cb10259f | 25 | DigitalOut myLED4(LED4); |
dreamselec | 0:fe5cb0a8fc5a | 26 | |
dreamselec | 0:fe5cb0a8fc5a | 27 | MCP23017 *i2cport; |
dreamselec | 0:fe5cb0a8fc5a | 28 | WattBob_TextLCD *lcd; |
dreamselec | 0:fe5cb0a8fc5a | 29 | |
dreamselec | 2:7a55cb10259f | 30 | TCS3472_I2C rgb_sensor(p28, p27); |
dreamselec | 2:7a55cb10259f | 31 | Serial pc(USBTX, USBRX); |
dreamselec | 3:843b830ee8bd | 32 | uint8_t rxBuffer[5 + 1]; |
dreamselec | 2:7a55cb10259f | 33 | |
dreamselec | 2:7a55cb10259f | 34 | Command myCommand; |
dreamselec | 3:843b830ee8bd | 35 | bool connectedToPC = false; |
dreamselec | 2:7a55cb10259f | 36 | |
dreamselec | 3:843b830ee8bd | 37 | void detectPC(); |
dreamselec | 3:843b830ee8bd | 38 | |
dreamselec | 3:843b830ee8bd | 39 | void initInternal(){ |
dreamselec | 3:843b830ee8bd | 40 | i2cport = new MCP23017(p9, p10, 0x40); |
dreamselec | 3:843b830ee8bd | 41 | lcd = new WattBob_TextLCD(i2cport); |
dreamselec | 3:843b830ee8bd | 42 | myLED1 = 1; |
dreamselec | 3:843b830ee8bd | 43 | BACKLIGHT_ON(i2cport); |
dreamselec | 3:843b830ee8bd | 44 | CLEAR_LCD(); |
dreamselec | 3:843b830ee8bd | 45 | LCDFL(); |
dreamselec | 3:843b830ee8bd | 46 | PRINT_LCD("Initilizing..."); |
dreamselec | 3:843b830ee8bd | 47 | myLED2 = 1; |
dreamselec | 3:843b830ee8bd | 48 | return; |
dreamselec | 3:843b830ee8bd | 49 | } |
dreamselec | 3:843b830ee8bd | 50 | |
dreamselec | 3:843b830ee8bd | 51 | void initPort(int baudRate=gBaudRate){ |
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 | 3:843b830ee8bd | 60 | int readSwitches(){ |
dreamselec | 3:843b830ee8bd | 61 | do { |
dreamselec | 3:843b830ee8bd | 62 | if(i2cport->read_bit(8)) { |
dreamselec | 3:843b830ee8bd | 63 | return 1; |
dreamselec | 3:843b830ee8bd | 64 | } else if (i2cport->read_bit(9)) { |
dreamselec | 3:843b830ee8bd | 65 | return 2; |
dreamselec | 3:843b830ee8bd | 66 | } else if (i2cport->read_bit(10)) { |
dreamselec | 3:843b830ee8bd | 67 | return 3; |
dreamselec | 3:843b830ee8bd | 68 | } else if (i2cport->read_bit(11)) { |
dreamselec | 3:843b830ee8bd | 69 | return 4; |
dreamselec | 3:843b830ee8bd | 70 | } else { |
dreamselec | 3:843b830ee8bd | 71 | return 0; |
dreamselec | 3:843b830ee8bd | 72 | } |
dreamselec | 3:843b830ee8bd | 73 | } while (1); |
dreamselec | 3:843b830ee8bd | 74 | } |
dreamselec | 3:843b830ee8bd | 75 | |
dreamselec | 3:843b830ee8bd | 76 | void detectPC(){ |
dreamselec | 3:843b830ee8bd | 77 | CLEAR_LCD(); |
dreamselec | 3:843b830ee8bd | 78 | LCDFL(); |
dreamselec | 3:843b830ee8bd | 79 | lcd->printf("Detected PC."); |
dreamselec | 3:843b830ee8bd | 80 | LCDSL(); |
dreamselec | 3:843b830ee8bd | 81 | lcd->printf("Connecting"); |
dreamselec | 3:843b830ee8bd | 82 | initPort(); |
dreamselec | 3:843b830ee8bd | 83 | } |
dreamselec | 3:843b830ee8bd | 84 | |
dreamselec | 3:843b830ee8bd | 85 | bool displayAboardDialog(){ |
dreamselec | 3:843b830ee8bd | 86 | CLEAR_LCD(); |
dreamselec | 3:843b830ee8bd | 87 | LCDFL(); |
dreamselec | 3:843b830ee8bd | 88 | lcd->printf("Aboard operation?"); |
dreamselec | 3:843b830ee8bd | 89 | LCDSL(); |
dreamselec | 3:843b830ee8bd | 90 | lcd->printf("1: YES, 2,3,4: NO"); |
dreamselec | 3:843b830ee8bd | 91 | int reply = readSwitches(); |
dreamselec | 3:843b830ee8bd | 92 | if (reply == 1) { return true; } |
dreamselec | 3:843b830ee8bd | 93 | else { return false; } |
dreamselec | 2:7a55cb10259f | 94 | } |
dreamselec | 0:fe5cb0a8fc5a | 95 | |
dreamselec | 0:fe5cb0a8fc5a | 96 | int main(){ |
dreamselec | 3:843b830ee8bd | 97 | initInternal(); |
dreamselec | 2:7a55cb10259f | 98 | initPort(); |
dreamselec | 1:92a2a5ef65a8 | 99 | srand((unsigned)time(NULL)); |
dreamselec | 2:7a55cb10259f | 100 | U_LEDS_OFF(); |
dreamselec | 2:7a55cb10259f | 101 | CLEAR_LCD(); |
dreamselec | 2:7a55cb10259f | 102 | myLED1 = 1; |
dreamselec | 0:fe5cb0a8fc5a | 103 | |
dreamselec | 3:843b830ee8bd | 104 | // Create a serial intereput for RxIrq so when PC is connected it sends '$' to tell MBED it's there. |
dreamselec | 3:843b830ee8bd | 105 | // https://developer.mbed.org/cookbook/Serial-Interrupts |
dreamselec | 3:843b830ee8bd | 106 | |
dreamselec | 3:843b830ee8bd | 107 | for (;;) { |
dreamselec | 3:843b830ee8bd | 108 | i2cport->write_bit(1, 12); |
dreamselec | 3:843b830ee8bd | 109 | lcd->printf("1: Start sorting."); |
dreamselec | 3:843b830ee8bd | 110 | LCDSL(); |
dreamselec | 3:843b830ee8bd | 111 | i2cport->write_bit(1,13); |
dreamselec | 3:843b830ee8bd | 112 | lcd->printf("2: Connect to PC"); |
dreamselec | 3:843b830ee8bd | 113 | |
dreamselec | 3:843b830ee8bd | 114 | int selection = 0; |
dreamselec | 3:843b830ee8bd | 115 | do { |
dreamselec | 3:843b830ee8bd | 116 | selection = readSwitches(); |
dreamselec | 3:843b830ee8bd | 117 | } while (selection == 1 || selection == 2); |
dreamselec | 3:843b830ee8bd | 118 | |
dreamselec | 3:843b830ee8bd | 119 | if (selection == 1) { |
dreamselec | 3:843b830ee8bd | 120 | // User selected op 1: Start sorting autonomaously. |
dreamselec | 3:843b830ee8bd | 121 | i2cport->write_bit(0, 13); |
dreamselec | 3:843b830ee8bd | 122 | CLEAR_LCD(); |
dreamselec | 3:843b830ee8bd | 123 | LCDFL(); |
dreamselec | 3:843b830ee8bd | 124 | lcd->printf("Starting sorting operation"); |
dreamselec | 3:843b830ee8bd | 125 | wait(1); |
dreamselec | 3:843b830ee8bd | 126 | |
dreamselec | 3:843b830ee8bd | 127 | for(;;) { |
dreamselec | 3:843b830ee8bd | 128 | CLEAR_LCD(); |
dreamselec | 3:843b830ee8bd | 129 | lcd->printf("Waiting for block"); |
dreamselec | 3:843b830ee8bd | 130 | i2cport->write_bit(1, 15); |
dreamselec | 3:843b830ee8bd | 131 | bool aboardOperation = false; |
dreamselec | 3:843b830ee8bd | 132 | |
dreamselec | 3:843b830ee8bd | 133 | int blockInserted = 0; |
dreamselec | 3:843b830ee8bd | 134 | // Wait until a block is breaking the beam, or button 4 is pressed to aboard. |
dreamselec | 3:843b830ee8bd | 135 | do { |
dreamselec | 3:843b830ee8bd | 136 | blockInserted = blockIsInserted(); |
dreamselec | 3:843b830ee8bd | 137 | if (i2cport->read_bit(11)) { |
dreamselec | 3:843b830ee8bd | 138 | aboardOperation = displayAboardDialog(); |
dreamselec | 3:843b830ee8bd | 139 | // Cancel the aboard |
dreamselec | 3:843b830ee8bd | 140 | if (aboardOperation == false) { |
dreamselec | 3:843b830ee8bd | 141 | CLEAR_LCD(); |
dreamselec | 3:843b830ee8bd | 142 | LCDFL(); |
dreamselec | 3:843b830ee8bd | 143 | lcd->printf("Waiting for block"); |
dreamselec | 3:843b830ee8bd | 144 | } |
dreamselec | 3:843b830ee8bd | 145 | } |
dreamselec | 3:843b830ee8bd | 146 | } while (aboardOperation == false && blockInserted != 1); |
dreamselec | 3:843b830ee8bd | 147 | |
dreamselec | 3:843b830ee8bd | 148 | // Break and return to main menu i.e. Start Op, or Connect to PC. |
dreamselec | 3:843b830ee8bd | 149 | if (aboardOperation == true) { break; } |
dreamselec | 3:843b830ee8bd | 150 | // Cannot aboard any longer. Block is inserted. |
dreamselec | 3:843b830ee8bd | 151 | int largeBlock = blockIsLarge(); |
dreamselec | 3:843b830ee8bd | 152 | |
dreamselec | 3:843b830ee8bd | 153 | if (largeBlock == hazBlock.blockSizeEnum){ |
dreamselec | 3:843b830ee8bd | 154 | // detectColour() |
dreamselec | 3:843b830ee8bd | 155 | CLEAR_LCD(); |
dreamselec | 3:843b830ee8bd | 156 | lcd->printf("Detecting Colour"); |
dreamselec | 3:843b830ee8bd | 157 | } |
dreamselec | 3:843b830ee8bd | 158 | } |
dreamselec | 3:843b830ee8bd | 159 | |
dreamselec | 3:843b830ee8bd | 160 | } |
dreamselec | 3:843b830ee8bd | 161 | |
dreamselec | 3:843b830ee8bd | 162 | else if (selection == 2) { |
dreamselec | 3:843b830ee8bd | 163 | // Wait for PC to send '$' char to make sure it's there. |
dreamselec | 3:843b830ee8bd | 164 | CLEAR_LCD(); |
dreamselec | 3:843b830ee8bd | 165 | LCDFL(); |
dreamselec | 3:843b830ee8bd | 166 | lcd->printf("Waiting for PC..."); |
dreamselec | 3:843b830ee8bd | 167 | LCDSL(); |
dreamselec | 3:843b830ee8bd | 168 | lcd->printf("4: Main Menu"); |
dreamselec | 3:843b830ee8bd | 169 | |
dreamselec | 3:843b830ee8bd | 170 | } |
dreamselec | 3:843b830ee8bd | 171 | |
dreamselec | 3:843b830ee8bd | 172 | } |
dreamselec | 3:843b830ee8bd | 173 | |
dreamselec | 3:843b830ee8bd | 174 | |
dreamselec | 2:7a55cb10259f | 175 | FOREVER { |
dreamselec | 3:843b830ee8bd | 176 | pc.printf("In FOREVER\n"); |
dreamselec | 3:843b830ee8bd | 177 | Type type = INVALID; |
dreamselec | 3:843b830ee8bd | 178 | pc.printf("Type set to: %i\r\n", type); |
dreamselec | 2:7a55cb10259f | 179 | do { |
dreamselec | 3:843b830ee8bd | 180 | pc.printf("In DO-WHILE loop\r\n"); |
dreamselec | 2:7a55cb10259f | 181 | type = listenForCommand(); |
dreamselec | 3:843b830ee8bd | 182 | if (i2cport->read_bit(8)){ |
dreamselec | 3:843b830ee8bd | 183 | pc.printf("Listening\r\n"); |
dreamselec | 3:843b830ee8bd | 184 | } |
dreamselec | 3:843b830ee8bd | 185 | } while (type == -1); |
dreamselec | 3:843b830ee8bd | 186 | |
dreamselec | 3:843b830ee8bd | 187 | pc.printf("Command Inbound\r\n"); |
dreamselec | 3:843b830ee8bd | 188 | |
dreamselec | 3:843b830ee8bd | 189 | myCommand.typeEnum = type; |
dreamselec | 3:843b830ee8bd | 190 | pc.printf("Type: %i\r\n-------------\r\n", myCommand.typeEnum); |
dreamselec | 2:7a55cb10259f | 191 | recieveCommand(&myCommand, type); |
dreamselec | 3:843b830ee8bd | 192 | |
dreamselec | 3:843b830ee8bd | 193 | 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 | 194 | // decodeCommand(&myCommand); |
dreamselec | 2:7a55cb10259f | 195 | |
dreamselec | 2:7a55cb10259f | 196 | |
dreamselec | 2:7a55cb10259f | 197 | } |
dreamselec | 0:fe5cb0a8fc5a | 198 | } |