Skip to content

Commit cc146c7

Browse files
committed
Encoder motors now use both A and B pins for each encoder
1 parent 9974e96 commit cc146c7

File tree

3 files changed

+37
-32
lines changed

3 files changed

+37
-32
lines changed

coderbot.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,32 @@
2525
import mpu
2626
from wheelsaxel import WheelsAxel
2727

28+
# GPIO
29+
# motors
2830
PIN_MOTOR_ENABLE = 22
2931
PIN_LEFT_FORWARD = 24
3032
PIN_LEFT_BACKWARD = 25
3133
PIN_RIGHT_FORWARD = 17
3234
PIN_RIGHT_BACKWARD = 4
35+
#?
3336
PIN_PUSHBUTTON = 11
37+
# servo
3438
PIN_SERVO_3 = 9
3539
PIN_SERVO_4 = 10
40+
# sonar
3641
PIN_SONAR_1_TRIGGER = 18
3742
PIN_SONAR_1_ECHO = 7
3843
PIN_SONAR_2_TRIGGER = 18
3944
PIN_SONAR_2_ECHO = 8
4045
PIN_SONAR_3_TRIGGER = 18
4146
PIN_SONAR_3_ECHO = 23
42-
PIN_ENCODER_LEFT = 15
43-
PIN_ENCODER_RIGHT = 14
47+
# encoder
48+
PIN_ENCODER_LEFT_A = 14
49+
PIN_ENCODER_LEFT_B = 6
50+
PIN_ENCODER_RIGHT_A = 15
51+
PIN_ENCODER_RIGHT_B = 12
4452

53+
# PWM
4554
PWM_FREQUENCY = 100 #Hz
4655
PWM_RANGE = 100 #0-100
4756

@@ -51,24 +60,25 @@ class CoderBot(object):
5160

5261
_pin_out = [PIN_MOTOR_ENABLE, PIN_LEFT_FORWARD, PIN_RIGHT_FORWARD, PIN_LEFT_BACKWARD, PIN_RIGHT_BACKWARD, PIN_SERVO_3, PIN_SERVO_4]
5362

54-
def __init__(self, servo=False, motor_trim_factor=1.0, encoder=True):
63+
def __init__(self, motor_trim_factor=1.0, encoder=True):
5564
self.pi = pigpio.pi('localhost')
5665
self.pi.set_mode(PIN_PUSHBUTTON, pigpio.INPUT)
5766
self._cb = dict()
5867
self._cb_last_tick = dict()
5968
self._cb_elapse = dict()
60-
self._servo = servo
6169
self._encoder = encoder
6270
self._motor_trim_factor = motor_trim_factor
6371
self._twin_motors_enc = WheelsAxel(
6472
self.pi,
6573
enable_pin=PIN_MOTOR_ENABLE,
6674
left_forward_pin=PIN_LEFT_FORWARD,
6775
left_backward_pin=PIN_LEFT_BACKWARD,
68-
left_encoder_feedback_pin=PIN_ENCODER_LEFT,
76+
left_encoder_feedback_pin_A=PIN_ENCODER_LEFT_A,
77+
left_encoder_feedback_pin_B=PIN_ENCODER_LEFT_B,
6978
right_forward_pin=PIN_RIGHT_FORWARD,
7079
right_backward_pin=PIN_RIGHT_BACKWARD,
71-
right_encoder_feedback_pin=PIN_ENCODER_RIGHT)
80+
right_encoder_feedback_pin_A=PIN_ENCODER_RIGHT_A,
81+
right_encoder_feedback_pin_B=PIN_ENCODER_RIGHT_B)
7282
self.motor_control = self._dc_enc_motor
7383

7484
self._cb1 = self.pi.callback(PIN_PUSHBUTTON, pigpio.EITHER_EDGE, self._cb_button)
@@ -177,6 +187,5 @@ def restart(self):
177187
def reboot(self):
178188
os.system('sudo reboot')
179189

180-
181-
def _dc_enc_motor(self, speed_left=100, speed_right=100, elapse=-1):
190+
def _dc_enc_motor(self, speed_left=100, speed_right=100, elapse=0):
182191
self._twin_motors_enc.control_time(power_left=speed_left, power_right=speed_right, time_elapse=elapse)

rotary_encoder/motorencoder.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ class MotorEncoder:
1515
concurrency problems on GPIO READ/WRITE """
1616

1717
# default constructor
18-
def __init__(self, pi, enable_pin, forward_pin, backward_pin, encoder_feedback_pin):
18+
def __init__(self, pi, enable_pin, forward_pin, backward_pin, feedback_pin_A, feedback_pin_B):
1919
# setting pin variables
2020
self._pi = pi
2121
self._enable_pin = enable_pin
2222
self._forward_pin = forward_pin
2323
self._backward_pin = backward_pin
24-
self._encoder_feedback_pin = encoder_feedback_pin
24+
self._feedback_pin_A = feedback_pin_A
25+
self._feedback_pin_B = feedback_pin_B
2526

2627
# setting movement variables
2728
self._direction = 0
@@ -33,7 +34,7 @@ def __init__(self, pi, enable_pin, forward_pin, backward_pin, encoder_feedback_p
3334

3435
# other
3536
self._motor_lock = threading.RLock()
36-
self._rotary_decoder = RotaryDecoder(pi, encoder_feedback_pin, self.rotary_callback)
37+
self._rotary_decoder = RotaryDecoder(pi, feedback_pin_A, feedback_pin_B, self.rotary_callback)
3738

3839
# GETTERS
3940
# ticks

rotary_encoder/wheelsaxel.py

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class WheelsAxel:
1414
that makes one wheel go slower than the other """
1515

1616
def __init__(self, pi, enable_pin,
17-
left_forward_pin, left_backward_pin, left_encoder_feedback_pin,
18-
right_forward_pin, right_backward_pin, right_encoder_feedback_pin):
17+
left_forward_pin, left_backward_pin, left_encoder_feedback_pin_A, left_encoder_feedback_pin_B,
18+
right_forward_pin, right_backward_pin, right_encoder_feedback_pin_A, right_encoder_feedback_pin_B):
1919

2020
# state variables
2121
self._is_moving = False
@@ -25,13 +25,15 @@ def __init__(self, pi, enable_pin,
2525
enable_pin,
2626
left_forward_pin,
2727
left_backward_pin,
28-
left_encoder_feedback_pin)
28+
left_encoder_feedback_pin_A,
29+
left_encoder_feedback_pin_B)
2930
# right motor
3031
self._right_motor = MotorEncoder(pi,
3132
enable_pin,
3233
right_forward_pin,
3334
right_backward_pin,
34-
right_encoder_feedback_pin)
35+
right_encoder_feedback_pin_B,
36+
right_encoder_feedback_pin_A)
3537

3638
# other
3739
self._wheelsAxle_lock = threading.Condition() # race condition lock
@@ -55,16 +57,16 @@ def speed(self):
5557
""" Motor time control allows the motors
5658
to run for a certain amount of time """
5759
def control_time(self, power_left=100, power_right=100, time_elapse=0):
58-
self._wheelsAxle_lock.acquire() # wheelsAxle lock acquire
60+
self._wheelsAxle_lock.acquire() # wheelsAxle lock acquire
5961

60-
# applying tension to motors
61-
self._left_motor.control(power_left)
62-
self._right_motor.control(power_right)
63-
self._is_moving = True
62+
# applying tension to motors
63+
self._left_motor.control(power_left)
64+
self._right_motor.control(power_right)
65+
self._is_moving = True
6466

65-
# moving for desired time
66-
sleep(time_elapse)
67-
self.stop()
67+
# moving for desired time
68+
sleep(time_elapse)
69+
self.stop()
6870

6971
""" Motor distance control allows the motors
7072
to run for a certain amount of distance (mm) """
@@ -85,11 +87,6 @@ def control_distance(self, power_left=100, power_right=100, target_distance=0):
8587
# robot arrived
8688
self.stop()
8789

88-
""" Motor speed control allows the motors
89-
to run at a certaing amount of speed (mm/s) """
90-
def control_speed(self):
91-
pass
92-
9390
# old control distance, trying to adjust power
9491
# def control_distance(self, power_left = 100, power_right = 100, target_distance = 0):
9592
# self._is_moving = True
@@ -120,8 +117,8 @@ def control_speed(self):
120117
# self.stop()
121118

122119
""" The stop function calls the two stop functions of the two
123-
correspondent motors
124-
locks are automatically obtained """
120+
correspondent motors.
121+
Locks are automatically obtained """
125122
def stop(self):
126123
self._left_motor.stop()
127124
self._right_motor.stop()
@@ -130,8 +127,6 @@ def stop(self):
130127
self._wheelsAxle_lock.release()
131128

132129
# CALLBACK
133-
134-
135130
def cancel_callback(self):
136131
self._right_motor.cancel_callback()
137132
self._left_motor.cancel_callback()

0 commit comments

Comments
 (0)