1717# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1818############################################################################
1919
20- import os
21- import sys
2220import time
2321from math import copysign
2422import logging
@@ -102,11 +100,12 @@ class CoderBot(object):
102100 # pylint: disable=too-many-instance-attributes
103101
104102 def __init__ (self , motor_trim_factor = 1.0 , motor_min_power = 0 , motor_max_power = 100 , hw_version = "5" , pid_params = (0.8 , 0.1 , 0.01 , 200 , 0.01 )):
103+ self ._mpu = None
105104 try :
106105 self ._mpu = mpu .AccelGyroMag ()
107106 logging .info ("MPU available" )
108- except :
109- logging .info ("MPU not available" )
107+ except Exception :
108+ logging .warning ("MPU not available" )
110109
111110 self .GPIOS = HW_VERSIONS .get (hw_version , GPIO_CODERBOT_V_5 ())
112111 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_1 , self .GPIOS .PIN_SERVO_2 ]
@@ -178,11 +177,20 @@ def turn(self, speed=100, elapse=None, distance=None):
178177 self .motor_control (speed_left = speed_left , speed_right = speed_right , time_elapse = elapse , target_distance = distance )
179178
180179 def turn_angle (self , speed = 100 , angle = 0 ):
181- z = self ._mpu .get_gyro ()[2 ]
180+ if self ._mpu is None :
181+ logging .warning ("MPU not available, cannot turn by angle" )
182+ return
183+ accumulated = 0.0
182184 self .turn (speed , elapse = 0 )
183- while abs (z - self ._mpu .get_gyro ()[2 ]) < angle :
185+ t = time .time ()
186+ while abs (accumulated ) < angle :
187+ now = time .time ()
188+ dt = now - t
189+ t = now
190+ gyro_z = self ._mpu .get_gyro ()[2 ]
191+ accumulated += gyro_z * dt
192+ logging .info ("turn_angle: accumulated=%.2f target=%s" , accumulated , angle )
184193 time .sleep (0.05 )
185- logging .info (self ._mpu .get_gyro ()[2 ])
186194 self .stop ()
187195
188196 def forward (self , speed = 100 , elapse = None , distance = None ):
@@ -204,24 +212,32 @@ def get_sonar_distance(self, sonar_id=0):
204212 return self .sonar [sonar_id ].get_distance ()
205213
206214 def get_mpu_accel (self , axis = None ):
215+ if self ._mpu is None :
216+ return None
207217 acc = self ._mpu .get_acc ()
208218 if axis is None :
209219 return acc
210220 else :
211221 return int (acc [axis ]* 100.0 )/ 100.0
212222
213223 def get_mpu_gyro (self , axis = None ):
224+ if self ._mpu is None :
225+ return None
214226 gyro = self ._mpu .get_gyro ()
215227 if axis is None :
216228 return gyro
217229 else :
218- return int (gyro [axis ]* 100.0 )/ 200 .0
230+ return int (gyro [axis ]* 100.0 )/ 100 .0
219231
220232 def get_mpu_heading (self ):
233+ if self ._mpu is None :
234+ return None
221235 hdg = self ._mpu .get_hdg ()
222236 return int (hdg )
223237
224238 def get_mpu_temp (self ):
239+ if self ._mpu is None :
240+ return None
225241 temp = self ._mpu .get_temp ()
226242 return int (temp * 100.0 )/ 100.0
227243
@@ -242,7 +258,7 @@ def stop(self):
242258 self ._twin_motors_enc .stop ()
243259
244260 def is_moving (self ):
245- return self ._twin_motors_enc ._is_moving
261+ return self ._twin_motors_enc .is_moving ()
246262
247263 # Distance travelled getter
248264 def distance (self ):
@@ -254,7 +270,7 @@ def speed(self):
254270
255271 # CoderBot direction getter
256272 def direction (self ):
257- return self ._twin_motors_enc .speed ()
273+ return self ._twin_motors_enc .direction ()
258274
259275 def set_callback (self , gpio , callback , elapse ):
260276 self ._cb_elapse [gpio ] = elapse * 1000
0 commit comments