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 12:06:26 2014 +0000
Revision:
19:78d4b78fa736
Parent:
18:12e2c82a5a6c
Child:
22:8f11d1c178ab
Added lift operation

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