Buggy bois / Mbed 2 deprecated HEATS_1

Dependencies:   mbed

Encoder.h

Committer:
mazdo25
Date:
2019-02-18
Revision:
0:f45212966fb1

File content as of revision 0:f45212966fb1:

//works only in x2 encoding (512 counts per second)
class Encoder {  
    private:
    InterruptIn ChannelA; //connect this to channel A, 
    DigitalIn ChannelB; //connect this to channel B,
    Timer dT; //to calculate rate of change of encoder ticks.
    int PET;
    int CET;
    
    public:
    
    
    Encoder(PinName A, PinName B) : ChannelA(A),ChannelB(B){
        dT.reset();
        ChannelA.rise(callback(this, &Encoder::riseEncode)); //
        ChannelA.fall(callback(this, &Encoder::fallEncode));
        CET = 0;
        PET = 0;
        dT.start();
        };
        
    void riseEncode(){ //logic for what happens when you get a pulse in channel A
        if (ChannelB.read() != 0) //essentially if the rise happens while B is 1 (high) means its going in reverse
            {
            CET --;
            } else
            {
            CET ++;   
            }
        };
        
    void fallEncode(){ //logic, same as above but a fall in channel A
        if (ChannelB.read() != 0){ //essentially if fall pulse in channel A happens while b ia high, means going forward
            CET ++; 
            } else
            {
            CET --;
            }
        };  
        //refer to technical handbook quadrature encoder section.
        
    float encoderTickRate() {
        int Temp = PET;
        PET = CET;
        dT.reset();
        return (CET-Temp)/(int)dT.read();
        };

};