class Gripp3r(IRBeaconRemoteControlledTank): def __init__(self, left_motor_port: str = OUTPUT_B, right_motor_port: str = OUTPUT_C, grip_motor_port: str = OUTPUT_A, touch_sensor_port: str = INPUT_1, ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1): super().__init__(left_motor_port=left_motor_port, right_motor_port=right_motor_port, ir_sensor_port=ir_sensor_port, ir_beacon_channel=ir_beacon_channel) self.grip_motor = MediumMotor(address=grip_motor_port) self.touch_sensor = TouchSensor(address=touch_sensor_port) self.ir_sensor = InfraredSensor(address=ir_sensor_port) self.ir_beacon_channel = ir_beacon_channel self.beacon = RemoteControl(sensor=self.ir_sensor, channel=ir_beacon_channel) self.speaker = Sound() def grip_or_release_by_ir_beacon(self, speed: float = 50): while True: if self.beacon.beacon: if self.touch_sensor.is_pressed: self.speaker.play( wav_file='/home/robot/sound/Air release.wav') self.grip_motor.run_timed( speed_sp=500, time_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) self.grip_motor.wait_while(Motor.STATE_RUNNING) else: self.speaker.play( wav_file='/home/robot/sound/Airbrake.wav') self.grip_motor.run_forever(speed_sp=-500) while not self.touch_sensor.is_pressed: pass self.grip_motor.stop(stop_action=Motor.STOP_ACTION_BRAKE) while self.beacon.beacon: pass def main(self, speed: float = 1000): self.grip_motor.run_timed(speed_sp=-500, time_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) self.grip_motor.wait_while(Motor.STATE_RUNNING) Process(target=self.grip_or_release_by_ir_beacon, daemon=True).start() self.keep_driving_by_ir_beacon(speed=speed)
class CuriosityRov3r(IRBeaconRemoteControlledTank): def __init__( self, left_motor_port: str = OUTPUT_B, right_motor_port: str = OUTPUT_C, medium_motor_port: str = OUTPUT_A, touch_sensor_port: str = INPUT_1, color_sensor_port: str = INPUT_3, ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1): super().__init__( left_motor_port=left_motor_port, right_motor_port=right_motor_port, ir_sensor_port=ir_sensor_port, ir_beacon_channel=ir_beacon_channel) self.medium_motor = MediumMotor(address=medium_motor_port) self.touch_sensor = TouchSensor(address=touch_sensor_port) self.color_sensor = ColorSensor(address=color_sensor_port) self.ir_sensor = InfraredSensor(address=ir_sensor_port) self.ir_beacon_channel = ir_beacon_channel self.beacon = RemoteControl(sensor=self.ir_sensor, channel=ir_beacon_channel) self.dis = Screen() self.noise = Sound() def spin_fan(self, speed: float = 1000): while True: if self.color_sensor.reflected_light_intensity > 20: self.medium_motor.run_forever(speed_sp=speed) else: self.medium_motor.stop(stop_action=Motor.STOP_ACTION_HOLD) def say_when_touched(self): while True: if self.touch_sensor.is_pressed: self.dis.image.paste(im=Image.open('/home/robot/image/Angry.bmp')) self.dis.update() self.noise.play(wav_file='/home/robot/sound/No.wav').wait() self.medium_motor.run_timed( speed_sp=-500, time_sp=3000, stop_action=Motor.STOP_ACTION_BRAKE) self.medium_motor.wait_while(Motor.STATE_RUNNING) def main(self, speed: float = 1000): Thread(target=self.say_when_touched, daemon=True).start() Thread(target=self.spin_fan, daemon=True).start() self.keep_driving_by_ir_beacon(speed=speed)
class MarsRov3r(IRBeaconRemoteControlledTank): def __init__(self, left_motor_port: str = OUTPUT_B, right_motor_port: str = OUTPUT_C, grip_motor_port: str = OUTPUT_A, touch_sensor_port: str = INPUT_1, color_sensor_port: str = INPUT_3, ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1): super().__init__(left_motor_port=left_motor_port, right_motor_port=right_motor_port, ir_sensor_port=ir_sensor_port, ir_beacon_channel=ir_beacon_channel) self.grip_motor = MediumMotor(address=grip_motor_port) self.touch_sensor = TouchSensor(address=touch_sensor_port) self.color_sensor = ColorSensor(address=color_sensor_port) self.ir_sensor = InfraredSensor(address=ir_sensor_port) self.ir_beacon_channel = ir_beacon_channel self.beacon = RemoteControl(sensor=self.ir_sensor, channel=ir_beacon_channel) self.speaker = Sound() is_gripping = False def grip_or_release_claw_by_ir_beacon(self): while True: if self.beacon.beacon: if self.is_gripping: self.grip_motor.run_timed( speed_sp=1000, time_sp=2000, stop_action=Motor.STOP_ACTION_BRAKE) self.grip_motor.wait_while(Motor.STATE_RUNNING) self.speaker.play( wav_file='/home/robot/sound/Air release.wav').wait() self.is_gripping = False else: self.grip_motor.run_timed( speed_sp=-1000, time_sp=2000, stop_action=Motor.STOP_ACTION_BRAKE) self.grip_motor.wait_while(Motor.STATE_RUNNING) self.speaker.play( wav_file='/home/robot/sound/Airbrake.wav').wait() self.is_gripping = True while self.beacon.beacon: pass def main(self, speed: float = 1000): self.grip_motor.run_timed(speed_sp=500, time_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) self.grip_motor.wait_while(Motor.STATE_RUNNING) Thread(target=self.grip_or_release_claw_by_ir_beacon, daemon=True).start() self.keep_driving_by_ir_beacon(speed=speed)
MEDIUM_MOTOR = MediumMotor(address=OUTPUT_A) GO_MOTOR = LargeMotor(address=OUTPUT_B) STING_MOTOR = LargeMotor(address=OUTPUT_D) INFRARED_SENSOR = InfraredSensor(address=INPUT_4) SPEAKER = Sound() MEDIUM_MOTOR.run_timed( speed_sp=500, time_sp=1000, stop_action=Motor.STOP_ACTION_HOLD) MEDIUM_MOTOR.wait_while(Motor.STATE_RUNNING) MEDIUM_MOTOR.run_timed( speed_sp=-500, time_sp=0.3 * 1000, stop_action=Motor.STOP_ACTION_HOLD) MEDIUM_MOTOR.wait_while(Motor.STATE_RUNNING) STING_MOTOR.run_timed( speed_sp=400, time_sp=1000, stop_action=Motor.STOP_ACTION_HOLD) STING_MOTOR.wait_while(Motor.STATE_RUNNING) for i in range(3): GO_MOTOR.run_forever(speed_sp=-1000)
class Ev3rstorm: def __init__(self, left_foot_track_motor_port: str = OUTPUT_B, right_foot_track_motor_port: str = OUTPUT_C, bazooka_blast_motor_port: str = OUTPUT_A, touch_sensor_port: str = INPUT_1, color_sensor_port: str = INPUT_3, ir_sensor_port: str = INPUT_4, driving_ir_beacon_channel: int = 1, shooting_ir_beacon_channel: int = 2): self.left_foot_track_motor = LargeMotor( address=left_foot_track_motor_port) self.right_foot_track_motor = LargeMotor( address=right_foot_track_motor_port) self.bazooka_blast_motor = MediumMotor( address=bazooka_blast_motor_port) self.ir_sensor = InfraredSensor(address=ir_sensor_port) self.driving_remote_control = RemoteControl( sensor=self.ir_sensor, channel=driving_ir_beacon_channel) self.shooting_remote_control = RemoteControl( sensor=self.ir_sensor, channel=shooting_ir_beacon_channel) self.touch_sensor = TouchSensor(address=INPUT_1) self.color_sensor = ColorSensor(address=INPUT_3) self.leds = Leds() self.screen = Screen() assert self.left_foot_track_motor.connected assert self.right_foot_track_motor.connected assert self.bazooka_blast_motor.connected assert self.ir_sensor.connected assert self.touch_sensor.connected assert self.color_sensor.connected # reset the motors for motor in (self.left_foot_track_motor, self.right_foot_track_motor, self.bazooka_blast_motor): motor.reset() motor.position = 0 motor.stop_action = Motor.STOP_ACTION_BRAKE self.draw_face() def draw_face(self): w, h = self.screen.shape y = h // 2 eye_xrad = 20 eye_yrad = 30 pup_xrad = 10 pup_yrad = 10 def draw_eye(x): self.screen.draw.ellipse( (x - eye_xrad, y - eye_yrad, x + eye_xrad, y + eye_yrad)) self.screen.draw.ellipse( (x - pup_xrad, y - pup_yrad, x + pup_xrad, y + pup_yrad), fill='black') draw_eye(w // 3) draw_eye(2 * w // 3) self.screen.update() def shoot(self, direction='up'): """ Shot a ball in the specified direction (valid choices are 'up' and 'down') """ self.bazooka_blast_motor.run_to_rel_pos( speed_sp=900, # degrees per second position_sp=-3 * 360 if direction == 'up' else 3 * 360, # degrees stop_action=Motor.STOP_ACTION_BRAKE) self.bazooka_blast_motor.wait_while(Motor.STATE_RUNNING) def rc_loop( self, driving_speed: float = 1000 # degrees per second ): """ Enter the remote control loop. RC buttons on channel 1 control the robot movement, channel 2 is for shooting things. The loop ends when the touch sensor is pressed. """ def roll(motor: Motor, led_group: str, speed: float): """ Generate remote control event handler. It rolls given motor into given direction (1 for forward, -1 for backward). When motor rolls forward, the given led group flashes green, when backward -- red. When motor stops, the leds are turned off. The on_press function has signature required by RemoteControl class. It takes boolean state parameter; True when button is pressed, False otherwise. """ def on_press(state: int): if state: # roll when button is pressed motor.run_forever(speed_sp=speed) self.leds.set_color( group=led_group, color=Leds.GREEN if speed > 0 else Leds.RED, pct=1) else: # stop otherwise motor.stop(stop_action=Motor.STOP_ACTION_COAST) self.leds.set_color(group=led_group, color=Leds.BLACK, pct=1) return on_press self.driving_remote_control.on_red_up = \ roll(motor=self.right_foot_track_motor, led_group=Leds.RIGHT, speed=driving_speed) self.driving_remote_control.on_red_down = \ roll(motor=self.right_foot_track_motor, led_group=Leds.RIGHT, speed=-driving_speed) self.driving_remote_control.on_blue_up = \ roll(motor=self.left_foot_track_motor, led_group=Leds.LEFT, speed=driving_speed) self.driving_remote_control.on_blue_down = \ roll(motor=self.left_foot_track_motor, led_group=Leds.LEFT, speed=-driving_speed) def shoot(direction: str): def on_press(state: int): if state: self.shoot(direction) return on_press self.shooting_remote_control.on_red_up = shoot('up') self.shooting_remote_control.on_blue_up = shoot('up') self.shooting_remote_control.on_red_down = shoot('down') self.shooting_remote_control.on_blue_down = shoot('down') # now that the event handlers are assigned, # let's enter the processing loop: while not self.touch_sensor.is_pressed: self.driving_remote_control.process() self.shooting_remote_control.process()
LEDS.set_color(group=Leds.LEFT, color=Leds.ORANGE, pct=1) LEDS.set_color(group=Leds.RIGHT, color=Leds.ORANGE, pct=1) if REMOTE_CONTROL.beacon: heading_difference = BEACON_SEEKER.heading - (-3) proximity_difference = BEACON_SEEKER.distance - 70 if (heading_difference == 0) and (proximity_difference == 0): LEFT_FOOT_MOTOR.stop(stop_action=Motor.STOP_ACTION_HOLD) RIGHT_FOOT_MOTOR.stop(stop_action=Motor.STOP_ACTION_HOLD) LEDS.set_color(group=Leds.LEFT, color=Leds.RED, pct=1) LEDS.set_color(group=Leds.RIGHT, color=Leds.RED, pct=1) BAZOOKA_BLAST_MOTOR.run_to_rel_pos( speed_sp=1000, # degrees per second position_sp=3 * 360, # degrees stop_action=Motor.STOP_ACTION_HOLD) BAZOOKA_BLAST_MOTOR.wait_while(Motor.STATE_RUNNING) SPEAKER.play(wav_file='/home/robot/sound/Laughing 2.wav').wait() else: # TODO ... else: LEFT_FOOT_MOTOR.stop(stop_action=Motor.STOP_ACTION_HOLD) RIGHT_FOOT_MOTOR.stop(stop_action=Motor.STOP_ACTION_HOLD)
class Gripp3r: def __init__(self, left_motor_port: str = OUTPUT_B, right_motor_port: str = OUTPUT_C, grip_motor_port: str = OUTPUT_A, touch_sensor_port: str = INPUT_1, ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1): self.left_motor = LargeMotor(address=left_motor_port) self.right_motor = LargeMotor(address=right_motor_port) self.grip_motor = MediumMotor(address=grip_motor_port) self.touch_sensor = TouchSensor(address=touch_sensor_port) self.ir_sensor = InfraredSensor(address=ir_sensor_port) self.ir_beacon_channel = ir_beacon_channel self.beacon = RemoteControl(sensor=self.ir_sensor, channel=ir_beacon_channel) self.speaker = Sound() def keep_driving_by_ir_beacon(self, speed: float = 1000): while True: if self.beacon.red_up and self.beacon.blue_up: # go forward self.left_motor.run_forever(speed_sp=speed) self.right_motor.run_forever(speed_sp=speed) elif self.beacon.red_down and self.beacon.blue_down: # go backward self.left_motor.run_forever(speed_sp=-speed) self.right_motor.run_forever(speed_sp=-speed) elif self.beacon.red_up and self.beacon.blue_down: # turn around left self.left_motor.run_forever(speed_sp=-speed) self.right_motor.run_forever(speed_sp=speed) elif self.beacon.red_down and self.beacon.blue_up: # turn around right self.left_motor.run_forever(speed_sp=speed) self.right_motor.run_forever(speed_sp=-speed) elif self.beacon.red_up: # turn left self.left_motor.run_forever(speed_sp=0) self.right_motor.run_forever(speed_sp=speed) elif self.beacon.blue_up: # turn right self.left_motor.run_forever(speed_sp=speed) self.right_motor.run_forever(speed_sp=0) elif self.beacon.red_down: # left backward self.left_motor.run_forever(speed_sp=0) self.right_motor.run_forever(speed_sp=-speed) elif self.beacon.blue_down: # right backward self.left_motor.run_forever(speed_sp=-speed) self.right_motor.run_forever(speed_sp=0) else: self.left_motor.stop(stop_action=Motor.STOP_ACTION_BRAKE) self.right_motor.stop(stop_action=Motor.STOP_ACTION_BRAKE) def grip_or_release_by_ir_beacon(self, speed: float = 50): while True: if self.beacon.beacon: if self.touch_sensor.is_pressed: self.speaker.play( wav_file='/home/robot/sound/Air release.wav') self.grip_motor.run_timed( speed_sp=500, time_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) self.grip_motor.wait_while(Motor.STATE_RUNNING) else: self.speaker.play( wav_file='/home/robot/sound/Airbrake.wav') self.grip_motor.run_forever(speed_sp=-500) while not self.touch_sensor.is_pressed: pass self.grip_motor.stop(stop_action=Motor.STOP_ACTION_BRAKE) while self.beacon.beacon: pass def main(self, speed: float = 1000): self.grip_motor.run_timed(speed_sp=-500, time_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) self.grip_motor.wait_while(Motor.STATE_RUNNING) Thread(target=self.grip_or_release_by_ir_beacon, daemon=True).start() self.keep_driving_by_ir_beacon(speed=speed)
class Sweep3r(IRBeaconRemoteControlledTank): def __init__(self, right_motor_port: str = OUTPUT_C, left_motor_port: str = OUTPUT_B, medium_motor_port: str = OUTPUT_A, touch_sensor_port: str = INPUT_1, color_sensor_port: str = INPUT_3, ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1): super().__init__(left_motor_port=left_motor_port, right_motor_port=right_motor_port, ir_sensor_port=ir_sensor_port, ir_beacon_channel=ir_beacon_channel) self.medium_motor = MediumMotor(address=medium_motor_port) self.touch_sensor = TouchSensor(address=touch_sensor_port) self.color_sensor = ColorSensor(address=color_sensor_port) self.screen = Screen() self.speaker = Sound() self.ir_sensor = InfraredSensor(address=ir_sensor_port) self.remote_control = RemoteControl(sensor=self.ir_sensor, channel=ir_beacon_channel) def drill(self): while True: if self.remote_control.beacon: self.medium_motor.run_timed( speed_sp=1000, # deg/s time_sp=0.3 * 1000, # ms stop_action=Motor.STOP_ACTION_HOLD) self.medium_motor.wait_while(Motor.STATE_RUNNING) def move_when_touched(self): while True: if self.touch_sensor.is_pressed: self.right_motor.run_timed(time_sp=1000, speed_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) self.right_motor.wait_while(Motor.STATE_RUNNING) def move_when_see_smothing(self): while True: if self.color_sensor.reflected_light_intensity > 30: self.left_motor.run_timed(time_sp=1000, speed_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) self.left_motor.wait_while(Motor.STATE_RUNNING) def main( self, speed: float = 1000 # degrees per second ): self.screen.image.paste( im=Image.open('/home/robot/image/Pinch middle.bmp')) self.screen.update() Process(target=self.move_when_touched, daemon=True).start() Process(target=self.move_when_see_smothing, daemon=True).start() Process(target=self.drill, daemon=True).start() self.keep_driving_by_ir_beacon(speed=speed)
class R3ptar: def __init__(self, turn_motor_port: str = OUTPUT_A, move_motor_port: str = OUTPUT_B, scare_motor_port: str = OUTPUT_D, touch_sensor_port: str = INPUT_1, color_sensor_port: str = INPUT_3, ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1): self.turn_motor = MediumMotor(address=turn_motor_port) self.move_motor = LargeMotor(address=move_motor_port) self.scare_motor = LargeMotor(address=scare_motor_port) self.touch_sensor = TouchSensor(address=touch_sensor_port) self.color_sensor = ColorSensor(address=color_sensor_port) self.ir_sensor = InfraredSensor(address=ir_sensor_port) self.beacon = RemoteControl(sensor=self.ir_sensor, channel=ir_beacon_channel) self.noise = Sound() def keep_driving_by_ir_beacon(self, speed: float = 1000): while True: if self.beacon.red_up and self.beacon.blue_up: self.move_motor.run_forever(speed_sp=speed) elif self.beacon.red_down and self.beacon.blue_down: self.move_motor.run_forever(speed_sp=-speed) elif self.beacon.red_up: self.turn_motor.run_forever(speed_sp=-500) self.move_motor.run_forever(speed_sp=speed) elif self.beacon.blue_up: self.turn_motor.run_forever(speed_sp=500) self.move_motor.run_forever(speed_sp=speed) elif self.beacon.red_down: self.turn_motor.run_forever(speed_sp=-500) self.move_motor.run_forever(speed_sp=-speed) elif self.beacon.blue_down: self.turn_motor.run_forever(speed_sp=500) self.move_motor.run_forever(speed_sp=-speed) else: self.turn_motor.stop(stop_action=Motor.STOP_ACTION_HOLD) self.move_motor.stop(stop_action=Motor.STOP_ACTION_COAST) def bite_by_ir_beacon(self, speed: float = 1000): while True: if self.beacon.beacon: self.noise.play(wav_file='/home/robot/sound/Snake hiss.wav') self.scare_motor.run_timed(speed_sp=speed, time_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) self.scare_motor.wait_while(Motor.STATE_RUNNING) self.scare_motor.run_timed(speed_sp=-300, time_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) self.scare_motor.wait_while(Motor.STATE_RUNNING) while self.beacon.beacon: pass def run_away_if_chased(self): while True: if self.color_sensor.reflected_light_intensity > 30: self.move_motor.run_timed(speed_sp=500, time_sp=4000, stop_action=Motor.STOP_ACTION_BRAKE) self.move_motor.wait_while(Motor.STATE_RUNNING) for i in range(2): self.turn_motor.run_timed( speed_sp=500, time_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) self.turn_motor.wait_while(Motor.STATE_RUNNING) self.turn_motor.run_timed( speed_sp=-500, time_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) self.turn_motor.wait_while(Motor.STATE_RUNNING) def bite_if_touched(self): while True: if self.touch_sensor.is_pressed: self.noise.play(wav_file='/home/robot/sound/Snake hiss.wav') self.scare_motor.run_timed(speed_sp=1000, time_sp=1000, stop_action=Motor.STOP_ACTION_COAST) self.scare_motor.wait_while(Motor.STATE_RUNNING) self.scare_motor.run_timed(speed_sp=-300, time_sp=1000, stop_action=Motor.STOP_ACTION_COAST) self.scare_motor.wait_while(Motor.STATE_RUNNING) def main(self, speed: float = 1000): Process(target=self.bite_by_ir_beacon, daemon=True).start() Process(target=self.bite_if_touched, daemon=True).start() Process(target=self.run_away_if_chased, daemon=True).start() self.keep_driving_by_ir_beacon(speed=speed)
class Dinor3x(IRBeaconRemoteControlledTank): """ Challenges: - Can you make DINOR3X remote controlled with the IR-Beacon? - Can you attach a colorsensor to DINOR3X, and make it behave differently depending on which color is in front of the sensor (red = walk fast, white = walk slow, etc.)? """ # https://sites.google.com/site/ev3python/learn_ev3_python/using-motors MEDIUM_MOTOR_POWER_FACTOR = 1.4 def __init__(self, left_motor_port: str = OUTPUT_B, right_motor_port: str = OUTPUT_C, jaw_motor_port: str = OUTPUT_A, touch_sensor_port: str = INPUT_1, ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1): super().__init__(left_motor_port=left_motor_port, right_motor_port=right_motor_port, ir_sensor_port=ir_sensor_port, ir_beacon_channel=ir_beacon_channel) self.jaw_motor = MediumMotor(address=jaw_motor_port) self.touch_sensor = TouchSensor(address=touch_sensor_port) self.ir_sensor = InfraredSensor(address=ir_sensor_port) self.beacon = RemoteControl(sensor=self.ir_sensor, channel=ir_beacon_channel) self.button = Button() self.speaker = Sound() def calibrate_legs(self): self.left_motor.run_forever(speed_sp=100) self.right_motor.run_forever(speed_sp=200) while self.touch_sensor.is_pressed: pass self.left_motor.stop(stop_action=Motor.STOP_ACTION_BRAKE) self.right_motor.stop(stop_action=Motor.STOP_ACTION_BRAKE) self.left_motor.run_forever(speed_sp=400) while not self.touch_sensor.is_pressed: pass self.left_motor.stop(stop_action=Motor.STOP_ACTION_BRAKE) self.left_motor.run_to_rel_pos(position_sp=-0.2 * 360, speed_sp=500, stop_action=Motor.STOP_ACTION_BRAKE) self.left_motor.wait_while(Motor.STATE_RUNNING) self.right_motor.run_forever(speed_sp=400) while not self.touch_sensor.is_pressed: pass self.right_motor.stop(stop_action=Motor.STOP_ACTION_BRAKE) self.right_motor.run_to_rel_pos(position_sp=-0.2 * 360, speed_sp=500, stop_action=Motor.STOP_ACTION_BRAKE) self.right_motor.wait_while(Motor.STATE_RUNNING) self.left_motor.reset() self.right_motor.reset() def close_mouth(self): self.jaw_motor.run_forever(speed_sp=self.MEDIUM_MOTOR_POWER_FACTOR * 200) sleep(1) self.jaw_motor.stop(stop_action=Motor.STOP_ACTION_BRAKE) def roar(self): self.speaker.play(wav_file='/home/robot/sound/T-rex roar.wav') self.jaw_motor.run_to_rel_pos(position_sp=-60, speed_sp=self.MEDIUM_MOTOR_POWER_FACTOR * 400, stop_action=Motor.STOP_ACTION_BRAKE) self.jaw_motor.wait_while(Motor.STATE_RUNNING) # FIXME: jaw keeps opening wider and wider and doesn't close for i in range(12): self.jaw_motor.run_timed(speed_sp=-self.MEDIUM_MOTOR_POWER_FACTOR * 400, time_sp=0.05 * 1000, stop_action=Motor.STOP_ACTION_BRAKE) self.jaw_motor.wait_while(Motor.STATE_RUNNING) self.jaw_motor.run_timed(speed_sp=self.MEDIUM_MOTOR_POWER_FACTOR * 400, time_sp=0.05 * 1000, stop_action=Motor.STOP_ACTION_BRAKE) self.jaw_motor.wait_while(Motor.STATE_RUNNING) self.jaw_motor.run_forever(speed_sp=self.MEDIUM_MOTOR_POWER_FACTOR * 200) sleep(0.5) def walk_until_blocked(self): self.left_motor.run_forever(speed_sp=-400) self.right_motor.run_forever(speed_sp=-400) while self.ir_sensor.proximity >= 25: pass self.left_motor.stop(stop_action=Motor.STOP_ACTION_BRAKE) self.right_motor.stop(stop_action=Motor.STOP_ACTION_BRAKE) def run_away(self): self.left_motor.run_to_rel_pos(speed_sp=750, position_sp=3 * 360, stop_action=Motor.STOP_ACTION_BRAKE) self.right_motor.run_to_rel_pos(speed_sp=750, position_sp=3 * 360, stop_action=Motor.STOP_ACTION_BRAKE) self.left_motor.wait_while(Motor.STATE_RUNNING) self.right_motor.wait_while(Motor.STATE_RUNNING) def jump(self): """ Dinor3x Mission 02 Challenge: make it jump """ ... # TRANSLATED FROM EV3-G MY BLOCKS # ------------------------------- def leg_adjust(self, cyclic_degrees: float, speed: float = 1000, leg_offset_percent: float = 0, mirrored_adjust: bool = False, brake: bool = True): ... def leg_to_pos(self, speed: float = 1000, left_position: float = 0, right_position: float = 0): self.left_motor.stop(stop_action=Motor.STOP_ACTION_BRAKE) self.right_motor.stop(stop_action=Motor.STOP_ACTION_BRAKE) self.left_motor.run_to_rel_pos( speed_sp=speed, position_sp=left_position - cyclic_position_offset( rotation_sensor=self.left_motor.position, cyclic_degrees=360), stop_action=Motor.STOP_ACTION_BRAKE) self.left_motor.wait_while(Motor.STATE_RUNNING) self.right_motor.run_to_rel_pos( speed_sp=speed, position_sp=right_position - cyclic_position_offset( rotation_sensor=self.right_motor.position, cyclic_degrees=360), stop_action=Motor.STOP_ACTION_BRAKE) self.right_motor.wait_while(Motor.STATE_RUNNING) def turn(self, speed: float = 1000, n_steps: int = 1): ... def walk(self, speed: float = 1000): ... def walk_steps(self, speed: float = 1000, n_steps: int = 1): ...
class Catapult(IRBeaconRemoteControlledTank): def __init__(self, left_motor_port: str = OUTPUT_B, right_motor_port: str = OUTPUT_C, catapult_motor_port: str = OUTPUT_A, touch_sensor_port: str = INPUT_1, color_sensor_port: str = INPUT_3, ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1): super().__init__(left_motor_port=left_motor_port, right_motor_port=right_motor_port, ir_sensor_port=ir_sensor_port, ir_beacon_channel=ir_beacon_channel) self.catapult_motor = MediumMotor(address=catapult_motor_port) self.touch_sensor = TouchSensor(address=touch_sensor_port) self.color_sensor = ColorSensor(address=color_sensor_port) self.ir_sensor = InfraredSensor(address=ir_sensor_port) self.beacon = RemoteControl(sensor=self.ir_sensor, channel=ir_beacon_channel) self.speaker = Sound() def scan_colors(self): if self.color_sensor.color == ColorSensor.COLOR_YELLOW: pass elif self.color_sensor.color == ColorSensor.COLOR_WHITE: self.speaker.play(wav_file='/home/robot/sound/Good.wav').wait() def make_noise_when_touched(self): if self.touch_sensor.is_pressed: self.speaker.play(wav_file='/home/robot/sound/Ouch.wav').wait() def throw_by_ir_beacon(self): if self.beacon.beacon: self.catapult_motor.run_to_rel_pos( speed_sp=1000, position_sp=-150, stop_action=Motor.STOP_ACTION_HOLD) self.catapult_motor.wait_while(Motor.STATE_RUNNING) self.catapult_motor.run_to_rel_pos( speed_sp=1000, position_sp=150, stop_action=Motor.STOP_ACTION_HOLD) self.catapult_motor.wait_while(Motor.STATE_RUNNING) while self.beacon.beacon: pass def main(self): self.speaker.play(wav_file='/home/robot/sound/Yes.wav').wait() while True: self.drive_once_by_ir_beacon(speed=1000) self.make_noise_when_touched() self.throw_by_ir_beacon() self.scan_colors()
class Spik3r: def __init__(self, sting_motor_port: str = OUTPUT_D, go_motor_port: str = OUTPUT_B, claw_motor_port: str = OUTPUT_A, touch_sensor_port: str = INPUT_1, color_sensor_port: str = INPUT_3, ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1): self.sting_motor = LargeMotor(address=sting_motor_port) self.go_motor = LargeMotor(address=go_motor_port) self.claw_motor = MediumMotor(address=claw_motor_port) self.ir_sensor = InfraredSensor(address=ir_sensor_port) self.ir_beacon_channel = ir_beacon_channel self.beacon = RemoteControl(sensor=self.ir_sensor, channel=self.ir_beacon_channel) self.touch_sensor = TouchSensor(address=touch_sensor_port) self.color_sensor = ColorSensor(address=color_sensor_port) self.dis = Screen() self.speaker = Sound() def sting_by_ir_beacon(self): while True: if self.beacon.beacon: self.sting_motor.run_to_rel_pos( speed_sp=750, position=-220, stop_action=Motor.STOP_ACTION_HOLD) self.sting_motor.wait_while(Motor.STATE_RUNNING) self.speaker.play( wav_file='/home/robot/sound/Blip 3.wav').wait() self.sting_motor.run_timed(speed_sp=-1000, time_sp=1000, stop_action=Motor.STOP_ACTION_HOLD) self.sting_motor.wait_while(Motor.STATE_RUNNING) self.sting_motor.run_timed(speed_sp=1000, time_sp=1000, stop_action=Motor.STOP_ACTION_HOLD) self.sting_motor.wait_while(Motor.STATE_RUNNING) while self.beacon.beacon: pass def be_noisy_to_people(self): while True: if self.color_sensor.reflected_light_intensity > 30: for i in range(4): self.speaker.play_song( (('D4', 'e3'), ('D4', 'e3'), ('D4', 'e3'), ('G4', 'h'), ('D5', 'h'), ('C5', 'e3'), ('B4', 'e3'), ('A4', 'e3'), ('G5', 'h'), ('D5', 'q'), ('C5', 'e3'), ('B4', 'e3'), ('A4', 'e3'), ('G5', 'h'), ('D5', 'q'), ('C5', 'e3'), ('B4', 'e3'), ('C5', 'e3'), ('A4', 'h.'))) def pinch_if_touched(self): while True: if self.touch_sensor.is_pressed: self.claw_motor.run_timed(speed_sp=500, time_sp=1000, stop_action=Motor.STOP_ACTION_HOLD) self.claw_motor.wait_while(Motor.STATE_RUNNING) self.claw_motor.run_timed(speed_sp=-500, time_sp=0.3 * 1000, stop_action=Motor.STOP_ACTION_HOLD) self.claw_motor.wait_while(Motor.STATE_RUNNING) def keep_driving_by_ir_beacon(self): while True: if self.beacon.red_up and self.beacon.blue_up: self.go_motor.run_forever(speed_sp=910) elif self.beacon.blue_up: self.go_motor.run_forever(speed_sp=-1000) else: self.go_motor.stop(stop_action=Motor.STOP_ACTION_HOLD) def main(self): self.dis.image.paste(im=Image.open('/home/robot/image/Evil.bmp')) self.dis.update() # FIXME: ValueError: invalid literal for int() with base 10: '' or '9\n9' # when multiple threads access the same Sensor Thread(target=self.pinch_if_touched, daemon=True).start() Thread(target=self.be_noisy_to_people, daemon=True).start() Thread(target=self.sting_by_ir_beacon, daemon=True).start() self.keep_driving_by_ir_beacon()