![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
The MBED firmware used on the Chipin sorter, developed over 12 weeks for a 3rd year university systems project. Chipin is a token sorter, it sorts tokens by colours and dispenses them to order through an online booking system and card reader. This program interfaces with an FPGA, PC and LCD screen to control the sorter. The sorter has an operation mode where it can process orders when a card is entered into the machine. There is also a maintenance mode where the device responds to maintenance instructions such as 'dispense all'. More information at http://www.ionsystems.uk/
Dependencies: MCP23017 TCS3472_I2C WattBob_TextLCD mbed-rtos mbed
serialCommunication.h@12:814a8fdbb6f7, 2014-11-17 (annotated)
- Committer:
- IonSystems
- Date:
- Mon Nov 17 21:31:42 2014 +0000
- Revision:
- 12:814a8fdbb6f7
- Parent:
- 10:8c0696b99692
- Child:
- 15:0c5f20e15b6a
dispense and sort working well, also stored correct chip values
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
IonSystems | 10:8c0696b99692 | 1 | #include "cardReader.h" |
IonSystems | 10:8c0696b99692 | 2 | |
IonSystems | 10:8c0696b99692 | 3 | |
IonSystems | 10:8c0696b99692 | 4 | |
IonSystems | 10:8c0696b99692 | 5 | Serial pc(USBTX, USBRX); |
IonSystems | 10:8c0696b99692 | 6 | /* valueToChar(int value): |
IonSystems | 10:8c0696b99692 | 7 | Expects an int value what it 0 or 1. |
IonSystems | 10:8c0696b99692 | 8 | Returns the int value as a char. |
IonSystems | 10:8c0696b99692 | 9 | 0 will return '0' |
IonSystems | 10:8c0696b99692 | 10 | 1 will return '1' |
IonSystems | 10:8c0696b99692 | 11 | Used to read the bits in the card reader. |
IonSystems | 10:8c0696b99692 | 12 | */ |
IonSystems | 10:8c0696b99692 | 13 | char valueToChar(int value){ |
IonSystems | 10:8c0696b99692 | 14 | if(value == 0){ |
IonSystems | 10:8c0696b99692 | 15 | return '0'; |
IonSystems | 10:8c0696b99692 | 16 | }else if(value == 1){ |
IonSystems | 10:8c0696b99692 | 17 | return '1'; |
IonSystems | 10:8c0696b99692 | 18 | }else{ |
IonSystems | 10:8c0696b99692 | 19 | return NULL; |
IonSystems | 10:8c0696b99692 | 20 | } |
IonSystems | 10:8c0696b99692 | 21 | } |
IonSystems | 10:8c0696b99692 | 22 | |
IonSystems | 10:8c0696b99692 | 23 | void sendCharacter(char ch){ |
IonSystems | 10:8c0696b99692 | 24 | pc.putc(ch); |
IonSystems | 10:8c0696b99692 | 25 | } |
IonSystems | 10:8c0696b99692 | 26 | |
IonSystems | 10:8c0696b99692 | 27 | void sendString(char* ch){ |
IonSystems | 10:8c0696b99692 | 28 | pc.puts(ch); |
IonSystems | 10:8c0696b99692 | 29 | } |
IonSystems | 10:8c0696b99692 | 30 | |
IonSystems | 10:8c0696b99692 | 31 | |
IonSystems | 6:e64796f1f384 | 32 | char readBit(int bitNumber){ |
IonSystems | 6:e64796f1f384 | 33 | int value = 0; |
IonSystems | 6:e64796f1f384 | 34 | switch(bitNumber){ |
IonSystems | 6:e64796f1f384 | 35 | case 1: |
IonSystems | 6:e64796f1f384 | 36 | value = cardBit1; |
IonSystems | 6:e64796f1f384 | 37 | break; |
IonSystems | 6:e64796f1f384 | 38 | case 2: |
IonSystems | 6:e64796f1f384 | 39 | value = cardBit2; |
IonSystems | 6:e64796f1f384 | 40 | break; |
IonSystems | 6:e64796f1f384 | 41 | case 3: |
IonSystems | 6:e64796f1f384 | 42 | value = cardBit3; |
IonSystems | 6:e64796f1f384 | 43 | break; |
IonSystems | 6:e64796f1f384 | 44 | case 4: |
IonSystems | 6:e64796f1f384 | 45 | value = cardBit4; |
IonSystems | 6:e64796f1f384 | 46 | break; |
IonSystems | 6:e64796f1f384 | 47 | case 5: |
IonSystems | 6:e64796f1f384 | 48 | value = cardBit5; |
IonSystems | 6:e64796f1f384 | 49 | break; |
IonSystems | 6:e64796f1f384 | 50 | case 6: |
IonSystems | 6:e64796f1f384 | 51 | value = cardBit6; |
IonSystems | 6:e64796f1f384 | 52 | break; |
IonSystems | 6:e64796f1f384 | 53 | } |
IonSystems | 6:e64796f1f384 | 54 | return valueToChar(value); |
IonSystems | 10:8c0696b99692 | 55 | } |
IonSystems | 10:8c0696b99692 | 56 | void processState(StateMachine stateMachine){ |
IonSystems | 10:8c0696b99692 | 57 | switch(stateMachine){ |
IonSystems | 10:8c0696b99692 | 58 | case INITIALISING: |
IonSystems | 10:8c0696b99692 | 59 | setupLCD(); |
IonSystems | 10:8c0696b99692 | 60 | printLCD("Welcome to the Chipin Sorter"); |
IonSystems | 10:8c0696b99692 | 61 | wait(1); |
IonSystems | 10:8c0696b99692 | 62 | break; |
IonSystems | 10:8c0696b99692 | 63 | case READING_RGB_VALUES: |
IonSystems | 10:8c0696b99692 | 64 | readChipNumbers(); |
IonSystems | 10:8c0696b99692 | 65 | break; |
IonSystems | 10:8c0696b99692 | 66 | case DISPENSE_RED: |
IonSystems | 10:8c0696b99692 | 67 | dispense(RED); |
IonSystems | 10:8c0696b99692 | 68 | break; |
IonSystems | 10:8c0696b99692 | 69 | case DISPENSE_GREEN: |
IonSystems | 10:8c0696b99692 | 70 | dispense(GREEN); |
IonSystems | 10:8c0696b99692 | 71 | break; |
IonSystems | 10:8c0696b99692 | 72 | case DISPENSE_BLUE: |
IonSystems | 10:8c0696b99692 | 73 | dispense(BLUE); |
IonSystems | 10:8c0696b99692 | 74 | break; |
IonSystems | 10:8c0696b99692 | 75 | case SORT_RED: |
IonSystems | 10:8c0696b99692 | 76 | sort(RED); |
IonSystems | 10:8c0696b99692 | 77 | break; |
IonSystems | 10:8c0696b99692 | 78 | case SORT_GREEN: |
IonSystems | 10:8c0696b99692 | 79 | sort(GREEN); |
IonSystems | 10:8c0696b99692 | 80 | break; |
IonSystems | 10:8c0696b99692 | 81 | case SORT_BLUE: |
IonSystems | 10:8c0696b99692 | 82 | sort(BLUE); |
IonSystems | 10:8c0696b99692 | 83 | break; |
IonSystems | 10:8c0696b99692 | 84 | case SORT_BIN: |
IonSystems | 10:8c0696b99692 | 85 | sort(BIN); |
IonSystems | 10:8c0696b99692 | 86 | break; |
IonSystems | 10:8c0696b99692 | 87 | case SORT_RECYCLE: |
IonSystems | 10:8c0696b99692 | 88 | sort(RECYCLE); |
IonSystems | 10:8c0696b99692 | 89 | break; |
IonSystems | 12:814a8fdbb6f7 | 90 | case DISPENSE_ALL: |
IonSystems | 12:814a8fdbb6f7 | 91 | dispenseAll(); |
IonSystems | 12:814a8fdbb6f7 | 92 | break; |
IonSystems | 10:8c0696b99692 | 93 | /* case WAITING_FOR_ACK: |
IonSystems | 10:8c0696b99692 | 94 | checkSerial(); |
IonSystems | 10:8c0696b99692 | 95 | break; |
IonSystems | 10:8c0696b99692 | 96 | case WAITING_FOR_CHAR: |
IonSystems | 10:8c0696b99692 | 97 | log("About to check serial"); |
IonSystems | 10:8c0696b99692 | 98 | printLCD("WAITING_FOR_CHAR"); |
IonSystems | 10:8c0696b99692 | 99 | checkSerial(); |
IonSystems | 10:8c0696b99692 | 100 | break;*/ |
IonSystems | 10:8c0696b99692 | 101 | |
IonSystems | 6:e64796f1f384 | 102 | } |
IonSystems | 10:8c0696b99692 | 103 | } |
IonSystems | 12:814a8fdbb6f7 | 104 | |
IonSystems | 12:814a8fdbb6f7 | 105 | |
IonSystems | 6:e64796f1f384 | 106 | |
IonSystems | 10:8c0696b99692 | 107 | |
IonSystems | 10:8c0696b99692 | 108 | void processMessage(char c){ |
IonSystems | 6:e64796f1f384 | 109 | switch(c){ |
IonSystems | 10:8c0696b99692 | 110 | case 'a' : //Dispense red |
IonSystems | 10:8c0696b99692 | 111 | dispense(RED); |
IonSystems | 10:8c0696b99692 | 112 | sendCharacter('A'); |
IonSystems | 6:e64796f1f384 | 113 | break; |
IonSystems | 10:8c0696b99692 | 114 | case 'b' : //dispense green |
IonSystems | 10:8c0696b99692 | 115 | dispense(GREEN); |
IonSystems | 10:8c0696b99692 | 116 | sendCharacter('B'); |
IonSystems | 6:e64796f1f384 | 117 | break; |
IonSystems | 10:8c0696b99692 | 118 | case 'c': //dispense blue |
IonSystems | 10:8c0696b99692 | 119 | dispense(BLUE); |
IonSystems | 10:8c0696b99692 | 120 | sendCharacter('C'); |
IonSystems | 6:e64796f1f384 | 121 | break; |
IonSystems | 6:e64796f1f384 | 122 | case 'C':// Read card command |
IonSystems | 6:e64796f1f384 | 123 | sendCharacter('R'); //Notify PC that command has been recieved |
IonSystems | 6:e64796f1f384 | 124 | printLCD("Reading Card"); |
IonSystems | 6:e64796f1f384 | 125 | //read card here |
IonSystems | 6:e64796f1f384 | 126 | char value = ' '; |
IonSystems | 6:e64796f1f384 | 127 | for(int num = 1;num <= 6;num++){ |
IonSystems | 6:e64796f1f384 | 128 | value = readBit(num); |
IonSystems | 6:e64796f1f384 | 129 | // sendCharacter('R'); |
IonSystems | 6:e64796f1f384 | 130 | sendCharacter(value); |
IonSystems | 6:e64796f1f384 | 131 | printLCD("Bit read"); |
IonSystems | 6:e64796f1f384 | 132 | //wait(0.1); |
IonSystems | 6:e64796f1f384 | 133 | } |
IonSystems | 6:e64796f1f384 | 134 | sendCharacter('C'); //Tells PC that the card has been read |
IonSystems | 6:e64796f1f384 | 135 | printLCD("Card read done"); |
IonSystems | 6:e64796f1f384 | 136 | break; |
IonSystems | 6:e64796f1f384 | 137 | case 'X': |
IonSystems | 6:e64796f1f384 | 138 | sendCharacter('X'); //Notify the PC that this is an MBED |
IonSystems | 6:e64796f1f384 | 139 | printLCD("Connected to PC"); |
IonSystems | 6:e64796f1f384 | 140 | break; |
IonSystems | 6:e64796f1f384 | 141 | case 'd': //Dispense a chip |
IonSystems | 6:e64796f1f384 | 142 | sendCharacter('D'); |
IonSystems | 6:e64796f1f384 | 143 | printLCD("Dispensing chip"); |
IonSystems | 6:e64796f1f384 | 144 | |
IonSystems | 6:e64796f1f384 | 145 | //read from color sensor |
IonSystems | 6:e64796f1f384 | 146 | //convert readings to colour |
IonSystems | 6:e64796f1f384 | 147 | //convert colour to 3-bit colour code |
IonSystems | 6:e64796f1f384 | 148 | //set the FPGA colour inputs to the colour code |
IonSystems | 6:e64796f1f384 | 149 | startSort = true; |
IonSystems | 6:e64796f1f384 | 150 | break; |
IonSystems | 10:8c0696b99692 | 151 | |
IonSystems | 12:814a8fdbb6f7 | 152 | case 'o': //testServoredBlueLeft |
IonSystems | 12:814a8fdbb6f7 | 153 | maintain(RB_LEFT); |
IonSystems | 12:814a8fdbb6f7 | 154 | sendCharacter('O'); |
IonSystems | 10:8c0696b99692 | 155 | break; |
IonSystems | 10:8c0696b99692 | 156 | |
IonSystems | 10:8c0696b99692 | 157 | case 'p': |
IonSystems | 12:814a8fdbb6f7 | 158 | maintain(RB_CENTRE); |
IonSystems | 12:814a8fdbb6f7 | 159 | sendCharacter('P'); |
IonSystems | 10:8c0696b99692 | 160 | break; |
IonSystems | 10:8c0696b99692 | 161 | |
IonSystems | 10:8c0696b99692 | 162 | case 'q': |
IonSystems | 12:814a8fdbb6f7 | 163 | maintain(RB_RIGHT); |
IonSystems | 12:814a8fdbb6f7 | 164 | sendCharacter('O'); |
IonSystems | 10:8c0696b99692 | 165 | break; |
IonSystems | 10:8c0696b99692 | 166 | |
IonSystems | 10:8c0696b99692 | 167 | case 's': |
IonSystems | 12:814a8fdbb6f7 | 168 | maintain(GO_UP); |
IonSystems | 12:814a8fdbb6f7 | 169 | sendCharacter('S'); |
IonSystems | 10:8c0696b99692 | 170 | break; |
IonSystems | 10:8c0696b99692 | 171 | |
IonSystems | 10:8c0696b99692 | 172 | case 't': |
IonSystems | 12:814a8fdbb6f7 | 173 | maintain(GO_CENTRE); |
IonSystems | 12:814a8fdbb6f7 | 174 | sendCharacter('T'); |
IonSystems | 10:8c0696b99692 | 175 | break; |
IonSystems | 10:8c0696b99692 | 176 | |
IonSystems | 10:8c0696b99692 | 177 | case 'u': |
IonSystems | 12:814a8fdbb6f7 | 178 | maintain(GO_DOWN); |
IonSystems | 12:814a8fdbb6f7 | 179 | sendCharacter('U'); |
IonSystems | 10:8c0696b99692 | 180 | break; |
IonSystems | 10:8c0696b99692 | 181 | |
IonSystems | 10:8c0696b99692 | 182 | |
IonSystems | 10:8c0696b99692 | 183 | |
IonSystems | 10:8c0696b99692 | 184 | case 'v': |
IonSystems | 12:814a8fdbb6f7 | 185 | maintain(BR_LEFT); |
IonSystems | 12:814a8fdbb6f7 | 186 | sendCharacter('V'); |
IonSystems | 10:8c0696b99692 | 187 | break; |
IonSystems | 10:8c0696b99692 | 188 | |
IonSystems | 10:8c0696b99692 | 189 | case 'w': |
IonSystems | 12:814a8fdbb6f7 | 190 | maintain(BR_LEFT); |
IonSystems | 12:814a8fdbb6f7 | 191 | sendCharacter('O'); |
IonSystems | 10:8c0696b99692 | 192 | break; |
IonSystems | 10:8c0696b99692 | 193 | case 'j': |
IonSystems | 10:8c0696b99692 | 194 | sort(RED); |
IonSystems | 10:8c0696b99692 | 195 | sendCharacter('J'); |
IonSystems | 10:8c0696b99692 | 196 | break; |
IonSystems | 10:8c0696b99692 | 197 | case 'k': |
IonSystems | 10:8c0696b99692 | 198 | sort(GREEN); |
IonSystems | 10:8c0696b99692 | 199 | sendCharacter('K'); |
IonSystems | 10:8c0696b99692 | 200 | break; |
IonSystems | 10:8c0696b99692 | 201 | case 'l': |
IonSystems | 10:8c0696b99692 | 202 | sort(BLUE); |
IonSystems | 10:8c0696b99692 | 203 | sendCharacter('L'); |
IonSystems | 10:8c0696b99692 | 204 | break; |
IonSystems | 10:8c0696b99692 | 205 | |
IonSystems | 12:814a8fdbb6f7 | 206 | case '{': //Dispense all |
IonSystems | 12:814a8fdbb6f7 | 207 | dispenseAll(); |
IonSystems | 12:814a8fdbb6f7 | 208 | sendCharacter('}'); |
IonSystems | 12:814a8fdbb6f7 | 209 | break; |
IonSystems | 10:8c0696b99692 | 210 | |
IonSystems | 10:8c0696b99692 | 211 | |
IonSystems | 10:8c0696b99692 | 212 | |
IonSystems | 10:8c0696b99692 | 213 | |
IonSystems | 10:8c0696b99692 | 214 | |
IonSystems | 10:8c0696b99692 | 215 | |
IonSystems | 10:8c0696b99692 | 216 | |
IonSystems | 10:8c0696b99692 | 217 | |
IonSystems | 10:8c0696b99692 | 218 | |
IonSystems | 10:8c0696b99692 | 219 | |
IonSystems | 10:8c0696b99692 | 220 | |
IonSystems | 10:8c0696b99692 | 221 | /*case 't'://Test 180 servo |
IonSystems | 6:e64796f1f384 | 222 | sendCharacter('T'); |
IonSystems | 6:e64796f1f384 | 223 | printLCD("Servo signal on"); |
IonSystems | 6:e64796f1f384 | 224 | servoTest = true; |
IonSystems | 6:e64796f1f384 | 225 | break; |
IonSystems | 6:e64796f1f384 | 226 | case 'w'://Test 180 servo |
IonSystems | 6:e64796f1f384 | 227 | sendCharacter('W'); |
IonSystems | 6:e64796f1f384 | 228 | printLCD("Servo signal off"); |
IonSystems | 6:e64796f1f384 | 229 | servoTest = false; |
IonSystems | 10:8c0696b99692 | 230 | break;*/ |
IonSystems | 10:8c0696b99692 | 231 | } |
IonSystems | 10:8c0696b99692 | 232 | } |
IonSystems | 6:e64796f1f384 | 233 | |
IonSystems | 10:8c0696b99692 | 234 | /*else if (operationMode){ |
IonSystems | 6:e64796f1f384 | 235 | switch(c){ |
IonSystems | 6:e64796f1f384 | 236 | case 'n':// Start sorting |
IonSystems | 6:e64796f1f384 | 237 | sendCharacter('N'); //Confirm to PC that we are processing the instruction. |
IonSystems | 6:e64796f1f384 | 238 | startSort = true; //Pulse the startSort control line to FPGA |
IonSystems | 6:e64796f1f384 | 239 | wait(0.1); |
IonSystems | 6:e64796f1f384 | 240 | startSort = false; |
IonSystems | 6:e64796f1f384 | 241 | break; |
IonSystems | 6:e64796f1f384 | 242 | case 'o': //Start dispensing |
IonSystems | 6:e64796f1f384 | 243 | sendCharacter('O'); |
IonSystems | 6:e64796f1f384 | 244 | startDispense = true; //Pulse the startDispense control line to FPGA |
IonSystems | 6:e64796f1f384 | 245 | wait(0.1); |
IonSystems | 6:e64796f1f384 | 246 | startDispense = false; |
IonSystems | 6:e64796f1f384 | 247 | break; |
IonSystems | 6:e64796f1f384 | 248 | case 'p': // |
IonSystems | 6:e64796f1f384 | 249 | sendCharacter('P'); |
IonSystems | 6:e64796f1f384 | 250 | break; |
IonSystems | 6:e64796f1f384 | 251 | } |
IonSystems | 6:e64796f1f384 | 252 | } |
IonSystems | 10:8c0696b99692 | 253 | }*/ |
IonSystems | 10:8c0696b99692 | 254 | bool checkSerial(){ |
IonSystems | 10:8c0696b99692 | 255 | log("Checking serial"); |
IonSystems | 10:8c0696b99692 | 256 | printLCD(""); |
IonSystems | 10:8c0696b99692 | 257 | if(pc.readable()){ |
IonSystems | 10:8c0696b99692 | 258 | log("serial port is readable, reading char and processing"); |
IonSystems | 10:8c0696b99692 | 259 | char c = pc.getc(); |
IonSystems | 10:8c0696b99692 | 260 | processMessage(c); |
IonSystems | 10:8c0696b99692 | 261 | return true; |
IonSystems | 10:8c0696b99692 | 262 | }else{ |
IonSystems | 10:8c0696b99692 | 263 | log("serial port not readable, doing nothing"); |
IonSystems | 10:8c0696b99692 | 264 | return false; |
IonSystems | 10:8c0696b99692 | 265 | } |
IonSystems | 6:e64796f1f384 | 266 | } |
IonSystems | 10:8c0696b99692 | 267 | bool readCharacter(){ |
IonSystems | 10:8c0696b99692 | 268 | char c = pc.getc(); //wait for a serial character to be recieved. |
IonSystems | 10:8c0696b99692 | 269 | processMessage(c); //Do something, based on the character recieved. |
IonSystems | 10:8c0696b99692 | 270 | return true; |
IonSystems | 6:e64796f1f384 | 271 | } |
IonSystems | 6:e64796f1f384 | 272 | |
IonSystems | 10:8c0696b99692 | 273 |