2626from rotary_encoder .wheelsaxel import WheelsAxel
2727
2828# GPIO
29- # motors
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
35- #?
36- PIN_PUSHBUTTON = 16 #11
37- # servo
38- PIN_SERVO_3 = 7 #9
39- PIN_SERVO_4 = 1 #10
40- # sonar
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-
50- # encoder
51- PIN_ENCODER_LEFT_A = 14
52- PIN_ENCODER_LEFT_B = 15 #6
53- PIN_ENCODER_RIGHT_A = 24 #15
54- PIN_ENCODER_RIGHT_B = 25 #12
29+ class GPIO_CODERBOT_V_4 ():
30+ # motors
31+ PIN_MOTOR_ENABLE = 22
32+ PIN_LEFT_FORWARD = 25
33+ PIN_LEFT_BACKWARD = 24
34+ PIN_RIGHT_FORWARD = 4
35+ PIN_RIGHT_BACKWARD = 17
36+
37+ PIN_PUSHBUTTON = 11
38+ # servo
39+ PIN_SERVO_3 = 9
40+ PIN_SERVO_4 = 10
41+ # sonar
42+ PIN_SONAR_1_TRIGGER = 18
43+ PIN_SONAR_1_ECHO = 7
44+ PIN_SONAR_2_TRIGGER = 18
45+ PIN_SONAR_2_ECHO = 8
46+ PIN_SONAR_3_TRIGGER = 18
47+ PIN_SONAR_3_ECHO = 23
48+
49+ # encoder
50+ PIN_ENCODER_LEFT_A = 14
51+ PIN_ENCODER_LEFT_B = 6
52+ PIN_ENCODER_RIGHT_A = 15
53+ PIN_ENCODER_RIGHT_B = 12
54+
55+ class GPIO_CODERBOT_V_5 ():
56+ # motors
57+ PIN_MOTOR_ENABLE = None #22
58+ PIN_LEFT_FORWARD = 17 #25
59+ PIN_LEFT_BACKWARD = 18 # 24
60+ PIN_RIGHT_FORWARD = 22 # 4
61+ PIN_RIGHT_BACKWARD = 23 #17
62+
63+ PIN_PUSHBUTTON = 16 #11
64+ # servo
65+ PIN_SERVO_3 = 7 #9
66+ PIN_SERVO_4 = 1 #10
67+ # sonar
68+ PIN_SONAR_1_TRIGGER = 5 #18
69+ PIN_SONAR_1_ECHO = 27 #7
70+ PIN_SONAR_2_TRIGGER = 5 #18
71+ PIN_SONAR_2_ECHO = 6 #8
72+ PIN_SONAR_3_TRIGGER = 5 #18
73+ PIN_SONAR_3_ECHO = 12 #23
74+ PIN_SONAR_4_TRIGGER = 5 #18
75+ PIN_SONAR_4_ECHO = 13 #23
76+
77+ # encoder
78+ PIN_ENCODER_LEFT_A = 14
79+ PIN_ENCODER_LEFT_B = 15 #6
80+ PIN_ENCODER_RIGHT_A = 24 #15
81+ PIN_ENCODER_RIGHT_B = 25 #12
5582
5683# PWM
5784PWM_FREQUENCY = 100 #Hz
@@ -61,44 +88,47 @@ class CoderBot(object):
6188
6289 # pylint: disable=too-many-instance-attributes
6390
64- _pin_out = [PIN_LEFT_FORWARD , PIN_RIGHT_FORWARD , PIN_LEFT_BACKWARD , PIN_RIGHT_BACKWARD , PIN_SERVO_3 , PIN_SERVO_4 ]
65-
6691 def __init__ (self , motor_trim_factor = 1.0 , encoder = True ):
92+ try :
93+ self ._mpu = mpu .AccelGyroMag ()
94+ self .GPIOS = GPIO_CODERBOT_V_5 ()
95+ logging .info ("MPU available" )
96+ except :
97+ logging .info ("MPU not available" )
98+ self .GPIOS = GPIO_CODERBOT_V_4 ()
99+
100+ self ._pin_out = [self .GPIOS .PIN_LEFT_FORWARD , self .GPIOS .PIN_RIGHT_FORWARD , self .GPIOS .PIN_LEFT_BACKWARD , self .GPIOS .PIN_RIGHT_BACKWARD , self .GPIOS .PIN_SERVO_3 , self .GPIOS .PIN_SERVO_4 ]
67101 self .pi = pigpio .pi ('localhost' )
68- self .pi .set_mode (PIN_PUSHBUTTON , pigpio .INPUT )
102+ self .pi .set_mode (self . GPIOS . PIN_PUSHBUTTON , pigpio .INPUT )
69103 self ._cb = dict ()
70104 self ._cb_last_tick = dict ()
71105 self ._cb_elapse = dict ()
72106 self ._encoder = encoder
73107 self ._motor_trim_factor = motor_trim_factor
74108 self ._twin_motors_enc = WheelsAxel (
75109 self .pi ,
76- enable_pin = PIN_MOTOR_ENABLE ,
77- left_forward_pin = PIN_LEFT_FORWARD ,
78- left_backward_pin = PIN_LEFT_BACKWARD ,
79- left_encoder_feedback_pin_A = PIN_ENCODER_LEFT_A ,
80- left_encoder_feedback_pin_B = PIN_ENCODER_LEFT_B ,
81- right_forward_pin = PIN_RIGHT_FORWARD ,
82- right_backward_pin = PIN_RIGHT_BACKWARD ,
83- right_encoder_feedback_pin_A = PIN_ENCODER_RIGHT_A ,
84- right_encoder_feedback_pin_B = PIN_ENCODER_RIGHT_B )
110+ enable_pin = self . GPIOS . PIN_MOTOR_ENABLE ,
111+ left_forward_pin = self . GPIOS . PIN_LEFT_FORWARD ,
112+ left_backward_pin = self . GPIOS . PIN_LEFT_BACKWARD ,
113+ left_encoder_feedback_pin_A = self . GPIOS . PIN_ENCODER_LEFT_A ,
114+ left_encoder_feedback_pin_B = self . GPIOS . PIN_ENCODER_LEFT_B ,
115+ right_forward_pin = self . GPIOS . PIN_RIGHT_FORWARD ,
116+ right_backward_pin = self . GPIOS . PIN_RIGHT_BACKWARD ,
117+ right_encoder_feedback_pin_A = self . GPIOS . PIN_ENCODER_RIGHT_A ,
118+ right_encoder_feedback_pin_B = self . GPIOS . PIN_ENCODER_RIGHT_B )
85119 self .motor_control = self ._dc_enc_motor
86120
87- self ._cb1 = self .pi .callback (PIN_PUSHBUTTON , pigpio .EITHER_EDGE , self ._cb_button )
121+ self ._cb1 = self .pi .callback (self . GPIOS . PIN_PUSHBUTTON , pigpio .EITHER_EDGE , self ._cb_button )
88122
89123 for pin in self ._pin_out :
90124 self .pi .set_PWM_frequency (pin , PWM_FREQUENCY )
91125 self .pi .set_PWM_range (pin , PWM_RANGE )
92126
93- self .sonar = [sonar .Sonar (self .pi , PIN_SONAR_1_TRIGGER , PIN_SONAR_1_ECHO ),
94- sonar .Sonar (self .pi , PIN_SONAR_2_TRIGGER , PIN_SONAR_2_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 )]
127+ self .sonar = [sonar .Sonar (self .pi , self . GPIOS . PIN_SONAR_1_TRIGGER , self . GPIOS . PIN_SONAR_1_ECHO ),
128+ sonar .Sonar (self .pi , self . GPIOS . PIN_SONAR_2_TRIGGER , self . GPIOS . PIN_SONAR_2_ECHO ),
129+ sonar .Sonar (self .pi , self . GPIOS . PIN_SONAR_3_TRIGGER , self . GPIOS . PIN_SONAR_3_ECHO ),
130+ sonar .Sonar (self .pi , self . GPIOS . PIN_SONAR_4_TRIGGER , self . GPIOS . PIN_SONAR_4_ECHO )]
97131
98- try :
99- self ._mpu = mpu .AccelGyroMag ()
100- except :
101- logging .info ("MPU not available" )
102132
103133 #self.stop()
104134 self ._is_moving = False
@@ -163,22 +193,22 @@ def get_mpu_accel(self, axis=None):
163193 if axis is None :
164194 return acc
165195 else :
166- return acc [axis ]
196+ return int ( acc [axis ]* 100.0 ) / 100.0
167197
168198 def get_mpu_gyro (self , axis = None ):
169199 gyro = self ._mpu .get_gyro ()
170200 if axis is None :
171201 return gyro
172202 else :
173- return gyro [axis ]
203+ return int ( gyro [axis ]* 100.0 ) / 200.0
174204
175205 def get_mpu_heading (self ):
176206 hdg = self ._mpu .get_hdg ()
177- return hdg
207+ return int ( hdg )
178208
179209 def get_mpu_temp (self ):
180210 temp = self ._mpu .get_temp ()
181- return temp
211+ return int ( temp * 100.0 ) / 100.0
182212
183213 def _servo_control (self , pin , angle ):
184214 duty = ((angle + 90 ) * 100 / 180 ) + 25
0 commit comments