@@ -32,12 +32,14 @@ def __init__(self, pi, enable_pin, forward_pin, backward_pin, feedback_pin_A, fe
32
32
self ._encoder_speed = 0
33
33
self ._is_moving = False
34
34
35
- # other
36
- self ._motor_lock = threading .RLock ()
35
+ # quadrature encoder variables
37
36
self ._start_timer = 0
38
37
self ._current_timer = 0
39
- self ._ticks_threshold = 10
38
+ self ._ticks_threshold = 3
40
39
self ._ticks_counter = 0
40
+
41
+ # other
42
+ self ._motor_lock = threading .RLock ()
41
43
self ._rotary_decoder = RotaryDecoder (pi , feedback_pin_A , feedback_pin_B , self .rotary_callback )
42
44
43
45
# GETTERS
@@ -137,14 +139,21 @@ def reset_state(self):
137
139
- Gearbox ratio: 120:1 (1 wheel revolution = 120 motor revolution)
138
140
- Encoder ratio: 16:1 encoder ticks for 1 motor revolution
139
141
- 1 wheel revolution = 120 * 16 = 1920 ticks
140
- - R = 30mm
141
- - 1 wheel revolution = 2πR = 2 * π * 30mm = 188.5mm
142
+ - R = 30mm - 1 wheel revolution = 2πR = 2 * π * 30mm = 188.5mm
142
143
- 1920 ticks = 188.5mm
143
144
- 1 tick = 0.0981mm
144
- - 1 tick : 0.0981mm = x : 1000mm -> x = 10193 ticks approximately """
145
+ - 1 tick : 0.0981mm = x : 1000mm -> x = 10193 ticks approximately
146
+ So 0.0981 is the ticks->distance(mm) conversion coefficient
147
+
148
+ The callback function calculates current velocity by taking groups of
149
+ ticks_threshold ticks"""
145
150
146
151
"""
147
152
# callback function
153
+ # it calculates velocity via approssimation
154
+ # it doeas a mean on current time passed and actual distance travelled
155
+ # NOT USEFUL FOR VELOCITY REGULATION since we need to know the current
156
+ # velocity updated each time
148
157
def rotary_callback(self, tick):
149
158
self._motor_lock.acquire()
150
159
@@ -175,15 +184,25 @@ def rotary_callback(self, tick):
175
184
self ._start_timer = time () # clock started
176
185
elif (self ._ticks_counter == self ._ticks_threshold ):
177
186
self ._current_timer = time ()
178
- elapse = self ._current_timer - self ._start_timer
187
+ elapse = self ._current_timer - self ._start_timer # calculating time elapse
188
+ # calculating current speed
179
189
self ._encoder_speed = self ._ticks_threshold * 0.0981 / elapse # (mm/s)
180
190
181
191
self ._ticks += tick # updating ticks
182
192
self ._distance = self ._ticks * 0.0981 # (mm) travelled so far
183
193
184
- self ._ticks_counter += 1 % (self ._ticks_threshold + 1 )
185
194
186
- self ._motor_lock .release ()
195
+ if (self ._ticks_counter < self ._ticks_threshold ):
196
+ self ._ticks_counter += 1
197
+ else :
198
+ self ._ticks_counter = 0
199
+
200
+ # updating ticks counter using module
201
+ # 0, 1, 2, ... 8, 9, 10, 0, 1, 2, ...
202
+ # not ideal, module on ticks counter not precise, may miss an interrupt
203
+ #self._ticks_counter += 1 % (self._ticks_threshold + 1)
204
+
205
+ self ._motor_lock .release () # releasing lock
187
206
188
207
# callback cancelling
189
208
def cancel_callback (self ):
0 commit comments