Browse Source

add some basic scaling of encoder value according to turning velocity

Thomas Buck 1 month ago
parent
commit
6035458eaf
2 changed files with 38 additions and 4 deletions
  1. 1
    0
      include/encoder.h
  2. 37
    4
      src/encoder.c

+ 1
- 0
include/encoder.h View File

26
 
26
 
27
 int32_t encoder_pos(void);
27
 int32_t encoder_pos(void);
28
 int32_t encoder_get_diff(void);
28
 int32_t encoder_get_diff(void);
29
+uint32_t encoder_get_rpm(void);
29
 
30
 
30
 #endif // __ENCODER_H__
31
 #endif // __ENCODER_H__

+ 37
- 4
src/encoder.c View File

14
 #include "pico/stdlib.h"
14
 #include "pico/stdlib.h"
15
 
15
 
16
 #include "main.h"
16
 #include "main.h"
17
+#include "log.h"
17
 #include "encoder.h"
18
 #include "encoder.h"
18
 
19
 
19
 #define LATCH0 0
20
 #define LATCH0 0
44
 static int32_t position;
45
 static int32_t position;
45
 static int32_t positionExt;
46
 static int32_t positionExt;
46
 static int32_t positionExtPrev;
47
 static int32_t positionExtPrev;
48
+static uint32_t positionExtTime;
49
+static uint32_t positionExtTimePrev;
47
 
50
 
48
 void encoder_init(void) {
51
 void encoder_init(void) {
49
     for (uint i = 0; i < 2; i++) {
52
     for (uint i = 0; i < 2; i++) {
67
     position = 0;
70
     position = 0;
68
     positionExt = 0;
71
     positionExt = 0;
69
     positionExtPrev = 0;
72
     positionExtPrev = 0;
73
+    positionExtTime = 0;
74
+    positionExtTimePrev = 0;
70
 }
75
 }
71
 
76
 
72
 int32_t encoder_pos(void) {
77
 int32_t encoder_pos(void) {
73
     return positionExt;
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
 int32_t encoder_get_diff(void) {
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
     return diff;
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
 void encoder_run(void) {
108
 void encoder_run(void) {
85
     int8_t thisState = 0;
109
     int8_t thisState = 0;
86
     if (hw_type == HW_PROTOTYPE) {
110
     if (hw_type == HW_PROTOTYPE) {
99
                 // The hardware has 4 steps with a latch on the input state 3
123
                 // The hardware has 4 steps with a latch on the input state 3
100
                 positionExt = position >> 2;
124
                 positionExt = position >> 2;
101
                 positionExt = -positionExt;
125
                 positionExt = -positionExt;
126
+
127
+                positionExtTimePrev = positionExtTime;
128
+                positionExtTime = to_ms_since_boot(get_absolute_time());
102
             }
129
             }
103
             break;
130
             break;
104
 
131
 
107
                 // The hardware has 4 steps with a latch on the input state 0
134
                 // The hardware has 4 steps with a latch on the input state 0
108
                 positionExt = position >> 2;
135
                 positionExt = position >> 2;
109
                 positionExt = -positionExt;
136
                 positionExt = -positionExt;
137
+
138
+                positionExtTimePrev = positionExtTime;
139
+                positionExtTime = to_ms_since_boot(get_absolute_time());
110
             }
140
             }
111
             break;
141
             break;
112
 
142
 
115
                 // The hardware has 2 steps with a latch on the input state 0 and 3
145
                 // The hardware has 2 steps with a latch on the input state 0 and 3
116
                 positionExt = position >> 1;
146
                 positionExt = position >> 1;
117
                 positionExt = -positionExt;
147
                 positionExt = -positionExt;
148
+
149
+                positionExtTimePrev = positionExtTime;
150
+                positionExtTime = to_ms_since_boot(get_absolute_time());
118
             }
151
             }
119
             break;
152
             break;
120
         }
153
         }

Loading…
Cancel
Save