Pārlūkot izejas kodu

add some basic scaling of encoder value according to turning velocity

Thomas Buck 6 mēnešus atpakaļ
vecāks
revīzija
6035458eaf
2 mainītis faili ar 38 papildinājumiem un 4 dzēšanām
  1. 1
    0
      include/encoder.h
  2. 37
    4
      src/encoder.c

+ 1
- 0
include/encoder.h Parādīt failu

@@ -26,5 +26,6 @@ void encoder_run(void);
26 26
 
27 27
 int32_t encoder_pos(void);
28 28
 int32_t encoder_get_diff(void);
29
+uint32_t encoder_get_rpm(void);
29 30
 
30 31
 #endif // __ENCODER_H__

+ 37
- 4
src/encoder.c Parādīt failu

@@ -14,6 +14,7 @@
14 14
 #include "pico/stdlib.h"
15 15
 
16 16
 #include "main.h"
17
+#include "log.h"
17 18
 #include "encoder.h"
18 19
 
19 20
 #define LATCH0 0
@@ -44,6 +45,8 @@ static int8_t oldState;
44 45
 static int32_t position;
45 46
 static int32_t positionExt;
46 47
 static int32_t positionExtPrev;
48
+static uint32_t positionExtTime;
49
+static uint32_t positionExtTimePrev;
47 50
 
48 51
 void encoder_init(void) {
49 52
     for (uint i = 0; i < 2; i++) {
@@ -67,20 +70,41 @@ void encoder_init(void) {
67 70
     position = 0;
68 71
     positionExt = 0;
69 72
     positionExtPrev = 0;
73
+    positionExtTime = 0;
74
+    positionExtTimePrev = 0;
70 75
 }
71 76
 
72 77
 int32_t encoder_pos(void) {
73 78
     return positionExt;
74 79
 }
75 80
 
81
+// TODO should be adaptive depending on value range to be changed
82
+#define ENCODER_RPM_VALUE_FACTOR 100.0f
83
+
76 84
 int32_t encoder_get_diff(void) {
77
-    static int32_t last_epos = 0;
78
-    int32_t epos = encoder_pos();
79
-    int32_t diff = epos - last_epos;
80
-    last_epos = epos;
85
+    int32_t diff = positionExt - positionExtPrev;
86
+    positionExtPrev = positionExt;
87
+
88
+#ifdef ENCODER_RPM_VALUE_FACTOR
89
+    if (diff != 0) {
90
+        uint32_t rpm = encoder_get_rpm();
91
+        float f = 1.0f + ((float)rpm / ENCODER_RPM_VALUE_FACTOR);
92
+        //debug("diff=%"PRIi32" rpm=%"PRIu32" result=%.1f", diff, rpm, diff * f);
93
+        return diff * f;
94
+    }
95
+#endif
96
+
81 97
     return diff;
82 98
 }
83 99
 
100
+uint32_t encoder_get_rpm(void) {
101
+    // calculate max of difference in time between last position changes or last change and now.
102
+    uint32_t timeBetweenLastPositions = positionExtTime - positionExtTimePrev;
103
+    uint32_t timeToLastPosition = to_ms_since_boot(get_absolute_time()) - positionExtTime;
104
+    uint32_t t = MAX(timeBetweenLastPositions, timeToLastPosition);
105
+    return (60.0f * 1000.0f) / ((float)(t * 20));
106
+}
107
+
84 108
 void encoder_run(void) {
85 109
     int8_t thisState = 0;
86 110
     if (hw_type == HW_PROTOTYPE) {
@@ -99,6 +123,9 @@ void encoder_run(void) {
99 123
                 // The hardware has 4 steps with a latch on the input state 3
100 124
                 positionExt = position >> 2;
101 125
                 positionExt = -positionExt;
126
+
127
+                positionExtTimePrev = positionExtTime;
128
+                positionExtTime = to_ms_since_boot(get_absolute_time());
102 129
             }
103 130
             break;
104 131
 
@@ -107,6 +134,9 @@ void encoder_run(void) {
107 134
                 // The hardware has 4 steps with a latch on the input state 0
108 135
                 positionExt = position >> 2;
109 136
                 positionExt = -positionExt;
137
+
138
+                positionExtTimePrev = positionExtTime;
139
+                positionExtTime = to_ms_since_boot(get_absolute_time());
110 140
             }
111 141
             break;
112 142
 
@@ -115,6 +145,9 @@ void encoder_run(void) {
115 145
                 // The hardware has 2 steps with a latch on the input state 0 and 3
116 146
                 positionExt = position >> 1;
117 147
                 positionExt = -positionExt;
148
+
149
+                positionExtTimePrev = positionExtTime;
150
+                positionExtTime = to_ms_since_boot(get_absolute_time());
118 151
             }
119 152
             break;
120 153
         }

Notiek ielāde…
Atcelt
Saglabāt