Skip to content

Commit d139a8c

Browse files
committed
Encoder motors now use quadrature encoder feedback for each motor
1 parent b13d935 commit d139a8c

File tree

5 files changed

+20
-13
lines changed

5 files changed

+20
-13
lines changed

rotary_encoder/motorencoder.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,21 @@ def stop(self):
117117
# CALLBACK
118118
""" The callback function rotary_callback is called on FALLING_EDGE by the
119119
rotary_decoder with a parameter value of 1 (1 new tick)
120+
120121
- Gearbox ratio: 120:1 (1 wheel revolution = 120 motor revolution)
121-
- Encoder ratio: 8:1 encoder ticks for 1 motor revolution
122-
- 1 wheel revolution = 128 * 8 = 960 ticks
122+
- Encoder ratio: 16:1 encoder ticks for 1 motor revolution
123+
- 1 wheel revolution = 120 * 16 = 1920 ticks
123124
- R = 30mm
124125
- 1 wheel revolution = 2πR = 2 * π * 30mm = 188.5mm
125-
- 960 ticks = 188.5mm
126-
- 1 tick = 0.196mm
127-
- 1 tick : 0.196mm = x(ticks) : y(mm) """
126+
- 1920 ticks = 188.5mm
127+
- 1 tick = 0.0981mm
128+
- 1 tick : 0.0981mm = x : 1000mm -> x = 10193 ticks aproximately """
128129

129130
# callback function
130131
def rotary_callback(self, tick):
131132
self._motor_lock.acquire()
132133
self._ticks += tick # updating ticks
133-
self._distance = self._ticks * 0.196 # (mm) travelled
134+
self._distance = self._ticks * 0.0981 # (mm) travelled
134135
#self._encoder_speed = (mm/s)
135136
self._motor_lock.release()
136137

rotary_encoder/rotarydecoder.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,21 @@ def _pulse(self, gpio, level, tick):
5858
self._lastGpio = gpio
5959

6060
if (gpio == self._feedback_pin_A and level == 1):
61+
if (self._levelB == 0):
62+
self._callback(1) # A leading B, moving forward
63+
self._direction = 1 # forward
64+
elif (gpio == self._feedback_pin_A and level == 0):
6165
if (self._levelB == 1):
6266
self._callback(1) # A leading B, moving forward
6367
self._direction = 1 # forward
64-
6568
elif (gpio == self._feedback_pin_B and level == 1):
66-
if (self._levelA == 1):
69+
if (self._levelA == 0):
6770
self._callback(-1) # B leading A, moving forward
6871
self._direction = -1 # backwards
69-
72+
elif (gpio == self._feedback_pin_B and level == 0):
73+
if (self._levelA == 1):
74+
self._callback(-1) # A leading B, moving forward
75+
self._direction = -1 # forward
7076

7177
def cancel(self):
7278

rotary_encoder/wheelsaxel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def control_distance(self, power_left=100, power_right=100, target_distance=0):
9191
self._right_motor.control(power_right)
9292

9393
# moving for certaing amount of distance
94-
while(target_distance > 0):
94+
while(self.distance() < target_distance):
9595
sleep(0.05) # check if arrived every 50ms,
9696
print(str(self.distance()))
9797
target_distance = target_distance - self.distance() # updating target distance

rotary_encoder_tests/rotary_encoder_PWM_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import time
1313
import pigpio
1414

15-
#pi = pigpio.pi('coderbot.local')
15+
#pi = pigpio.pi('backend.local')
1616
pi = pigpio.pi()
1717
print("Connected, waiting for signal")
1818

rotary_encoder_tests/rotary_encoder_ticks_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ def callback_right(way):
3131
print("pos_RIGHT={}".format(pos_right))
3232

3333

34-
# pi = pigpio.pi('coderbot.local')
34+
# pi = pigpio.pi('backend.local')
3535
pi = pigpio.pi()
3636

37-
decoder_left = RotaryDecoder(pi, PIN_ENCODER_LEFT_A, PIN_ENCODER_LEFT_B, callback_left)
37+
decoder_left = RotaryDecoder(pi, PIN_ENCODER_LEFT_B, PIN_ENCODER_LEFT_A, callback_left)
3838
decoder_right = RotaryDecoder(pi, PIN_ENCODER_RIGHT_A, PIN_ENCODER_RIGHT_B, callback_right)
3939

4040
time.sleep(1000)

0 commit comments

Comments
 (0)