Library to read out speed and position from a quadrature encoder. This library uses X2 decoding.

Dependents:   BMT-K9_encoder BMT-K9-Regelaar K9motoraansturing_copy EMGverwerking ... more

Files at this revision

API Documentation at this revision

Comitter:
vsluiter
Date:
Wed Sep 25 14:55:08 2013 +0000
Child:
1:2dd7853c911a
Commit message:
Initial Commit

Changed in this revision

encoder.cpp Show annotated file Show diff for this revision Revisions of this file
encoder.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/encoder.cpp	Wed Sep 25 14:55:08 2013 +0000
@@ -0,0 +1,68 @@
+
+#include "encoder.h"
+
+Encoder::Encoder(PinName int_a, PinName int_b) : pin_a(int_a), pin_b(int_b)
+{
+    EncoderTimer.start();
+    pin_a.fall(this,&Encoder::encoderFalling);
+    pin_a.rise(this,&Encoder::encoderRising);
+    m_position = 0;
+    m_speed = 0;
+    zero_speed = false;
+}
+ 
+void Encoder::encoderFalling(void)
+{
+    //temporary speed storage, in case higher interrupt level does stuff
+    float temp_speed;
+    int motortime_now = EncoderTimer.read_us();
+    EncoderTimer.reset();
+    EncoderTimeout.attach(this,&Encoder::timeouthandler,0.1);
+    /*calculate as ticks per second*/
+    if(zero_speed)
+        temp_speed  = 0;
+    else
+        temp_speed = 1000000./motortime_now;
+    zero_speed = false;
+    if(pin_b)
+    {
+        m_position++;
+        m_speed = temp_speed;
+    }
+    else
+    {
+        m_position--;    
+        m_speed  = -temp_speed; //negative speed
+    }
+}
+
+void Encoder::encoderRising(void)
+{
+    //temporary speed storage, in case higher interrupt level does stuff
+    float temp_speed;
+    int motortime_now = EncoderTimer.read_us();
+    EncoderTimer.reset();
+    EncoderTimeout.attach(this,&Encoder::encoderFalling,0.1);
+    /*calculate as ticks per second*/
+    if(zero_speed)
+        temp_speed  = 0;
+    else
+        temp_speed = 1000000./motortime_now;
+    zero_speed = false;
+    if(pin_b)
+    {
+        m_position--;
+        m_speed = -temp_speed; //negative speed
+    }
+    else
+    {
+        m_position++;    
+        m_speed  = temp_speed; 
+    }
+}
+
+void Encoder::timeouthandler(void)
+{
+    m_speed  = 0;
+    zero_speed = true;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/encoder.h	Wed Sep 25 14:55:08 2013 +0000
@@ -0,0 +1,27 @@
+#ifndef _ENCODER_H_
+#define _ENCODER_H_
+
+#include "mbed.h"
+
+class Encoder
+{
+    public:
+    Encoder(PinName int_a, PinName int_b);
+    int32_t getPosition(){return m_position;}
+    void    setPosition(int32_t pos){m_position = pos;}
+    float   getSpeed(){return m_speed;}
+    private:
+    void encoderFalling(void);
+    void encoderRising(void);
+    Timer EncoderTimer;
+    Timeout EncoderTimeout;
+    InterruptIn pin_a;
+    DigitalIn pin_b;
+    int32_t m_position;
+    float   m_speed;
+    void timeouthandler(void);
+    bool zero_speed;
+};
+
+
+#endif //_ENCODER_H_
\ No newline at end of file