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 09:39:09 2014 +0000
Revision:
1:a8a01df48d1a
Parent:
0:8d54ffcf256e
Child:
2:168850019d5a
Added more comments and another case for startDispense control line pulse.

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 0:8d54ffcf256e 175 }
IonSystems 0:8d54ffcf256e 176 }
IonSystems 0:8d54ffcf256e 177 else if (operation){
IonSystems 0:8d54ffcf256e 178 switch(c){
IonSystems 1:a8a01df48d1a 179 case 'n':// Start sorting
IonSystems 1:a8a01df48d1a 180 sendCharacter('N'); //Confirm to PC that we are processing the instruction.
IonSystems 1:a8a01df48d1a 181 startSort = true; //Pulse the startSort control line to FPGA
IonSystems 1:a8a01df48d1a 182 wait(0.1);
IonSystems 1:a8a01df48d1a 183 startSort = false;
IonSystems 1:a8a01df48d1a 184 break;
IonSystems 1:a8a01df48d1a 185 case 'o': //Start dispensing
IonSystems 1:a8a01df48d1a 186 sendCharacter('O');
IonSystems 1:a8a01df48d1a 187 startDispense = true; //Pulse the startDispense control line to FPGA
IonSystems 1:a8a01df48d1a 188 wait(0.1);
IonSystems 1:a8a01df48d1a 189 startDispense = false;
IonSystems 0:8d54ffcf256e 190 break;
IonSystems 0:8d54ffcf256e 191 }
IonSystems 0:8d54ffcf256e 192 }
IonSystems 0:8d54ffcf256e 193 }
IonSystems 0:8d54ffcf256e 194
IonSystems 0:8d54ffcf256e 195 void colourSensorTick(){
IonSystems 0:8d54ffcf256e 196 rgb_sensor.getAllColors(rgb_readings);
IonSystems 0:8d54ffcf256e 197
IonSystems 0:8d54ffcf256e 198 double redd = (rgb_readings[1] /gMax) * 255;
IonSystems 0:8d54ffcf256e 199 double greend = (rgb_readings[2] /bMax) * 255;
IonSystems 0:8d54ffcf256e 200 double blued = (rgb_readings[0] /rMax) * 255;
IonSystems 0:8d54ffcf256e 201
IonSystems 0:8d54ffcf256e 202 int red = redd;
IonSystems 0:8d54ffcf256e 203 int green = greend;
IonSystems 0:8d54ffcf256e 204 int blue = blued;
IonSystems 0:8d54ffcf256e 205
IonSystems 0:8d54ffcf256e 206 lcd->cls(); // clear display
IonSystems 0:8d54ffcf256e 207 lcd->locate(0,0);
IonSystems 0:8d54ffcf256e 208 lcd->printf("R:%d G:%d B:%d",red,green,blue);
IonSystems 0:8d54ffcf256e 209
IonSystems 0:8d54ffcf256e 210 lcd->locate(1,0);
IonSystems 0:8d54ffcf256e 211 if(red > 55){
IonSystems 0:8d54ffcf256e 212 lcd->printf("RED");
IonSystems 0:8d54ffcf256e 213 }
IonSystems 0:8d54ffcf256e 214 if(green > 55){
IonSystems 0:8d54ffcf256e 215 lcd->printf("GREEN");
IonSystems 0:8d54ffcf256e 216 }
IonSystems 0:8d54ffcf256e 217 if(red < 30 && green > 30 && blue > 30){
IonSystems 0:8d54ffcf256e 218 lcd->printf("BLUE");
IonSystems 0:8d54ffcf256e 219 }
IonSystems 0:8d54ffcf256e 220 }
IonSystems 0:8d54ffcf256e 221
IonSystems 0:8d54ffcf256e 222 int main() {
IonSystems 0:8d54ffcf256e 223 //Setting up all the global variables
IonSystems 0:8d54ffcf256e 224 par_port = new MCP23017(p9, p10, 0x40);
IonSystems 0:8d54ffcf256e 225 par_port->config(0x0F00, 0x0F00, 0x0F00); // configure MCP23017 chip on WattBob
IonSystems 0:8d54ffcf256e 226 BACK_LIGHT_ON(par_port);
IonSystems 0:8d54ffcf256e 227 lcd = new WattBob_TextLCD(par_port);
IonSystems 0:8d54ffcf256e 228 rgb_sensor.enablePowerAndRGBC();
IonSystems 0:8d54ffcf256e 229 rgb_sensor.setIntegrationTime(100);
IonSystems 0:8d54ffcf256e 230 //Writing initial text on LCD
IonSystems 0:8d54ffcf256e 231 lcd->printf("Welcome to the");
IonSystems 0:8d54ffcf256e 232 lcd->locate(1,0); //Going to new line
IonSystems 0:8d54ffcf256e 233 lcd->printf("Chipin Sorter");
IonSystems 0:8d54ffcf256e 234 wait(1); //Wait 1 second
IonSystems 0:8d54ffcf256e 235 lcd->reset(); //Clear LCD
IonSystems 0:8d54ffcf256e 236
IonSystems 0:8d54ffcf256e 237 while(1){
IonSystems 0:8d54ffcf256e 238 char c = pc.getc();
IonSystems 0:8d54ffcf256e 239 processMessage(c);
IonSystems 0:8d54ffcf256e 240 }
IonSystems 0:8d54ffcf256e 241
IonSystems 0:8d54ffcf256e 242
IonSystems 0:8d54ffcf256e 243 /*
IonSystems 0:8d54ffcf256e 244 if(par_port->read_bit(8)){
IonSystems 0:8d54ffcf256e 245 lcd->printf("ON");
IonSystems 0:8d54ffcf256e 246 pc.printf("btn1");
IonSystems 0:8d54ffcf256e 247 wait(0.5);
IonSystems 0:8d54ffcf256e 248 lcd->reset();
IonSystems 0:8d54ffcf256e 249 }else{
IonSystems 0:8d54ffcf256e 250 lcd->printf("OFF");
IonSystems 0:8d54ffcf256e 251 pc.printf("btn0");
IonSystems 0:8d54ffcf256e 252 wait(0.5);
IonSystems 0:8d54ffcf256e 253 lcd->reset();
IonSystems 0:8d54ffcf256e 254 }
IonSystems 0:8d54ffcf256e 255
IonSystems 0:8d54ffcf256e 256 */
IonSystems 0:8d54ffcf256e 257 }