Nawor3565

Line follow

Apr 29th, 2022 (edited)
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.11 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.nxtdevices import (LightSensor)
  6. from pybricks.parameters import Port, Stop, Direction, Button, Color
  7. from pybricks.tools import wait, StopWatch, DataLog
  8. from pybricks.robotics import DriveBase
  9. from pybricks.media.ev3dev import SoundFile, ImageFile
  10.  
  11.  
  12. # This program requires LEGO EV3 MicroPython v2.0 or higher.
  13. # Click "Open user guide" on the EV3 extension tab for more information.
  14.  
  15.  
  16. # Create your objects here.
  17. ev3 = EV3Brick()
  18. right_motor = Motor(Port.B)
  19. left_motor = Motor(Port.C)
  20. lift_motor = Motor(Port.A)
  21. claw = Motor(Port.D)
  22. light_sensor = LightSensor(Port.S3)
  23. #color_sensor_one = ColorSensor(Port.S2)
  24. #color_sensor_two = ColorSensor(Port.S1)
  25. wheel_diameter = 43.3
  26. axle_track = 130
  27.  
  28. # Configure drivebase and set speed/acceleration options
  29. driver = DriveBase(left_motor, right_motor, wheel_diameter, axle_track)
  30. driver.settings(800, 200, 30, 1200)
  31.  
  32. # Calculate the light threshold. Choose values based on your measurements.
  33. BLACK = 15
  34. WHITE = 30
  35. threshold = (BLACK + WHITE) / 2
  36.  
  37. # Set the drive speed at 100 millimeters per second.
  38. DRIVE_SPEED = 100
  39.  
  40. # Set the gain of the proportional line controller. This means that for every
  41. # percentage point of light deviating from the threshold, we set the turn
  42. # rate of the drivebase to 1.2 degrees per second.
  43.  
  44. # For example, if the light value deviates from the threshold by 10, the robot
  45. # steers at 10*1.2 = 12 degrees per second.
  46. PROPORTIONAL_GAIN = 1.2
  47.  
  48. # Write your program here.
  49. ev3.speaker.beep()
  50. wait(300)
  51.  
  52. # Start following the line endlessly.
  53. while True:
  54.     # Calculate the deviation from the threshold.
  55.     deviation = light_sensor.reflection() - threshold
  56.  
  57.     # Calculate the turn rate.
  58.     turn_rate = PROPORTIONAL_GAIN * deviation
  59.  
  60.     # Set the drive base speed and turn rate.
  61.     driver.drive(DRIVE_SPEED, turn_rate)
  62.  
  63.     # You can wait for a short time or do other things in this loop.
  64.     wait(10)
Add Comment
Please, Sign In to add comment