Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 3:cf7ae37c6930, committed 2021-03-07
- Comitter:
 - ryosukenanoda
 - Date:
 - Sun Mar 07 09:25:55 2021 +0000
 - Parent:
 - 2:36572948b0d4
 - Commit message:
 - calculate total rotation
 
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 | 
--- a/Encoder.cpp	Mon Jan 25 14:26:30 2021 +0000
+++ b/Encoder.cpp	Sun Mar 07 09:25:55 2021 +0000
@@ -3,7 +3,7 @@
 
 Encoder::Encoder(PinName pin_A, PinName pin_B): _pin_A(pin_A), _pin_B(pin_B) {
     max_time = 2^30; //mbet can count only 30 minits --- https://os.mbed.com/handbook/Timer
-    dt_to_raito = 2 * PI / (RESOLUTION_COUNT * GEAR_RAITO); // dt_to_raito / dt = raito
+    to_raito = 2 * PI / (RESOLUTION_COUNT * GEAR_RAITO); // to_raito / dt = raito
     timer.start();
     _pin_A.rise(callback(this, &Encoder::did_rise));
 };
@@ -12,6 +12,10 @@
     return raito;
 };
 
+float Encoder::get_rotation() {
+    return float(total_count) * to_raito;
+};
+
 void Encoder::did_rise() {
     int now_time = timer.read_us();
     if( false ) { //TODO: reset timer if time_maxmum
@@ -20,21 +24,26 @@
         return;
     }
     for (int i = 0; i < 3; i++) {
+        wait_us(10);
         if (!_pin_A) {
-            wait_us(10);
             return;
         }
     }
     int dt = now_time - last_time;
     stop_checker.attach(callback(this, &Encoder::check_stop), 0.01);
     update_raito(dt);
+    total_count += direction();
     last_time = now_time;
 };
 
 void Encoder::update_raito(int dt) {
     float t = dt;
     t /= pow(10.0, 6.0);
-    raito = dt_to_raito / t * direction();
+    t = to_raito / t * direction();
+    if (abs(t) > 50) {
+        return;
+    }
+    raito = t;
 };
 
 float Encoder::direction() {
--- a/Encoder.h	Mon Jan 25 14:26:30 2021 +0000
+++ b/Encoder.h	Sun Mar 07 09:25:55 2021 +0000
@@ -9,6 +9,8 @@
     Encoder(PinName pin_A, PinName pin_B);
 
     float get_raito();
+    
+    float get_rotation();
 
 private:
     Timer timer;
@@ -19,10 +21,12 @@
 
     int max_time;
     
-    float dt_to_raito;
+    float to_raito;  //TODO: rename
 
     float raito;
 
+    int total_count;
+
     void did_rise();
 
     void update_raito(int dt); //dt microsec