fushi

ultrasonic_trial01

Oct 28th, 2025 (edited)
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.85 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.  
  11. # This program requires LEGO EV3 MicroPython v2.0 or higher.
  12. # Click "Open user guide" on the EV3 extension tab for more information.
  13.  
  14. ev3 = EV3Brick()
  15. ultrasonic =  UltrasonicSensor(Port.S4)
  16. sensor = ColorSensor(Port.S2)
  17. my_left_kidney_ran_away_from_home_and_i_am_really_sad = Motor(Port.C)
  18. right_motor = Motor(Port.B)
  19. sacrifice = Motor(Port.A)
  20. devil = DriveBase(my_left_kidney_ran_away_from_home_and_i_am_really_sad, right_motor, wheel_diameter=57.15, axle_track=88.9)
  21. one = 1 or 2
  22. # 1 & 2 tend to be the line's defined values, so its defined as a "or" so the program checks for both values within the loops
  23. t = 60
  24. w = 500
  25. noturning = 1
  26.  
  27. # This loop is for debugging. You can view the current reflection and adjust values based off it.
  28. # You can hit center to exit debugger. In Visual Code, highlighting it and pressing Ctrl+/ will uncomment it.
  29. ev3.speaker.beep()
  30. print('Program start')
  31. devil.reset()
  32. while True:
  33.     while noturning == 0:
  34.         print ('#1,', sensor.reflection())
  35.         if ultrasonic.distance() <= 150:
  36.             noturning = 1
  37.         elif sensor.reflection() == one:
  38.             devil.drive(150, 0)
  39.         elif sensor.reflection() == 0:
  40.             devil.drive(0, 120)
  41.             print("No line nor paper detected.")
  42.         else:
  43.             while sensor.reflection() > one:
  44.                 print ('#2,', sensor.reflection())
  45.                 devil.drive(0, t)
  46.                 if sensor.reflection() == one:
  47.                     break
  48.                 wait(w)
  49.                 devil.drive(0, -t)
  50.                 if sensor.reflection() == one:
  51.                     break
  52.                 wait(w)
  53.                 devil.drive(0, -t)
  54.                 if sensor.reflection() == one:
  55.                     break
  56.                 wait(w)
  57.                 devil.drive(0, t)
  58.                 if sensor.reflection() == one:
  59.                     break
  60.                 wait(w)
  61.                 t = t + 10
  62.                 if t <= 90:
  63.                     w = w + 10
  64.                 elif t >= 120:
  65.                     w = w + 25
  66.                 # If the turn amount is still under 120 per turn, it only adds a wait amount of 10(ms)
  67.                 # 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.
  68.     while noturning == 1:
  69.         devil.straight(-10)
  70.         devil.turn(180)
  71.         wait(500)
  72.         noturning = 0
Advertisement