def __init__(self, ultra_sonic_sensor_timeout): if type(self) == BaseEngine: # Prevent direct instatiation of this class raise NotImplementedError # Length in time of each move command in seconds self.ultra_sonic_sensor_timeout = ultra_sonic_sensor_timeout if os.environ.get('country_bot_env') != "TESTING": GPIO.setwarnings(False) global _engine_initialized if _engine_initialized == False: self.initialize_engine()
def initialize_engine(self): """ These RPi initialization tasks only occur once """ # Set up the Raspberry Pi Board configuration GPIO.setmode(GPIO.BOARD) # Set up ultrasonic sensor pins GPIO.setup(self.TRIG_PIN, GPIO.OUT) GPIO.setup(self.ECHO_PIN, GPIO.IN) # Set up and reset motor pins for pin in self.MOTOR_PINS: GPIO.setup(pin, GPIO.OUT) global _engine_initialized _engine_initialized = True
def _move_straight_and_turn(self, turn_pin): """ Turn car in direction of pin specified """ if not turn_pin in self.MOTOR_PINS: raise ValueError # Turn the steering GPIO.output(turn_pin, True) time.sleep(self.BRAKE_AND_TURN_PAUSE_TIME) # Move forward GPIO.output(self.MOTOR_STRAIGHT_PIN, True) time.sleep(self.MOVE_DURATION) # Stop car self._apply_brakes() GPIO.output(turn_pin, False)
def get_us_distance(self): """ Measures the distance using the ultrasonic sensor. Returns a distance in cm. Returns `math.inf` if distance cannot be measured """ try: # Send off trigger, and wait for sensor to settle GPIO.output(self.TRIG_PIN, False) time.sleep(self.ULTRASONIC_SENSOR_SETTLE_TIME) # Create the pulse for a short amount of time GPIO.output(self.TRIG_PIN, True) time.sleep(0.00001) GPIO.output(self.TRIG_PIN, False) pulse_start_time, pusle_end_time = time.time(), time.time() # grab start time cycles = 0 while GPIO.input(self.ECHO_PIN) == 0: pulse_start_time = time.time() cycles += 1 if cycles > self.ultra_sonic_sensor_timeout: return math.inf cycles = 0 while GPIO.input(self.ECHO_PIN) == 1: pusle_end_time = time.time() cycles += 1 if cycles > self.ultra_sonic_sensor_timeout: return math.inf duration = pusle_end_time - pulse_start_time return duration * self.SPEED_OF_SOUND / 2 except: # Error occured, return infinity for distance return math.inf
def turn_wheels_forward(self): """ Straighten the wheels """ GPIO.output(self.MOTOR_LEFT_PIN, False) GPIO.output(self.MOTOR_RIGHT_PIN, False)
def turn_wheels_right(self): """ Turn wheels to the right, removing pin output to 'left' wheels """ GPIO.output(self.MOTOR_LEFT_PIN, False) GPIO.output(self.MOTOR_RIGHT_PIN, True)
def __init__(self, ultra_sonic_sensor_timeout=500000): super().__init__(ultra_sonic_sensor_timeout=ultra_sonic_sensor_timeout) # Set up PWM pin for slower forward motion self.pwm = GPIO.PWM(self.MOTOR_STRAIGHT_PIN, self.PWM_FREQUENCY)
def go_straight(self): """ Moves the car forward for MOVE_DURATION, applying motion to given pin """ GPIO.output(self.MOTOR_STRAIGHT_PIN, True) time.sleep(self.MOVE_DURATION) GPIO.output(self.MOTOR_STRAIGHT_PIN, False) self._apply_brakes()
def _apply_brakes(self): """ Momentarily applies brakes to prevent car from rolling """ GPIO.output(self.MOTOR_STRAIGHT_PIN, False) GPIO.output(self.MOTOR_REVERSE_PIN, True) time.sleep(self.BRAKE_AND_TURN_PAUSE_TIME) GPIO.output(self.MOTOR_REVERSE_PIN, False)