Buggy bois / Mbed 2 deprecated HEATS_1

Dependencies:   mbed

Committer:
mazdo25
Date:
Mon Feb 18 13:13:34 2019 +0000
Revision:
0:f45212966fb1
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mazdo25 0:f45212966fb1 1 //works only in x2 encoding (512 counts per second)
mazdo25 0:f45212966fb1 2 class Encoder {
mazdo25 0:f45212966fb1 3 private:
mazdo25 0:f45212966fb1 4 InterruptIn ChannelA; //connect this to channel A,
mazdo25 0:f45212966fb1 5 DigitalIn ChannelB; //connect this to channel B,
mazdo25 0:f45212966fb1 6 Timer dT; //to calculate rate of change of encoder ticks.
mazdo25 0:f45212966fb1 7 int PET;
mazdo25 0:f45212966fb1 8 int CET;
mazdo25 0:f45212966fb1 9
mazdo25 0:f45212966fb1 10 public:
mazdo25 0:f45212966fb1 11
mazdo25 0:f45212966fb1 12
mazdo25 0:f45212966fb1 13 Encoder(PinName A, PinName B) : ChannelA(A),ChannelB(B){
mazdo25 0:f45212966fb1 14 dT.reset();
mazdo25 0:f45212966fb1 15 ChannelA.rise(callback(this, &Encoder::riseEncode)); //
mazdo25 0:f45212966fb1 16 ChannelA.fall(callback(this, &Encoder::fallEncode));
mazdo25 0:f45212966fb1 17 CET = 0;
mazdo25 0:f45212966fb1 18 PET = 0;
mazdo25 0:f45212966fb1 19 dT.start();
mazdo25 0:f45212966fb1 20 };
mazdo25 0:f45212966fb1 21
mazdo25 0:f45212966fb1 22 void riseEncode(){ //logic for what happens when you get a pulse in channel A
mazdo25 0:f45212966fb1 23 if (ChannelB.read() != 0) //essentially if the rise happens while B is 1 (high) means its going in reverse
mazdo25 0:f45212966fb1 24 {
mazdo25 0:f45212966fb1 25 CET --;
mazdo25 0:f45212966fb1 26 } else
mazdo25 0:f45212966fb1 27 {
mazdo25 0:f45212966fb1 28 CET ++;
mazdo25 0:f45212966fb1 29 }
mazdo25 0:f45212966fb1 30 };
mazdo25 0:f45212966fb1 31
mazdo25 0:f45212966fb1 32 void fallEncode(){ //logic, same as above but a fall in channel A
mazdo25 0:f45212966fb1 33 if (ChannelB.read() != 0){ //essentially if fall pulse in channel A happens while b ia high, means going forward
mazdo25 0:f45212966fb1 34 CET ++;
mazdo25 0:f45212966fb1 35 } else
mazdo25 0:f45212966fb1 36 {
mazdo25 0:f45212966fb1 37 CET --;
mazdo25 0:f45212966fb1 38 }
mazdo25 0:f45212966fb1 39 };
mazdo25 0:f45212966fb1 40 //refer to technical handbook quadrature encoder section.
mazdo25 0:f45212966fb1 41
mazdo25 0:f45212966fb1 42 float encoderTickRate() {
mazdo25 0:f45212966fb1 43 int Temp = PET;
mazdo25 0:f45212966fb1 44 PET = CET;
mazdo25 0:f45212966fb1 45 dT.reset();
mazdo25 0:f45212966fb1 46 return (CET-Temp)/(int)dT.read();
mazdo25 0:f45212966fb1 47 };
mazdo25 0:f45212966fb1 48
mazdo25 0:f45212966fb1 49 };