Colour sensors calibrated

Dependencies:   mbed-rtos mbed Servo QEI

Fork of ICRSEurobot13 by Thomas Branch

Sensors/Colour/Colour.h

Committer:
twighk
Date:
2013-04-03
Revision:
4:1be0f6c6ceae
Parent:
3:717de74f6ebd
Child:
5:56a5fdd373c9

File content as of revision 4:1be0f6c6ceae:


// Eurobot13 Colour.h

#include "mbed.h"
#include "Led.h"
#include "Phototransistor.h"

enum ColourEnum {BLUE, RED, WHITE, INCONCLUSIVE, BUG};

class Colour{
private:
    Led blue;   float bavg, bstdev;
    Led red;    float ravg, rstdev; 
    Phototransistor pt;
    
public:
    Colour(PinName bluePin, PinName redPin, PinName phototransistorPin)
        : blue(bluePin)
        , red (redPin)
        , pt (phototransistorPin)
        {
        LedsOff();
    }

    void Calibrate(){
        ReadLed(blue, bavg, bstdev);
        ReadLed( red, ravg, rstdev);
    }
        
    ColourEnum getColour(){
        bool blueb = isColour(blue, bavg, bstdev)
            , redb = isColour( red, ravg, rstdev);
                  
        if      ( blueb &&  redb)
                    {return WHITE;}
        else if ( blueb && !redb)
                    {return BLUE;}
        else if (!blueb &&  redb)
                    {return RED;}
        else if (!blueb && !redb)
                    {return INCONCLUSIVE;} 
        return BUG;            
    }

private:
    void LedsOff(){blue.off(); red.off();}
    void ReadLed (Led &led, float &avg, float &stdev, const int measureNum = 25); // Colour.cpp
    bool isColour(Led &led, const float &avg, const float &stdev, const float numstddev = 2); // Colour.cpp
    
};