basic Quadrature Encoder Library

Dependents:   ESP8266_pid_mtrPos_webserver_SDcard_v2 ESP8266_pid_mtrSpeed_Webserver_SDcard ESP8266_pid_spd_and_pos_webserver_SDcard pid_encoder_speed_demo ... more

Files at this revision

API Documentation at this revision

Comitter:
electromotivated
Date:
Mon Nov 23 02:44:15 2015 +0000
Commit message:
Basic encoder library upload;

Changed in this revision

QEI.cpp Show annotated file Show diff for this revision Revisions of this file
QEI.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 50aae578cb89 QEI.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QEI.cpp	Mon Nov 23 02:44:15 2015 +0000
@@ -0,0 +1,21 @@
+#include "QEI.h"
+
+QEI::QEI(PinName encA, PinName encB): _encA(encA), _encB(encB){
+    _encA.mode(PullUp);
+    _encA.rise(this, &QEI::callback);
+    
+    _encB.mode(PullUp);
+}
+
+long QEI::read(){
+    return count;
+}
+
+void QEI::reset(){
+    count = 0;
+}
+
+void QEI::callback(){
+    if(_encB) count--;  // CCW count
+    else count++;       // CW count    
+}
\ No newline at end of file
diff -r 000000000000 -r 50aae578cb89 QEI.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QEI.h	Mon Nov 23 02:44:15 2015 +0000
@@ -0,0 +1,46 @@
+#ifndef QEI_H
+#define QEI_H
+
+/*
+    Basic Quadrature Encoder Interface (QEI) Class.
+    
+    TODO: Expand to allow setting of quad mode, i.e. count on 
+    rising of A only, rising and falling of A, rising and falling 
+    of both A and B. (Forgot what these modes are called but 
+    can be found in literature online.) 
+*/
+
+#include "mbed.h"
+
+class QEI{
+    public:
+        /*
+            Constructor for QEI objects
+            @param encA     The mbed pin that encoder input A is on
+            @param encB     The mbed pin that encoder input B is on
+        */
+        QEI(PinName encA, PinName encB);
+        /*
+            read() returns total number of counts of the encoder.
+            Count can be +/- and indicates the overall direction,
+            (+): CW (-): CCW
+            @return     The toltal number of counts of the encoder.
+            
+            TODO: Add Conversion Overload for this method
+        */
+        long read();
+        /*
+            reset() clears the counter to 0. 
+        */
+        void reset();
+    private:
+        long count;         // Total number of counts since start.
+        InterruptIn _encA;  // Encoder A interrupt pin
+        DigitalIn _encB;    // Encoder B input pin
+        /*
+            Increments/Decrements count on interrrupt.
+        */
+        void callback();    // Interrupt callback function
+};
+
+#endif
\ No newline at end of file