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

Committer:
IonSystems
Date:
Sun Nov 23 18:45:52 2014 +0000
Revision:
16:498359d078dc
Parent:
15:0c5f20e15b6a
Child:
17:6a0bb0ad5bb4
Storage mode now increments all stored values by 2 on button 2 press, rather than set all values to 2.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IonSystems 12:814a8fdbb6f7 1 bool operationMode = true; //the MBED starts in operation mode.
IonSystems 0:8d54ffcf256e 2 #include "mbed.h"
IonSystems 15:0c5f20e15b6a 3 #include "Language.h"
IonSystems 15:0c5f20e15b6a 4 DigitalIn languageButton(p30);
IonSystems 15:0c5f20e15b6a 5 Language currentLanguage = ENGLISH;
IonSystems 6:e64796f1f384 6 #include <sstream>
IonSystems 12:814a8fdbb6f7 7 #include <string>
IonSystems 15:0c5f20e15b6a 8
IonSystems 6:e64796f1f384 9 #include "lcdCommands.h"
IonSystems 6:e64796f1f384 10 #include "mbedStorage.h"
IonSystems 6:e64796f1f384 11 #include "rgbLED.h"
IonSystems 6:e64796f1f384 12 #include "mbedLCD.h"
IonSystems 7:6ba00694f9cd 13 #include "stateMachine.h"
IonSystems 8:b7771391adc9 14 #include "FPGAcomms.h"
IonSystems 10:8c0696b99692 15 #ifndef serialComm
IonSystems 10:8c0696b99692 16 #define serialComm
IonSystems 15:0c5f20e15b6a 17
IonSystems 10:8c0696b99692 18 #include "serialCommunication.h"
IonSystems 10:8c0696b99692 19 #endif
IonSystems 10:8c0696b99692 20 #include "testFunctions.h"
IonSystems 5:644bca33c1ca 21
IonSystems 0:8d54ffcf256e 22
IonSystems 11:06f6e82b40a8 23
IonSystems 11:06f6e82b40a8 24
IonSystems 7:6ba00694f9cd 25 StateMachine stateMachine;
IonSystems 7:6ba00694f9cd 26
IonSystems 6:e64796f1f384 27 //Boolean values to easily enable and disable certain features for individual testing
IonSystems 6:e64796f1f384 28 bool colourSensor = true;
IonSystems 6:e64796f1f384 29 bool cardReader = true;
IonSystems 6:e64796f1f384 30 bool sorter = true;
IonSystems 6:e64796f1f384 31 bool dispensor = true;
IonSystems 10:8c0696b99692 32 bool testFunctions = true;
IonSystems 10:8c0696b99692 33 bool serialComms = true;
IonSystems 6:e64796f1f384 34
IonSystems 6:e64796f1f384 35 bool colourDataAcquired = false;
IonSystems 6:e64796f1f384 36 bool chipDetected = false;
IonSystems 12:814a8fdbb6f7 37
IonSystems 3:97668a4cd69d 38
IonSystems 15:0c5f20e15b6a 39 int mode = 4;
IonSystems 13:0661d658d9d1 40 int maintenanceModePointer = 0;
IonSystems 14:31ba3e56c788 41 Colour lastColour = NONE;
IonSystems 10:8c0696b99692 42
IonSystems 10:8c0696b99692 43 void checkSortDispenseButtons(){
IonSystems 11:06f6e82b40a8 44
IonSystems 11:06f6e82b40a8 45 if(par_port->read_bit(11)){
IonSystems 12:814a8fdbb6f7 46 mode++;
IonSystems 13:0661d658d9d1 47 if(mode >= 7) mode = 0;
IonSystems 11:06f6e82b40a8 48 }
IonSystems 11:06f6e82b40a8 49
IonSystems 11:06f6e82b40a8 50
IonSystems 13:0661d658d9d1 51 if(mode == 0){ //Dispense Mode
IonSystems 11:06f6e82b40a8 52 printLCD("Dispense Mode");
IonSystems 10:8c0696b99692 53 if(par_port->read_bit(8)){
IonSystems 10:8c0696b99692 54 log("Red Button pressed");
IonSystems 10:8c0696b99692 55 stateMachine = DISPENSE_RED;
IonSystems 10:8c0696b99692 56 processState(stateMachine);
IonSystems 10:8c0696b99692 57 }else if(par_port->read_bit(9)){
IonSystems 10:8c0696b99692 58 log("Green Button pressed");
IonSystems 10:8c0696b99692 59 stateMachine = DISPENSE_GREEN;
IonSystems 10:8c0696b99692 60 processState(stateMachine);
IonSystems 10:8c0696b99692 61 }else if(par_port->read_bit(10)){
IonSystems 10:8c0696b99692 62 log("Blue Button pressed");
IonSystems 10:8c0696b99692 63 stateMachine = DISPENSE_BLUE;
IonSystems 10:8c0696b99692 64 processState(stateMachine);
IonSystems 10:8c0696b99692 65 }
IonSystems 13:0661d658d9d1 66 }else if(mode == 1){ //Sort Mode
IonSystems 11:06f6e82b40a8 67 printLCD("Sort Mode");
IonSystems 10:8c0696b99692 68 if(par_port->read_bit(8)){
IonSystems 10:8c0696b99692 69 log("Red Button pressed");
IonSystems 10:8c0696b99692 70 stateMachine = SORT_RED;
IonSystems 10:8c0696b99692 71 processState(stateMachine);
IonSystems 10:8c0696b99692 72 }else if(par_port->read_bit(9)){
IonSystems 13:0661d658d9d1 73 log("Green Button pressed");
IonSystems 10:8c0696b99692 74 stateMachine = SORT_GREEN;
IonSystems 10:8c0696b99692 75 processState(stateMachine);
IonSystems 10:8c0696b99692 76 }else if(par_port->read_bit(10)){
IonSystems 10:8c0696b99692 77 log("Blue Button pressed");
IonSystems 10:8c0696b99692 78 stateMachine = SORT_BLUE;
IonSystems 10:8c0696b99692 79 processState(stateMachine);
IonSystems 10:8c0696b99692 80 }
IonSystems 11:06f6e82b40a8 81
IonSystems 13:0661d658d9d1 82 }else if(mode == 2){ //Bin/Recycle Mode
IonSystems 11:06f6e82b40a8 83 printLCD("Bin/Recycle Mode");
IonSystems 11:06f6e82b40a8 84 if(par_port->read_bit(8)){
IonSystems 11:06f6e82b40a8 85 log("Bin button pressed");
IonSystems 11:06f6e82b40a8 86 stateMachine = SORT_BIN;
IonSystems 11:06f6e82b40a8 87 processState(stateMachine);
IonSystems 11:06f6e82b40a8 88 }else if(par_port->read_bit(9)){
IonSystems 11:06f6e82b40a8 89 log("Recycle Button");
IonSystems 11:06f6e82b40a8 90 stateMachine = SORT_RECYCLE;
IonSystems 11:06f6e82b40a8 91 processState(stateMachine);
IonSystems 11:06f6e82b40a8 92 }
IonSystems 14:31ba3e56c788 93 }else if(mode == 3){
IonSystems 14:31ba3e56c788 94 printLCD("Colour Sensor Mode"); //Colour Sensing Mode
IonSystems 11:06f6e82b40a8 95 if(colourSensor){
IonSystems 11:06f6e82b40a8 96 Colour colour = readColourSensor();
IonSystems 15:0c5f20e15b6a 97
IonSystems 15:0c5f20e15b6a 98 /* Tries to prevent a colour mis-read. When a chip is read as BIN or NONE,
IonSystems 15:0c5f20e15b6a 99 the chip may be falling down the tube as the reading was taken. The chip may
IonSystems 15:0c5f20e15b6a 100 also be out of position on the slider. Ignoring the next reading is trying to
IonSystems 15:0c5f20e15b6a 101 prevent a mis-read being valid.
IonSystems 15:0c5f20e15b6a 102 */
IonSystems 15:0c5f20e15b6a 103 if(lastColour == NONE || lastColour == BIN){
IonSystems 14:31ba3e56c788 104 return;
IonSystems 14:31ba3e56c788 105 }
IonSystems 15:0c5f20e15b6a 106 lastColour = colour;
IonSystems 14:31ba3e56c788 107 if(sorter) sort(colour);
IonSystems 14:31ba3e56c788 108 //Put (sorter && colour != BIN) in if statement.
IonSystems 11:06f6e82b40a8 109 }
IonSystems 14:31ba3e56c788 110 //if
IonSystems 13:0661d658d9d1 111 }else if(mode == 4){ //Card Reader Mode
IonSystems 12:814a8fdbb6f7 112 if(cardReader && cardDetect){
IonSystems 12:814a8fdbb6f7 113 cardAcquisition();
IonSystems 12:814a8fdbb6f7 114
IonSystems 12:814a8fdbb6f7 115 }
IonSystems 13:0661d658d9d1 116 }else if(mode == 5){ //Storage Mode
IonSystems 12:814a8fdbb6f7 117 stringstream ss;
IonSystems 12:814a8fdbb6f7 118 ss << "Stored Values " << "R:" << redAmount << "G:" << greenAmount << "B:" << blueAmount;
IonSystems 12:814a8fdbb6f7 119 string tmp = ss.str();
IonSystems 12:814a8fdbb6f7 120 printLCD(tmp.c_str());
IonSystems 12:814a8fdbb6f7 121 wait(0.1);
IonSystems 12:814a8fdbb6f7 122 if(par_port->read_bit(8)){
IonSystems 12:814a8fdbb6f7 123 log("Dispense all button pressed");
IonSystems 12:814a8fdbb6f7 124 stateMachine = DISPENSE_ALL;
IonSystems 12:814a8fdbb6f7 125 processState(stateMachine);
IonSystems 12:814a8fdbb6f7 126 }else if(par_port->read_bit(9)){
IonSystems 12:814a8fdbb6f7 127 log("Clear stored numbers button pressed");
IonSystems 12:814a8fdbb6f7 128 writeFile(0,0,0);
IonSystems 12:814a8fdbb6f7 129 redAmount = 0;greenAmount = 0;blueAmount = 0;
IonSystems 12:814a8fdbb6f7 130 printLCD("Cleared values");
IonSystems 12:814a8fdbb6f7 131
IonSystems 12:814a8fdbb6f7 132 }else if(par_port->read_bit(10)){
IonSystems 16:498359d078dc 133 log("Increment all stored values by 2.");
IonSystems 16:498359d078dc 134 redAmount += 2;greenAmount += 2;blueAmount += 2;
IonSystems 16:498359d078dc 135 writeFile(redAmount,greenAmount,blueAmount);
IonSystems 16:498359d078dc 136
IonSystems 13:0661d658d9d1 137 printLCD("set values to 2");
IonSystems 12:814a8fdbb6f7 138
IonSystems 12:814a8fdbb6f7 139 }
IonSystems 13:0661d658d9d1 140 }else if(mode == 6){ //Maintenance Mode
IonSystems 14:31ba3e56c788 141 printLCD("Maintenace Mode");
IonSystems 13:0661d658d9d1 142 if(par_port->read_bit(8)){ //Next maintenance function
IonSystems 13:0661d658d9d1 143 maintenanceModePointer++;
IonSystems 13:0661d658d9d1 144 if(maintenanceModePointer > 12) maintenanceModePointer = 0;
IonSystems 13:0661d658d9d1 145 }else if(par_port->read_bit(9)){ //Previous maintenance function
IonSystems 13:0661d658d9d1 146 maintenanceModePointer--;
IonSystems 13:0661d658d9d1 147 if(maintenanceModePointer < 0) maintenanceModePointer = 12;
IonSystems 13:0661d658d9d1 148 }else if(par_port->read_bit(10)){ //Process Instruction
IonSystems 13:0661d658d9d1 149 switch(maintenanceModePointer){
IonSystems 13:0661d658d9d1 150 case 0://RB_LEFT:
IonSystems 14:31ba3e56c788 151 printLCD("Moving Red/Blue Slider left.");
IonSystems 13:0661d658d9d1 152 maintain(RB_LEFT);
IonSystems 13:0661d658d9d1 153 break;
IonSystems 13:0661d658d9d1 154
IonSystems 13:0661d658d9d1 155 case 1://RB_CENTRE:
IonSystems 14:31ba3e56c788 156 printLCD("Centering Red/Blue Slider.");
IonSystems 13:0661d658d9d1 157 maintain(RB_CENTRE);
IonSystems 13:0661d658d9d1 158 break;
IonSystems 13:0661d658d9d1 159
IonSystems 13:0661d658d9d1 160 case 2://RB_RIGHT:
IonSystems 14:31ba3e56c788 161 printLCD("Moving Red/Blue Slider right.");
IonSystems 13:0661d658d9d1 162 maintain(RB_RIGHT);
IonSystems 13:0661d658d9d1 163 break;
IonSystems 13:0661d658d9d1 164
IonSystems 13:0661d658d9d1 165 case 3://GO_UP:
IonSystems 14:31ba3e56c788 166 printLCD("Moving Green Slider up.");
IonSystems 13:0661d658d9d1 167 maintain(GO_UP);
IonSystems 13:0661d658d9d1 168 break;
IonSystems 13:0661d658d9d1 169
IonSystems 13:0661d658d9d1 170 case 4://GO_CENTRE:
IonSystems 14:31ba3e56c788 171 printLCD("Centering Green Slider.");
IonSystems 13:0661d658d9d1 172 maintain(GO_CENTRE);
IonSystems 13:0661d658d9d1 173 break;
IonSystems 13:0661d658d9d1 174
IonSystems 13:0661d658d9d1 175 case 5://GO_DOWN:
IonSystems 14:31ba3e56c788 176 printLCD("Moving Green Slider down.");
IonSystems 13:0661d658d9d1 177 maintain(GO_DOWN);
IonSystems 13:0661d658d9d1 178 break;
IonSystems 13:0661d658d9d1 179
IonSystems 13:0661d658d9d1 180 case 6://BR_LEFT:
IonSystems 14:31ba3e56c788 181 printLCD("Moving Bin Slider left.");
IonSystems 13:0661d658d9d1 182 maintain(BR_LEFT);
IonSystems 13:0661d658d9d1 183 break;
IonSystems 13:0661d658d9d1 184
IonSystems 13:0661d658d9d1 185 case 7://BR_RIGHT:
IonSystems 14:31ba3e56c788 186 printLCD("Moving Bin Slider right.");
IonSystems 13:0661d658d9d1 187 maintain(BR_RIGHT);
IonSystems 13:0661d658d9d1 188 break;
IonSystems 13:0661d658d9d1 189
IonSystems 13:0661d658d9d1 190 case 8://R_PUSH:
IonSystems 14:31ba3e56c788 191 printLCD("Pushing red dispensor");
IonSystems 13:0661d658d9d1 192 maintain(R_PUSH);
IonSystems 13:0661d658d9d1 193 break;
IonSystems 13:0661d658d9d1 194
IonSystems 13:0661d658d9d1 195 case 9://R_HOME:
IonSystems 14:31ba3e56c788 196 printLCD("Homing red dispensor");
IonSystems 14:31ba3e56c788 197 maintain(R_HOME);
IonSystems 13:0661d658d9d1 198 break;
IonSystems 13:0661d658d9d1 199
IonSystems 13:0661d658d9d1 200 case 10://GB_LEFT:
IonSystems 14:31ba3e56c788 201 printLCD("Pushing green dispensor");
IonSystems 13:0661d658d9d1 202 maintain(GB_LEFT);
IonSystems 13:0661d658d9d1 203 break;
IonSystems 13:0661d658d9d1 204
IonSystems 13:0661d658d9d1 205 case 11://GB_CENTRE:
IonSystems 14:31ba3e56c788 206 printLCD("Centre green/blue dispensor");
IonSystems 13:0661d658d9d1 207 maintain(GB_CENTRE);
IonSystems 13:0661d658d9d1 208 break;
IonSystems 13:0661d658d9d1 209
IonSystems 13:0661d658d9d1 210 case 12://GB_RIGHT:
IonSystems 14:31ba3e56c788 211 printLCD("Pushing blue dispensor");
IonSystems 14:31ba3e56c788 212 maintain(GB_RIGHT );
IonSystems 13:0661d658d9d1 213 break;
IonSystems 13:0661d658d9d1 214
IonSystems 13:0661d658d9d1 215
IonSystems 13:0661d658d9d1 216
IonSystems 14:31ba3e56c788 217 } printLCD("Ready for next command");
IonSystems 14:31ba3e56c788 218 wait(1);
IonSystems 13:0661d658d9d1 219 }
IonSystems 12:814a8fdbb6f7 220 }
IonSystems 9:8d78eb55ad5e 221 }
IonSystems 12:814a8fdbb6f7 222
IonSystems 15:0c5f20e15b6a 223 void changeLanguage(){
IonSystems 15:0c5f20e15b6a 224 currentLanguage = nextLanguage(currentLanguage);
IonSystems 15:0c5f20e15b6a 225 //tell PC to change language
IonSystems 15:0c5f20e15b6a 226 sendLanguageCharacter(); //Tell PC to change language
IonSystems 15:0c5f20e15b6a 227 updateLCDLanguage();
IonSystems 15:0c5f20e15b6a 228 printLCD("setting language to english");
IonSystems 15:0c5f20e15b6a 229 wait(0.5);
IonSystems 15:0c5f20e15b6a 230 }
IonSystems 6:e64796f1f384 231
IonSystems 6:e64796f1f384 232 int main() {
IonSystems 12:814a8fdbb6f7 233
IonSystems 7:6ba00694f9cd 234 stateMachine = INITIALISING;
IonSystems 8:b7771391adc9 235 processState(stateMachine);
IonSystems 12:814a8fdbb6f7 236 readChipNumbers();
IonSystems 12:814a8fdbb6f7 237 printStoredChipValues();
IonSystems 14:31ba3e56c788 238 //displayOperationMode();
IonSystems 8:b7771391adc9 239 while(true){
IonSystems 10:8c0696b99692 240 if(serialComms){
IonSystems 10:8c0696b99692 241 checkSerial();
IonSystems 12:814a8fdbb6f7 242 }if(testFunctions) checkSortDispenseButtons();
IonSystems 15:0c5f20e15b6a 243 if(languageButton) changeLanguage();
IonSystems 15:0c5f20e15b6a 244
IonSystems 15:0c5f20e15b6a 245
IonSystems 15:0c5f20e15b6a 246
IonSystems 15:0c5f20e15b6a 247
IonSystems 11:06f6e82b40a8 248 wait(0.05);
IonSystems 12:814a8fdbb6f7 249
IonSystems 8:b7771391adc9 250 }
IonSystems 10:8c0696b99692 251 /*char c;
IonSystems 10:8c0696b99692 252 if(c = readCharacter()){
IonSystems 10:8c0696b99692 253 processMessage(c);
IonSystems 10:8c0696b99692 254 }*/
IonSystems 11:06f6e82b40a8 255
IonSystems 11:06f6e82b40a8 256
IonSystems 10:8c0696b99692 257 }
IonSystems 9:8d78eb55ad5e 258
IonSystems 9:8d78eb55ad5e 259
IonSystems 9:8d78eb55ad5e 260
IonSystems 8:b7771391adc9 261
IonSystems 8:b7771391adc9 262
IonSystems 8:b7771391adc9 263
IonSystems 8:b7771391adc9 264
IonSystems 8:b7771391adc9 265
IonSystems 8:b7771391adc9 266
IonSystems 8:b7771391adc9 267 //printStoredChipValues();
IonSystems 6:e64796f1f384 268 /*
IonSystems 0:8d54ffcf256e 269 while(1){
IonSystems 3:97668a4cd69d 270 if(par_port->read_bit(11)) resetForNextCustomer();
IonSystems 7:6ba00694f9cd 271
IonSystems 3:97668a4cd69d 272 if(cardDetect & !cardDataAcquired) cardAcquisition();
IonSystems 3:97668a4cd69d 273 setLEDs();
IonSystems 3:97668a4cd69d 274 readButtons();
IonSystems 3:97668a4cd69d 275
IonSystems 4:f3be545b3826 276 if(chipDetected & !colourDataAcquired){
IonSystems 4:f3be545b3826 277 Colour colour = readColourSensor();
IonSystems 4:f3be545b3826 278 sendColourSignal(colour);
IonSystems 4:f3be545b3826 279 }
IonSystems 4:f3be545b3826 280
IonSystems 5:644bca33c1ca 281 writeFile(redQueue, greenQueue, blueQueue);
IonSystems 4:f3be545b3826 282 readFile();
IonSystems 4:f3be545b3826 283 wait(2);
IonSystems 4:f3be545b3826 284
IonSystems 0:8d54ffcf256e 285 }
IonSystems 6:e64796f1f384 286 */
IonSystems 6:e64796f1f384 287
IonSystems 8:b7771391adc9 288 /* setLEDcolour(255,0,0);
IonSystems 6:e64796f1f384 289 wait(0.1);
IonSystems 6:e64796f1f384 290 setLEDcolour(0,255,0);
IonSystems 6:e64796f1f384 291 wait(0.1);
IonSystems 6:e64796f1f384 292 setLEDcolour(0,0,255);
IonSystems 6:e64796f1f384 293 wait(0.1);
IonSystems 6:e64796f1f384 294
IonSystems 8:b7771391adc9 295 displayOperationMode(); */
IonSystems 3:97668a4cd69d 296
IonSystems 10:8c0696b99692 297
IonSystems 10:8c0696b99692 298