class MyMotor(object): def __init__(self, motor, speed=0): self._motor = LargeMotor(motor) assert self._motor.connected self._motor.reset() self._motor.position = 0 self._motor.stop_action = 'brake' self._set_speed(speed) @property def speed(self): return int(self._speed/10) def _set_speed(self, speed): assert speed >= -100 and speed <= 100 self._speed = speed*10 def run_forever(self, speed): if speed is not None: self._set_speed(speed) self._motor.run_forever(speed_sp=self._speed) def run_timed(self, timed, speed): if speed is not None: self._set_speed(speed) runtimed = timed * 1000 self._motor.run_timed(speed_sp=self._speed, time_sp=runtimed) def run_position(self, postion, speed, lastpostion=False): if speed is not None: self._set_speed(speed) if lastpostion: self._motor.run_to_abs_pos(speed_sp=self._speed) else: self._motor.run_to_rel_pos(speed_sp=self._speed) def stop(self): self._motor.stop() def run_back(self, speed): if not self._speed: self.stop() self._set_speed(speed) self._motor.run_forever(speed_sp=self._speed)
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) SPEAKER.play(wav_file='/home/robot/sound/Blip 2.wav').wait() while INFRARED_SENSOR.proximity >= 40: pass GO_MOTOR.run_forever(speed_sp=250) SPEAKER.play(wav_file='/home/robot/sound/Blip 4.wav').wait()
OUTPUT_D, InfraredSensor, INPUT_4, Leds, Sound) from random import randint from time import sleep MEDIUM_MOTOR = MediumMotor(address=OUTPUT_A) TAIL_MOTOR = LargeMotor(address=OUTPUT_B) CHEST_MOTOR = LargeMotor(address=OUTPUT_D) IR_SENSOR = InfraredSensor(address=INPUT_4) LIGHTS = Leds() SPEAKER = Sound() CHEST_MOTOR.run_timed(speed_sp=-300, time_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) CHEST_MOTOR.wait_while(Motor.STATE_RUNNING) while True: if IR_SENSOR.proximity < 30: LIGHTS.set_color(group=Leds.LEFT, color=Leds.RED, pct=1) LIGHTS.set_color(group=Leds.RIGHT, color=Leds.RED, pct=1) MEDIUM_MOTOR.stop(stop_action=Motor.STOP_ACTION_HOLD) TAIL_MOTOR.stop(stop_action=Motor.STOP_ACTION_BRAKE) SPEAKER.play(wav_file='/home/robot/sound/Snake hiss.wav')
class Robot(object): def __init__(self): self.left_color_sensor = ColorSensor('in4') self.right_color_sensor = ColorSensor('in1') self.left_large_motor = LargeMotor('outD') self.right_large_motor = LargeMotor('outA') self.touch_sensor = TouchSensor('in3') self.listeners = [] self.prev_is_pressed = False def move_forward(self): self.left_large_motor.run_forever(speed_sp=DEFAULT_POWER) self.right_large_motor.run_forever(speed_sp=DEFAULT_POWER) def move_backward(self): self.left_large_motor.run_forever(speed_sp=-DEFAULT_POWER) self.right_large_motor.run_forever(speed_sp=-DEFAULT_POWER) def steer_left(self): self.left_large_motor.run_forever(speed_sp=NON_STEERING_POWER) self.right_large_motor.run_forever(speed_sp=STEERING_POWER) def steer_right(self): self.left_large_motor.run_forever(speed_sp=STEERING_POWER) self.right_large_motor.run_forever(speed_sp=NON_STEERING_POWER) def turn_left(self): self.stop() self.left_large_motor.run_timed(speed_sp=-TURNING_POWER, time_sp=TURNING_MILLISECONDS) self.right_large_motor.run_timed(speed_sp=TURNING_POWER, time_sp=TURNING_MILLISECONDS) # Block any calls to the motor while the train is turning self.left_large_motor.wait_while('running') self.right_large_motor.wait_while('running') def turn_right(self): self.stop() self.left_large_motor.run_timed(speed_sp=TURNING_POWER, time_sp=TURNING_MILLISECONDS) self.right_large_motor.run_timed(speed_sp=-TURNING_POWER, time_sp=TURNING_MILLISECONDS) # Block any calls to the motor while the train is turning self.left_large_motor.wait_while('running') self.right_large_motor.wait_while('running') def stop(self): self.left_large_motor.stop() self.right_large_motor.stop() def step(self): left_color = self.left_color_sensor.color right_color = self.right_color_sensor.color if left_color == Color.INVALID or right_color == Color.INVALID: for listener in self.listeners: listener.on_invalid(self, left_color == Color.INVALID, right_color == Color.INVALID) if left_color == Color.BLACK or right_color == Color.BLACK: for listener in self.listeners: listener.on_black(self, left_color == Color.BLACK, right_color == Color.BLACK) if left_color == Color.BLUE or right_color == Color.BLUE: for listener in self.listeners: listener.on_blue(self, left_color == Color.BLUE, right_color == Color.BLUE) if left_color == Color.GREEN or right_color == Color.GREEN: for listener in self.listeners: listener.on_green(self, left_color == Color.GREEN, right_color == Color.GREEN) if left_color == Color.YELLOW or right_color == Color.YELLOW: for listener in self.listeners: listener.on_yellow(self, left_color == Color.YELLOW, right_color == Color.YELLOW) if left_color == Color.RED or right_color == Color.RED: for listener in self.listeners: listener.on_red(self, left_color == Color.RED, right_color == Color.RED) if left_color == Color.WHITE or right_color == Color.WHITE: for listener in self.listeners: listener.on_white(self, left_color == Color.WHITE, right_color == Color.WHITE) if left_color == Color.BROWN or right_color == Color.BROWN: for listener in self.listeners: listener.on_brown(self, left_color == Color.BROWN, right_color == Color.BROWN) if self.prev_is_pressed and not self.touch_sensor.is_pressed: for listener in self.listeners: listener.on_click(self) self.prev_is_pressed = self.touch_sensor.is_pressed def add_listener(self, listener: 'RobotListener'): self.listeners.append(listener)
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 Ev3rstorm: def __init__(self, left_foot_motor_port: str = OUTPUT_B, right_foot_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, ir_beacon_channel: int = 1): self.left_foot_motor = LargeMotor(address=left_foot_motor_port) self.right_foot_motor = LargeMotor(address=right_foot_motor_port) self.bazooka_blast_motor = MediumMotor( address=bazooka_blast_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.remote_control = RemoteControl(sensor=self.ir_sensor, channel=ir_beacon_channel) self.leds = Leds() self.screen = Screen() self.speaker = Sound() def drive_once_by_ir_beacon( self, speed: float = 1000 # degrees per second ): # forward if self.remote_control.red_up and self.remote_control.blue_up: self.left_foot_motor.run_forever(speed_sp=speed) self.right_foot_motor.run_forever(speed_sp=speed) # backward elif self.remote_control.red_down and self.remote_control.blue_down: self.left_foot_motor.run_forever(speed_sp=-speed) self.right_foot_motor.run_forever(speed_sp=-speed) # turn left on the spot elif self.remote_control.red_up and self.remote_control.blue_down: self.left_foot_motor.run_forever(speed_sp=-speed) self.right_foot_motor.run_forever(speed_sp=speed) # turn right on the spot elif self.remote_control.red_down and self.remote_control.blue_up: self.left_foot_motor.run_forever(speed_sp=speed) self.right_foot_motor.run_forever(speed_sp=-speed) # turn left forward elif self.remote_control.red_up: self.right_foot_motor.run_forever(speed_sp=speed) # turn right forward elif self.remote_control.blue_up: self.left_foot_motor.run_forever(speed_sp=speed) # turn left backward elif self.remote_control.red_down: self.right_foot_motor.run_forever(speed_sp=-speed) # turn right backward elif self.remote_control.blue_down: self.left_foot_motor.run_forever(speed_sp=-speed) # otherwise stop else: self.left_foot_motor.stop(stop_action=Motor.STOP_ACTION_COAST) self.right_foot_motor.stop(stop_action=Motor.STOP_ACTION_COAST) def dance_if_ir_beacon_pressed(self): while self.remote_control.beacon: self.left_foot_motor.run_timed(speed_sp=randint(-1000, 1000), time_sp=1000, stop_action=Motor.STOP_ACTION_COAST) self.right_foot_motor.run_timed( speed_sp=randint(-1000, 1000), time_sp=1000, stop_action=Motor.STOP_ACTION_COAST) self.left_foot_motor.wait_while(Motor.STATE_RUNNING) self.right_foot_motor.wait_while(Motor.STATE_RUNNING) def detect_object_by_ir_sensor(self): if self.ir_sensor.proximity < 25: self.leds.set_color(group=Leds.LEFT, color=Leds.RED, pct=1) self.leds.set_color(group=Leds.RIGHT, color=Leds.RED, pct=1) self.speaker.play(wav_file='/home/robot/sound/Object.wav').wait() self.speaker.play(wav_file='/home/robot/sound/Detected.wav').wait() self.speaker.play( wav_file='/home/robot/sound/Error alarm.wav').wait() else: self.leds.all_off() def blast_bazooka_if_touched(self): if self.touch_sensor.is_pressed: if self.color_sensor.ambient_light_intensity < 5: # 15 not dark enough self.speaker.play(wav_file='/home/robot/sound/Up.wav').wait() self.bazooka_blast_motor.run_to_rel_pos( speed_sp=1000, # degrees per second position_sp=-3 * 360, # degrees stop_action=Motor.STOP_ACTION_HOLD) else: self.speaker.play(wav_file='/home/robot/sound/Down.wav').wait() self.bazooka_blast_motor.run_to_rel_pos( speed_sp=1000, # degrees per second position_sp=3 * 360, # degrees stop_action=Motor.STOP_ACTION_HOLD) while self.touch_sensor.is_pressed: pass def main( self, driving_speed: float = 1000 # degrees per second ): self.screen.image.paste(im=Image.open('/home/robot/image/Target.bmp')) self.screen.update() while True: self.drive_once_by_ir_beacon(speed=driving_speed) self.dance_if_ir_beacon_pressed() # DON'T use IR Sensor in 2 different modes in the same program / loop # - https://github.com/pybricks/support/issues/62 # - https://github.com/ev3dev/ev3dev/issues/1401 # self.detect_object_by_ir_sensor() self.blast_bazooka_if_touched()
class Kraz33Hors3: def __init__( self, back_foot_motor_port: str = OUTPUT_C, front_foot_motor_port: str = OUTPUT_B, gear_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.front_foot_motor = LargeMotor(address=front_foot_motor_port) self.back_foot_motor = LargeMotor(address=back_foot_motor_port) self.gear_motor = MediumMotor(address=gear_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.remote_control = RemoteControl(sensor=self.ir_sensor, channel=ir_beacon_channel) def drive_once_by_ir_beacon( self, speed: float = 1000 # deg/s ): # forward if self.remote_control.red_up and self.remote_control.blue_up: self.front_foot_motor.run_timed( speed_sp=speed, time_sp=1000, # ms stop_action=Motor.STOP_ACTION_COAST) self.back_foot_motor.run_timed( speed_sp=-speed, time_sp=1000, # ms stop_action=Motor.STOP_ACTION_COAST) self.front_foot_motor.wait_while(Motor.STATE_RUNNING) self.back_foot_motor.wait_while(Motor.STATE_RUNNING) # backward elif self.remote_control.red_down and self.remote_control.blue_down: self.front_foot_motor.run_timed( speed_sp=-speed, time_sp=1000, # ms stop_action=Motor.STOP_ACTION_COAST) self.back_foot_motor.run_timed( speed_sp=speed, time_sp=1000, # ms stop_action=Motor.STOP_ACTION_COAST) self.front_foot_motor.wait_while(Motor.STATE_RUNNING) self.back_foot_motor.wait_while(Motor.STATE_RUNNING) # move crazily elif self.remote_control.beacon: self.gear_motor.run_forever(speed_sp=speed) self.front_foot_motor.run_timed( speed_sp=speed / 3, time_sp=1000, # ms stop_action=Motor.STOP_ACTION_COAST) self.back_foot_motor.run_timed( speed_sp=speed / 3, time_sp=1000, # ms stop_action=Motor.STOP_ACTION_COAST) self.front_foot_motor.wait_while(Motor.STATE_RUNNING) self.back_foot_motor.wait_while(Motor.STATE_RUNNING) else: self.gear_motor.stop(stop_action=Motor.STOP_ACTION_COAST) def keep_driving_by_ir_beacon( self, speed: float = 1000 # deg/s ): while True: self.drive_once_by_ir_beacon(speed=speed) def back_whenever_touched( self, speed: float = 1000 # deg/s ): while True: if self.touch_sensor.is_pressed: self.front_foot_motor.run_timed( speed_sp=-speed, time_sp=1000, # ms stop_action=Motor.STOP_ACTION_COAST) self.back_foot_motor.run_timed( speed_sp=speed, time_sp=1000, # ms stop_action=Motor.STOP_ACTION_COAST) self.front_foot_motor.wait_while(Motor.STATE_RUNNING) self.back_foot_motor.wait_while(Motor.STATE_RUNNING) def main(self, speed: float = 1000 # deg/s ): Process(target=self.back_whenever_touched).start() self.keep_driving_by_ir_beacon(speed=speed)
#!/usr/bin/env python3 from ev3dev.ev3 import (Motor, LargeMotor, OUTPUT_B, OUTPUT_D, Sound) STING_MOTOR = LargeMotor(address=OUTPUT_D) GO_MOTOR = LargeMotor(address=OUTPUT_B) SPEAKER = Sound() STING_MOTOR.run_timed(speed_sp=400, time_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) STING_MOTOR.wait_while(Motor.STATE_RUNNING) GO_MOTOR.run_to_rel_pos(speed_sp=1000, position_sp=3 * 360, stop_action=Motor.STOP_ACTION_HOLD) GO_MOTOR.wait_while(Motor.STATE_RUNNING) SPEAKER.play(wav_file='/home/robot/sound/Blip 2.wav').wait() GO_MOTOR.run_to_rel_pos(speed_sp=1000, position_sp=-2 * 360, stop_action=Motor.STOP_ACTION_HOLD) GO_MOTOR.wait_while(Motor.STATE_RUNNING) SPEAKER.play(wav_file='/home/robot/sound/Blip 4.wav').wait() STING_MOTOR.run_to_rel_pos(speed_sp=750, position_sp=-220,
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()
MEDIUM_MOTOR = MediumMotor(address=OUTPUT_A) TOUCH_SENSOR = TouchSensor(address=INPUT_1) COLOR_SENSOR = ColorSensor(address=INPUT_3) SCREEN = Screen() SPEAKER = Sound() while True: if COLOR_SENSOR.ambient_light_intensity < 5: # 15 not dark enough SCREEN.image.paste(im=Image.open('/home/robot/image/Middle left.bmp'), box=(0, 0)) SCREEN.update() LEFT_FOOT_MOTOR.run_timed( speed_sp=-800, # degrees per second time_sp=1500, # miliseconds stop_action=Motor.STOP_ACTION_BRAKE) RIGHT_FOOT_MOTOR.run_timed( speed_sp=-1000, # degrees per second time_sp=1500, # miliseconds stop_action=Motor.STOP_ACTION_BRAKE) LEFT_FOOT_MOTOR.wait_while(Motor.STATE_RUNNING) RIGHT_FOOT_MOTOR.wait_while(Motor.STATE_RUNNING) SCREEN.image.paste(im=Image.open('/home/robot/image/Middle right.bmp'), box=(0, 0)) SCREEN.update() LEFT_FOOT_MOTOR.run_timed( speed_sp=-1000, # degrees per second time_sp=1500, # miliseconds