encoder library

Files at this revision

API Documentation at this revision

Comitter:
YutaTogashi
Date:
Thu Dec 13 08:19:50 2018 +0000
Child:
1:5dde31466ecb
Child:
2:10ce3d24df8e
Commit message:
A;

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	Thu Dec 13 08:19:50 2018 +0000
@@ -0,0 +1,89 @@
+#include "Encoder.h"
+
+Encoder::Encoder(PinName Apulse,PinName Bpulse) : Apulse(Apulse),Bpulse(Bpulse) {
+    Encoder::Apulse.rise(this,&Encoder::Apulse_Up);
+    Encoder::Apulse.fall(this,&Encoder::Apulse_Down);
+    Encoder::Bpulse.rise(this,&Encoder::Bpulse_Up);
+    Encoder::Bpulse.fall(this,&Encoder::Bpulse_Down);
+    RpmCalculateTimer.attach(this,&Encoder::RpmCalculate,CALCULATE_PERIOD);
+}
+
+void Encoder::Apulse_Up() {
+    if(Bpulse) {
+        count--;
+    } else {
+        count++;
+    } 
+}
+
+void Encoder::Apulse_Down() {
+    if(Bpulse) {
+        count++;
+    } else {
+        count--;
+    }
+}
+
+void Encoder::Bpulse_Up() {
+    if(Apulse) {
+        count++;
+    } else {
+        count--;
+    }
+}
+
+void Encoder::Bpulse_Down() {
+    if(Apulse) {
+        count--;
+    } else {
+        count++;
+    }
+}
+
+void Encoder::RpmCalculate() {
+    static float rotationBuffer = 0;
+    rotation = count / PPR;
+    rpm = (rotation - rotationBuffer) / CALCULATE_PERIOD * MINUTE;
+    rotationBuffer = rotation;
+    
+    angle = rotation * RADIAN;
+    position = angle - (float)((int)angle / RADIAN) * RADIAN;
+}
+
+void Encoder::setup(int Ppr,int Diameter) {
+    PPR = Ppr;
+    DIAMETER = Diameter;
+}
+
+void Encoder::calculate() {
+    //rotation = count / PPR;
+    //distance = DIAMETER * rotation * PI;
+    //angle = rotation * RADIAN;
+    //position = angle - (float)((int)angle / RADIAN) * RADIAN;
+}
+/****値返す用の関数****/
+float Encoder::getData(short ch) {
+    switch(ch) {
+        case COUNT:
+            return count;               //カウント
+            break;
+        case ROTATION:
+            return rotation;            //回転量
+            break;
+        case RPM:
+            return rpm;                 //rpm
+            break;
+        case DISTANCE:
+            return distance;            //距離(mm)
+            break;
+        case ANGLE:
+            return angle;               //角度
+            break;
+        case POSITION:
+            return position;            //角度(0度から360度までの間の値)
+            break;
+        default:
+            return 0;
+            break;
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Encoder.h	Thu Dec 13 08:19:50 2018 +0000
@@ -0,0 +1,43 @@
+#ifndef ENCODER_H
+#define ENCODER_H
+
+#include "mbed.h"
+
+/****定数****/
+#define PI 3.1415f
+#define RADIAN 360
+#define MINUTE 60
+#define CALCULATE_PERIOD 0.01f
+
+enum DATA_CATEGORY{
+    COUNT,
+    ROTATION,
+    RPM,
+    DISTANCE,
+    ANGLE,
+    POSITION,
+};
+
+
+class Encoder {
+    public:
+        Encoder(PinName Apulse,PinName Bpulse);
+        void setup(int Ppr = 400,int Diameter = 0);
+        void calculate();
+        float getData(short ch);
+        
+    private:
+        InterruptIn Apulse;
+        InterruptIn Bpulse;
+        Ticker RpmCalculateTimer;
+    
+        void Apulse_Up();
+        void Apulse_Down();
+        void Bpulse_Up();
+        void Bpulse_Down();
+        void RpmCalculate();
+        
+        float PPR,DIAMETER,count,rotation,rpm,distance,angle,position;
+};
+
+#endif
\ No newline at end of file