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:
Tue Nov 25 22:32:26 2014 +0000
Revision:
22:8f11d1c178ab
Parent:
19:78d4b78fa736
Child:
23:f9e7e64784be
almost working sort, working dispense

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