class Robot(object): wheel_diameter_mm = 69.0 ticks_per_revolution = 40.0 wheel_distance_mm = 140.0 def __init__(self, motorhat_addr=0x6f, drive_enabled=True): # Setup the motorhat with the passed in address self._mh = Raspi_MotorHAT(addr=motorhat_addr) # get local variable for each motor self.left_motor = self._mh.getMotor(1) self.right_motor = self._mh.getMotor(2) self.drive_enabled = drive_enabled # ensure the motors get stopped when the code exits atexit.register(self.stop_all) # Setup the line sensors self.left_line_sensor = LineSensor(23, queue_len=3, pull_up=True) self.right_line_sensor = LineSensor(16, queue_len=3, pull_up=True) # Setup The Distance Sensors self.left_distance_sensor = DistanceSensor(17, 27) self.right_distance_sensor = DistanceSensor(5, 6) # Setup the Encoders EncoderCounter.set_constants(self.wheel_diameter_mm, self.ticks_per_revolution) self.left_encoder = EncoderCounter(4) self.right_encoder = EncoderCounter(26) # Setup the Leds self.leds = leds_8_apa102c.Leds() # Set up servo motors for pan and tilt. self.servos = Servos(addr=motorhat_addr) def stop_all(self): self.stop_motors() # Clear any sensor handlers self.left_line_sensor.when_line = None self.left_line_sensor.when_no_line = None self.right_line_sensor.when_line = None self.right_line_sensor.when_no_line = None self.left_encoder.stop() self.right_encoder.stop() # Clear the display self.leds.clear() self.leds.show() # Reset the servos self.servos.stop_all() def convert_speed(self, speed): mode = Raspi_MotorHAT.RELEASE if speed > 0: mode = Raspi_MotorHAT.FORWARD elif speed < 0: mode = Raspi_MotorHAT.BACKWARD output_speed = (abs(speed) * 255) / 100 return mode, output_speed def set_left(self, speed): if not self.drive_enabled: return mode, output_speed = self.convert_speed(speed) self.left_motor.setSpeed(output_speed) self.left_motor.run(mode) def set_right(self, speed): if not self.drive_enabled: return mode, output_speed = self.convert_speed(speed) self.right_motor.setSpeed(output_speed) self.right_motor.run(mode) def stop_motors(self): self.left_motor.run(Raspi_MotorHAT.RELEASE) self.right_motor.run(Raspi_MotorHAT.RELEASE) def set_pan(self, angle): self.servos.set_servo_angle(1, angle) def set_tilt(self, angle): self.servos.set_servo_angle(0, angle)
class Robot: wheel_diameter_mm = 70.0 ticks_per_revolution = 40.0 wheel_distance_mm = 132.0 def __init__(self, motorhat_addr=0x6f): # Setup the motorhat with the passed in address self._mh = Raspi_MotorHAT(addr=motorhat_addr) self.left_motor = self._mh.getMotor(1) self.right_motor = self._mh.getMotor(2) # Setup the Leds self.leds = leds_led_shim.Leds() # Set up servo motors for pan and tilt. self.servos = Servos(addr=motorhat_addr, deflect_90_in_ms=1) # Setup The Distance Sensors self.left_distance_sensor = DistanceSensor(echo=17, trigger=27, queue_len=2) self.right_distance_sensor = DistanceSensor(echo=5, trigger=6, queue_len=2) # Setup the Encoders EncoderCounter.set_constants(self.wheel_diameter_mm, self.ticks_per_revolution) self.left_encoder = EncoderCounter(4) self.right_encoder = EncoderCounter(26) # ensure the motors get stopped when the code exits atexit.register(self.stop_all) def convert_speed(self, speed): # Choose the running mode mode = Raspi_MotorHAT.RELEASE if speed > 0: mode = Raspi_MotorHAT.FORWARD elif speed < 0: mode = Raspi_MotorHAT.BACKWARD # Scale the speed output_speed = (abs(speed) * 255) // 100 return mode, int(output_speed) def set_left(self, speed): mode, output_speed = self.convert_speed(speed) self.left_motor.setSpeed(output_speed) self.left_motor.run(mode) def set_right(self, speed): mode, output_speed = self.convert_speed(speed) self.right_motor.setSpeed(output_speed) self.right_motor.run(mode) def stop_motors(self): self.left_motor.run(Raspi_MotorHAT.RELEASE) self.right_motor.run(Raspi_MotorHAT.RELEASE) def stop_all(self): self.stop_motors() # Clear the display self.leds.clear() self.leds.show() # Clear any sensor handlers self.left_encoder.stop() self.right_encoder.stop() # Reset the servos self.servos.stop_all() def set_pan(self, angle): self.servos.set_servo_angle(1, angle) def set_tilt(self, angle): self.servos.set_servo_angle(0, angle)
class Robot: def __init__(self, motorhat_addr=0x6f): # Setup the motorhat with the passed in address self._mh = Raspi_MotorHAT(addr=motorhat_addr) # get local variable for each motor self.left_motor = self._mh.getMotor(1) self.right_motor = self._mh.getMotor(2) # Setup The Distance Sensors self.left_distance_sensor = DistanceSensor(echo=17, trigger=27, queue_len=2) self.right_distance_sensor = DistanceSensor(echo=5, trigger=6, queue_len=2) # Setup the Leds self.leds = leds_led_shim.Leds() # Set up servo motors for pan and tilt. self.servos = Servos(addr=motorhat_addr) # ensure the motors get stopped when the code exits atexit.register(self.stop_all) def convert_speed(self, speed): # Choose the running mode mode = Raspi_MotorHAT.RELEASE if speed > 0: mode = Raspi_MotorHAT.FORWARD elif speed < 0: mode = Raspi_MotorHAT.BACKWARD # Scale the speed output_speed = (abs(speed) * 255) // 100 return mode, int(output_speed) def set_left(self, speed): mode, output_speed = self.convert_speed(speed) self.left_motor.setSpeed(output_speed) self.left_motor.run(mode) def set_right(self, speed): mode, output_speed = self.convert_speed(speed) self.right_motor.setSpeed(output_speed) self.right_motor.run(mode) def stop_motors(self): self.left_motor.run(Raspi_MotorHAT.RELEASE) self.right_motor.run(Raspi_MotorHAT.RELEASE) def stop_all(self): self.stop_motors() # Clear the display self.leds.clear() self.leds.show() # Reset the servos self.servos.stop_all() def set_pan(self, angle): self.servos.set_servo_angle(1, angle) def set_tilt(self, angle): self.servos.set_servo_angle(0, angle)
class Robot(object): def __init__(self, motorhat_addr=0x6f): self._mh = Raspi_MotorHAT(addr=motorhat_addr) self.left_motor = self._mh.getMotor(1) self.right_motor = self._mh.getMotor(2) atexit.register(self.stop_motors) # Setup the line sensors self.right_line_sensor = LineSensor(23, queue_len=3, pull_up=True) self.left_line_sensor = LineSensor(16, queue_len=3, pull_up=True) self.right_line_sensor_stuck = "" self.left_line_sensor_stuck = "" self.servos = Servos(addr=motorhat_addr) def stop_motors(self): self.left_motor.run(Raspi_MotorHAT.RELEASE) self.right_motor.run(Raspi_MotorHAT.RELEASE) def stop_all(self): self.stop_motors() self.servos.stop_all() # Clear sensor handlers self.left_line_sensor.when_line = None self.left_line_sensor.when_no_line = None self.right_line_sensor.when_line = None self.right_line_sensor.when_line = None def convert_speed(self, speed): #Choose the running mode mode = Raspi_MotorHAT.RELEASE if speed > 0: mode = Raspi_MotorHAT.FORWARD elif speed < 0: mode = Raspi_MotorHAT.BACKWARD output_speed = (abs(speed) * 255) / 100 return mode, int(output_speed) def set_left(self, speed): mode, output_speed = self.convert_speed(speed) self.left_motor.setSpeed(output_speed) self.left_motor.run(mode) def set_right(self, speed): mode, output_speed = self.convert_speed(speed) self.right_motor.setSpeed(output_speed) self.right_motor.run(mode) def set_left_line_sensor_stuck(self, stuck): self.left_line_sensor_stuck = stuck def set_right_line_sensor_stuck(self, stuck): self.right_line_sensor_stuck = stuck def set_pan(self, angle): self.servos.set_servo_angle(14, angle) def set_tilt(self, angle): self.servos.set_servo_angle(15, angle)
class Robot(object): def __init__(self, motorhat_addr=0x6f): # set up motorhat with address parameter self._mh = Raspi_MotorHAT(addr=motorhat_addr) # get local variable for each motor self.left_motor_rear = self._mh.getMotor(1) self.left_motor_front = self._mh.getMotor(2) self.right_motor_rear = self._mh.getMotor(4) self.right_motor_front = self._mh.getMotor(3) # make sure motors stop when code exits atexit.register(self.stop_motors) # Set up servo motors for pan and tilt self.servos = Servos(addr=motorhat_addr) # convert speed from 0-100 to robot speeds def convert_speed(self, speed): # choose the running mode mode = Raspi_MotorHAT.RELEASE if speed > 0: mode = Raspi_MotorHAT.FORWARD elif speed < 0: mode = Raspi_MotorHAT.BACKWARD #Scale the speed output_speed = (abs(speed) * 255) / 100 return mode, output_speed # release motors def stop_motors(self): self.left_motor_rear.run(Raspi_MotorHAT.RELEASE) self.right_motor_rear.run(Raspi_MotorHAT.RELEASE) self.left_motor_front.run(Raspi_MotorHAT.RELEASE) self.right_motor_front.run(Raspi_MotorHAT.RELEASE) # sets speeds of left wheels def set_left(self, speed): mode, output_speed = self.convert_speed(speed) self.left_motor_rear.setSpeed(output_speed) self.left_motor_front.setSpeed(output_speed) self.left_motor_rear.run(mode) self.left_motor_front.run(mode) # sets speeds of right wheels def set_right(self, speed): mode, output_speed = self.convert_speed(speed) self.right_motor_rear.setSpeed(output_speed) self.right_motor_front.setSpeed(output_speed) self.right_motor_rear.run(mode) self.right_motor_front.run(mode) def move(self, speed): self.set_left(speed) self.set_right(speed) def set_pan(self, angle): self.servos.set_servo_angle(1, angle) def set_tilt(self, angle): self.servos.set_servo_angle(0, angle) def stop_all(self): self.stop_motors() # Reset the servos self.servos.stop_all()
class Robot(object): def __init__(self, motorhat_addr=0x6f): # Setup the motorhat with the passed in address self._mh = Raspi_MotorHAT(addr=motorhat_addr) # get local variable for each motor self.left_motor = self._mh.getMotor(1) self.right_motor = self._mh.getMotor(2) # ensure the motors get stopped when the code exits atexit.register(self.stop_all) # Setup the line sensors self.left_line_sensor = LineSensor(23, queue_len=3, pull_up=True) self.right_line_sensor = LineSensor(16, queue_len=3, pull_up=True) # Setup the Leds self.leds = leds_8_apa102c.Leds() # Set up servo motors for pan and tilt. self.servos = Servos(addr=motorhat_addr) def stop_all(self): self.stop_motors() # Clear any sensor handlers self.left_line_sensor.when_line = None self.left_line_sensor.when_no_line = None self.right_line_sensor.when_line = None self.right_line_sensor.when_no_line = None # Clear the display self.leds.clear() self.leds.show() # Reset the servos self.servos.stop_all() def convert_speed(self, speed): mode = Raspi_MotorHAT.RELEASE if speed > 0: mode = Raspi_MotorHAT.FORWARD elif speed < 0: mode = Raspi_MotorHAT.BACKWARD output_speed = (abs(speed) * 255) / 100 return mode, int(output_speed) def set_left(self, speed): mode, output_speed = self.convert_speed(speed) self.left_motor.setSpeed(output_speed) self.left_motor.run(mode) def set_right(self, speed): mode, output_speed = self.convert_speed(speed) self.right_motor.setSpeed(output_speed) self.right_motor.run(mode) def stop_motors(self): self.left_motor.run(Raspi_MotorHAT.RELEASE) self.right_motor.run(Raspi_MotorHAT.RELEASE) def set_pan(self, angle): self.servos.set_servo_angle(1, angle) def set_tilt(self, angle): self.servos.set_servo_angle(0, angle)