@@ -35,6 +35,9 @@ def __init__(self, pi, enable_pin, forward_pin, backward_pin, feedback_pin_A, fe
35
35
# other
36
36
self ._motor_lock = threading .RLock ()
37
37
self ._start_timer = 0
38
+ self ._current_timer = 0
39
+ self ._ticks_threshold = 10
40
+ self ._ticks_counter = 0
38
41
self ._rotary_decoder = RotaryDecoder (pi , feedback_pin_A , feedback_pin_B , self .rotary_callback )
39
42
40
43
# GETTERS
@@ -123,6 +126,8 @@ def reset_state(self):
123
126
self ._encoder_speed = 0 # resetting encoder speed
124
127
self ._direction = 0 # resetting direction
125
128
self ._start_timer = 0
129
+ self ._current_timer = 0
130
+ self ._ticks_counter = 0
126
131
self ._is_moving = False # resetting moving flag
127
132
128
133
# CALLBACK
@@ -138,16 +143,45 @@ def reset_state(self):
138
143
- 1 tick = 0.0981mm
139
144
- 1 tick : 0.0981mm = x : 1000mm -> x = 10193 ticks approximately """
140
145
146
+ """
141
147
# callback function
142
148
def rotary_callback(self, tick):
143
149
self._motor_lock.acquire()
150
+
151
+ # on first movement
152
+ if(self._distance == 0):
153
+ self._start_timer = time() # clock started
154
+
155
+
144
156
self._ticks += tick # updating ticks
145
157
self._distance = self._ticks * 0.0981 # (mm) travelled
146
158
147
159
# velocity calculation
148
- elapse = time () - self ._start_timer
160
+ self._current_timer = time()
161
+
162
+ elapse = self._current_timer - self._start_timer
149
163
if(elapse != 0):
150
- self ._encoder_speed = (self ._distance / 1000 ) / elapse #(mm/s)
164
+ self._encoder_speed = self._distance / elapse #(mm/s)
165
+
166
+ self._motor_lock.release()
167
+ """
168
+
169
+ # callback function
170
+ def rotary_callback (self , tick ):
171
+ self ._motor_lock .acquire ()
172
+
173
+ # taking groups of n ticks each
174
+ if (self ._ticks_counter == 0 ):
175
+ self ._start_timer = time () # clock started
176
+ elif (self ._ticks_counter == self ._ticks_threshold ):
177
+ self ._current_timer = time ()
178
+ elapse = self ._current_timer - self ._start_timer
179
+ self ._encoder_speed = self ._ticks_threshold * 0.0981 / elapse # (mm/s)
180
+
181
+ self ._ticks += tick # updating ticks
182
+ self ._distance = self ._ticks * 0.0981 # (mm) travelled so far
183
+
184
+ self ._ticks_counter += 1 % (self ._ticks_threshold + 1 )
151
185
152
186
self ._motor_lock .release ()
153
187
0 commit comments