Colour sensors calibrated

Dependencies:   mbed-rtos mbed Servo QEI

Fork of ICRSEurobot13 by Thomas Branch

Sensors/Colour/Colour.h

Committer:
madcowswe
Date:
2013-04-04
Revision:
5:56a5fdd373c9
Parent:
4:1be0f6c6ceae
Child:
7:4340355261f9

File content as of revision 5:56a5fdd373c9:


// Eurobot13 Colour.h

//red led use 45ohm
//blue led use 10ohm

#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
    
};