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:
Thu Nov 20 22:02:54 2014 +0000
Revision:
14:31ba3e56c788
Parent:
13:0661d658d9d1
Child:
15:0c5f20e15b6a
Euan broke dispense mode

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