2727
2828# GPIO
2929# motors
30- PIN_MOTOR_ENABLE = 22
31- PIN_LEFT_FORWARD = 25
32- PIN_LEFT_BACKWARD = 24
33- PIN_RIGHT_FORWARD = 4
34- PIN_RIGHT_BACKWARD = 17
30+ PIN_MOTOR_ENABLE = None # 22
31+ PIN_LEFT_FORWARD = 17 # 25
32+ PIN_LEFT_BACKWARD = 18 # 24
33+ PIN_RIGHT_FORWARD = 22 # 4
34+ PIN_RIGHT_BACKWARD = 23 # 17
3535#?
36- PIN_PUSHBUTTON = 11
36+ PIN_PUSHBUTTON = 16 # 11
3737# servo
38- PIN_SERVO_3 = 9
39- PIN_SERVO_4 = 10
38+ PIN_SERVO_3 = 7 # 9
39+ PIN_SERVO_4 = 1 # 10
4040# sonar
41- PIN_SONAR_1_TRIGGER = 18
42- PIN_SONAR_1_ECHO = 7
43- PIN_SONAR_2_TRIGGER = 18
44- PIN_SONAR_2_ECHO = 8
45- PIN_SONAR_3_TRIGGER = 18
46- PIN_SONAR_3_ECHO = 23
41+ PIN_SONAR_1_TRIGGER = 5 #18
42+ PIN_SONAR_1_ECHO = 27 #7
43+ PIN_SONAR_2_TRIGGER = 5 #18
44+ PIN_SONAR_2_ECHO = 6 #8
45+ PIN_SONAR_3_TRIGGER = 5 #18
46+ PIN_SONAR_3_ECHO = 12 #23
47+ PIN_SONAR_4_TRIGGER = 5 #18
48+ PIN_SONAR_4_ECHO = 13 #23
49+
4750# encoder
4851PIN_ENCODER_LEFT_A = 14
49- PIN_ENCODER_LEFT_B = 6
50- PIN_ENCODER_RIGHT_A = 15
51- PIN_ENCODER_RIGHT_B = 12
52+ PIN_ENCODER_LEFT_B = 15 # 6
53+ PIN_ENCODER_RIGHT_A = 24 # 15
54+ PIN_ENCODER_RIGHT_B = 25 # 12
5255
5356# PWM
5457PWM_FREQUENCY = 100 #Hz
@@ -58,7 +61,7 @@ class CoderBot(object):
5861
5962 # pylint: disable=too-many-instance-attributes
6063
61- _pin_out = [PIN_MOTOR_ENABLE , PIN_LEFT_FORWARD , PIN_RIGHT_FORWARD , PIN_LEFT_BACKWARD , PIN_RIGHT_BACKWARD , PIN_SERVO_3 , PIN_SERVO_4 ]
64+ _pin_out = [PIN_LEFT_FORWARD , PIN_RIGHT_FORWARD , PIN_LEFT_BACKWARD , PIN_RIGHT_BACKWARD , PIN_SERVO_3 , PIN_SERVO_4 ]
6265
6366 def __init__ (self , motor_trim_factor = 1.0 , encoder = True ):
6467 self .pi = pigpio .pi ('localhost' )
@@ -89,7 +92,8 @@ def __init__(self, motor_trim_factor=1.0, encoder=True):
8992
9093 self .sonar = [sonar .Sonar (self .pi , PIN_SONAR_1_TRIGGER , PIN_SONAR_1_ECHO ),
9194 sonar .Sonar (self .pi , PIN_SONAR_2_TRIGGER , PIN_SONAR_2_ECHO ),
92- sonar .Sonar (self .pi , PIN_SONAR_3_TRIGGER , PIN_SONAR_3_ECHO )]
95+ sonar .Sonar (self .pi , PIN_SONAR_3_TRIGGER , PIN_SONAR_3_ECHO ),
96+ sonar .Sonar (self .pi , PIN_SONAR_4_TRIGGER , PIN_SONAR_4_ECHO )]
9397
9498 try :
9599 self ._ag = mpu .AccelGyro ()
@@ -126,11 +130,11 @@ def turn(self, speed=100, elapse=0, steps=-1):
126130 self .motor_control (speed_left = speed_left , speed_right = speed_right , time_elapse = elapse )
127131
128132 def turn_angle (self , speed = 100 , angle = 0 ):
129- z = self ._ag .get_gyro_data ()['z' ]
133+ z = self ._ag .get_gyro ()[2 ]
130134 self .turn (speed , elapse = 0 )
131- while abs (z - self ._ag .get_gyro_data ()['z' ]) < angle :
135+ while abs (z - self ._ag .get_gyro ()[2 ]) < angle :
132136 time .sleep (0.05 )
133- logging .info (self ._ag .get_gyro_data ()['z' ])
137+ logging .info (self ._ag .get_gyro ()[2 ])
134138 self .stop ()
135139
136140 def forward (self , speed = 100 , elapse = 0 , distance = 0 ):
@@ -145,9 +149,22 @@ def left(self, speed=100, elapse=0):
145149 def right (self , speed = 100 , elapse = 0 ):
146150 self .turn (speed = speed , elapse = elapse )
147151
152+ def servo3 (self , angle ):
153+ self ._servo_control (PIN_SERVO_3 , angle )
154+
155+ def servo4 (self , angle ):
156+ self ._servo_control (PIN_SERVO_4 , angle )
157+
148158 def get_sonar_distance (self , sonar_id = 0 ):
149159 return self .sonar [sonar_id ].get_distance ()
150160
161+ def _servo_control (self , pin , angle ):
162+ duty = ((angle + 90 ) * 100 / 180 ) + 25
163+
164+ self .pi .set_PWM_range (pin , 1000 )
165+ self .pi .set_PWM_frequency (pin , 50 )
166+ self .pi .set_PWM_dutycycle (pin , duty )
167+
151168 def stop (self ):
152169 if self ._encoder :
153170 self ._twin_motors_enc .stop ()
0 commit comments