def GreenMission(): # Green Run (Boccia Frame, Boccia Share, and Dance Mission) #!/usr/bin/env pybricks-micropython from pybricks.hubs import EV3Brick from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor) from pybricks.parameters import Port, Stop, Direction, Button, Color from pybricks.tools import wait, StopWatch, DataLog from pybricks.robotics import DriveBase from pybricks.media.ev3dev import SoundFile, ImageFile # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. # Create your objects here. ev3 = EV3Brick() left_motor = Motor(Port.C) right_motor = Motor(Port.B) medium_motor = Motor(Port.A) front_largeMotor = Motor(Port.D) wheel_diameter = 56 axle_track = 114.3 robot = DriveBase(left_motor, right_motor, wheel_diameter, axle_track) ## Write your code here: robot.settings(300) # Speed Change ## The robot goes straight until the Boccia Mission's target. robot.straight(1050) ## The robot moves the large motor down to drop the cubes into the target. front_largeMotor.run_angle(80, 110, then=Stop.HOLD, wait=True) ## BOCCIA SHARE !!! robot.straight(-220) robot.turn(-100) robot.straight(135) front_largeMotor.run_angle(-80, 105, then=Stop.HOLD, wait=True) robot.straight(-60) robot.turn(-100) robot.straight(-80) # This is the DANCE Mission! robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.stop(Stop.BRAKE)
def BlackMission(): # Black Run (Innovatice Architecture, Health Units, Hopscotch, Bringing Slide Figures back HOME) #!/usr/bin/env pybricks-micropython from pybricks.hubs import EV3Brick from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor) from pybricks.parameters import Port, Stop, Direction, Button, Color from pybricks.tools import wait, StopWatch, DataLog from pybricks.robotics import DriveBase from pybricks.media.ev3dev import SoundFile, ImageFile # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. # define your variables ev3 = EV3Brick() left_motor = Motor(Port.C) right_motor = Motor(Port.B) medium_motor = Motor(Port.A) large_motor = Motor(Port.D) wheel_diameter = 56 axle_track = 115 line_sensor = ColorSensor(Port.S2) line_sensor1 = ColorSensor(Port.S3) robot = DriveBase(left_motor, right_motor, wheel_diameter, axle_track) robot.settings(140) # To change the SPEED # Pushing our innovative architecture and the health units. robot.straight(350) robot.straight(-97) robot.turn(-40) robot.straight(40) # Dropping the cube into hopscotch area and returning back to base. large_motor.run_angle(30,50,then=Stop.HOLD, wait=True) ev3.speaker.beep(15) large_motor.run_angle(60,-180,then=Stop.HOLD, wait=False) robot.turn(30) robot.straight(-300) # (In base) Wait block for attachment change. wait(6000) # Bringing slide figures to base. robot.stop(Stop.BRAKE) robot.settings(240) # Speed change ev3.speaker.beep(20) robot.straight(390) ev3.speaker.beep(300) large_motor.run_angle(60,130) robot.straight(-90) large_motor.run_angle(60,40,then=Stop.HOLD, wait=True) robot.straight(-500) large_motor.run_angle(60,-100,then=Stop.HOLD, wait=True) robot.stop(Stop.BRAKE)
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 RedMission(): # Red Run (Bench Mission (including backrest removal)) #!/usr/bin/env pybricks-micropython from pybricks.hubs import EV3Brick from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor) from pybricks.parameters import Port, Stop, Direction, Button, Color from pybricks.tools import wait, StopWatch, DataLog from pybricks.robotics import DriveBase from pybricks.media.ev3dev import SoundFile, ImageFile # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. # Create your objects here. ev3 = EV3Brick() left_motor = Motor(Port.C) right_motor = Motor(Port.B) wheel_diameter = 56 axle_track = 115 robot = DriveBase(left_motor, right_motor, wheel_diameter, axle_track) Medium_Motor = Motor(Port.A) Large_Motor = Motor(Port.D) leftcolorsensor = ColorSensor(Port.S3) rightcolorsensor = ColorSensor(Port.S2) robot.settings(300) # Speed change # Starts off from base and approaches the bench model. robot.straight(200) robot.turn(-115) # Removes backrest and flattens the bench. Medium_Motor.run_angle(300, 135, then=Stop.HOLD, wait=True) robot.stop(Stop.BRAKE) robot.settings(500) robot.turn(-60) # Returns to base. robot.straight(400) Large_Motor.run_angle(80, 95, then=Stop.HOLD, wait=True) robot.stop(Stop.BRAKE)
class Parts(): def __init__(self): self.ev3 = EV3Brick() #self.motorleft = Motor(Port.D) self.motorright = Motor(Port.A) self.right = ColorSensor(Port.S3) self.left = ColorSensor(Port.S2) #self.gyro = GyroSensor(Port.S4) self.leftambient = 0 self.rightambient = 0 self.delta = 0 #self.arrayturnleft = [0, 0, 0, 0, 0] #self.arrayturnright = [0, 0, 0, 0, 0] #self.savefile = open("testcheck.txt", "a") def check(self): self.leftambient = self.left.ambient() self.rightambient = self.right.ambient() self.delta = self.leftambient - self.rightambient #return [self.leftambient, self.rightambient] return self.delta #print("LEFT: %d | RIGHT: %d" % (self.leftambient, self.rightambient)) def checkGyro(self): print(self.gyro) def whileCheck(self): #for x in range(5): # self.arrayturnleft[x], self.arrayturnleft[x] = self.check() # time.sleep(0.1) #self.delta = (sum(self.arrayturnleft)/len(self.arrayturnleft) - sum(self.arrayturnright)/len(self.arrayturnright)) #print(self.delta) if (self.check() > 0.5): #self.delta if (self.motorright.angle() > 45): print("OVERRIDE") else: self.motorright.run_angle(10, -10) #10 left print("TURNED THIS WAY") elif (self.check() < -0.5): #self.delta if (self.motorright.angle() < -45): print("OVERRIDE") else: self.motorright.run_angle(10, 10) #-10 right print("TURNED THAT WAY") #brickthing.savefile.write("LEFT: %d | RIGHT: %d" % (brickthing.leftambient, brickthing.rightambient))''' def rapidcheck(self): while brickthing.check() > 1 or brickthing.check( ) < -1: #while True: | self.delta, self.delta brickthing.whileCheck() print(brickthing.check()) print("BREAK") time.sleep(5)
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 kalibriere(self): headmotor = Motor(Port.A, Direction.COUNTERCLOCKWISE) farbsensor = ColorSensor(Port.S3) headmotor.run_until_stalled(speed=10, duty_limit=50) debug('winkel=' + str(headmotor.angle())) headmotor.run_target(speed=10, target_angle=-120, wait=False) while (farbsensor.reflection() < 10): # & (headmotor.speed() != 0): debug('farbwert=' + str(farbsensor.reflection())) time.sleep(0.1) headmotor.stop() headmotor.run_angle(speed=10, rotation_angle=15) debug(str(farbsensor.reflection())) # winkel auf 0 headmotor.reset_angle(0) self.angle_ist = 0 self._schreibe_winkel()
def BlackMission(): #!/usr/bin/env pybricks-micropython from pybricks.hubs import EV3Brick from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor) from pybricks.parameters import Port, Stop, Direction, Button, Color from pybricks.tools import wait, StopWatch, DataLog from pybricks.robotics import DriveBase from pybricks.media.ev3dev import SoundFile, ImageFile # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. #define your variables ev3 = EV3Brick() left_motor = Motor(Port.C) right_motor = Motor(Port.B) medium_motor = Motor(Port.A) large_motor = Motor(Port.D) wheel_diameter = 56 axle_track = 115 line_sensor = ColorSensor(Port.S2) line_sensor1 = ColorSensor(Port.S3) robot = DriveBase(left_motor, right_motor, wheel_diameter, axle_track) robot.settings(140) robot.straight(350) robot.straight(-97) robot.turn(-40) large_motor.run_angle(30,50,then=Stop.HOLD, wait=True) ev3.speaker.beep(15) large_motor.run_angle(60,-180,then=Stop.HOLD, wait=False) robot.straight(-300) wait(10000) robot.stop(Stop.BRAKE) robot.settings(240) ev3.speaker.beep(20) robot.straight(390) ev3.speaker.beep(300) large_motor.run_angle(60,130) robot.straight(-90) large_motor.run_angle(60,40,then=Stop.HOLD, wait=True) robot.straight(-500) # test straight after beep and see if it works... robot.stop(Stop.BRAKE)
def GreenMission(): # Green Run (Boccia Frame, Boccia Share, and Dance Mission) # RIGHT BUTTON #!/usr/bin/env pybricks-micropython from pybricks.hubs import EV3Brick from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor) from pybricks.parameters import Port, Stop, Direction, Button, Color from pybricks.tools import wait, StopWatch, DataLog from pybricks.robotics import DriveBase from pybricks.media.ev3dev import SoundFile, ImageFile # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. # Create your objects here. ev3 = EV3Brick() left_motor = Motor(Port.C) right_motor = Motor(Port.B) medium_motor = Motor(Port.A) front_largeMotor = Motor(Port.D) wheel_diameter = 56 axle_track = 114.3 robot = DriveBase(left_motor, right_motor, wheel_diameter, axle_track) robot.settings(300) robot.straight(1050) front_largeMotor.run_angle(80, 110, then=Stop.HOLD, wait=True) front_largeMotor.run_angle(80, -110, then=Stop.HOLD, wait=False) robot.straight(-1100) wait(7000) robot.straight(380) front_largeMotor.run_angle(60,90) robot.straight(-150) front_largeMotor.run_angle(60,40,then=Stop.HOLD, wait=True) robot.straight(-500) front_largeMotor.run_angle(60,-130,then=Stop.HOLD, wait=True) ev3.speaker.beep(7000) robot.stop(Stop.BRAKE)
def BlackMission( ): # Black Run (Innovatice Architecture, Health Units, Hopscotch, Bringing Slide Figures back HOME) #!/usr/bin/env pybricks-micropython from pybricks.hubs import EV3Brick from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor) from pybricks.parameters import Port, Stop, Direction, Button, Color from pybricks.tools import wait, StopWatch, DataLog from pybricks.robotics import DriveBase from pybricks.media.ev3dev import SoundFile, ImageFile # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. # define your variables ev3 = EV3Brick() left_motor = Motor(Port.C) right_motor = Motor(Port.B) medium_motor = Motor(Port.A) large_motor = Motor(Port.D) wheel_diameter = 56 axle_track = 115 line_sensor = ColorSensor(Port.S2) line_sensor1 = ColorSensor(Port.S3) robot = DriveBase(left_motor, right_motor, wheel_diameter, axle_track) robot.settings(500) # To change the SPEED # Pushing our innovative architecture and the health units. robot.straight(-350) robot.straight(50) robot.turn(-15) robot.straight(-70) robot.turn(-206) robot.straight(15) large_motor.run_angle(60, 80, then=Stop.HOLD, wait=True) robot.stop(Stop.BRAKE) robot.straight(-340) robot.stop(Stop.BRAKE)
def run(): motor_b = Motor(Port.B) motor_c = Motor(Port.C) Gyro = GyroSensor(Port.S1) Gyro.reset_angle(0) motor_b.run_angle(500, 720, Stop.BRAKE, False) motor_c.run_angle(500, 720, Stop.BRAKE, True) wait(10) while Gyro.angle() <= 180: motor_c.run(100) motor_b.run(-100) wait(10) motor_b.run_angle(500, 720, Stop.BRAKE, False) motor_c.run_angle(500, 720, Stop.BRAKE, True)
class SmallMotor: def __init__(self, ev3, port): self.ev3 = ev3 self.motor = Motor(port, Direction.COUNTERCLOCKWISE) self.motor.reset_angle(0) def reset(self): self.motor.run_until_stalled(100) self.motor.run_angle(800, -300) self.motor.reset_angle(0) def moveTo(self, angle, speed = 800, wait = False): print(self.motor.angle()) self.motor.run_target(speed, angle, Stop.HOLD, wait) print(self.motor.angle()) def move(self, speed = 20): self.motor.run(speed) def brake(self): self.motor.brake()
def doLuggage(wheels, cSensor, pickup=True): global brick_state, blocks_delivered if pickup: stapler = Motor(Port.A, Direction.CLOCKWISE) stapler.run_until_stalled(20, Stop.BRAKE) brick.sound.file(SoundFile.HORN_1,1000) brick_state = Status.CARRYING_LUGGAGE else: stapler = Motor(Port.A, Direction.COUNTERCLOCKWISE) You_spin_me_right_round_baby_Right_round_like_a_record_baby_Right_round_round_round(wheels, 180) stapler.run_angle(50, 100, Stop.BRAKE) wheels.drive_time(-50, 0, 1000) stapler.run_angle(50, 25, Stop.BRAKE) wheels.drive_time(-113/3, 45/3, 1000*3) wheels.drive_time(0,45/3,1000*3) blocks_delivered += 1 brick_state = Status.SEARCHING
def RedMission(): # Red Run (Bench Mission (including backrest removal)) # UP BUTTON #!/usr/bin/env pybricks-micropython from pybricks.hubs import EV3Brick from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor) from pybricks.parameters import Port, Stop, Direction, Button, Color from pybricks.tools import wait, StopWatch, DataLog from pybricks.robotics import DriveBase from pybricks.media.ev3dev import SoundFile, ImageFile # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. # Create your objects here. ev3 = EV3Brick() left_motor = Motor(Port.C) right_motor = Motor(Port.B) wheel_diameter = 56 axle_track = 115 robot = DriveBase(left_motor, right_motor, wheel_diameter, axle_track) Medium_Motor = Motor(Port.A) Large_Motor = Motor(Port.D) leftcolorsensor = ColorSensor(Port.S3) rightcolorsensor = ColorSensor(Port.S2) robot.settings(500) robot.straight(290) robot.stop(Stop.BRAKE) robot.settings(700,400,700,400) robot.turn(110) robot.stop(Stop.BRAKE) while True: robot.drive(200,0) if leftcolorsensor.color() == Color.BLACK: robot.stop(Stop.BRAKE) break robot.stop(Stop.BRAKE) ev3.speaker.beep(3) robot.turn(-110) robot.straight(80) BLACK = 9 WHITE = 85 threshold = (BLACK + WHITE) / 2 # Set the drive speed at 100 millimeters per second. DRIVE_SPEED = 100 # Set the gain of the proportional line controller. This means that for every PROPORTIONAL_GAIN = 1.2 runWhile = True robot.reset() ev3.speaker.beep() while True: # Calculate the deviation from the threshold. deviation = rightcolorsensor.reflection() - threshold # Calculate the turn rate. turn_rate = PROPORTIONAL_GAIN * deviation # Set the drive base speed and turn rate. robot.drive(DRIVE_SPEED, turn_rate) wait(10) print(rightcolorsensor.color()) if robot.distance() >= 100: robot.stop(Stop.BRAKE) break robot.stop(Stop.BRAKE) BLACK = 9 WHITE = 85 threshold = (BLACK + WHITE) / 2 # Set the drive speed at 100 millimeters per second. DRIVE_SPEED = 100 # Set the gain of the proportional line controller. This means that for every PROPORTIONAL_GAIN = 1.2 runWhile = True robot.reset() while True: # Calculate the deviation from the threshold. deviation = rightcolorsensor.reflection() - threshold # Calculate the turn rate. turn_rate = PROPORTIONAL_GAIN * deviation # Set the drive base speed and turn rate. robot.drive(DRIVE_SPEED, turn_rate) wait(10) print(rightcolorsensor.color()) if leftcolorsensor.color() == Color.BLACK: robot.stop(Stop.BRAKE) break robot.turn(-25) robot.straight(230) Large_Motor.run_angle(50,100,then = Stop.HOLD, wait = True) robot.straight(-60) robot.turn(35) robot.straight(-10) Large_Motor.run_angle(50,-50,then = Stop.HOLD, wait = True) robot.straight(-85) robot.turn(-85) robot.straight(500) robot.turn(-20) robot.straight(250) Large_Motor.run_angle(50,-70,then = Stop.HOLD, wait = False) robot.turn(110) robot.stop(Stop.BRAKE)
def RedMission1(): #!/usr/bin/env pybricks-micropython from pybricks.hubs import EV3Brick from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor) from pybricks.parameters import Port, Stop, Direction, Button, Color from pybricks.tools import wait, StopWatch, DataLog from pybricks.robotics import DriveBase from pybricks.media.ev3dev import SoundFile, ImageFile # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. # Create your objects here. ev3 = EV3Brick() left_motor = Motor(Port.C) right_motor = Motor(Port.B) wheel_diameter = 56 axle_track = 115 robot = DriveBase(left_motor, right_motor, wheel_diameter, axle_track) Medium_Motor = Motor(Port.A) Large_Motor = Motor(Port.D) leftcolorsensor = ColorSensor(Port.S3) rightcolorsensor = ColorSensor(Port.S2) ##### BLACK = 9 WHITE = 85 threshold = (BLACK + WHITE) / 2 ###### robot.straight(320) robot.turn(110) while True: robot.drive(90, 0) if leftcolorsensor.reflection() <= 9: robot.stop(Stop.BRAKE) break robot.turn(-110) robot.straight(200) # Calculate the light threshold. Choose values based on your measurements. BLACK = 6 WHITE = 85 threshold = (BLACK + WHITE) / 2 # Set the drive speed at 100 millimeters per second. DRIVE_SPEED = 110 # Set the gain of the proportional line controller. This means that for every # percentage point of light deviating from the threshold, we set the turn # rate of the drivebase to 1.2 degrees per second. # For example, if the light value deviates from the threshold by 10, the robot # steers at 10*1.2 = 12 degrees per second. PROPORTIONAL_GAIN = 1.2 runWhile = True # Start following the line endlessly. while runWhile: # Calculate the deviation from the threshold. deviation = rightcolorsensor.reflection() - threshold # Calculate the turn rate. turn_rate = PROPORTIONAL_GAIN * deviation # Set the drive base speed and turn rate. robot.drive(DRIVE_SPEED, turn_rate) if robot.distance() == 1000: runWhile = False # robot stops after finishing up line following code robot.stop(Stop.BRAKE) robot.straight(-40) robot.turn(-50) robot.straight(145) Large_Motor.run_angle(50, 90, then=Stop.HOLD, wait=True) #robot continues run, to do Boccia mission while True: robot.drive(-80, 0) if leftcolorsensor.reflection() <= 10: robot.stop(Stop.BRAKE) break robot.straight(80) robot.turn(60) robot.straight(100) Large_Motor.run_angle(-50, 150, then=Stop.HOLD, wait=True) robot.straight(-40) Large_Motor.run_angle(50, 150, then=Stop.HOLD, wait=True) robot.straight(-40) while True: robot.drive(-80, 0) if leftcolorsensor.reflection() <= 9: robot.stop(Stop.BRAKE) break robot.straight(40) robot.turn(-85) robot.straight(340) robot.turn(165) robot.straight(55) Large_Motor.run_angle(-50, 150, then=Stop.HOLD, wait=True) robot.straight(20) Medium_Motor.run_angle(150, 250, then=Stop.HOLD, wait=True) robot.turn(70) Medium_Motor.run_angle(-150, 250, then=Stop.HOLD, wait=True) robot.turn(-20) robot.straight(-35) Medium_Motor.run_angle(150, 250, then=Stop.HOLD, wait=True) robot.turn(30) robot.straight(-130)
def YellowMission(): #!/usr/bin/env pybricks-micropython from pybricks.hubs import EV3Brick from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor) from pybricks.parameters import Port, Stop, Direction, Button, Color from pybricks.tools import wait, StopWatch, DataLog from pybricks.robotics import DriveBase from pybricks.media.ev3dev import SoundFile, ImageFile # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. # Create your objects here. ev3 = EV3Brick() left_motor = Motor(Port.C) right_motor = Motor(Port.B) medium_motor = Motor(Port.A) front_largeMotor = Motor(Port.D) wheel_diameter = 56 axle_track = 115 robot = DriveBase(left_motor, right_motor, wheel_diameter, axle_track) # Initialize the color sensor. line_sensor = ColorSensor(Port.S2) line_sensor2 = ColorSensor(Port.S3) robot.straight(110) # Calculate the light threshold. Choose values based on your measurements. BLACK = 9 WHITE = 85 threshold = (BLACK + WHITE) / 2 # Set the drive speed at 100 millimeters per second. DRIVE_SPEED = 100 # Set the gain of the proportional line controller. This means that for every # percentage point of light deviating from the threshold, we set the turn # rate of the drivebase to 1.2 degrees per second. # For example, if the light value deviates from the threshold by 10, the robot # steers at 10*1.2 = 12 degrees per second. PROPORTIONAL_GAIN = 1.2 runWhile = True # Start following the line endlessly. while runWhile: # Calculate the deviation from the threshold. deviation = line_sensor.reflection() - threshold # Calculate the turn rate. turn_rate = PROPORTIONAL_GAIN * deviation # Set the drive base speed and turn rate. robot.drive(DRIVE_SPEED, turn_rate) if robot.distance() == 800: runWhile = False robot.stop(Stop.BRAKE) robot.drive(100, 0) if line_sensor2 and cl.Color() == Color.BLACK: robot.stop(Stop.BRAKE) # robot stops after finishing up line following code robot.stop(Stop.BRAKE) # robot turns after finishing up line following code robot.turn(-103.5) # robot goes straight as it heads towards the mission robot.straight(138) # robot turns right for 90 degrees robot.turn(80) # robot goes straight towards the mission to line the attachment to the wheel robot.straight(97) # large motor attachment goes down to trap the wheel in front_largeMotor.run_angle(60, 162) # robot moves backwards to bring wheel outside of the large circle robot.straight(-115) # large motor releases the trapped tire front_largeMotor.run_angle(60, -148) # robot moves straight to get closer the wheel robot.straight(38) # robot turns so the wheel can get into the smaller target robot.turn(-40) robot.stop(Stop.BRAKE) # robot goes backwards to leave the target and the wheel inside of it robot.straight(-110) # robot turns towards the weight machine robot.turn(-30) # going straight from row machine to weight machine robot.straight(505) # stopping for accuracy. robot.stop(Stop.BRAKE) # turning towards the weight machine. robot.turn(30) # robot goes straight to get closer to the weight machine robot.straight(145) # large motor going down to complete mission (weight machine). front_largeMotor.run_angle(120, 130) # going backwards away from the weight machine robot.straight(-120) # large motor goes back up # front_largeMotor.run_angle(50, -100) ## The robot is turning away from the Weight Machine and towards the Boccia. robot.turn(-127) ## The robot is moving straight towards the Boccia Mission. robot.straight(290) # the robot turns right to turn the aim boccia with the yellow axle on the bottom of the bot. robot.turn(60) # robot.straight(-10) # robot.turn(15) # front_largeMotor.run_angle(50, 60) # robot.straight(55) # the large motor goes up to push the yellow cube down into the target area. front_largeMotor.run_angle(50, -50) robot.straight(-100) robot.turn(-45) robot.straight(900) robot.turn(25) robot.straight(700)
def BlackandGreen(): #!/usr/bin/env pybricks-micropython from pybricks.hubs import EV3Brick from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor) from pybricks.parameters import Port, Stop, Direction, Button, Color from pybricks.tools import wait, StopWatch, DataLog from pybricks.robotics import DriveBase from pybricks.media.ev3dev import SoundFile, ImageFile # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. #define your variables ev3 = EV3Brick() left_motor = Motor(Port.C) right_motor = Motor(Port.B) medium_motor = Motor(Port.A) large_motor = Motor(Port.D) wheel_diameter = 56 axle_track = 115 line_sensor = ColorSensor(Port.S2) line_sensor1 = ColorSensor(Port.S3) robot = DriveBase(left_motor, right_motor, wheel_diameter, axle_track) #follows the line underneath the pull up bar until the leftsensor detects black BLACK = 9 WHITE = 85 threshold = (BLACK + WHITE) / 2 # Set the drive speed at 100 millimeters per second. DRIVE_SPEED = 100 # Set the gain of the proportional line controller. This means that for every PROPORTIONAL_GAIN = 1.2 runWhile = True #goes straight to get ready for line following then resets the distance robot.straight(250) robot.reset() #starts to follow the line towards the replay logo while True: # Calculate the deviation from the threshold. deviation = line_sensor.reflection() - threshold # Calculate the turn rate. turn_rate = PROPORTIONAL_GAIN * deviation # Set the drive base speed and turn rate. robot.drive(DRIVE_SPEED, turn_rate) wait(10) print(robot.distance()) if (robot.distance() >= 450): robot.stop(Stop.BRAKE) break #the robot pushes the phone into the replay logo and moves back to get ready to drop the health units into the replay logo robot.straight(-75) robot.stop(Stop.BRAKE) #the robot then turns so it is going to be perfectly into the replay logo robot.turn(-35) #the robot drops the health units large_motor.run_angle(100, 150) #then turns to an angle to go back to base robot.turn(50) robot.straight(-1000) wait(50) #!/usr/bin/env pybricks-micropython from pybricks.hubs import EV3Brick from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor) from pybricks.parameters import Port, Stop, Direction, Button, Color from pybricks.tools import wait, StopWatch, DataLog from pybricks.robotics import DriveBase from pybricks.media.ev3dev import SoundFile, ImageFile # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. # Create your objects here. ev3 = EV3Brick() left_motor = Motor(Port.C) right_motor = Motor(Port.B) medium_motor = Motor(Port.A) front_largeMotor = Motor(Port.D) wheel_diameter = 56 axle_track = 114.3 robot = DriveBase(left_motor, right_motor, wheel_diameter, axle_track) ## Write your code here: ## The robot goes straight until the Boccia Mission's target. robot.straight(1060) ## The robot moves the large motor down to drop the cubes in the target. front_largeMotor.run_angle(80, 70, then=Stop.HOLD, wait=True) front_largeMotor.run_angle(-80, 70, then=Stop.HOLD, wait=True) ## Dance Mission ## The robot moves backwards to reach the Dance Floor so it can Dance as the last mission. robot.straight(-185) robot.turn(-70) robot.straight(138) ## The following code is all the dance moves we do for the Dance Mission. robot.turn(160) robot.turn(-160) robot.straight(60) front_largeMotor.run_target(500, 60) front_largeMotor.run_target(500, -40) robot.straight(-60) robot.turn(260) robot.turn(-260) robot.turn(100) robot.straight(40) robot.turn(100) front_largeMotor.run_angle(500, 30)
def BlueMission(): #!/usr/bin/env pybricks-micropython from pybricks.hubs import EV3Brick from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor) from pybricks.parameters import Port, Stop, Direction, Button, Color from pybricks.tools import wait, StopWatch, DataLog from pybricks.robotics import DriveBase from pybricks.media.ev3dev import SoundFile, ImageFile # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. #define your variables ev3 = EV3Brick() left_motor = Motor(Port.C) right_motor = Motor(Port.B) medium_motor = Motor(Port.A) large_motor = Motor(Port.D) wheel_diameter = 56 axle_track = 115 line_sensor = ColorSensor(Port.S2) line_sensor1 = ColorSensor(Port.S3) robot = DriveBase(left_motor, right_motor, wheel_diameter, axle_track) #go front towards the step counter robot.straight(650) robot.stop(Stop.BRAKE) wait(20) #makes the robot go slower robot.settings(40) #slowly pushes the step counter by going back and front 2 times robot.straight(140) robot.stop(Stop.BRAKE) robot.straight(-45) robot.stop(Stop.BRAKE) robot.straight(120) robot.stop(Stop.BRAKE) robot.settings(100) robot.straight(-30) robot.stop(Stop.BRAKE) #the robot then turns and goes backwards robot.turn(45) robot.straight(-100) # the robot then goes back until the right color sensor detects back while True: robot.drive(-30, 0) if line_sensor.color() == Color.BLACK: robot.stop(Stop.BRAKE) break #the large motor attatchment comes down at the same time the robot takes a turn towards the black line underneath the pull up bar large_motor.run_angle(50, 170, then=Stop.HOLD, wait=False) left_motor.run_angle(50, -300, then=Stop.HOLD, wait=True) #the robot then goes straight towards that line robot.straight(120) robot.stop(Stop.BRAKE) #robot continues to go forwards until the left color sensor detects black while True: robot.drive(30, 0) if line_sensor.color() == Color.BLACK: robot.stop(Stop.BRAKE) break right_motor.run_angle(50, 150, then=Stop.HOLD, wait=True) #the robot then turns with the right motor until it detects black while True: right_motor.run(85) if line_sensor.color() == Color.BLACK: robot.stop(Stop.BRAKE) break #follows the line underneath the pull up bar until the leftsensor detects black BLACK = 9 WHITE = 85 threshold = (BLACK + WHITE) / 2 # Set the drive speed at 100 millimeters per second. DRIVE_SPEED = 100 # Set the gain of the proportional line controller. This means that for every PROPORTIONAL_GAIN = 1.2 runWhile = True robot.reset() while True: # Calculate the deviation from the threshold. deviation = line_sensor.reflection() - threshold # Calculate the turn rate. turn_rate = PROPORTIONAL_GAIN * deviation # Set the drive base speed and turn rate. robot.drive(DRIVE_SPEED, turn_rate) wait(10) print(line_sensor.color()) if line_sensor1.color() == Color.BLACK: robot.stop(Stop.BRAKE) break #the robot then turns towards the boccia aim and moves straight to push it towards the target and finishes the misison robot.straight(100) #after line following, it goes straight for 100 mm robot.turn(50) robot.straight(100) robot.straight(-30) large_motor.run_angle(100, -65) robot.straight(-60) #the robot then takes a turn (at the same time bringing the attatchment down) towards the slide mission and completes the mission large_motor.run_angle(50, 80, then=Stop.HOLD, wait=False) robot.turn(-195) robot.straight(165) large_motor.run_angle(300, -120, then=Stop.HOLD, wait=True) robot.straight(-30) large_motor.run_angle(200, 120, then=Stop.HOLD, wait=True) ## The robot moves straight towards the mission, getting ready to attempt to push the slide figures off once more. (In case it didn't work before.) robot.straight(30) large_motor.run_angle(300, -120, then=Stop.HOLD, wait=True) robot.straight(-50) '''
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)
This program requires LEGO® EV3 MicroPython v2.0. Download: https://education.lego.com/en-us/support/mindstorms-ev3/python-for-ev3 Building instructions can be found at: https://education.lego.com/en-us/support/mindstorms-ev3/building-instructions#robot """ from pybricks.hubs import EV3Brick from pybricks.ev3devices import Motor from pybricks.parameters import Port from pybricks.robotics import DriveBase # Initialize the EV3 Brick. ev3 = EV3Brick() # Initialize the motors. left_motor = Motor(Port.B) right_motor = Motor(Port.C) lift_motor = Motor(Port.A) # Initialize the drive base. robot = DriveBase(left_motor, right_motor, wheel_diameter=94.2, axle_track=110) robot.settings(300, 620, 620, 300) ev3.screen.draw_text(50, 60, "Pigeons!") ev3.speaker.beep() robot.straight(1050) lift_motor.run_angle(100, 180) robot.straight(-1200)
from pybricks.hubs import EV3Brick from pybricks.ev3devices import Motor from pybricks.media.ev3dev import SoundFile from pybricks.parameters import Direction, Port, Stop BRICK = EV3Brick() STING_MOTOR = Motor(port=Port.D, positive_direction=Direction.CLOCKWISE) GO_MOTOR = Motor(port=Port.B, positive_direction=Direction.CLOCKWISE) STING_MOTOR.run_time(speed=400, time=1000, then=Stop.HOLD, wait=True) # This block controls how far SPIK3R crawls. GO_MOTOR.run_angle(speed=1000, rotation_angle=3 * 360, then=Stop.HOLD, wait=True) BRICK.speaker.play_file(file=SoundFile.ERROR_ALARM) GO_MOTOR.run_angle(speed=-1000, rotation_angle=2 * 360, then=Stop.HOLD, wait=True) BRICK.speaker.play_file(file=SoundFile.ERROR_ALARM) STING_MOTOR.run_angle(speed=-750, rotation_angle=220, then=Stop.HOLD, wait=True)
def BlueMission(): # Blue Run (Step Counter, Pull-Up Bar, Boccia Aim, Slide, Health Unit - 1) # DOWN BUTTON #!/usr/bin/env pybricks-micropython from pybricks.hubs import EV3Brick from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor) from pybricks.parameters import Port, Stop, Direction, Button, Color from pybricks.tools import wait, StopWatch, DataLog from pybricks.robotics import DriveBase from pybricks.media.ev3dev import SoundFile, ImageFile # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. #define your variables ev3 = EV3Brick() left_motor = Motor(Port.C) right_motor = Motor(Port.B) medium_motor = Motor(Port.A) large_motor = Motor(Port.D) wheel_diameter = 56 axle_track = 115 line_sensor = ColorSensor(Port.S2) line_sensor1 = ColorSensor(Port.S3) robot = DriveBase(left_motor, right_motor, wheel_diameter, axle_track) # Go towards the step counter mission from base robot.settings(800) # Speed Change robot.straight(650) robot.stop(Stop.BRAKE) wait(20) # Slow the robot down to succesfully push the step counter. robot.settings(200) # Slowly pushes the step counter by going backward and forward a couple times to increase reliability. robot.straight(230) robot.straight(-20) robot.straight(50) robot.stop(Stop.BRAKE) #robot.straight(-45) #robot.stop(Stop.BRAKE) #robot.straight(120) #robot.stop(Stop.BRAKE) robot.straight(-60) robot.stop(Stop.BRAKE) # The robot then turns and goes backwards until the right color sensor detects black. #robot.settings(250,300,250,300) robot.turn(45) robot.straight(-100) while True: robot.drive(-100,0) if line_sensor.color() == Color.BLACK: robot.stop(Stop.BRAKE) break #The large motor attatchment comes down at the same time the robot takes a turn towards #the black line underneath the pull up bar left_motor.run_angle(50,-300,then=Stop.HOLD, wait=True) # The robot then goes straight towards the line under the pull-up bar. robot.straight(120) robot.stop(Stop.BRAKE) # Robot continues to go forwards until the left color sensor detects black. while True: robot.drive(115,0) if line_sensor.color() == Color.BLACK: robot.stop(Stop.BRAKE) break right_motor.run_angle(100,150,then=Stop.HOLD, wait=True) # The robot turns using the right motor until it detects black. while True: right_motor.run(100) if line_sensor.color() == Color.BLACK: robot.stop(Stop.BRAKE) break robot.straight(-90) large_motor.run_angle(100,150,then=Stop.HOLD, wait=True) robot.stop(Stop.BRAKE) robot.stop(Stop.BRAKE) while True: right_motor.run(40) if line_sensor.color() == Color.BLACK: robot.stop(Stop.BRAKE) break robot.stop(Stop.BRAKE) ev3.speaker.beep() BLACK = 9 WHITE = 85 threshold = (BLACK + WHITE) / 2 # Set the drive speed at 100 millimeters per second. DRIVE_SPEED = 100 # Set the gain of the proportional line controller. This means that for every PROPORTIONAL_GAIN = 1.2 runWhile = True robot.reset() ev3.speaker.beep() while True: # Calculate the deviation from the threshold. deviation = line_sensor.reflection() - threshold # Calculate the turn rate. turn_rate = PROPORTIONAL_GAIN * deviation # Set the drive base speed and turn rate. robot.drive(DRIVE_SPEED, turn_rate) wait(10) print(line_sensor1.color()) if line_sensor1.color() == Color.BLACK: robot.stop(Stop.BRAKE) break robot.stop(Stop.BRAKE) robot.stop(Stop.BRAKE) large_motor.run_angle(-150, 150, then=Stop.HOLD, wait=False) robot.turn(20) robot.stop(Stop.BRAKE) robot.settings(800) robot.straight(280) ev3.speaker.beep(3) while True: robot.drive(-115,0) if line_sensor.color() == Color.BLACK: ev3.speaker.beep(10) robot.stop(Stop.BRAKE) break robot.stop(Stop.BRAKE) # robot.straight(-10) robot.stop(Stop.BRAKE) robot.turn(50) # left_motor.run_angle(100, 150) ''' large_motor.run_angle(30,-20,then=Stop.HOLD, wait=False) robot.turn(10) robot.stop(Stop.BRAKE) large_motor.run_angle(100, -50, then=Stop.HOLD, wait=False) robot.turn(90) robot.stop(Stop.BRAKE) ''' BLACK = 9 WHITE = 85 threshold = (BLACK + WHITE) / 2 # Set the drive speed at 100 millimeters per second. DRIVE_SPEED = 100 # Set the gain of the proportional line controller. This means that for every PROPORTIONAL_GAIN = 1.2 runWhile = True robot.reset() ev3.speaker.beep() while True: # Calculate the deviation from the threshold. deviation = line_sensor.reflection() - threshold # Calculate the turn rate. turn_rate = PROPORTIONAL_GAIN * deviation # Set the drive base speed and turn rate. robot.drive(DRIVE_SPEED, turn_rate) wait(10) print(line_sensor.color()) if robot.distance() >= 500: robot.stop(Stop.BRAKE) break ev3.speaker.beep(3) while True: robot.drive(40,0) if line_sensor1.color() == Color.BLACK: robot.stop(Stop.BRAKE) break robot.stop(Stop.BRAKE) ev3.speaker.beep() robot.straight(30) robot.turn(-100) robot.straight(70) large_motor.run_angle(600,150,then=Stop.HOLD, wait=True) while True: robot.drive(-50, 0) if line_sensor1.color() == Color.BLACK: robot.stop(Stop.BRAKE) break robot.stop(Stop.BRAKE) right_motor.run_angle(600,500,then=Stop.HOLD,wait=True) robot.straight(20) BLACK = 9 WHITE = 85 threshold = (BLACK + WHITE) / 2 # Set the drive speed at 100 millimeters per second. DRIVE_SPEED = 100 # Set the gain of the proportional line controller. This means that for every PROPORTIONAL_GAIN = 1.2 runWhile = True robot.reset() ev3.speaker.beep() while True: # Calculate the deviation from the threshold. deviation = line_sensor1.reflection() - threshold # Calculate the turn rate. turn_rate = PROPORTIONAL_GAIN * deviation # Set the drive base speed and turn rate. robot.drive(DRIVE_SPEED, turn_rate) wait(10) print(line_sensor1.color()) if robot.distance() >= 580: robot.stop(Stop.BRAKE) break robot.stop(Stop.BRAKE) while True: robot.drive(50, 0) if line_sensor.color() == Color.BLACK: robot.stop(Stop.BRAKE) break robot.stop(Stop.BRAKE) ev3.speaker.beep(3) robot.turn(-45) robot.stop(Stop.BRAKE) robot.straight(30) large_motor.run_angle(1000,-150,then=Stop.HOLD, wait=True) robot.straight(-40) large_motor.run_angle(1000,150,then=Stop.HOLD, wait=True) robot.straight(40) large_motor.run_angle(1000,-150,then=Stop.HOLD, wait=True) robot.straight(-115) robot.turn(95) robot.straight(420) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100)
angle_correction = -1 * PROPORTIONAL_GAIN * gyro_sensor.angle() robot.drive(reverseSpeed, angle_correction) wait(10) elif distance > 0: # move forwards while robot.distance() < distance: angle_correction = -1 * PROPORTIONAL_GAIN * gyro_sensor.angle() robot.drive(robotSpeed, angle_correction) wait(10) robot.stop() # Go to boccia gyro_straight(460, 75) gyro_turn(-88.7, 75) gyro_straight(575, 75) forklift_motor.run_angle(speed=400, rotation_angle=160) gyro_straight(-10, 100) # Move to basket forklift_motor.run_angle(speed=1000, rotation_angle=-160) gyro_straight(-30, 100) gyro_turn(-40, 100) gyro_straight(65, 100) forklift_motor.run_angle(speed=1000, rotation_angle=1.5 * 360) forklift_motor.run_angle(speed=1000, rotation_angle=-1.5 * 300) # Move to bench gyro_straight(-70, 100) gyro_turn(-70, 100) gyro_straight(220, 200) # Go back to home base gyro_straight(-43, 100) gyro_turn(20, 100)
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)
from pybricks.robotics import DriveBase # Create your objects here. ev3 = EV3Brick() # Initialize the Ultrasonic Sensors. obstacle_sensor = UltrasonicSensor(Port.S1) color_sensor = ColorSensor(Port.S4) # Initialize two motors. left_motor = Motor(Port.B) right_motor = Motor(Port.C) arm_motor = Motor(Port.D) # The DriveBase is composed of two motors, with a wheel on each motor. robot = DriveBase(left_motor, right_motor, wheel_diameter=55.5, axle_track=104) #drive_time(speed:mm/s, steering:deg/s, time:ms) robot.drive_time(100, 0, 1000) #Drive forward 10cm robot.stop() #run arm motor (speed mm/s, rotational angle) arm_motor.run_angle(-100, 60) #run time with wait as FALSE #run_time(speed deg/s, time ms, then=Stop.HOLD, wait=True) left_motor.run_time(-100, 1000, Stop.HOLD, False) right_motor.run_time(-100, 1000, Stop.HOLD, False) arm_motor.run_time(50, 1000, Stop.HOLD, False) wait(1000)
class Rac3Truck: WHEEL_DIAMETER = 30 # milimeters AXLE_TRACK = 120 # milimeters def __init__( self, left_motor_port: str = Port.B, right_motor_port: str = Port.C, polarity: str = 'inversed', steer_motor_port: str = Port.A, ir_sensor_port: str = Port.S4, ir_beacon_channel: int = 1): if polarity == 'normal': self.left_motor = Motor(port=left_motor_port, positive_direction=Direction.CLOCKWISE) self.right_motor = Motor(port=right_motor_port, positive_direction=Direction.CLOCKWISE) else: self.left_motor = \ Motor(port=left_motor_port, positive_direction=Direction.COUNTERCLOCKWISE) self.right_motor = \ Motor(port=right_motor_port, positive_direction=Direction.COUNTERCLOCKWISE) self.driver = DriveBase(left_motor=self.left_motor, right_motor=self.right_motor, wheel_diameter=self.WHEEL_DIAMETER, axle_track=self.AXLE_TRACK) self.steer_motor = Motor(port=steer_motor_port, positive_direction=Direction.CLOCKWISE) self.ir_sensor = InfraredSensor(port=ir_sensor_port) self.ir_beacon_channel = ir_beacon_channel def reset(self): self.steer_motor.run_time( speed=300, time=1500, then=Stop.COAST, wait=True) self.steer_motor.run_angle( speed=-500, rotation_angle=120, then=Stop.HOLD, wait=True) self.steer_motor.reset_angle(angle=0) def steer_left(self): if self.steer_motor.angle() > -65: self.steer_motor.run_target( speed=-200, target_angle=-65, then=Stop.HOLD, wait=True) else: self.steer_motor.hold() def steer_right(self): if self.steer_motor.angle() < 65: self.steer_motor.run_target( speed=200, target_angle=65, then=Stop.HOLD, wait=True) else: self.steer_motor.hold() def steer_center(self): if self.steer_motor.angle() < -7: self.steer_motor.run_target( speed=200, target_angle=4, then=Stop.HOLD, wait=True) elif self.steer_motor.angle() > 7: self.steer_motor.run_target( speed=-200, target_angle=-4, then=Stop.HOLD, wait=True) self.steer_motor.hold() wait(100) def drive_by_ir_beacon(self): 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.driver.drive( speed=800, turn_rate=0) self.steer_center() # backward elif ir_beacon_button_pressed == {Button.LEFT_DOWN, Button.RIGHT_DOWN}: self.driver.drive( speed=-800, turn_rate=0) self.steer_center() # turn left forward elif ir_beacon_button_pressed == {Button.LEFT_UP}: self.left_motor.run(speed=600) self.right_motor.run(speed=1000) self.steer_left() # turn right forward elif ir_beacon_button_pressed == {Button.RIGHT_UP}: self.left_motor.run(speed=1000) self.right_motor.run(speed=600) self.steer_right() # turn left backward elif ir_beacon_button_pressed == {Button.LEFT_DOWN}: self.left_motor.run(speed=-600) self.right_motor.run(speed=-1000) self.steer_left() # turn right backward elif ir_beacon_button_pressed == {Button.RIGHT_DOWN}: self.left_motor.run(speed=-1000) self.right_motor.run(speed=-600) self.steer_right() # otherwise stop else: self.driver.stop() self.steer_center()
# moved the sorter module all the way to the left. touch_sensor = TouchSensor(Port.S1) # Initialize the Color Sensor. It is used to detect the color of the objects. color_sensor = ColorSensor(Port.S3) # This is the main loop. It waits for you to scan and insert 8 colored objects. # Then it sorts them by color. Then the process starts over and you can scan # 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.
DRIVE_SPEED = 100 # Set the gain of the proportional line controller. This means that for every PROPORTIONAL_GAIN = 1.2 runWhile = True #goes straight to get ready for line following then resets the distance robot.straight(250) robot.reset() #starts to follow the line towards the replay logo while True: # Calculate the deviation from the threshold. deviation = line_sensor.reflection() - threshold # Calculate the turn rate. turn_rate = PROPORTIONAL_GAIN * deviation # Set the drive base speed and turn rate. robot.drive(DRIVE_SPEED, turn_rate) wait(10) print(robot.distance()) if (robot.distance() >= 450): robot.stop(Stop.BRAKE) break #the robot pushes the phone into the replay logo and moves back to get ready to drop the health units into the replay logo robot.straight(-75) robot.stop(Stop.BRAKE) #the robot then turns so it is going to be perfectly into the replay logo robot.turn(-35) #the robot drops the health units large_motor.run_angle(100, 150) #then turns to an angle to go back to base robot.turn(50) robot.straight(-1000)
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)
InfraredSensor, UltrasonicSensor, GyroSensor) from pybricks.robotics import DriveBase, Stop from pybricks.parameters import Port from pybricks.tools import wait # This is for initializing the large motors on your ev3 although this could also be used for medium motors # It tells python that We are going to use motors and their names are motor_b and motor_c motor_b = Motor( Port.B ) #We are setting motor_b to the known function that micropython has pre downloaded "Motor" and it has a parameter that needs the port in our case it's prot B on the ev3 motor_c = Motor( Port.C) # We are setting motor_c to the function "Motor" at port C # My first program is just moving forward and backards which in micropython is simple, but the thing is that it isn't one line with micropython you have to control them seperatly motor_b.run_angle( 500, 720, Stop.BRAKE, False ) # this is telling micropython that your using motor_b to move froward at a speed of 500 and moving 720 degrees which is 2 rotations # the third parameter is telling micropython that you want to break at the end, if you want to coast change the "BREAK" to "COAST" # and the last parameter is telling micropython that you have to go to the next line as well becuase we are also running the next motor motor_c.run_angle(500, 720, Stop.BRAKE, True) # this is telling micropython to now move motor_c # since the last parameter says true then that means we aren't doing anything else at the same time # okay now we have made the robot move forward, okay now how do we move backwards? # in our previous one we moved at a speed of 500 and a distance of 720 to move backwards wwe have to make one of those negative I usually make the speed negative becuase thats how I was taught and what ev3 normally does #So it will look something like this motor_b.run_angle( -500, 720, Stop.BRAKE, False ) # in this line we have done literally exactly the same thing as the previous one accept we have made the speed negative, that's all you have to change to make it backwards motor_c.run_angle( -500, 720, Stop.BRAKE, True) # this is also exactly the same accept I have negated the speed