Yuta Uenodai / encoder

Dependents:   backdrive backdrive_3

Revision:
3:6dce9d79da13
Parent:
2:4bb7c8730d91
Child:
4:c99eb511ac08
--- a/encoder.cpp	Sun Jul 22 00:15:08 2018 +0000
+++ b/encoder.cpp	Tue Dec 04 09:42:15 2018 +0000
@@ -5,13 +5,13 @@
 void Encoder::calState(){
     //パルスの差分を更新
     count[DELTA][ONE_BEFORE] = count[NORMAL][ONE_BEFORE] - count[NORMAL][TWO_BEFORE];
-    count[DELTA][CURRENT]        = count[NORMAL][CURRENT] - count[NORMAL][ONE_BEFORE];
+    count[DELTA][CURRENT]    = count[NORMAL][CURRENT] - count[NORMAL][ONE_BEFORE];
     
     distance = count[NORMAL][CURRENT] * circle / ppr;
     rpm = (count[DELTA][CURRENT]/ppr) * 60 / dt;
     velocity = (count[DELTA][CURRENT]/ppr)*circle/dt;
-    acceleration = (count[DELTA][CURRENT]-count[DELTA][ONE_BEFORE])/(dt_square*ppr) * circle;
-    omega = (2*PI*count[DELTA][CURRENT])/(ppr*dt);
+    //acceleration = (count[DELTA][CURRENT]-count[DELTA][ONE_BEFORE])/(dt_square*ppr) * circle;
+    //omega = (2*PI*count[DELTA][CURRENT])/(ppr*dt);
     delta_distance = (count[DELTA][CURRENT]/ppr)*circle;
     //1つ前・2つ前のデータを更新
     count[NORMAL][TWO_BEFORE] = count[NORMAL][ONE_BEFORE];
@@ -50,7 +50,15 @@
     }
 }
 
-void Encoder::read(){
+void Encoder::readAphaseRise(){
+    if(bPhase){
+        count[NORMAL][CURRENT]--; 
+    } else {
+        count[NORMAL][CURRENT]++; 
+    }
+}
+
+void Encoder::readAphaseFall(){
     if(bPhase){
         count[NORMAL][CURRENT]++; 
     } else {
@@ -58,11 +66,25 @@
     }
 }
 
-void Encoder::init(float _ppr, float _radius,float _dt){
-    aPhase.rise(this,&Encoder::read);
+void Encoder::init(float _ppr, float _radius,float _dt,int mode = X1_ENCODE){
     ppr     = _ppr;
     dt      = _dt;
     dt_square = dt*dt;
     circle  = PI*2.0f*_radius;
     radius = _radius;
+    
+    switch(mode){
+        case X1_ENCODE:
+            aPhase.rise(this,&Encoder::readAphaseRise);
+            ppr = _ppr;
+            break;
+        case X2_ENCODE:              
+            aPhase.rise(this,&Encoder::readAphaseRise);
+            aPhase.fall(this,&Encoder::readAphaseFall);
+            ppr = _ppr*2;
+            break;
+        case X4_ENCODE:          
+            ppr = _ppr*4;
+            break;
+    }
 }