![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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
main.cpp
- Committer:
- IonSystems
- Date:
- 2014-10-29
- Revision:
- 2:168850019d5a
- Parent:
- 1:a8a01df48d1a
- Child:
- 3:97668a4cd69d
File content as of revision 2:168850019d5a:
#include "mbed.h" #include "MCP23017.h" #include "WattBob_TextLCD.h" #include "TCS3472_I2C.h" //This LED is on when the servo is rotating clockwise. DigitalOut clockwiseLED(LED1); //This LED is on when the servo is rotating anti-clockwise. DigitalOut anticlockwiseLED(LED2); //This is the control line that tells the FPGA to move the servo clockwise. DigitalOut clockwise(p22); //This is the control line that tells the FPGA to move the servo anti-clockwise. DigitalOut anticlockwise(p21); //Boolean values to easily enable and disable certain features for individual testing bool colourSensor = true; bool cardReader = true; bool sorter = true; bool dispensor = true; /* There are two high level states representing the two main functionalities of the MBED: Maintenance mode and Operation mode. When operationMode is true, the MBED is in operation mode. When operationMode is false, the MBED is in maintenance mode. */ bool operationMode = true; //the MBED starts in operation mode. #define BACK_LIGHT_ON(INTERFACE) INTERFACE->write_bit(1,BL_BIT) #define BACK_LIGHT_OFF(INTERFACE) INTERFACE->write_bit(0,BL_BIT) //Creating all the global variables. MCP23017 *par_port; WattBob_TextLCD *lcd; Serial pc(USBTX, USBRX); //Setup colour sensor variables int rgb_readings[4]; double rMax = 9244; double gMax = 3194; double bMax = 3590; TCS3472_I2C rgb_sensor(p28, p27); //setup Card Reader stuff DigitalIn cardBit1(p21); DigitalIn cardBit2(p22); DigitalIn cardBit3(p23); DigitalIn cardBit4(p24); DigitalIn cardBit5(p25); DigitalIn cardBit6(p26); //Setup pins to FPGA DigitalOut startSort(p5); //A positive edge tells the FPGA to start sorting. /* The command codes for the three sort select bits(ColourBit1 - ColourBit2) 000 Sort red chip 001 Sort green chip 010 Sort blue chip 011 Bin the chip 100 Recycle the chip 101 Nothing 110 Nothing 111 Nothing */ DigitalOut ColourBit1(p6); //The 3 bits below are select bits for the sorter. DigitalOut ColourBit2(p7); DigitalOut ColourBit3(p8); DigitalOut startDispense(p9); //A positive edge tells the FPGA to start dispensing. /* 00 Dispense red chip 01 Dispense green chip 10 Dispense blue chip 11 Nothing */ DigitalOut DispenseBit1(p10); //The 2 bits below are select bits for the dispenser. DigitalOut DispenseBit2(p11); //Functions go here void sendCharacter(char ch){ pc.putc(ch); } void printLCD(char * text){ lcd->reset(); lcd->printf(text); } /* valueToChar(int value): Expects an int value what it 0 or 1. Returns the int value as a char. 0 will return '0' 1 will return '1' Used to read the bits in the card reader. */ char valueToChar(int value){ if(value == 0){ return '0'; }else if(value == 1){ return '1'; }else{ return NULL; } } /* readBit(int bitNumber): Takes a bit number between 1 and 6 inclusive. Returns the value of the cardBit associated with the bit number as a char. */ char readBit(int bitNumber){ int value = 0; switch(bitNumber){ case 1: value = cardBit1; break; case 2: value = cardBit2; break; case 3: value = cardBit3; break; case 4: value = cardBit4; break; case 5: value = cardBit5; break; case 6: value = cardBit6; break; } return valueToChar(value); } /* processMessage(char c): Runs when the MBED recieves a character from the PC. Carries out tasks based on the char value. An acknowledge signal is always send back to the PC so that it knows the MBED has recieved the command. Has two case statements: one for maintenance mode and one for operation mode. */ void processMessage(char c){ if(!operation){ switch(c){ case 'a' : //servo 1 clockwise sendCharacter('a'); //bounce back for acknowledgement printLCD("S1 clockwise"); break; case 'b' : //servo 1 anticlockwise sendCharacter('b'); //bounce back for acknowledgement printLCD("S1 anticlockwise"); break; case 'c': sendCharacter('c'); //bounce back for acknowledgement printLCD("S1 stop"); break; case 'C':// Read card command sendCharacter('R'); //Notify PC that command has been recieved printLCD("Reading Card"); //read card here char value = ' '; for(int num = 1;num <= 6;num++){ value = readBit(num); // sendCharacter('R'); sendCharacter(value); printLCD("Bit read"); //wait(0.1); } sendCharacter('C'); //Tells PC that the card has been read printLCD("Card read success"); break; case 'X': sendCharacter('X'); //Notify the PC that this is an MBED printLCD("Connected to PC"); break; case 'd': //Dispense a chip sendCharacter("D"); printLCD("Dispensing chip"); //read from color sensor //convert readings to colour //convert colour to 3-bit colour code //set the FPGA colour inputs to the colour code sortStart = true; break; } } else if (operation){ switch(c){ case 'n':// Start sorting sendCharacter('N'); //Confirm to PC that we are processing the instruction. startSort = true; //Pulse the startSort control line to FPGA wait(0.1); startSort = false; break; case 'o': //Start dispensing sendCharacter('O'); startDispense = true; //Pulse the startDispense control line to FPGA wait(0.1); startDispense = false; break; case 'p': // sendCharacter('P'); break; } } } void colourSensorTick(){ rgb_sensor.getAllColors(rgb_readings); double redd = (rgb_readings[1] /gMax) * 255; double greend = (rgb_readings[2] /bMax) * 255; double blued = (rgb_readings[0] /rMax) * 255; int red = redd; int green = greend; int blue = blued; lcd->cls(); // clear display lcd->locate(0,0); lcd->printf("R:%d G:%d B:%d",red,green,blue); lcd->locate(1,0); if(red > 55){ lcd->printf("RED"); } if(green > 55){ lcd->printf("GREEN"); } if(red < 30 && green > 30 && blue > 30){ lcd->printf("BLUE"); } } int main() { //Setting up all the global variables par_port = new MCP23017(p9, p10, 0x40); par_port->config(0x0F00, 0x0F00, 0x0F00); // configure MCP23017 chip on WattBob BACK_LIGHT_ON(par_port); lcd = new WattBob_TextLCD(par_port); rgb_sensor.enablePowerAndRGBC(); rgb_sensor.setIntegrationTime(100); //Writing initial text on LCD lcd->printf("Welcome to the"); lcd->locate(1,0); //Going to new line lcd->printf("Chipin Sorter"); wait(1); //Wait 1 second //lcd->reset(); //Clear LCD while(1){ char c = pc.getc(); //wait for a serial character to be recieved. processMessage(c); //Do something, based on the character recieved. } /* if(par_port->read_bit(8)){ lcd->printf("ON"); pc.printf("btn1"); wait(0.5); lcd->reset(); }else{ lcd->printf("OFF"); pc.printf("btn0"); wait(0.5); lcd->reset(); } */ }