Skip to content

Commit 46e62df

Browse files
committed
calculated velocity on each wheel according to ticks per time
1 parent 0ee8253 commit 46e62df

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

rotary_encoder/motorencoder.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ def __init__(self, pi, enable_pin, forward_pin, backward_pin, feedback_pin_A, fe
3535
# other
3636
self._motor_lock = threading.RLock()
3737
self._start_timer = 0
38+
self._current_timer = 0
39+
self._ticks_threshold = 10
40+
self._ticks_counter = 0
3841
self._rotary_decoder = RotaryDecoder(pi, feedback_pin_A, feedback_pin_B, self.rotary_callback)
3942

4043
# GETTERS
@@ -123,6 +126,8 @@ def reset_state(self):
123126
self._encoder_speed = 0 # resetting encoder speed
124127
self._direction = 0 # resetting direction
125128
self._start_timer = 0
129+
self._current_timer = 0
130+
self._ticks_counter = 0
126131
self._is_moving = False # resetting moving flag
127132

128133
# CALLBACK
@@ -138,16 +143,45 @@ def reset_state(self):
138143
- 1 tick = 0.0981mm
139144
- 1 tick : 0.0981mm = x : 1000mm -> x = 10193 ticks approximately """
140145

146+
"""
141147
# callback function
142148
def rotary_callback(self, tick):
143149
self._motor_lock.acquire()
150+
151+
# on first movement
152+
if(self._distance == 0):
153+
self._start_timer = time() # clock started
154+
155+
144156
self._ticks += tick # updating ticks
145157
self._distance = self._ticks * 0.0981 # (mm) travelled
146158
147159
# velocity calculation
148-
elapse = time() - self._start_timer
160+
self._current_timer = time()
161+
162+
elapse = self._current_timer - self._start_timer
149163
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)
151185

152186
self._motor_lock.release()
153187

rotary_encoder_tests/velocity_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from time import sleep
2+
from coderbot import CoderBot
3+
4+
c = CoderBot.get_instance()
5+
6+
while(True):
7+
print("Speed: %d" % (c._twin_motors_enc._left_motor._encoder_speed))
8+
print("Distance: %d" % (c._twin_motors_enc._left_motor._distance))
9+
print("Start timer: %d" % (c._twin_motors_enc._left_motor._start_timer))
10+
print("Current timer: %d" % (c._twin_motors_enc._left_motor._current_timer))
11+
print("Elapse: %d" % (c._twin_motors_enc._left_motor._current_timer - c._twin_motors_enc._left_motor._start_timer))
12+
print("")
13+
sleep(0.3)
14+
15+

0 commit comments

Comments
 (0)