An interface for a simple, 1-track, incremental encoder.

Dependents:   AVC_20110423 incrementalencoder-pid-robot DataBus2018

Files at this revision

API Documentation at this revision

Comitter:
shimniok
Date:
Wed Apr 27 17:58:52 2011 +0000
Child:
1:5011d894d5a7
Commit message:

Changed in this revision

IncrementalEncoder.cpp Show annotated file Show diff for this revision Revisions of this file
IncrementalEncoder.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IncrementalEncoder.cpp	Wed Apr 27 17:58:52 2011 +0000
@@ -0,0 +1,29 @@
+#include "IncrementalEncoder.h"
+
+IncrementalEncoder::IncrementalEncoder(PinName pin):  _lastTicks(0),  _ticks(0), _interrupt(pin) {
+    _interrupt.mode(PullNone); // default is pulldown but my encoder board uses a pull-up and that just don't work
+    _interrupt.rise(this, &IncrementalEncoder::_increment); 
+    _interrupt.fall(this, &IncrementalEncoder::_increment); 
+}
+
+unsigned int IncrementalEncoder::read() {
+// disable interrupts?
+    unsigned int ticks = _ticks - _lastTicks;
+    _lastTicks = _ticks;
+    
+    return ticks;
+}    
+
+unsigned int IncrementalEncoder::readTotal() {
+    return _ticks;
+}
+
+    
+void IncrementalEncoder::reset() {
+    _ticks = _lastTicks = 0;
+}  
+
+
+void IncrementalEncoder::_increment() {
+    _ticks++;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IncrementalEncoder.h	Wed Apr 27 17:58:52 2011 +0000
@@ -0,0 +1,38 @@
+#include "mbed.h"
+
+/** An interface for a simple, 1-track, incremental encoder. If using a simple reflectance sensor, then a voltage comparator
+ *  circuit will be required to generate the pulsetrain.  See: http://www.bot-thoughts.com/2011/03/avc-bot-wheel-encoders.html
+ *
+ */
+class IncrementalEncoder
+{
+    public:
+        /** Create an incremental encoder interface.  Increments counter at every rise and fall signal 
+         *
+         * @param pin -- the pin to which a digital pulsetrain is sent
+         */
+        IncrementalEncoder(PinName pin);
+
+        /** Get ticks since last call
+         *
+         * @returns the number of ticks since the last call to this method
+         */        
+        unsigned int read();
+
+        /** Get total tick count since last reset
+         *
+         * @returns total ticks since the last reset or instantiation
+         */
+        unsigned int readTotal();
+       
+        /** Reset the tick counter
+         *
+         */
+        void reset();
+
+    private:
+        unsigned int _lastTicks;
+        unsigned int _ticks;
+        InterruptIn _interrupt;
+        void _increment();
+};
\ No newline at end of file