Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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
- ev3 = EV3Brick()
- ultrasonic = UltrasonicSensor(Port.S4)
- sensor = ColorSensor(Port.S2)
- my_left_kidney_ran_away_from_home_and_i_am_really_sad = Motor(Port.C)
- right_motor = Motor(Port.B)
- sacrifice = Motor(Port.A)
- devil = DriveBase(my_left_kidney_ran_away_from_home_and_i_am_really_sad, right_motor, wheel_diameter=57.15, axle_track=88.9)
- one = 2
- t = 60 # Starting Turn Radius
- w = 1000 # Starting Wait Time (1000 = 1s)
- noturning = 0
- ev3.speaker.beep()
- print('Program start')
- while True:
- while noturning == 0: # Runs if the robot isn't by a wall.
- print ('#1,', sensor.reflection()) # Uses 1 to define that it's the first print
- if ultrasonic.distance() <= 300: # Immediately goes to check how far the robot is from a wall and prevents anything from running if it is.
- noturning = 1
- elif sensor.reflection() <= one:
- devil.drive(150, 0)
- t = 60 # These should match the values above them.
- w = 1000
- # Later on, I will probably change this to a function call because I really hate updating two values at once
- elif sensor.reflection() == 0:
- devil.drive(0, 120)
- print("No line nor paper detected.") # This elif was added because it'd run itself off the paper sometimes and barely be off from the line.
- else: # The Super Inefficient Loop For When It's Not On A Line
- while sensor.reflection() > one:
- print ('#2,', sensor.reflection())
- devil.drive(0, t)
- print("positive turn")
- if sensor.reflection() == one:
- break
- wait(w)
- devil.drive(0, -t)
- print("negative turn")
- if sensor.reflection() == one:
- break
- wait(w)
- devil.drive(0, -t)
- if sensor.reflection() == one:
- break
- wait(w)
- devil.drive(0, t)
- if sensor.reflection() == one:
- break
- wait(w)
- # No, I have no idea why it doesn't turn right further than its starting position.
- t = t + 10 # These sets of variables add onto the original variable so that it has a wider turning radius each scan.
- if t <= 90:
- w = w + 10
- elif t >= 120:
- w = w + 25
- # If the turn amount is still under 120 per turn, it only adds a wait amount of 10(ms)
- # Once it goes above 120, it starts adding 25(ms) as a result of the wider turns. The wider turns make it harder for the sensor to update fast enough.
- # Alternatively, for those who prefer a different solution, you can change the motor's speed based on turn radius.
- while noturning == 1: # This loop is meant for when the robot reaches the end line.
- devil.straight(-10) # Reverses away from wall because it is fat
- devil.turn(240) # Turns around. It's not 180 because I need to finetune drivebase's variable call. This is the quickest fix otherwise.
- wait(500)
- noturning = 0
Advertisement
Add Comment
Please, Sign In to add comment