fushi

light following final ver

Dec 3rd, 2025 (edited)
2,707
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.59 KB | None | 0 0
  1. #!/usr/bin/env pybricks-micropython
  2. from pybricks.hubs import EV3Brick
  3. from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor,
  4.                                  InfraredSensor, UltrasonicSensor, GyroSensor)
  5. from pybricks.parameters import Port, Stop, Direction, Button, Color
  6. from pybricks.tools import wait, StopWatch, DataLog
  7. from pybricks.robotics import DriveBase
  8. from pybricks.media.ev3dev import SoundFile, ImageFile
  9.  
  10. ev3 = EV3Brick()
  11. ultrasonic =  UltrasonicSensor(Port.S4)
  12. sensor = ColorSensor(Port.S2)
  13. my_left_kidney_ran_away_from_home_and_i_am_really_sad = Motor(Port.C)
  14. right_motor = Motor(Port.B)
  15. sacrifice = Motor(Port.A)
  16. devil = DriveBase(my_left_kidney_ran_away_from_home_and_i_am_really_sad, right_motor, wheel_diameter=57.15, axle_track=88.9)
  17. one = 2
  18. t = 60 # Starting Turn Radius
  19. w = 1000 # Starting Wait Time (1000 = 1s)
  20. noturning = 0
  21.  
  22. ev3.speaker.beep()
  23. print('Program start')
  24. while True:
  25.     while noturning == 0: # Runs if the robot isn't by a wall.
  26.         print ('#1,', sensor.reflection()) # Uses 1 to define that it's the first print
  27.         if ultrasonic.distance() <= 300: # Immediately goes to check how far the robot is from a wall and prevents anything from running if it is.
  28.             noturning = 1
  29.         elif sensor.reflection() <= one:
  30.             devil.drive(150, 0)
  31.             t = 60 # These should match the values above them.
  32.             w = 1000
  33.             # Later on, I will probably change this to a function call because I really hate updating two values at once
  34.         elif sensor.reflection() == 0:
  35.             devil.drive(0, 120)
  36.             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.
  37.         else: # The Super Inefficient Loop For When It's Not On A Line
  38.             while sensor.reflection() > one:
  39.                 print ('#2,', sensor.reflection())
  40.                 devil.drive(0, t)
  41.                 print("positive turn")
  42.                 if sensor.reflection() == one:
  43.                     break
  44.                 wait(w)
  45.                 devil.drive(0, -t)
  46.                 print("negative turn")
  47.                 if sensor.reflection() == one:
  48.                     break
  49.                 wait(w)
  50.                 devil.drive(0, -t)
  51.                 if sensor.reflection() == one:
  52.                     break
  53.                 wait(w)
  54.                 devil.drive(0, t)
  55.                 if sensor.reflection() == one:
  56.                     break
  57.                 wait(w)
  58.                 # No, I have no idea why it doesn't turn right further than its starting position.
  59.                 t = t + 10 # These sets of variables add onto the original variable so that it has a wider turning radius each scan.
  60.                 if t <= 90:
  61.                     w = w + 10
  62.                 elif t >= 120:
  63.                     w = w + 25
  64.                 # If the turn amount is still under 120 per turn, it only adds a wait amount of 10(ms)
  65.                 # 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.
  66.                 # Alternatively, for those who prefer a different solution, you can change the motor's speed based on turn radius.
  67.     while noturning == 1: # This loop is meant for when the robot reaches the end line.
  68.         devil.straight(-10) # Reverses away from wall because it is fat
  69.         devil.turn(240) # Turns around. It's not 180 because I need to finetune drivebase's variable call. This is the quickest fix otherwise.
  70.         wait(500)
  71.         noturning = 0
Advertisement
Add Comment
Please, Sign In to add comment