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:
Wed Oct 29 11:11:02 2014 +0000
Revision:
2:168850019d5a
Parent:
1:a8a01df48d1a
Child:
3:97668a4cd69d
More commenting and cases added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IonSystems 0:8d54ffcf256e 1 #include "mbed.h"
IonSystems 0:8d54ffcf256e 2 #include "MCP23017.h"
IonSystems 0:8d54ffcf256e 3 #include "WattBob_TextLCD.h"
IonSystems 0:8d54ffcf256e 4 #include "TCS3472_I2C.h"
IonSystems 0:8d54ffcf256e 5
IonSystems 0:8d54ffcf256e 6 //This LED is on when the servo is rotating clockwise.
IonSystems 0:8d54ffcf256e 7 DigitalOut clockwiseLED(LED1);
IonSystems 0:8d54ffcf256e 8 //This LED is on when the servo is rotating anti-clockwise.
IonSystems 0:8d54ffcf256e 9 DigitalOut anticlockwiseLED(LED2);
IonSystems 0:8d54ffcf256e 10 //This is the control line that tells the FPGA to move the servo clockwise.
IonSystems 0:8d54ffcf256e 11 DigitalOut clockwise(p22);
IonSystems 0:8d54ffcf256e 12 //This is the control line that tells the FPGA to move the servo anti-clockwise.
IonSystems 0:8d54ffcf256e 13 DigitalOut anticlockwise(p21);
IonSystems 0:8d54ffcf256e 14
IonSystems 0:8d54ffcf256e 15 //Boolean values to easily enable and disable certain features for individual testing
IonSystems 0:8d54ffcf256e 16 bool colourSensor = true;
IonSystems 0:8d54ffcf256e 17 bool cardReader = true;
IonSystems 0:8d54ffcf256e 18 bool sorter = true;
IonSystems 0:8d54ffcf256e 19 bool dispensor = true;
IonSystems 0:8d54ffcf256e 20
IonSystems 0:8d54ffcf256e 21 /*
IonSystems 0:8d54ffcf256e 22 There are two high level states representing the two main
IonSystems 0:8d54ffcf256e 23 functionalities of the MBED: Maintenance mode and Operation mode.
IonSystems 0:8d54ffcf256e 24
IonSystems 0:8d54ffcf256e 25 When operationMode is true, the MBED is in operation mode.
IonSystems 0:8d54ffcf256e 26 When operationMode is false, the MBED is in maintenance mode.
IonSystems 0:8d54ffcf256e 27 */
IonSystems 0:8d54ffcf256e 28
IonSystems 0:8d54ffcf256e 29 bool operationMode = true; //the MBED starts in operation mode.
IonSystems 0:8d54ffcf256e 30
IonSystems 0:8d54ffcf256e 31 #define BACK_LIGHT_ON(INTERFACE) INTERFACE->write_bit(1,BL_BIT)
IonSystems 0:8d54ffcf256e 32 #define BACK_LIGHT_OFF(INTERFACE) INTERFACE->write_bit(0,BL_BIT)
IonSystems 0:8d54ffcf256e 33
IonSystems 0:8d54ffcf256e 34 //Creating all the global variables.
IonSystems 0:8d54ffcf256e 35 MCP23017 *par_port;
IonSystems 0:8d54ffcf256e 36 WattBob_TextLCD *lcd;
IonSystems 0:8d54ffcf256e 37
IonSystems 0:8d54ffcf256e 38 Serial pc(USBTX, USBRX);
IonSystems 0:8d54ffcf256e 39
IonSystems 0:8d54ffcf256e 40 //Setup colour sensor variables
IonSystems 0:8d54ffcf256e 41 int rgb_readings[4];
IonSystems 0:8d54ffcf256e 42 double rMax = 9244;
IonSystems 0:8d54ffcf256e 43 double gMax = 3194;
IonSystems 0:8d54ffcf256e 44 double bMax = 3590;
IonSystems 0:8d54ffcf256e 45 TCS3472_I2C rgb_sensor(p28, p27);
IonSystems 0:8d54ffcf256e 46
IonSystems 0:8d54ffcf256e 47 //setup Card Reader stuff
IonSystems 0:8d54ffcf256e 48 DigitalIn cardBit1(p21);
IonSystems 0:8d54ffcf256e 49 DigitalIn cardBit2(p22);
IonSystems 0:8d54ffcf256e 50 DigitalIn cardBit3(p23);
IonSystems 0:8d54ffcf256e 51 DigitalIn cardBit4(p24);
IonSystems 0:8d54ffcf256e 52 DigitalIn cardBit5(p25);
IonSystems 0:8d54ffcf256e 53 DigitalIn cardBit6(p26);
IonSystems 0:8d54ffcf256e 54
IonSystems 0:8d54ffcf256e 55 //Setup pins to FPGA
IonSystems 0:8d54ffcf256e 56 DigitalOut startSort(p5); //A positive edge tells the FPGA to start sorting.
IonSystems 0:8d54ffcf256e 57 /* The command codes for the three sort select bits(ColourBit1 - ColourBit2)
IonSystems 0:8d54ffcf256e 58 000 Sort red chip
IonSystems 0:8d54ffcf256e 59 001 Sort green chip
IonSystems 0:8d54ffcf256e 60 010 Sort blue chip
IonSystems 0:8d54ffcf256e 61 011 Bin the chip
IonSystems 0:8d54ffcf256e 62 100 Recycle the chip
IonSystems 0:8d54ffcf256e 63 101 Nothing
IonSystems 0:8d54ffcf256e 64 110 Nothing
IonSystems 0:8d54ffcf256e 65 111 Nothing
IonSystems 0:8d54ffcf256e 66 */
IonSystems 0:8d54ffcf256e 67 DigitalOut ColourBit1(p6); //The 3 bits below are select bits for the sorter.
IonSystems 0:8d54ffcf256e 68 DigitalOut ColourBit2(p7);
IonSystems 0:8d54ffcf256e 69 DigitalOut ColourBit3(p8);
IonSystems 0:8d54ffcf256e 70 DigitalOut startDispense(p9); //A positive edge tells the FPGA to start dispensing.
IonSystems 0:8d54ffcf256e 71 /*
IonSystems 0:8d54ffcf256e 72 00 Dispense red chip
IonSystems 0:8d54ffcf256e 73 01 Dispense green chip
IonSystems 0:8d54ffcf256e 74 10 Dispense blue chip
IonSystems 0:8d54ffcf256e 75 11 Nothing
IonSystems 0:8d54ffcf256e 76 */
IonSystems 0:8d54ffcf256e 77 DigitalOut DispenseBit1(p10); //The 2 bits below are select bits for the dispenser.
IonSystems 0:8d54ffcf256e 78 DigitalOut DispenseBit2(p11);
IonSystems 0:8d54ffcf256e 79
IonSystems 0:8d54ffcf256e 80
IonSystems 0:8d54ffcf256e 81 //Functions go here
IonSystems 0:8d54ffcf256e 82 void sendCharacter(char ch){
IonSystems 0:8d54ffcf256e 83 pc.putc(ch);
IonSystems 0:8d54ffcf256e 84 }
IonSystems 0:8d54ffcf256e 85
IonSystems 0:8d54ffcf256e 86 void printLCD(char * text){
IonSystems 0:8d54ffcf256e 87 lcd->reset();
IonSystems 0:8d54ffcf256e 88 lcd->printf(text);
IonSystems 0:8d54ffcf256e 89 }
IonSystems 1:a8a01df48d1a 90 /* valueToChar(int value):
IonSystems 1:a8a01df48d1a 91 Expects an int value what it 0 or 1.
IonSystems 1:a8a01df48d1a 92 Returns the int value as a char.
IonSystems 1:a8a01df48d1a 93 0 will return '0'
IonSystems 1:a8a01df48d1a 94 1 will return '1'
IonSystems 1:a8a01df48d1a 95 Used to read the bits in the card reader.
IonSystems 1:a8a01df48d1a 96 */
IonSystems 0:8d54ffcf256e 97 char valueToChar(int value){
IonSystems 1:a8a01df48d1a 98 if(value == 0){
IonSystems 0:8d54ffcf256e 99 return '0';
IonSystems 1:a8a01df48d1a 100 }else if(value == 1){
IonSystems 1:a8a01df48d1a 101 return '1';
IonSystems 1:a8a01df48d1a 102 }else{
IonSystems 1:a8a01df48d1a 103 return NULL;
IonSystems 1:a8a01df48d1a 104 }
IonSystems 0:8d54ffcf256e 105 }
IonSystems 0:8d54ffcf256e 106 /* readBit(int bitNumber):
IonSystems 0:8d54ffcf256e 107 Takes a bit number between 1 and 6 inclusive.
IonSystems 0:8d54ffcf256e 108 Returns the value of the cardBit associated with the bit number as a char.
IonSystems 0:8d54ffcf256e 109 */
IonSystems 0:8d54ffcf256e 110 char readBit(int bitNumber){
IonSystems 0:8d54ffcf256e 111 int value = 0;
IonSystems 0:8d54ffcf256e 112 switch(bitNumber){
IonSystems 0:8d54ffcf256e 113 case 1:
IonSystems 0:8d54ffcf256e 114 value = cardBit1;
IonSystems 0:8d54ffcf256e 115 break;
IonSystems 0:8d54ffcf256e 116 case 2:
IonSystems 0:8d54ffcf256e 117 value = cardBit2;
IonSystems 0:8d54ffcf256e 118 break;
IonSystems 0:8d54ffcf256e 119 case 3:
IonSystems 0:8d54ffcf256e 120 value = cardBit3;
IonSystems 0:8d54ffcf256e 121 break;
IonSystems 0:8d54ffcf256e 122 case 4:
IonSystems 0:8d54ffcf256e 123 value = cardBit4;
IonSystems 0:8d54ffcf256e 124 break;
IonSystems 0:8d54ffcf256e 125 case 5:
IonSystems 0:8d54ffcf256e 126 value = cardBit5;
IonSystems 0:8d54ffcf256e 127 break;
IonSystems 0:8d54ffcf256e 128 case 6:
IonSystems 0:8d54ffcf256e 129 value = cardBit6;
IonSystems 0:8d54ffcf256e 130 break;
IonSystems 0:8d54ffcf256e 131 }
IonSystems 0:8d54ffcf256e 132 return valueToChar(value);
IonSystems 0:8d54ffcf256e 133 }
IonSystems 1:a8a01df48d1a 134 /* processMessage(char c):
IonSystems 1:a8a01df48d1a 135 Runs when the MBED recieves a character from the PC.
IonSystems 1:a8a01df48d1a 136 Carries out tasks based on the char value.
IonSystems 1:a8a01df48d1a 137 An acknowledge signal is always send back to the PC so that it knows the MBED
IonSystems 1:a8a01df48d1a 138 has recieved the command.
IonSystems 1:a8a01df48d1a 139 Has two case statements: one for maintenance mode and one for operation mode.
IonSystems 0:8d54ffcf256e 140 */
IonSystems 0:8d54ffcf256e 141
IonSystems 0:8d54ffcf256e 142 void processMessage(char c){
IonSystems 0:8d54ffcf256e 143 if(!operation){ switch(c){
IonSystems 0:8d54ffcf256e 144 case 'a' : //servo 1 clockwise
IonSystems 0:8d54ffcf256e 145 sendCharacter('a'); //bounce back for acknowledgement
IonSystems 0:8d54ffcf256e 146 printLCD("S1 clockwise");
IonSystems 0:8d54ffcf256e 147 break;
IonSystems 0:8d54ffcf256e 148 case 'b' : //servo 1 anticlockwise
IonSystems 0:8d54ffcf256e 149 sendCharacter('b'); //bounce back for acknowledgement
IonSystems 0:8d54ffcf256e 150 printLCD("S1 anticlockwise");
IonSystems 0:8d54ffcf256e 151 break;
IonSystems 0:8d54ffcf256e 152 case 'c':
IonSystems 0:8d54ffcf256e 153 sendCharacter('c'); //bounce back for acknowledgement
IonSystems 0:8d54ffcf256e 154 printLCD("S1 stop");
IonSystems 0:8d54ffcf256e 155 break;
IonSystems 0:8d54ffcf256e 156 case 'C':// Read card command
IonSystems 0:8d54ffcf256e 157 sendCharacter('R'); //Notify PC that command has been recieved
IonSystems 0:8d54ffcf256e 158 printLCD("Reading Card");
IonSystems 0:8d54ffcf256e 159 //read card here
IonSystems 0:8d54ffcf256e 160 char value = ' ';
IonSystems 0:8d54ffcf256e 161 for(int num = 1;num <= 6;num++){
IonSystems 0:8d54ffcf256e 162 value = readBit(num);
IonSystems 0:8d54ffcf256e 163 // sendCharacter('R');
IonSystems 0:8d54ffcf256e 164 sendCharacter(value);
IonSystems 0:8d54ffcf256e 165 printLCD("Bit read");
IonSystems 0:8d54ffcf256e 166 //wait(0.1);
IonSystems 0:8d54ffcf256e 167 }
IonSystems 0:8d54ffcf256e 168 sendCharacter('C'); //Tells PC that the card has been read
IonSystems 0:8d54ffcf256e 169 printLCD("Card read success");
IonSystems 0:8d54ffcf256e 170 break;
IonSystems 0:8d54ffcf256e 171 case 'X':
IonSystems 0:8d54ffcf256e 172 sendCharacter('X'); //Notify the PC that this is an MBED
IonSystems 0:8d54ffcf256e 173 printLCD("Connected to PC");
IonSystems 0:8d54ffcf256e 174 break;
IonSystems 2:168850019d5a 175 case 'd': //Dispense a chip
IonSystems 2:168850019d5a 176 sendCharacter("D");
IonSystems 2:168850019d5a 177 printLCD("Dispensing chip");
IonSystems 2:168850019d5a 178
IonSystems 2:168850019d5a 179 //read from color sensor
IonSystems 2:168850019d5a 180 //convert readings to colour
IonSystems 2:168850019d5a 181 //convert colour to 3-bit colour code
IonSystems 2:168850019d5a 182 //set the FPGA colour inputs to the colour code
IonSystems 2:168850019d5a 183 sortStart = true;
IonSystems 2:168850019d5a 184 break;
IonSystems 0:8d54ffcf256e 185 }
IonSystems 0:8d54ffcf256e 186 }
IonSystems 0:8d54ffcf256e 187 else if (operation){
IonSystems 0:8d54ffcf256e 188 switch(c){
IonSystems 1:a8a01df48d1a 189 case 'n':// Start sorting
IonSystems 1:a8a01df48d1a 190 sendCharacter('N'); //Confirm to PC that we are processing the instruction.
IonSystems 1:a8a01df48d1a 191 startSort = true; //Pulse the startSort control line to FPGA
IonSystems 1:a8a01df48d1a 192 wait(0.1);
IonSystems 1:a8a01df48d1a 193 startSort = false;
IonSystems 1:a8a01df48d1a 194 break;
IonSystems 1:a8a01df48d1a 195 case 'o': //Start dispensing
IonSystems 1:a8a01df48d1a 196 sendCharacter('O');
IonSystems 1:a8a01df48d1a 197 startDispense = true; //Pulse the startDispense control line to FPGA
IonSystems 1:a8a01df48d1a 198 wait(0.1);
IonSystems 1:a8a01df48d1a 199 startDispense = false;
IonSystems 0:8d54ffcf256e 200 break;
IonSystems 2:168850019d5a 201 case 'p': //
IonSystems 2:168850019d5a 202 sendCharacter('P');
IonSystems 2:168850019d5a 203 break;
IonSystems 0:8d54ffcf256e 204 }
IonSystems 0:8d54ffcf256e 205 }
IonSystems 0:8d54ffcf256e 206 }
IonSystems 0:8d54ffcf256e 207
IonSystems 0:8d54ffcf256e 208 void colourSensorTick(){
IonSystems 0:8d54ffcf256e 209 rgb_sensor.getAllColors(rgb_readings);
IonSystems 0:8d54ffcf256e 210
IonSystems 0:8d54ffcf256e 211 double redd = (rgb_readings[1] /gMax) * 255;
IonSystems 0:8d54ffcf256e 212 double greend = (rgb_readings[2] /bMax) * 255;
IonSystems 0:8d54ffcf256e 213 double blued = (rgb_readings[0] /rMax) * 255;
IonSystems 0:8d54ffcf256e 214
IonSystems 0:8d54ffcf256e 215 int red = redd;
IonSystems 0:8d54ffcf256e 216 int green = greend;
IonSystems 0:8d54ffcf256e 217 int blue = blued;
IonSystems 0:8d54ffcf256e 218
IonSystems 0:8d54ffcf256e 219 lcd->cls(); // clear display
IonSystems 0:8d54ffcf256e 220 lcd->locate(0,0);
IonSystems 0:8d54ffcf256e 221 lcd->printf("R:%d G:%d B:%d",red,green,blue);
IonSystems 0:8d54ffcf256e 222
IonSystems 0:8d54ffcf256e 223 lcd->locate(1,0);
IonSystems 0:8d54ffcf256e 224 if(red > 55){
IonSystems 0:8d54ffcf256e 225 lcd->printf("RED");
IonSystems 0:8d54ffcf256e 226 }
IonSystems 0:8d54ffcf256e 227 if(green > 55){
IonSystems 0:8d54ffcf256e 228 lcd->printf("GREEN");
IonSystems 0:8d54ffcf256e 229 }
IonSystems 0:8d54ffcf256e 230 if(red < 30 && green > 30 && blue > 30){
IonSystems 0:8d54ffcf256e 231 lcd->printf("BLUE");
IonSystems 0:8d54ffcf256e 232 }
IonSystems 0:8d54ffcf256e 233 }
IonSystems 0:8d54ffcf256e 234
IonSystems 0:8d54ffcf256e 235 int main() {
IonSystems 0:8d54ffcf256e 236 //Setting up all the global variables
IonSystems 0:8d54ffcf256e 237 par_port = new MCP23017(p9, p10, 0x40);
IonSystems 0:8d54ffcf256e 238 par_port->config(0x0F00, 0x0F00, 0x0F00); // configure MCP23017 chip on WattBob
IonSystems 0:8d54ffcf256e 239 BACK_LIGHT_ON(par_port);
IonSystems 0:8d54ffcf256e 240 lcd = new WattBob_TextLCD(par_port);
IonSystems 0:8d54ffcf256e 241 rgb_sensor.enablePowerAndRGBC();
IonSystems 0:8d54ffcf256e 242 rgb_sensor.setIntegrationTime(100);
IonSystems 0:8d54ffcf256e 243 //Writing initial text on LCD
IonSystems 0:8d54ffcf256e 244 lcd->printf("Welcome to the");
IonSystems 0:8d54ffcf256e 245 lcd->locate(1,0); //Going to new line
IonSystems 0:8d54ffcf256e 246 lcd->printf("Chipin Sorter");
IonSystems 0:8d54ffcf256e 247 wait(1); //Wait 1 second
IonSystems 2:168850019d5a 248 //lcd->reset(); //Clear LCD
IonSystems 0:8d54ffcf256e 249
IonSystems 0:8d54ffcf256e 250 while(1){
IonSystems 2:168850019d5a 251 char c = pc.getc(); //wait for a serial character to be recieved.
IonSystems 2:168850019d5a 252 processMessage(c); //Do something, based on the character recieved.
IonSystems 0:8d54ffcf256e 253 }
IonSystems 0:8d54ffcf256e 254
IonSystems 0:8d54ffcf256e 255
IonSystems 0:8d54ffcf256e 256 /*
IonSystems 0:8d54ffcf256e 257 if(par_port->read_bit(8)){
IonSystems 0:8d54ffcf256e 258 lcd->printf("ON");
IonSystems 0:8d54ffcf256e 259 pc.printf("btn1");
IonSystems 0:8d54ffcf256e 260 wait(0.5);
IonSystems 0:8d54ffcf256e 261 lcd->reset();
IonSystems 0:8d54ffcf256e 262 }else{
IonSystems 0:8d54ffcf256e 263 lcd->printf("OFF");
IonSystems 0:8d54ffcf256e 264 pc.printf("btn0");
IonSystems 0:8d54ffcf256e 265 wait(0.5);
IonSystems 0:8d54ffcf256e 266 lcd->reset();
IonSystems 0:8d54ffcf256e 267 }
IonSystems 0:8d54ffcf256e 268
IonSystems 0:8d54ffcf256e 269 */
IonSystems 0:8d54ffcf256e 270 }