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

Revision:
3:97668a4cd69d
Parent:
2:168850019d5a
Child:
4:f3be545b3826
diff -r 168850019d5a -r 97668a4cd69d main.cpp
--- a/main.cpp	Wed Oct 29 11:11:02 2014 +0000
+++ b/main.cpp	Tue Nov 04 17:30:49 2014 +0000
@@ -2,22 +2,23 @@
 #include "MCP23017.h"
 #include "WattBob_TextLCD.h"
 #include "TCS3472_I2C.h"
+#include <string>
 
-//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);
+//Setup test LEDs
+DigitalOut testLED1(LED1);
+DigitalOut testLED2(LED2);
+DigitalOut testLED3(LED3);
+DigitalOut testLED4(LED4);
+
+//Setup Buttons
+DigitalIn testButton1(P1_8);
 
 //Boolean values to easily enable and disable certain features for individual testing
-bool colourSensor =  true;
+/*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.
@@ -42,15 +43,26 @@
 double rMax = 9244;
 double gMax = 3194;
 double bMax = 3590;
-TCS3472_I2C rgb_sensor(p28, p27);
+TCS3472_I2C rgb_sensor(p28,p27); //p28 =sda, p27=scl
+//thresholds are in the RGB format.
+int redLowerThresholds [3] = {79,23,41};
+int redUpperThresholds [3]= {92,28,49};
+int greenLowerThresholds [3] = {46,81,61};
+int greenUpperThresholds [3]= {57,100,75};
+int blueLowerThresholds [3]= {25,47,47};
+int blueUpperThresholds [3]= {28,52,52};
 
-//setup Card Reader stuff
+//setup Card Reader pins
 DigitalIn cardBit1(p21);
 DigitalIn cardBit2(p22);
 DigitalIn cardBit3(p23);
 DigitalIn cardBit4(p24);
 DigitalIn cardBit5(p25);
 DigitalIn cardBit6(p26);
+DigitalIn cardDetect(p29);
+bool cardDataAcquired = false;
+bool colourDataAcquired = false;
+bool chipDetected = false;
 
 //Setup pins to FPGA
 DigitalOut startSort(p5);       //A positive edge tells the FPGA to start sorting.
@@ -77,16 +89,94 @@
 DigitalOut DispenseBit1(p10);  //The 2 bits below are select bits for the dispenser.
 DigitalOut DispenseBit2(p11);
 
+DigitalOut servoTest(p21); //for basic servo test fro deadline day 31/10/14
+
+//Global card bits
+int cardValue1 = 0;
+int cardValue2 = 0;
+int cardValue3 = 0;
+int cardValue4 = 0;
+int cardValue5 = 0;
+int cardValue6 = 0;
 
 //Functions go here
 void sendCharacter(char ch){
         pc.putc(ch);
         }
         
-void printLCD(char * text){
+        
+void setLEDs(){
+    testLED1 = cardDetect;
+    testLED4 = true;
+}        
+
+/*  A simple print function for use on the MBED LCD Screen.
+ *  Will format a char array into two lines and display the char array on the LCD.
+ */      
+void printLCD(const char * text){
+        std::string txt_str = std::string(text); //Convert ther character array to a std::string, 
+                                                 //to use in the string manipulation functions.
         lcd->reset();
-        lcd->printf(text);
+        if(txt_str.length() > 32){
+            int length = txt_str.length();
+            txt_str = txt_str.erase(32,length-32);
+            lcd->locate(0,0);   //Going to new line
+            text = txt_str.c_str(); 
+        }
+            if(txt_str.length() > 16){
+            string line1 = txt_str.substr(0,16);
+            string line2 = txt_str.substr(16,16);
+            lcd->locate(0,0);   //Going to new line
+            lcd->printf(line1.c_str());
+            lcd->locate(1,0);   //Going to new line
+            lcd->printf(line2.c_str());
+            lcd->locate(0,0);   //Going to new line
+            }
+      
 }
+
+
+void readButtons(){
+    chipDetected = par_port->read_bit(8); 
+    testLED1 = par_port->read_bit(8); 
+    
+    }
+/* Reads all 6 bits from the card IO pins and prints them to the MBED LCD.
+ * Stores the values read to the global variables cardValue1 to cardValue6.
+*/
+void cardAcquisition(){
+        lcd->reset();
+        lcd->locate(0,0);   //Going to new line
+        if(cardBit1) lcd->printf("p1=1,");
+        else lcd->printf("p1=0,");
+        cardValue1 = cardBit1;
+        
+        if(cardBit2) lcd->printf("p2=1,");
+        else lcd->printf("p2=0,");
+        cardValue2 = cardBit2;
+        
+        if(cardBit3) lcd->printf("p3=1");
+        else lcd->printf("p3=0");
+        cardValue3 = cardBit3;
+        
+        lcd->locate(1,0);   //Going to new line
+        
+        if(cardBit4) lcd->printf("p4=1");
+        else lcd->printf("p4=0,");
+        cardValue4 = cardBit4;
+        
+        
+        if(cardBit5) lcd->printf("p5=1,");
+        else lcd->printf("p5=0,");
+        cardValue5 = cardBit5;
+        
+        if(cardBit6) lcd->printf("p6=1");
+        else lcd->printf("p6=0");
+        cardValue6 = cardBit6;
+        
+        cardDataAcquired = true;
+        
+    }
 /*  valueToChar(int value):
     Expects an int value what it 0 or 1.
     Returns the int value as a char.
@@ -140,7 +230,7 @@
 */
    
 void processMessage(char c){
-       if(!operation){ switch(c){
+     switch(c){
             case 'a' : //servo 1 clockwise
                 sendCharacter('a'); //bounce back for acknowledgement
                 printLCD("S1 clockwise");
@@ -166,25 +256,35 @@
                     //wait(0.1);
                 }
                 sendCharacter('C'); //Tells PC that the card has been read
-                printLCD("Card read success");
+                printLCD("Card read done");
             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");
+                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; 
+                startSort = true; 
+            break;
+            case 't'://Test 180 servo
+            sendCharacter('T');
+            printLCD("Servo signal on");
+            servoTest = true;
             break;
-        }
-        }
-        else if (operation){
+            case 'w'://Test 180 servo
+            sendCharacter('W');
+            printLCD("Servo signal off");
+            servoTest = false;
+            break;
+        
+        
+   /*     else if (operationMode){
             switch(c){
                 case 'n':// Start sorting
                     sendCharacter('N'); //Confirm to PC that we are processing the instruction.
@@ -202,10 +302,10 @@
                     sendCharacter('P');
                 break;
                 }
-            }
+          */  }
 }
         
-void colourSensorTick(){
+void readColourSensor(){
         rgb_sensor.getAllColors(rgb_readings);
         
         double redd = (rgb_readings[1] /gMax) * 255;
@@ -215,7 +315,7 @@
         int red = redd;
         int green = greend;
         int blue = blued;  
-        
+        colourDataAcquired = true;
         lcd->cls(); // clear display 
         lcd->locate(0,0); 
         lcd->printf("R:%d G:%d B:%d",red,green,blue);
@@ -231,6 +331,12 @@
             lcd->printf("BLUE");
         }  
 }
+
+void resetForNextCustomer(){
+        colourDataAcquired = false;
+        cardDataAcquired = false;
+        printLCD("Ready for next customer");
+        }
         
 int main() {
     //Setting up all the global variables
@@ -241,30 +347,27 @@
     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
+   // lcd->printf("Welcome to the");
+  //  lcd->locate(1,0);   //Going to new line
+  //  lcd->printf("Chipin Sorter");
+  
+
+  
+    printLCD("Welcome to the  Chipin Sorter");
+    wait(1);
+    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(11)) resetForNextCustomer();
+       // char c = pc.getc(); //wait for a serial character to be recieved.
+       // processMessage(c);  //Do something, based on the character recieved.
+        if(cardDetect & !cardDataAcquired) cardAcquisition();
+        setLEDs();
+        readButtons();
+        
+        if(chipDetected & !colourDataAcquired) readColourSensor();
         }
       
         
-            /*
-            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();
-                    }
-        
-        */
+
     }