Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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();
};
};