Skip to content

Commit e3f6b5b

Browse files
committed
Velocity for each wheel is now available
1 parent 46e62df commit e3f6b5b

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

rotary_encoder/motorencoder.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ def __init__(self, pi, enable_pin, forward_pin, backward_pin, feedback_pin_A, fe
3232
self._encoder_speed = 0
3333
self._is_moving = False
3434

35-
# other
36-
self._motor_lock = threading.RLock()
35+
# quadrature encoder variables
3736
self._start_timer = 0
3837
self._current_timer = 0
39-
self._ticks_threshold = 10
38+
self._ticks_threshold = 3
4039
self._ticks_counter = 0
40+
41+
# other
42+
self._motor_lock = threading.RLock()
4143
self._rotary_decoder = RotaryDecoder(pi, feedback_pin_A, feedback_pin_B, self.rotary_callback)
4244

4345
# GETTERS
@@ -137,14 +139,21 @@ def reset_state(self):
137139
- Gearbox ratio: 120:1 (1 wheel revolution = 120 motor revolution)
138140
- Encoder ratio: 16:1 encoder ticks for 1 motor revolution
139141
- 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
142143
- 1920 ticks = 188.5mm
143144
- 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"""
145150

146151
"""
147152
# 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
148157
def rotary_callback(self, tick):
149158
self._motor_lock.acquire()
150159
@@ -175,15 +184,25 @@ def rotary_callback(self, tick):
175184
self._start_timer = time() # clock started
176185
elif (self._ticks_counter == self._ticks_threshold):
177186
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
179189
self._encoder_speed = self._ticks_threshold * 0.0981 / elapse # (mm/s)
180190

181191
self._ticks += tick # updating ticks
182192
self._distance = self._ticks * 0.0981 # (mm) travelled so far
183193

184-
self._ticks_counter += 1 % (self._ticks_threshold + 1)
185194

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
187206

188207
# callback cancelling
189208
def cancel_callback(self):

rotary_encoder/wheelsaxel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def speed(self):
5454
return (l_speed + r_speed) * 0.5
5555

5656
#direction
57-
def speed(self):
57+
def direction(self):
5858
l_dir = self._left_motor.direction()
5959
r_dir = self._right_motor.direction()
6060
if(l_dir == r_dir):

0 commit comments

Comments
 (0)