def slide(robot): robot.stop() motor_b, motor_c = Motor(Port.B, positive_direction=Direction.CLOCKWISE), Motor( Port.C, positive_direction=Direction.CLOCKWISE) robot = DriveBase(motor_b, motor_c, wheel_diameter=94.2, axle_track=95) robot.straight(220) wait(500) # robot.stop() while gyro.angle() < 265: robot.drive(-100, 80) dead_stop() motor_a.run_angle(1500, -1000, then=Stop.HOLD, wait=False) robot.straight(515) dead_stop() robot.turn(125) # 105 robot.stop() motor_b, motor_c = Motor( Port.B, positive_direction=Direction.COUNTERCLOCKWISE), Motor( Port.C, positive_direction=Direction.COUNTERCLOCKWISE) robot = DriveBase(motor_b, motor_c, wheel_diameter=94.2, axle_track=95) robot.straight(200) robot.turn(70) robot.turn(-60) LineFollow(100, 1.05, robot, 50) robot.straight(250)
def return_home(): # Initialize two motors and a drive base left = Motor(Port.B) right = Motor(Port.C) robot = DriveBase(left, right, 56, 114) # Initialize a sensor sensor = UltrasonicSensor(Port.S4) # Drive forward until an object is detected robot.drive(100, 0) while sensor.distance() > 500: wait(10) robot.stop()
def driveStraight( self, speed, distance, angle ): # this drives straight by turning when thrown off to the same relative angle specified. self.reset() drivebase = DriveBase(self.motorC, self.motorB, 62.4, 101) brick.display.clear() while self.motorC.angle( ) < distance * self.DEG_TO_ROT and self.motorB.angle( ) < distance * self.DEG_TO_ROT: error = self.gyroSensor.angle() - angle error = error * -4 print(error) drivebase.drive(speed, error) self.motorB.run(0) self.motorC.run(0)
class Driver(): def __init__(self, leftMotor, rightMotor, diameter, axle): self.driver = DriveBase(leftMotor, rightMotor, diameter, axle) self.x = 0 self.y = 0 self.speed = 0 self.steering = 0 def drive(self, speed, steering): self.speed = speed self.steering = steering if self.speed == 0: self.driver.stop() else: self.driver.drive(self.speed, self.steering)
def erstes(): ev3.speaker.beep() m_r = Motor(Port.C, Direction.COUNTERCLOCKWISE) m_l = Motor(Port.B, Direction.COUNTERCLOCKWISE) db = DriveBase(m_l, m_r, wheel_diameter=30, axle_track=140) db.straight(distance=50) db.turn(90) db.straight(distance=-50) # play some sound and get angry #im = Image('./Angry.bmp') im = ImageFile.ANGRY ev3.screen.load_image(im) ev3.speaker.play_file(SoundFile.CAT_PURR) # drive up to a distance of 100 mm db.drive(speed=10, turn_rate=0) # start driving while abs(db.distance()) < 100: time.sleep(0.1) # wait 100 msec before querying distance again db.stop()
def follow(self, number_of_seconds): print('SKILLS - Follow the line for', number_of_seconds,'seconds') y = 1 while y <= number_of_seconds: left_motor = Motor(Port.A) right_motor = Motor(Port.D) color_sensor_left = ColorSensor(Port.S3) color_sensor_right = ColorSensor(Port.S4) robot = DriveBase(left_motor, right_motor, wheel_diameteR, axle_track) BLACK = 9 WHITE = 85 threshold = (BLACK + WHITE) / 2 DRIVE_SPEED = 100 PROPORTIONAL_GAIN = 1.2 while True: deviation = line_sensor.reflection() - threshold turn_rate = PROPORTIONAL_GAIN * deviation robot.drive(DRIVE_SPEED, turn_rate) wait(10)
def bang_bang(calibrate_val): #initialize local variables wheelDiameter = 56 wheelBase = 175 car = DriveBase( left_motor, right_motor, wheelDiameter, wheelBase ) # used the DriveBase class in this controller for convenience left_val = left_sensor.read() right_val = right_sensor.read() run = True speed = 200 # values chosen from testing to be the best for our robot turn_speed = 120 tolerance_range = 100 # Effectively controls how sensitive the controller is to changes in the light signal while run: left_val = left_sensor.read( ) + calibrate_val # left sensor was determined through testing to need a static offeset to match the right sensor's value right_val = right_sensor.read() # decides to turn based on the difference between sensor values if (left_val > (right_val + tolerance_range)): # turn right car.drive(speed, turn_speed) elif (left_val < (right_val - tolerance_range)): # turn left car.drive(speed, -turn_speed) else: # drive forward car.drive(speed, 0) if Button.CENTER in brick.buttons(): # ends run loop run = False # ends main loop return True
from pybricks.ev3devices import Motor from pybricks.parameters import Port from pybricks.robotics import DriveBase from pybricks.tools import wait # Initialize the EV3 Brick. ev3 = EV3Brick() # Initialize the motors. left_motor = Motor(Port.B) right_motor = Motor(Port.C) # Initialize the drive base. robot = DriveBase(left_motor, right_motor, wheel_diameter=54.6, axle_track=104.1) ################################################################################# ev3.screen.draw_text(50, 60, "Pigeons!") ev3.speaker.beep() #robot.settings(1000 ,250 ,150 ,100) robot.straight(700) while robot.distance() < 800: robot.drive(250, 180) robot.straight(700)
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)
#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: robot.stop()
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 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) '''
# 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)) # Wait 200 milliseconds before starting the loop again. wait(200)
class Bobb3e: WHEEL_DIAMETER = 24 # milimeters AXLE_TRACK = 100 # milimeters def __init__(self, left_motor_port: str = Port.B, right_motor_port: str = Port.C, lift_motor_port: str = Port.A, ir_sensor_port: str = Port.S4, ir_beacon_channel: int = 1): self.ev3_brick = EV3Brick() left_motor = Motor(port=left_motor_port, positive_direction=Direction.COUNTERCLOCKWISE) right_motor = Motor(port=right_motor_port, positive_direction=Direction.COUNTERCLOCKWISE) self.drive_base = DriveBase(left_motor=left_motor, right_motor=right_motor, wheel_diameter=self.WHEEL_DIAMETER, axle_track=self.AXLE_TRACK) self.lift_motor = Motor(port=lift_motor_port, positive_direction=Direction.CLOCKWISE) self.ir_sensor = InfraredSensor(port=ir_sensor_port) self.ir_beacon_channel = ir_beacon_channel self.reversing = False def drive_or_operate_forks_by_ir_beacon( self, driving_speed: float = 1000, # mm/s turn_rate: float = 90 # rotational speed deg/s ): """ Read the commands from the remote control and convert them into actions such as go forward, lift and turn. """ while True: ir_beacon_button_pressed = \ set(self.ir_sensor.buttons(channel=self.ir_beacon_channel)) # lower the forks if ir_beacon_button_pressed == {Button.LEFT_UP, Button.LEFT_DOWN}: self.reversing = False self.drive_base.stop() self.lift_motor.run(speed=100) # raise the forks elif ir_beacon_button_pressed == \ {Button.RIGHT_UP, Button.RIGHT_DOWN}: self.reversing = False self.drive_base.stop() self.lift_motor.run(speed=-100) # forward elif ir_beacon_button_pressed == {Button.LEFT_UP, Button.RIGHT_UP}: self.reversing = False self.drive_base.drive(speed=driving_speed, turn_rate=0) self.lift_motor.hold() # backward elif ir_beacon_button_pressed == \ {Button.LEFT_DOWN, Button.RIGHT_DOWN}: self.reversing = True self.drive_base.drive(speed=-driving_speed, turn_rate=0) self.lift_motor.hold() # turn left on the spot elif ir_beacon_button_pressed == \ {Button.LEFT_UP, Button.RIGHT_DOWN}: self.reversing = False self.drive_base.drive(speed=0, turn_rate=-turn_rate) self.lift_motor.hold() # turn right on the spot elif ir_beacon_button_pressed == \ {Button.RIGHT_UP, Button.LEFT_DOWN}: self.reversing = False self.drive_base.drive(speed=0, turn_rate=turn_rate) self.lift_motor.hold() # turn left forward elif ir_beacon_button_pressed == {Button.LEFT_UP}: self.reversing = False self.drive_base.drive(speed=driving_speed, turn_rate=-turn_rate) self.lift_motor.hold() # turn right forward elif ir_beacon_button_pressed == {Button.RIGHT_UP}: self.reversing = False self.drive_base.drive(speed=driving_speed, turn_rate=turn_rate) self.lift_motor.hold() # turn left backward elif ir_beacon_button_pressed == {Button.LEFT_DOWN}: self.reversing = True self.drive_base.drive(speed=-driving_speed, turn_rate=turn_rate) self.lift_motor.hold() # turn right backward elif ir_beacon_button_pressed == {Button.RIGHT_DOWN}: self.reversing = True self.drive_base.drive(speed=-driving_speed, turn_rate=-turn_rate) self.lift_motor.hold() # otherwise stop else: self.reversing = False self.drive_base.stop() self.lift_motor.hold() wait(10) def sound_alarm_whenever_reversing(self): while True: if self.reversing: self.ev3_brick.speaker.play_file(file=SoundFile.BACKING_ALERT) wait(10)
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.settings(250) 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.straight(-30) robot.stop(Stop.BRAKE) #the robot then turns and goes backwards robot.settings(250,500,250,500) robot.turn(45) robot.straight(-100) # the robot then goes back until the right color sensor detects back while True: robot.drive(-115,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,200,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(115,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 #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_sensor1.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.turn(25) robot.straight(250) robot.straight(-50) # this is importate kekekekee large_motor.run_angle(75,-65) robot.straight(-60) while True: robot.drive(-70,0) if line_sensor.color() == Color.BLACK: robot.stop(Stop.BRAKE) ev3.speaker.beep() break while True: left_motor.run(-50) if line_sensor1.color() == Color.BLACK: robot.stop(Stop.BRAKE) ev3.speaker.beep() break left_motor.run_angle(50, -20) right_motor.run_angle(50, 20) robot.settings(200) robot.straight(60) robot.turn(-137) #this is also importante jekeke large_motor.run_angle(50,80) robot.straight(143) large_motor.run_angle(550, -120) robot.straight(-40) large_motor.run_angle(550, 120) robot.straight(40) large_motor.run_angle(550, -120) large_motor.run_angle(300, 30, then=Stop.HOLD, wait=True) #robot.straight(40) large_motor.run_angle(300, -100, then=Stop.HOLD, wait=True) #goes to collect the health unit near the basketball (goes back to base) robot.straight(-200) robot.turn(40) robot.straight(556) robot.straight(50) robot.stop(Stop.BRAKE) while True: left_motor.run(50) if line_sensor1.color() == Color.BLACK: robot.stop(Stop.BRAKE) ev3.speaker.beep() break robot.stop(Stop.BRAKE) robot.reset() # 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_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) if robot.distance() >= 210: runWhile = False break robot.stop(Stop.BRAKE) robot.turn(5.244312) robot.straight(700) #the robot then goes back until the right color sensor detects back ''' while True: if line_sensor1.color() == Color.BLACK: robot.stop(Stop.BRAKE) break #robot.straight(980) ''' robot.stop(Stop.BRAKE)
gyro_sensor = GyroSensor(Port.S3) # Initialize the color sensor. #line_sensor = ColorSensor(Port.S3) left_motor.reset_angle(0) right_motor.reset_angle(0) gyro_sensor.reset_angle(0) fudge=1 speed=100 angle=0 robot = DriveBase(left_motor, right_motor, wheel_diameter=30, axle_track=135) initial_distance = 0 robot.reset() while ((robot.distance() - initial_distance) < 350) : robot.drive(speed,angle) drift = gyro_sensor.angle() angle = (drift * fudge) * -1 wait(3) i=0 robot.stop() speed=50 while (i<2 ): fruit = gyro_sensor.angle() fruit = fruit * (-1) robot.drive(speed,fruit) wait(3) robot.drive((speed* (-1)),fruit) i = i + 1
def adjustRight(): while sensorRed.reflection() < 30 or sensorBlue.reflection() > 30: motors.drive_time(0, 30, 200) def sensorCheck(): print() print("yellow: " + str(sensorYellow.reflection())) print("red: " + str(sensorRed.reflection())) print("blue: " + str(sensorBlue.reflection())) while True: while sensorBlue.reflection() < 15: # Is motors on a path motors.drive(200, 0) # T intersection if sensorRed.reflection() < 15 and sensorYellow.reflection() < 15: sensorCheck() print('crossroads') choice = path[i] #choice = random.randrange(0, 3) if choice == 0: print('straight') motors.drive_time(speed_d, 0, speed_t) i += 1 if choice == 1: print('right') turnRight() if choice == 2:
oldstraight(400, 250) oldstraight(100, 150) wait(500) oldstraight(500, -500) while Button.CENTER not in ev3.buttons.pressed(): pass #ev3.speaker.say('Run 2b') #slide accangle = 0 gyroSensor.reset_angle(0) straight(550, 350, Kd=0.04) wait(1000) while robot.distance() > -100: robot.drive(-200, -2) while robot.distance() > -500: robot.drive(-250, 20) robot.stop() while Button.CENTER not in ev3.buttons.pressed(): pass #treadmill accangle = 0 gyroSensor.reset_angle(0) straight(270, 200) line_follow(rightcolor, 175, 1.6, 0.04, 0.1, 0.01, 1200) # arives at treadmill #turntoangle(0.01) robot.drive(95, -1.5) #80, -2 mediummotor2.run_time(300, 1000, wait=True) #rolls up platform
class Robot: def __init__(self): # micro controller ev3 = EV3Brick() ev3.speaker.beep() # sensors # low value = registers black tape # high value = aluminum self.sensorYellow = ColorSensor(Port.S1) self.sensorRed = ColorSensor(Port.S3) self.sensorBlue = ColorSensor(Port.S2) # motor left_motor = Motor(Port.A, gears=[40, 24]) right_motor = Motor(Port.B, gears=[40, 24]) axle_track = 205 wheel_diameter = 35 self.motors = DriveBase(left_motor, right_motor, wheel_diameter, axle_track) # constants # intersection detection of side sensors self.thresholdBlueSensor = 30 # value for making turns self.thresholdSideSensors = 15 # timer self.watch = StopWatch() self.timer = 0 def drive(self, directions): i = 0 while i < len(directions): self.timer = self.watch.time() if self.timer % 100 == 0: print(self.timer) self.correctPath() if self.senseIntersection() == True and self.timer >= 500: print('intersection') self.motors.drive_time(0, 0, 1000) # reduce this when done self.executeCommand(directions[i]) i += 1 self.motors.drive(-125, 0) def executeCommand(self, cmd): if cmd == 0: print('straight') self.driveStraight() if cmd == 1: print('right') self.turnRight() if cmd == 2: print('left') self.turnLeft() if cmd == 3: print('reverse') self.reverse() if cmd == 4: print('stop') # turning behaviours at intersection def turnLeft(self): self.motors.drive_time(-30, 44, 2000) self.watch.reset() def turnRight(self): self.motors.drive_time(-30, -46, 2000) self.watch.reset() def driveStraight(self): self.motors.drive_time(-60, 0, 1800) self.watch.reset() def reverse(self): self.motors.drive_time(60, 0, 1800) self.motors.drive_time(0, 94, 2000) self.motors.drive_time(-60, 0, 800) # intersection detection def senseIntersection(self): if self.sensorRed.reflection() < 2 or self.sensorYellow.reflection() < 2: return True # path correction # completely aluminum = 23 # completely black tape = 1 def correctPath(self): if self.sensorBlue.reflection() < 8: self.adjustLeft() if self.sensorBlue.reflection() > 16: self.adjustRight() # default: -125, angle def adjustLeft(self): angle = 12 - min(self.sensorBlue.reflection(), 10) step = 125 + (12 - min(self.sensorBlue.reflection(), 10)) self.motors.drive(-step, angle) def adjustRight(self): angle = max(self.sensorBlue.reflection(), 14) -12 step = 125 + (max(self.sensorBlue.reflection(), 14) -12) self.motors.drive(-step, -angle)
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)
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)
#!/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 # 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) # Assemble Crew Code. while color_sensor.reflection() < 50: robot.drive(200, 0) 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)
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()
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)
# 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)
#!/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 from time import sleep # Write your program here CL = ColorSensor(Port.S1) LM = Motor(Port.B) RM = Motor(Port.C) WD = 56 AT = 114 Robot = DriveBase(LM, RM, WD, AT) while True: LI = CL.ambient() # light intensity Robot.drive(50, (LI * 2) - 100)
#!/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.robotics import DriveBase import time # Write your program here brick.sound.beep() motorA = Motor(Port.A) motorD = Motor(Port.D) robot = DriveBase(motorA, motorD, 56, 114) cs = ColorSensor(Port.S4) #drive speed at 450 mm per second with 0 deg per second turn rate robot.drive(450, 0) while cs.color() != Color.YELLOW: time.sleep(0.01) robot.stop(Stop.BRAKE)
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)
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)