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
Diff: Encoder.h
- Revision:
- 0:f45212966fb1
diff -r 000000000000 -r f45212966fb1 Encoder.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Encoder.h Mon Feb 18 13:13:34 2019 +0000
@@ -0,0 +1,49 @@
+//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();
+ };
+
+};
\ No newline at end of file