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-11-25
Revision:
22:8f11d1c178ab
Parent:
19:78d4b78fa736
Child:
23:f9e7e64784be

File content as of revision 22:8f11d1c178ab:

bool operationMode      = true; //the MBED starts in operation mode.
#include "mbed.h"
#include "Language.h"
DigitalIn languageButton(p30);
Language currentLanguage = ENGLISH;
#include <sstream>
#include <string>
#include "mbedStorage.h"
#include "rgbLED.h"
#include "mbedLCD.h"
#include "stateMachine.h"
#include "FPGAcomms.h"
#ifndef serialComm
#define serialComm
#include "serialCommunication.h"
#endif
#include "testFunctions.h"

StateMachine stateMachine;

//Boolean values to easily enable and disable certain features for individual testing
bool colourSensor   =   true;
bool cardReader     =   true;
bool sorter         =   true;
bool dispensor      =   true;
bool testFunctions  =   true;
bool serialComms    =   true;

int mode = 4;
int maintenanceModeSelect = 0;

Colour lastColour = NONE;
Colour colourStore = NONE;

bool buttonPressed(int buttonNumber){
    if(buttonNumber > -1 && buttonNumber < 4){
        return par_port->read_bit(buttonNumber + 8);
    }
    return false;
}
    
void checkSortDispenseButtons(){
        
        if(buttonPressed(3)){
            mode++;
            if(mode >= 7) mode = 0;
        }
        
        
        if(mode == 0){                                     //Dispense Mode
            printLCD("Dispense Mode");
            operationMode=false;
            if(buttonPressed(0)){
                printLCD("Red Button pressed");
                dispense(RED);
            }else if(buttonPressed(1)){
                printLCD("Green Button pressed");
                dispense(GREEN);
            }else if(buttonPressed(2)){
                printLCD("Blue Button pressed");
                dispense(BLUE);
            }
        }else if(mode == 1){                                //Sort Mode
            printLCD("Sort Mode");
            operationMode=false;
            if(buttonPressed(0)){
                log("Red Button pressed");
                sort(RED);
            }else if(buttonPressed(1)){
                log("Green Button pressed");   
                sort(GREEN);
            }else if(buttonPressed(2)){
                log("Blue Button pressed");
                sort(BLUE);
            }
            
        }else if(mode == 2){                                //Bin/Recycle Mode
            printLCD("Bin/Recycle Mode");
            operationMode=false;
            if(buttonPressed(0)){
                log("Bin button pressed");
                sort(BIN);
            }else if(buttonPressed(1)){
                log("Recycle Button");
                recycle();
            }
        }else if(mode == 3){  
        printLCD("Colour Sensor Mode");                              //Colour Sensing Mode
            if(colourSensor){
                operationMode=true;
                wait(0.2);   
                Colour colour = readColourSensor();  
                Colour colour2 = readColourSensor();
                if(colour != colour2){
                    return;
                }
                if(colour == BIN){ //Make tripley sure that it is actuall a bin chip
                    printLCD("BIN CHECK");
                    wait(0.2);
                    Colour binCheck = readColourSensor();
                    if(binCheck != colour){
                        return;
                    }
                }
                lastColour = colourStore;
                colourStore = colour;  
                /* Tries to prevent a colour mis-read. When a chip is read as NONE,
                the chip may be falling down the tube as the reading was taken. The chip may
                also be out of position on the slider. Ignoring the next reading is trying to
                prevent a mis-read being valid.
                */
                if(lastColour == NONE){ 
                    
                    return;
                }
                //wait(5);
                if(sorter) sort(colour);  
                            //Put (sorter && colour != BIN) in if statement.
            }
            //if
        }else if(mode == 4){                                //Card Reader Mode
            operationMode= true;
            if(cardReader && cardDetect){
                cardAcquisition();
                
            }
        }else if(mode == 5){                                //Storage Mode
            operationMode=true;
            stringstream ss;
            ss << "Stored Values   " << "R:" << redAmount << "G:" << greenAmount << "B:" << blueAmount;
            string tmp = ss.str();
            printLCD(tmp.c_str());
            wait(0.1);
            if(buttonPressed(0)){
                log("Dispense all button pressed");
                dispenseAll();
            }else if(buttonPressed(1)){
                log("Clear stored numbers button pressed");
                writeFile(0,0,0);
                redAmount = 0;greenAmount = 0;blueAmount = 0;
                printLCD("Cleared values");
                
            }else if(buttonPressed(2)){
                log("Increment all stored values by 2.");
                redAmount += 2;greenAmount += 2;blueAmount += 2;
                writeFile(redAmount,greenAmount,blueAmount);
                
                printLCD("set values to 2");
                
            }
        }else if(mode == 6){ 
        operationMode = false;
        stringstream ss;
        ss << maintenanceModeSelect;
        string mainModeSelect = ss.str();
        printLCD("Maintenance: " + mainModeSelect);   
        wait(0.1);                           //Maintenance Mode
        //printLCD("Maintenance Mode");
            if(buttonPressed(0)){                      //Next maintenance function
            maintenanceModeSelect++;
            if(maintenanceModeSelect > 13) maintenanceModeSelect = 0;    
            }else if(buttonPressed(1)){                //Previous maintenance function
            maintenanceModeSelect--;
            if(maintenanceModeSelect < 0) maintenanceModeSelect = 13;    
            }else if(buttonPressed(2)){               //Process Instruction
                switch(maintenanceModeSelect){
                    case 0://RB_LEFT:
                        printLCD("Moving Red/Blue Slider left.");
                        maintain(RB_LEFT);
                    break;
                    
                    case 1://RB_CENTRE:
                        printLCD("Centering Red/Blue Slider.");
                        maintain(RB_CENTRE);
                    break;
                    
                    case 2://RB_RIGHT:
                        printLCD("Moving Red/Blue Slider right.");
                        maintain(RB_RIGHT);
                    break;
                    
                    case 3://GO_UP:
                        printLCD("Moving Green Slider up.");
                        maintain(GO_UP);
                    break;
                    
                    case 4://GO_CENTRE:
                        printLCD("Centering Green Slider.");
                        maintain(GO_CENTRE);
                    break;
                    
                    case 5://GO_DOWN:
                        printLCD("Moving Green Slider down.");
                        maintain(GO_DOWN);
                    break;
                    
                    case 6://BR_LEFT:
                        printLCD("Moving Bin Slider left.");
                        maintain(BR_LEFT);
                    break;
                    
                    case 7://BR_RIGHT:
                        printLCD("Moving Bin Slider right.");
                        maintain(BR_RIGHT);
                    break;
                    
                    case 8://R_PUSH:
                        printLCD("Pushing red dispensor");
                        maintain(R_PUSH);
                    break;
                    
                    case 9://R_HOME:
                        printLCD("Homing red dispensor");
                        maintain(R_HOME);
                    break;
                    
                    case 10://GB_LEFT:
                        printLCD("Pushing green dispensor");
                        maintain(GB_LEFT);
                    break;
                    
                    case 11://GB_CENTRE:
                        printLCD("Centre green/blue dispensor");
                        maintain(GB_CENTRE);
                    break;
                    
                    case 12://GB_RIGHT:
                        printLCD("Pushing blue dispensor");
                        maintain(GB_RIGHT );
                    break;
                    
                    case 13: //MaintenanceEND
                        printLCD("Ending Maintenance Mode");
                        maintain(maintenanceEND);
                    break;
                    
                
            }  printLCD("Ready for next command");
                wait(1);
                }
            }
        }
    
void changeLanguage(){
    currentLanguage = nextLanguage(currentLanguage);        //Change the language on teh MBED.
    sendLanguageCharacter();                                //Tell PC to change language
    //Change Language                                       //Tell Arduino to change language.
    printLCD("setting language to english");
    wait(1);
}   
      
int main() { 
    
    setupLCD();
    printLCD("Welcome to the  Chipin Sorter");
    wait(1);
    readChipNumbers();
    printStoredChipValues();
    //displayOperationMode();
    while(true){
        if(serialComms){
            checkSerial();
        }if(testFunctions) checkSortDispenseButtons();
        if(languageButton) changeLanguage();
        wait(0.05);    //Slowi8ng things down a little bit.
    } 
}