New QEI library with position (angle) outputs

Fork of QEI_modified by Chun Feng Huang

Files at this revision

API Documentation at this revision

Comitter:
benson516
Date:
Thu Mar 30 07:53:34 2017 +0000
Parent:
4:9699a757d4ed
Commit message:
New QEI

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
--- a/QEI.cpp	Tue Oct 25 13:05:41 2016 +0000
+++ b/QEI.cpp	Thu Mar 30 07:53:34 2017 +0000
@@ -174,9 +174,14 @@
     if (index !=  NC) {
         index_.rise(this, &QEI::index);
     }
-    // Parameters for unit changing
-    count_2_rad_s = 1/(pulsesPerRev_*1.0)*6.2831852/sampletime_/(arraysize_*1.0);
-    count_2_deg_s = 1/(pulsesPerRev_*1.0)*360.0/sampletime_/(arraysize_*1.0);
+
+    // Unit transformation
+    // Rotational speed
+    count_2_rad_s = 1.0/(pulsesPerRev_*1.0)*6.2831852/sampletime_/(arraysize_*1.0);
+    count_2_deg_s = 1.0/(pulsesPerRev_*1.0)*360.0/sampletime_/(arraysize_*1.0);
+    // Angle
+    count_2_rad = 1.0/(pulsesPerRev_*1.0)*6.2831852;
+    count_2_deg = 1.0/(pulsesPerRev_*1.0)*360.0;
 }
 
 void QEI::reset(void) {
@@ -280,7 +285,7 @@
 
         //Entered a new valid state.
         if (((currState_ ^ prevState_) != INVALID) && (currState_ != prevState_)) {
-            // 2 bit state. 
+            // 2 bit state.
             // (Right hand bit of prev) XOR (left hand bit of current)
             // B_(n-1) xor A_n (because of the 90deg phase shift and A is leading B when rotates clockwise)
             // gives 0 if clockwise rotation and 1 if counter clockwise rotation.
@@ -309,7 +314,7 @@
 void QEI::Calculate(){                      // Moving average: calculating the speed of the motor for feedback control
     // new input
     countArray[arrayPtr] = pulses_;
-    
+
     // nextPtr: Actually, it is the oldest one.
     uint8_t nextPtr = arrayPtr + 1;
     if(nextPtr > (arraysize_ - 1) ){nextPtr = 0;}
@@ -326,4 +331,27 @@
 
 double QEI::getAngularSpeed_deg_s(){
     return angularvelocity*count_2_deg_s;
-}
\ No newline at end of file
+}
+// Angle
+double QEI::getAngle(bool is_ranged){ // rad, if is_ranged, return 0~2*PI
+    //
+    int pulse_temp = this->pulses_;
+    //
+    if (is_ranged){
+        revolutions_ = pulse_temp/pulsesPerRev_;
+        return (pulse_temp % pulsesPerRev_)*count_2_rad;
+    }else{
+        return (pulse_temp*count_2_rad);
+    }
+}
+double QEI::getAngle_deg(bool is_ranged){ // deg, if is_ranged, return 0~360
+    //
+    int pulse_temp = this->pulses_;
+    //
+    if (is_ranged){
+        revolutions_ = pulse_temp/pulsesPerRev_;
+        return (pulse_temp % pulsesPerRev_)*count_2_deg;
+    }else{
+        return (pulse_temp*count_2_deg);
+    }
+}
--- a/QEI.h	Tue Oct 25 13:05:41 2016 +0000
+++ b/QEI.h	Thu Mar 30 07:53:34 2017 +0000
@@ -141,7 +141,7 @@
 public:
 
     volatile int pulses_;
-    
+
     int angularvelocity;
     int *countArray;
     uint8_t arrayPtr;
@@ -208,11 +208,16 @@
      * @return Number of revolutions which have occured on the index channel.
      */
     int getRevolutions(void);
-    
+
     void Calculate(void);
-    
+
+    // Get results
+    // Rotational speed
     double getAngularSpeed(void);
     double getAngularSpeed_deg_s(void);
+    // Angle
+    double getAngle(bool is_ranged); // rad, if is_ranged, return 0~2*PI
+    double getAngle_deg(bool is_ranged); // deg, if is_ranged, return 0~360
 
 private:
 
@@ -241,8 +246,14 @@
     int          pulsesPerRev_;
     int          prevState_;
     int          currState_;
+    
+    // Unit transformation
+    // Rotational speed
     double        count_2_rad_s;
     double        count_2_deg_s;
+    // Angle
+    double       count_2_rad;
+    double       count_2_deg;
 
 //    volatile int pulses_;
     volatile int revolutions_;