class Gripp3r(RemoteControlledTank): WHEEL_DIAMETER = 26 # milimeters AXLE_TRACK = 115 # milimeters def __init__( self, left_track_motor_port: Port = Port.B, right_track_motor_port: Port = Port.C, gripping_motor_port: Port = Port.A, touch_sensor_port: Port = Port.S1, ir_sensor_port: Port = Port.S4, ir_beacon_channel: int = 1): super().__init__( wheel_diameter=self.WHEEL_DIAMETER, axle_track=self.AXLE_TRACK, left_motor_port=left_track_motor_port, right_motor_port=right_track_motor_port, ir_sensor_port=ir_sensor_port, ir_beacon_channel=ir_beacon_channel) self.ev3_brick = EV3Brick() self.gripping_motor = Motor(port=gripping_motor_port, positive_direction=Direction.CLOCKWISE) self.touch_sensor = TouchSensor(port=touch_sensor_port) self.ir_sensor = InfraredSensor(port=ir_sensor_port) self.ir_beacon_channel = ir_beacon_channel def grip_or_release_by_ir_beacon(self, speed: float = 500): if Button.BEACON in \ self.ir_sensor.buttons(channel=self.ir_beacon_channel): if self.touch_sensor.pressed(): self.ev3_brick.speaker.play_file(file=SoundFile.AIR_RELEASE) self.gripping_motor.run_time( speed=speed, time=1000, then=Stop.COAST, wait=True) else: self.ev3_brick.speaker.play_file(file=SoundFile.AIRBRAKE) self.gripping_motor.run(speed=-speed) while not self.touch_sensor.pressed(): pass self.gripping_motor.stop() while Button.BEACON in \ self.ir_sensor.buttons(channel=self.ir_beacon_channel): pass
class Rov3r(IRBeaconRemoteControlledTank, EV3Brick): WHEEL_DIAMETER = 23 AXLE_TRACK = 65 def __init__(self, left_motor_port: Port = Port.B, right_motor_port: Port = Port.C, gear_motor_port: Port = Port.A, touch_sensor_port: Port = Port.S1, color_sensor_port: Port = Port.S3, ir_sensor_port: Port = Port.S4, ir_beacon_channel: int = 1): super().__init__(wheel_diameter=self.WHEEL_DIAMETER, axle_track=self.AXLE_TRACK, 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.gear_motor = Motor(port=gear_motor_port, positive_direction=Direction.CLOCKWISE) self.touch_sensor = TouchSensor(port=touch_sensor_port) self.color_sensor = ColorSensor(port=color_sensor_port) self.ir_sensor = InfraredSensor(port=ir_sensor_port) self.ir_beacon_channel = ir_beacon_channel def spin_gears(self, speed: float = 1000): while True: if Button.BEACON in self.ir_sensor.buttons( channel=self.ir_beacon_channel): self.gear_motor.run(speed=1000) else: self.gear_motor.stop() def change_screen_when_touched(self): while True: if self.touch_sensor.pressed(): self.screen.load_image(ImageFile.ANGRY) else: self.screen.load_image(ImageFile.FORWARD) def make_noise_when_seeing_black(self): while True: if self.color_sensor.color == Color.BLACK: self.speaker.play_file(file=SoundFile.OUCH) def main(self): self.speaker.play_file(file=SoundFile.YES) Process(target=self.make_noise_when_seeing_black).start() Process(target=self.spin_gears).start() Process(target=self.change_screen_when_touched).start() self.keep_driving_by_ir_beacon(speed=1000)
class EV3TouchSensor(EV3Sensor): def __init__(self, port): EV3Sensor.__init__(self, port) self.sensor = TouchSensor(self.port) def estDeclenchable(self): return self.sensor.pressed()
class Sweep3r(IRBeaconRemoteControlledTank, EV3Brick): WHEEL_DIAMETER = 40 AXLE_TRACK = 110 def __init__( self, left_foot_motor_port: Port = Port.B, right_foot_motor_port: Port = Port.C, medium_motor_port: Port = Port.A, touch_sensor_port: Port = Port.S1, color_sensor_port: Port = Port.S3, ir_sensor_port: Port = Port.S4, ir_beacon_channel: int = 1): super().__init__( wheel_diameter=self.WHEEL_DIAMETER, axle_track=self.AXLE_TRACK, left_motor_port=left_foot_motor_port, right_motor_port=right_foot_motor_port, ir_sensor_port=ir_sensor_port, ir_beacon_channel=ir_beacon_channel) self.medium_motor = Motor(port=medium_motor_port) self.touch_sensor = TouchSensor(port=touch_sensor_port) self.color_sensor = ColorSensor(port=color_sensor_port) self.ir_sensor = InfraredSensor(port=ir_sensor_port) self.ir_beacon_channel = ir_beacon_channel def drill(self): while True: if Button.BEACON in self.ir_sensor.buttons(channel=self.ir_beacon_channel): self.medium_motor.run_angle( speed=1000, rotation_angle=2 * 360, then=Stop.HOLD, wait=True) def move_when_touched(self): while True: if self.touch_sensor.pressed(): self.drive_base.turn(angle=100) def move_when_see_smothing(self): while True: if self.color_sensor.reflection() > 30: self.drive_base.turn(angle=-100) def main(self, speed: float = 1000): self.screen.load_image(ImageFile.PINCHED_MIDDLE) Process(target=self.move_when_touched).start() Process(target=self.move_when_see_smothing).start() Process(target=self.drill).start() self.keep_driving_by_ir_beacon(speed=speed)
def aua(): """ i do not like being touched : auaaaa.... """ beruehrung = TouchSensor(Port.S1) ev3.screen.load_image(ImageFile.NEUTRAL) while (beruehrung.pressed() == False): time.sleep(0.1) ev3.screen.load_image(ImageFile.KNOCKED_OUT) ev3.speaker.play_file(SoundFile.BACKING_ALERT)
class CuriosityRov3r(IRBeaconRemoteControlledTank, EV3Brick): WHEEL_DIAMETER = 35 # milimeters AXLE_TRACK = 130 # milimeters def __init__( self, left_motor_port: Port = Port.B, right_motor_port: Port = Port.C, medium_motor_port: Port = Port.A, touch_sensor_port: Port = Port.S1, color_sensor_port: Port = Port.S3, ir_sensor_port: Port = Port.S4, ir_beacon_channel: int = 1): super().__init__( wheel_diameter=self.WHEEL_DIAMETER, axle_track=self.AXLE_TRACK, 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 = Motor(port=medium_motor_port, positive_direction=Direction.CLOCKWISE) self.touch_sensor = TouchSensor(port=touch_sensor_port) self.color_sensor = ColorSensor(port=color_sensor_port) self.ir_sensor = InfraredSensor(port=ir_sensor_port) self.ir_beacon_channel = ir_beacon_channel def spin_fan(self, speed: float = 1000): while True: if self.color_sensor.reflection() > 20: self.medium_motor.run(speed=speed) else: self.medium_motor.stop() def say_when_touched(self): while True: if self.touch_sensor.pressed(): self.screen.load_image(ImageFile.ANGRY) self.speaker.play_file(file=SoundFile.NO) self.medium_motor.run_time( speed=-500, time=3000, then=Stop.HOLD, wait=True) def main(self, speed: float = 1000): run_parallel( self.say_when_touched, self.spin_fan, self.keep_driving_by_ir_beacon)
def aua_mit_auszeit(auszeit): """ you can make me angry by touching for as long as a duration of auszeit """ beruehrung = TouchSensor(Port.S1) tic = time.time() # start time while (time.time() - tic) < auszeit: if beruehrung.pressed(): ev3.screen.load_image(ImageFile.KNOCKED_OUT) ev3.speaker.play_file(SoundFile.BACKING_ALERT) else: ev3.screen.load_image(ImageFile.NEUTRAL) time.sleep(0.1)
class El3ctricGuitar: NOTES = [1318, 1174, 987, 880, 783, 659, 587, 493, 440, 392, 329, 293] N_NOTES = len(NOTES) def __init__(self, lever_motor_port: Port = Port.D, touch_sensor_port: Port = Port.S1, ir_sensor_port: Port = Port.S4): self.ev3_brick = EV3Brick() self.lever_motor = Motor(port=lever_motor_port, positive_direction=Direction.CLOCKWISE) self.touch_sensor = TouchSensor(port=touch_sensor_port) self.ir_sensor = InfraredSensor(port=ir_sensor_port) def start_up(self): self.ev3_brick.screen.load_image(ImageFile.EV3) self.ev3_brick.light.on(color=Color.ORANGE) self.lever_motor.run_time(speed=50, time=1000, then=Stop.COAST, wait=True) self.lever_motor.run_angle(speed=50, rotation_angle=-30, then=Stop.BRAKE, wait=True) wait(100) self.lever_motor.reset_angle(angle=0) def play_note(self): if not self.touch_sensor.pressed(): raw = sum(self.ir_sensor.distance() for _ in range(4)) / 4 self.ev3_brick.speaker.beep( frequency=self.NOTES[min(int(raw / 5), self.N_NOTES - 1)] - 11 * self.lever_motor.angle(), duration=100) wait(1)
def checkForTouch(): touchSensor = TouchSensor(Port.S3) beepPlz() t = 0 numberOfTouches = 0 while (t < 1000): if ( touchSensor.pressed() ): numberOfTouches += 1 beepPlz() t += 1 pass if (numberOfTouches == 0): routineBlue() elif (numberOfTouches < 5): routineYellow() elif (numberOfTouches < 10): routineGreen() else: routineRed() beepPlz()
def gameLoop(blocks_to_deliver): global brick_state leftMotor = Motor(Port.B) rightMotor = Motor(Port.C) wheels = DriveBase(Motor(Port.B), Motor(Port.C), 56, 114) uSensor = UltrasonicSensor(Port.S4) cSensor = ColorSensor(Port.S3) gSensor = GyroSensor(Port.S2) ts = TouchSensor(Port.S1) while not ts.pressed(): # brick.sound.file(SoundFile.MOTOR_IDLE, 1000) if blocks_delivered >= blocks_to_deliver: brick_state = Status.WAIT if brick_state == Status.WAIT: return elif brick_state == Status.SEARCHING: print('STATE: SEARCHING') t = threading.Thread(target=searchLuggage, args=(wheels, cSensor, gSensor)) t.start() brick_state = Status.COMPUTING elif brick_state == Status.CARRYING_LUGGAGE: print('STATE: CARRYING_LUGGAGE') t = threading.Thread(target=deliverLuggage, args=(wheels, cSensor, )) t.start() brick_state = Status.COMPUTING elif brick_state == Status.PICKING_UP: print('STATE: PICKING_UP') t = threading.Thread(target=doLuggage, args=(wheels, cSensor, True, )) t.start() brick_state = Status.COMPUTING elif brick_state == Status.DEPOSITING: print('STATE: DEPOSITING') t = threading.Thread(target=doLuggage, args=(wheels, cSensor,False, )) t.start() brick_state = Status.COMPUTING
from pybricks import ev3brick as brick from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor) from pybricks.parameters import (Port, Stop, Direction, Button, Color, SoundFile, ImageFile, Align) from pybricks.tools import print, wait, StopWatch from pybricks.robotics import DriveBase left = Motor(Port.B) right = Motor(Port.C) robot = DriveBase(left, right, 56, 114) left_button = TouchSensor(Port.S1) right_button = TouchSensor(Port.S2) # Remote Control Car # Build basic car, use two unattached buttons to control turning. while not left_button.pressed() and right_button.pressed(): robot.drive(100, 0) if left_button.pressed(): while left_button.pressed(): robot.drive(50, 90) if right_button.pressed(): while right_button.pressed(): robot.drive(50, -90)
ev3.speaker.set_volume(100, which='_all_') motor.reset_angle(20) soundMotor.reset_angle(0) motor.run_target(500, 75, wait=True) ev3.light.on(Color.GREEN) watch.reset() # main project loop while shutdown == False: ''' finishing the progamm ''' if Button.LEFT_UP in infraredSensor.buttons(4): shutdown = True ''' checking touch sensor ''' if touchSensor.pressed() == True: if manualLight == False and manualSound == False: manualLight = True manualSound = True else: manualLight = False manualSound = False wait(200) if manualLight == True and manualSound == True: ev3.light.on(Color.RED) manualLightControl() manualSoundControl()
class Gripp3r(EV3Brick): WHEEL_DIAMETER = 26 AXLE_TRACK = 115 def __init__(self, left_motor_port: Port = Port.B, right_motor_port: Port = Port.C, grip_motor_port: Port = Port.A, touch_sensor_port: Port = Port.S1, ir_sensor_port: Port = Port.S4, ir_beacon_channel: int = 1): self.drive_base = DriveBase( left_motor=Motor(port=left_motor_port, positive_direction=Direction.CLOCKWISE), right_motor=Motor(port=right_motor_port, positive_direction=Direction.CLOCKWISE), wheel_diameter=self.WHEEL_DIAMETER, axle_track=self.AXLE_TRACK) self.drive_base.settings( straight_speed=750, # milimeters per second straight_acceleration=750, turn_rate=90, # degrees per second turn_acceleration=90) self.grip_motor = Motor(port=grip_motor_port, positive_direction=Direction.CLOCKWISE) self.touch_sensor = TouchSensor(port=touch_sensor_port) self.ir_sensor = InfraredSensor(port=ir_sensor_port) self.ir_beacon_channel = ir_beacon_channel def keep_driving_by_ir_beacon( self, channel: int = 1, speed: float = 1000 # milimeters per second ): while True: ir_beacon_buttons_pressed = set( self.ir_sensor.buttons(channel=channel)) # forward if ir_beacon_buttons_pressed == {Button.LEFT_UP, Button.RIGHT_UP}: self.drive_base.drive( speed=speed, turn_rate=0 # degrees per second ) # backward elif ir_beacon_buttons_pressed == { Button.LEFT_DOWN, Button.RIGHT_DOWN }: self.drive_base.drive( speed=-speed, turn_rate=0 # degrees per second ) # turn left on the spot elif ir_beacon_buttons_pressed == { Button.LEFT_UP, Button.RIGHT_DOWN }: self.drive_base.drive( speed=0, turn_rate=-90 # degrees per second ) # turn right on the spot elif ir_beacon_buttons_pressed == { Button.LEFT_DOWN, Button.RIGHT_UP }: self.drive_base.drive( speed=0, turn_rate=90 # degrees per second ) # turn left forward elif ir_beacon_buttons_pressed == {Button.LEFT_UP}: self.drive_base.drive( speed=speed, turn_rate=-90 # degrees per second ) # turn right forward elif ir_beacon_buttons_pressed == {Button.RIGHT_UP}: self.drive_base.drive( speed=speed, turn_rate=90 # degrees per second ) # turn left backward elif ir_beacon_buttons_pressed == {Button.LEFT_DOWN}: self.drive_base.drive( speed=-speed, turn_rate=90 # degrees per second ) # turn right backward elif ir_beacon_buttons_pressed == {Button.RIGHT_DOWN}: self.drive_base.drive( speed=-speed, turn_rate=-90 # degrees per second ) # otherwise stop else: self.drive_base.stop() def grip_or_release_by_ir_beacon(self, speed: float = 500): while True: if Button.BEACON in self.ir_sensor.buttons( channel=self.ir_beacon_channel): if self.touch_sensor.pressed(): self.speaker.play_file(file=SoundFile.AIR_RELEASE) self.grip_motor.run_time(speed=speed, time=1000, then=Stop.BRAKE, wait=True) else: self.speaker.play_file(file=SoundFile.AIRBRAKE) self.grip_motor.run(speed=-speed) while not self.touch_sensor.pressed(): pass self.grip_motor.stop() while Button.BEACON in self.ir_sensor.buttons( channel=self.ir_beacon_channel): pass def main(self, speed: float = 1000): self.grip_motor.run_time(speed=-500, time=1000, then=Stop.BRAKE, wait=True) Thread(target=self.grip_or_release_by_ir_beacon).start() self.keep_driving_by_ir_beacon(speed=speed)
class Spik3r(EV3Brick): def __init__( self, sting_motor_port: Port = Port.D, go_motor_port: Port = Port.B, claw_motor_port: Port = Port.A, touch_sensor_port: Port = Port.S1, color_sensor_port: Port = Port.S3, ir_sensor_port: Port = Port.S4, ir_beacon_channel: int = 1): self.sting_motor = Motor(port=sting_motor_port, positive_direction=Direction.CLOCKWISE) self.go_motor = Motor(port=go_motor_port, positive_direction=Direction.CLOCKWISE) self.claw_motor = Motor(port=claw_motor_port, positive_direction=Direction.CLOCKWISE) self.ir_sensor = InfraredSensor(port=ir_sensor_port) self.ir_beacon_channel = ir_beacon_channel self.touch_sensor = TouchSensor(port=touch_sensor_port) self.color_sensor = ColorSensor(port=color_sensor_port) def sting_by_ir_beacon(self): while True: ir_buttons_pressed = set(self.ir_sensor.buttons(channel=self.ir_beacon_channel)) if ir_buttons_pressed == {Button.BEACON}: self.sting_motor.run_angle( speed=-750, rotation_angle=220, then=Stop.HOLD, wait=False) self.speaker.play_file(file=SoundFile.EV3) self.sting_motor.run_time( speed=-1000, time=1000, then=Stop.HOLD, wait=True) self.sting_motor.run_time( speed=1000, time=1000, then=Stop.HOLD, wait=True) while Button.BEACON in self.ir_sensor.buttons(channel=self.ir_beacon_channel): pass def be_noisy_to_people(self): while True: if self.color_sensor.reflection() > 30: for i in range(4): self.speaker.play_file(file=SoundFile.ERROR_ALARM) def pinch_if_touched(self): while True: if self.touch_sensor.pressed(): self.claw_motor.run_time( speed=500, time=1000, then=Stop.HOLD, wait=True) self.claw_motor.run_time( speed=-500, time=0.3 * 1000, then=Stop.HOLD, wait=True) def keep_driving_by_ir_beacon(self): while True: ir_buttons_pressed = set(self.ir_sensor.buttons(channel=self.ir_beacon_channel)) if ir_buttons_pressed == {Button.RIGHT_UP, Button.LEFT_UP}: self.go_motor.run(speed=910) elif ir_buttons_pressed == {Button.RIGHT_UP}: self.go_motor.run(speed=-1000) else: self.go_motor.stop() def main(self): self.screen.load_image(ImageFile.EVIL) run_parallel( self.be_noisy_to_people, self.sting_by_ir_beacon, self.pinch_if_touched, self.keep_driving_by_ir_beacon)
def send_note_off(note): if note in my_notes: pipe.write("80 " + note + " 00") else: # mistake ? pipe.write(ALL_NOTES_OFF) key_1_on = False key_2_on = False key_3_on = False key_4_on = False pipe = open("./midipipe", "w") send_note_off('ALL') while True : if ts1.pressed() == True : if key_1_on == False : send_note_on(my_notes[0]) key_1_on = True else: if key_1_on == True : send_note_off(my_notes[0]) key_1_on = False if ts2.pressed() == True : if key_2_on == False : send_note_on(my_notes[1]) key_2_on = True else: if key_2_on == True : send_note_off(my_notes[1])
class R3ptar: """ R3ptar can be driven around by the IR Remote Control, strikes when the Beacon button is pressed, and hisses when the Touch Sensor is pressed (inspiration from LEGO Mindstorms EV3 Home Edition: R3ptar: Tutorial #4) """ def __init__(self, steering_motor_port: Port = Port.A, driving_motor_port: Port = Port.B, striking_motor_port: Port = Port.D, touch_sensor_port: Port = Port.S1, ir_sensor_port: Port = Port.S4, ir_beacon_channel: int = 1): self.ev3_brick = EV3Brick() self.steering_motor = Motor(port=steering_motor_port, positive_direction=Direction.CLOCKWISE) self.driving_motor = Motor(port=driving_motor_port, positive_direction=Direction.CLOCKWISE) self.striking_motor = Motor(port=striking_motor_port, positive_direction=Direction.CLOCKWISE) self.touch_sensor = TouchSensor(port=touch_sensor_port) self.ir_sensor = InfraredSensor(port=ir_sensor_port) self.ir_beacon_channel = ir_beacon_channel def drive_by_ir_beacon( self, speed: float = 1000, # mm/s ): ir_beacons_pressed = \ set(self.ir_sensor.buttons(channel=self.ir_beacon_channel)) if ir_beacons_pressed == {Button.LEFT_UP, Button.RIGHT_UP}: self.driving_motor.run(speed=speed) elif ir_beacons_pressed == {Button.LEFT_DOWN, Button.RIGHT_DOWN}: self.driving_motor.run(speed=-speed) elif ir_beacons_pressed == {Button.LEFT_UP}: self.steering_motor.run(speed=-500) self.driving_motor.run(speed=speed) elif ir_beacons_pressed == {Button.RIGHT_UP}: self.steering_motor.run(speed=500) self.driving_motor.run(speed=speed) elif ir_beacons_pressed == {Button.LEFT_DOWN}: self.steering_motor.run(speed=-500) self.driving_motor.run(speed=-speed) elif ir_beacons_pressed == {Button.RIGHT_DOWN}: self.steering_motor.run(speed=500) self.driving_motor.run(speed=-speed) else: self.steering_motor.hold() self.driving_motor.stop() def strike_by_ir_beacon(self, speed: float = 1000): if Button.BEACON in \ self.ir_sensor.buttons(channel=self.ir_beacon_channel): self.striking_motor.run_time(speed=speed, time=1000, then=Stop.COAST, wait=True) self.striking_motor.run_time(speed=-speed, time=1000, then=Stop.COAST, wait=True) while Button.BEACON in \ self.ir_sensor.buttons(channel=self.ir_beacon_channel): pass def hiss_if_touched(self): if self.touch_sensor.pressed(): self.ev3_brick.speaker.play_file(file=SoundFile.SNAKE_HISS) def main(self, speed: float = 1000): """ R3ptar's main program performing various capabilities """ while True: self.drive_by_ir_beacon(speed=speed) self.strike_by_ir_beacon(speed=speed) self.hiss_if_touched() wait(1)
#!/usr/bin/env pybricks-micropython from pybricks import ev3brick as brick from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor) from pybricks.parameters import (Port, Stop, Direction, Button, Color, SoundFile, ImageFile, Align) from pybricks.tools import print, wait, StopWatch from pybricks.robotics import DriveBase left = Motor(Port.B) right = Motor(Port.C) robot = DriveBase(left, right, 56, 114) button = TouchSensor(Port.S1) # If Else Beeping # Build basic car, use two unattached buttons to control turning. while True: if button.pressed(): brick.sound.beep(200) else: brick.sound.beep(500)
speed = 0 # This is the main part of the program. It is a loop that repeats # endlessly. # # First, increase the speed variable if Touch Sensor 1 is pressed. # Second, decrease the speed variable if Touch Sensor 2 is pressed. # Finally, the robot updates its speed if the speed variable was # changed, and displays it on the screen. # # Then the loop starts over after a brief pause. while True: # Check whether Touch Sensor 1 is pressed, and increase the speed # variable by 10 mm per second if it is. if increase_touch_sensor.pressed(): speed += 10 # Check whether Touch Sensor 2 is pressed, and decrease the speed # variable by 10 mm per second if it is. if decrease_touch_sensor.pressed(): speed -= 10 # If the speed variable has changed, update the speed of the robot # and display the new speed in the center of the screen. if speed != old_speed: old_speed = speed robot.drive(speed, 0) brick.display.clear() brick.display.text(speed, (85, 70))
playerPositionX = 0 if enemy1PositionY > screenHeight: enemy1PositionY = -enemyHeight enemy1PositionX = randint(0, screenWidth - 60) if enemy2PositionY > screenHeight: enemy2PositionY = -enemyHeight enemy2PositionX = randint(0, screenWidth - 90) enemy1PositionY += 12 enemy2PositionY += 12 #if playerPositionX >= enemy1PositionX and playerPositionX <= (enemy1PositionX + enemyWidth) and playerPositionY >= enemy1PositionY and playerPositionY <= (enemy1PositionY + enemyHeight): # break # if (playerPositionX + playerWidth) >= enemy1PositionX and (playerPositionX + playerWidth) <= (enemy1PositionX + enemyWidth) and playerPositionY >= enemy1PositionY and playerPositionY <= (enemy1PositionY + enemyHeight): # break if touchR.pressed(): break # motorL.run_angle(100, gyro.angle() * 100, Stop.COAST, False) #motorR.run_angle(100, -(gyro.angle() * 100), Stop.COAST, False) playerPositionX -= gyro.angle() wait(60) brick.display.clear() brick.sound.file(SoundFile.GAME_OVER) brick.display.image('GameOver.png') wait(3000)
sense = AnalogSensor(Port.S3, False) uart = UARTDevice(Port.S3, 9600, timeout=2000) def VibrateMotor(): joystick.run_angle(1400, 180) def CompletionVibrate(): indicator.run_time(1400, 700) uart.clear() while True: while not (xBut.pressed() | yBut.pressed()): wait(100) if uart.waiting() > 0: msg = uart.read(1) uart.clear() #should clear automatically, but needed some help msg = str(msg) if msg[2] == 'T': VibrateMotor() print('|', end='') elif msg[2] == 'F': print('.', end='') elif msg[2] == 'C': CompletionVibrate() while (xBut.pressed() | yBut.pressed()):
print('IMU:', imu) print('deltaT: ', deltaT) print('tempy', temp_yA) print('tempz:', temp_zA) if deltaT != '': velY = float(temp_yA) * float(deltaT) velZ = float(temp_zA) * float(deltaT) print('velY: ', velY) print('velZ: ', velZ) # Tuning factors to determine the directions of the wand movement if touch.pressed() == True: if velZ > 3: ev3.speaker.beep(393) wait(200) elif velZ < -1.5: ev3.speaker.beep(436.7) wait(200) elif velY > 1: ev3.speaker.beep(491.2) wait(200) elif velY < -2: ev3.speaker.beep(524) wait(200) else: if velZ > 3: ev3.speaker.beep(262)
# Then make it go upwards slowly (15 degrees per second) until # the Color Sensor detects the white beam. Then reset the motor # angle to make this the zero point. Finally, hold the motor # in place so it does not move. elbow_motor.run_time(-30, 1000) elbow_motor.run(15) while elbow_sensor.reflection() < 32: wait(10) elbow_motor.reset_angle(0) elbow_motor.hold() # Initialize the base. First rotate it until the Touch Sensor # in the base is pressed. Reset the motor angle to make this # the zero point. Then hold the motor in place so it does not move. base_motor.run(-60) while not base_switch.pressed(): wait(10) base_motor.reset_angle(0) base_motor.hold() # Initialize the gripper. First rotate the motor until it stalls. # Stalling means that it cannot move any further. This position # corresponds to the closed position. Then rotate the motor # by 90 degrees such that the gripper is open. gripper_motor.run_until_stalled(200, then=Stop.COAST, duty_limit=50) gripper_motor.reset_angle(0) gripper_motor.run_target(200, -90) def robot_pick(position): # This function makes the robot base rotate to the indicated
B = Motor(port=Port.B) #Initialize the sensors. #us = UltrasonicSensor(port=Port.S1) colorSensorV = ColorSensor(port=Port.S2) colorSensorH = ColorSensor(port=Port.S3) touch = TouchSensor(port=Port.S4) RED = 10 GREEN = 10 BLUE = 20 robot = DriveBase(A, B, wheel_diameter=56, axle_track=130) start = True startag = False while (start): if (touch.pressed()): startag = True while (startag): print(colorSensorH.rgb()) print(colorSensorV.rgb()) (red, green, blue) = colorSensorV.rgb() is_black = red < RED or green < GREEN or blue < BLUE (red1, green1, blue1) = colorSensorH.rgb() is_black2 = red1 < RED or green1 < GREEN or blue1 < BLUE if not (is_black or is_black2): robot.drive(200, 0) elif (is_black): robot.drive(120, 90) elif (is_black2): robot.drive(120, -90) else:
right.stop() left.stop() if ultrasonicSensor.distance() < 485: right.run(-200) left.run(-215) wait(100) right.stop() left.stop() wait(1000) brick.sound.beep() while not Button.CENTER in brick.Buttons(): wait(10) TouchSensorR = TouchSensor(Port.S2) TouchSensorL = TouchSensor(Port.S4) while TouchSensorR.pressed() == False and TouchSensorL.pressed() == False: right.run(360) left.run(361) right.stop() left.stop() while ultrasonicSensor.distance() > 510 or ultrasonicSensor.distance() < 490: if ultrasonicSensor.distance() > 510: right.run(200) left.run(210) wait(100) right.stop() left.stop() if ultrasonicSensor.distance() < 490: right.run(-200)
# and insert the next set of colored objects. while True: # Get the feed motor in the correct starting position. # This is done by running the motor forward until it stalls. This # means that it cannot move any further. From this end point, the motor # rotates backward by 180 degrees. Then it is in the starting position. feed_motor.run_until_stalled(120, duty_limit=30) feed_motor.run_angle(450, -200) # Get the conveyor belt motor in the correct starting position. # This is done by first running the belt motor backward until the # touch sensor becomes pressed. Then the motor stops, and the the angle is # reset to zero. This means that when it rotates backward to zero later # on, it returns to this starting position. belt_motor.run(-500) while not touch_sensor.pressed(): pass belt_motor.stop() wait(1000) belt_motor.reset_angle(0) # When we scan the objects, we store all the color numbers in a list. # We start with an empty list. It will grow as we add colors to it. color_list = [] # This loop scans the colors of the objects. It repeats until 8 objects # are scanned and placed in the chute. This is done by repeating the loop # while the length of the list is still less than 8. while len(color_list) < 8: # Show an arrow that points to the color sensor. ev3.screen.load_image(ImageFile.RIGHT)
class Ev3rstorm(EV3Brick): WHEEL_DIAMETER = 26 # milimeters AXLE_TRACK = 102 # milimeters def __init__(self, left_foot_motor_port: Port = Port.B, right_foot_motor_port: Port = Port.C, bazooka_blast_motor_port: Port = Port.A, touch_sensor_port: Port = Port.S1, color_sensor_port: Port = Port.S3, ir_sensor_port: Port = Port.S4, ir_beacon_channel: int = 1): self.drive_base = DriveBase( left_motor=Motor(port=left_foot_motor_port, positive_direction=Direction.CLOCKWISE), right_motor=Motor(port=right_foot_motor_port, positive_direction=Direction.CLOCKWISE), wheel_diameter=self.WHEEL_DIAMETER, axle_track=self.AXLE_TRACK) self.bazooka_blast_motor = Motor( port=bazooka_blast_motor_port, positive_direction=Direction.CLOCKWISE) self.touch_sensor = TouchSensor(port=touch_sensor_port) self.color_sensor = ColorSensor(port=color_sensor_port) self.ir_sensor = InfraredSensor(port=ir_sensor_port) self.ir_beacon_channel = ir_beacon_channel def drive_once_by_ir_beacon( self, speed: float = 1000, # mm/s turn_rate: float = 90 # rotational speed deg/s ): ir_beacon_button_pressed = set( self.ir_sensor.buttons(channel=self.ir_beacon_channel)) # forward if ir_beacon_button_pressed == {Button.LEFT_UP, Button.RIGHT_UP}: self.drive_base.drive(speed=speed, turn_rate=0) # backward elif ir_beacon_button_pressed == {Button.LEFT_DOWN, Button.RIGHT_DOWN}: self.drive_base.drive(speed=-speed, turn_rate=0) # turn left on the spot elif ir_beacon_button_pressed == {Button.LEFT_UP, Button.RIGHT_DOWN}: self.drive_base.drive(speed=0, turn_rate=-turn_rate) # turn right on the spot elif ir_beacon_button_pressed == {Button.RIGHT_UP, Button.LEFT_DOWN}: self.drive_base.drive(speed=0, turn_rate=turn_rate) # turn left forward elif ir_beacon_button_pressed == {Button.LEFT_UP}: self.drive_base.drive(speed=speed, turn_rate=-turn_rate) # turn right forward elif ir_beacon_button_pressed == {Button.RIGHT_UP}: self.drive_base.drive(speed=speed, turn_rate=turn_rate) # turn left backward elif ir_beacon_button_pressed == {Button.LEFT_DOWN}: self.drive_base.drive(speed=-speed, turn_rate=turn_rate) # turn right backward elif ir_beacon_button_pressed == {Button.RIGHT_DOWN}: self.drive_base.drive(speed=-speed, turn_rate=-turn_rate) # otherwise stop else: self.drive_base.stop() def keep_driving_by_ir_beacon( self, speed: float = 1000, # mm/s turn_rate: float = 90 # rotational speed deg/s ): while True: self.drive_once_by_ir_beacon(speed=speed, turn_rate=turn_rate) def dance_whenever_ir_beacon_pressed(self): while True: while Button.BEACON in self.ir_sensor.buttons( channel=self.ir_beacon_channel): self.drive_base.turn(angle=randint(-360, 360)) def keep_detecting_objects_by_ir_sensor(self): while True: if self.ir_sensor.distance() < 25: self.light.on(color=Color.RED) self.speaker.play_file(file=SoundFile.OBJECT) self.speaker.play_file(file=SoundFile.DETECTED) self.speaker.play_file(file=SoundFile.ERROR_ALARM) else: self.light.off() def blast_bazooka_whenever_touched(self): MEDIUM_MOTOR_N_ROTATIONS_PER_BLAST = 3 MEDIUM_MOTOR_ROTATIONAL_DEGREES_PER_BLAST = MEDIUM_MOTOR_N_ROTATIONS_PER_BLAST * 360 while True: if self.touch_sensor.pressed(): if self.color_sensor.ambient() < 5: # 15 not dark enough self.speaker.play_file(file=SoundFile.UP) self.bazooka_blast_motor.run_angle( speed=2 * MEDIUM_MOTOR_ROTATIONAL_DEGREES_PER_BLAST, # shoot quickly in half a second rotation_angle= -MEDIUM_MOTOR_ROTATIONAL_DEGREES_PER_BLAST, then=Stop.HOLD, wait=True) self.speaker.play_file(file=SoundFile.LAUGHING_1) else: self.speaker.play_file(file=SoundFile.DOWN) self.bazooka_blast_motor.run_angle( speed=2 * MEDIUM_MOTOR_ROTATIONAL_DEGREES_PER_BLAST, # shoot quickly in half a second rotation_angle= MEDIUM_MOTOR_ROTATIONAL_DEGREES_PER_BLAST, then=Stop.HOLD, wait=True) self.speaker.play_file(file=SoundFile.LAUGHING_2) def main( self, driving_speed: float = 1000 # mm/s ): self.screen.load_image(ImageFile.TARGET) # FIXME: following thread seems to fail to run Thread(target=self.dance_whenever_ir_beacon_pressed).start() # 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 # Thread(target=self.keep_detecting_objects_by_ir_sensor).start() Thread(target=self.blast_bazooka_whenever_touched).start() self.keep_driving_by_ir_beacon(speed=driving_speed)
class Ev3rstorm(IRBeaconRemoteControlledTank, EV3Brick): WHEEL_DIAMETER = 26 # milimeters AXLE_TRACK = 102 # milimeters def __init__(self, left_leg_motor_port: Port = Port.B, right_leg_motor_port: Port = Port.C, bazooka_blast_motor_port: Port = Port.A, touch_sensor_port: Port = Port.S1, color_sensor_port: Port = Port.S3, ir_sensor_port: Port = Port.S4, ir_beacon_channel: int = 1): super().__init__(wheel_diameter=self.WHEEL_DIAMETER, axle_track=self.AXLE_TRACK, left_motor_port=left_leg_motor_port, right_motor_port=right_leg_motor_port, ir_sensor_port=ir_sensor_port, ir_beacon_channel=ir_beacon_channel) self.bazooka_blast_motor = Motor( port=bazooka_blast_motor_port, positive_direction=Direction.CLOCKWISE) self.touch_sensor = TouchSensor(port=touch_sensor_port) self.color_sensor = ColorSensor(port=color_sensor_port) def blast_bazooka_whenever_touched(self): """ Ev3rstorm blasts his bazooka when his Touch Sensor is pressed (inspiration from LEGO Mindstorms EV3 Home Edition: Ev3rstorm: Tutorial #5) """ MEDIUM_MOTOR_N_ROTATIONS_PER_BLAST = 3 MEDIUM_MOTOR_ROTATIONAL_DEGREES_PER_BLAST = MEDIUM_MOTOR_N_ROTATIONS_PER_BLAST * 360 while True: if self.touch_sensor.pressed(): if self.color_sensor.ambient() < 5: # 15 not dark enough self.speaker.play_file(file=SoundFile.UP) self.bazooka_blast_motor.run_angle( speed=2 * MEDIUM_MOTOR_ROTATIONAL_DEGREES_PER_BLAST, # shoot quickly in half a second rotation_angle= -MEDIUM_MOTOR_ROTATIONAL_DEGREES_PER_BLAST, then=Stop.HOLD, wait=True) else: self.speaker.play_file(file=SoundFile.DOWN) self.bazooka_blast_motor.run_angle( speed=2 * MEDIUM_MOTOR_ROTATIONAL_DEGREES_PER_BLAST, # shoot quickly in half a second rotation_angle= MEDIUM_MOTOR_ROTATIONAL_DEGREES_PER_BLAST, then=Stop.HOLD, wait=True) def main( self, driving_speed: float = 1000 # mm/s ): """ Ev3rstorm's main program performing various capabilities """ self.screen.load_image(ImageFile.TARGET) Thread(target=self.blast_bazooka_whenever_touched).start() self.keep_driving_by_ir_beacon(speed=driving_speed)
class Catapult(IRBeaconRemoteControlledTank, EV3Brick): WHEEL_DIAMETER = 23 AXLE_TRACK = 65 def __init__(self, left_motor_port: Port = Port.B, right_motor_port: Port = Port.C, catapult_motor_port: Port = Port.A, touch_sensor_port: Port = Port.S1, color_sensor_port: Port = Port.S3, ir_sensor_port: Port = Port.S4, ir_beacon_channel: int = 1): super().__init__(wheel_diameter=self.WHEEL_DIAMETER, axle_track=self.AXLE_TRACK, 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 = Motor(port=catapult_motor_port, positive_direction=Direction.CLOCKWISE) self.touch_sensor = TouchSensor(port=touch_sensor_port) self.color_sensor = ColorSensor(port=color_sensor_port) self.ir_sensor = InfraredSensor(port=ir_sensor_port) self.ir_beacon_channel = ir_beacon_channel def scan_colors(self): while True: if self.color_sensor.color() == Color.YELLOW: pass elif self.color_sensor.color() == Color.WHITE: self.speaker.play_file(file=SoundFile.GOOD) def make_noise_when_touched(self): while True: if self.touch_sensor.pressed(): self.speaker.play_file(file=SoundFile.OUCH) def throw_by_ir_beacon(self): while True: if Button.BEACON in self.ir_sensor.buttons( channel=self.ir_beacon_channel): self.catapult_motor.run_angle(speed=3500, rotation_angle=-150, then=Stop.HOLD, wait=True) self.catapult_motor.run_angle(speed=3500, rotation_angle=150, then=Stop.HOLD, wait=True) while Button.BEACON in self.ir_sensor.buttons( channel=self.ir_beacon_channel): pass def main(self): self.speaker.play_file(file=SoundFile.YES) Process(target=self.make_noise_when_touched).start() Process(target=self.throw_by_ir_beacon).start() Process(target=self.scan_colors).start() self.keep_driving_by_ir_beacon(speed=1000)
def jugar_piedra(): pass def jugar_papel(): for motor in motors: motor.track_target(GRADOS) wait(500) def jugar_tijera(): motor_tijera.track_target(GRADOS) motor_papel.track_target(GRADOS) wait(500) jugadas = [jugar_papel, jugar_tijera, jugar_piedra] jugadas_names = ["papel", "tijera", "piedra"] jugadas_sonidos = ["papel.wav", "tijera.mp3", "piedra.mp3"] pulsador = TouchSensor(Port.S1) while True: if pulsador.pressed(): n = random.randint(0, len(jugadas) - 1) jugadas[n]() print(jugadas_names[n]) reset() brick.sound.file(jugadas_sonidos[n])
# This drives at 100 mm/sec straight import random a = random.randint(-90, 90) while True: robot.drive(200, 0) while irSensor.distance() < 50: a = random.randint(0, 100) b = random.randint(45, 180) c = random.randint(-180, -45) robot.straight(-50) if a <= 50: robot.turn(b) else: robot.turn(c) while touchsensor.pressed(): robot.straight(-50) a = random.randint(0, 100) b = random.randint(45, 180) c = random.randint(-180, -45) if a <= 50: robot.turn(b) sound.beep() else: robot.turn(c) sound.beep