Nawor3565

Untitled

Apr 26th, 2022
914
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.09 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.  
  15. # Create your objects here.
  16. ev3 = EV3Brick()
  17. right_motor = Motor(Port.B)
  18. left_motor = Motor(Port.C)
  19. lift_motor = Motor(Port.A)
  20. claw = Motor(Port.D)
  21. #light_sensor = LightSensor(Port.S)
  22. #color_sensor_one = ColorSensor(Port.S2)
  23. color_sensor_two = ColorSensor(Port.S1)
  24. wheel_diameter = 43.3
  25. axle_track = 130
  26.  
  27. # Configure drivebase and set speed/acceleration options
  28. driver = DriveBase(left_motor, right_motor, wheel_diameter, axle_track)
  29. driver.settings(800, 200, 30, 1200)
  30.  
  31. # Write your program here.
  32.  
  33. # Beep the speaker so you can tell when the code is running
  34. ev3.speaker.beep()
  35. wait(300)
  36.  
  37. # Define code to run when a green fish is detected
  38. def green_fish():
  39.     # Drive back so axis of rotation lines up with fish, then rotate 90 degrees and drive forward a bit
  40.     driver.straight(-40)
  41.     driver.turn(90)
  42.     driver.straight(20)
  43.  
  44.     # Reset angles
  45.     claw.reset_angle(0)
  46.     lift_motor.reset_angle(0)
  47.  
  48.     # Open claw
  49.     claw.track_target(95)
  50.     wait(200)
  51.     claw.hold()
  52.     wait(1000)
  53.  
  54.     # Lower tray
  55.     lift_motor.run_angle(100, -180, then=Stop.HOLD, wait=True)
  56.     wait(1000)
  57.  
  58.     # Close claw
  59.     claw.track_target(0)
  60.     wait(200)
  61.     claw.hold()
  62.     wait(1000)
  63.  
  64.     # Lift tray
  65.     lift_motor.run_angle(100, 180, then=Stop.HOLD, wait=True)
  66.  
  67.     # Drive back and rotate to original heading
  68.     driver.straight(-20)
  69.     driver.turn(-90)
  70.  
  71. # Define code to drive around a circle of a given diameter
  72. def circle_pond(pond_radius):
  73.  
  74.     # This variable is used to keep track of how far along the circle the robot has traveled, which obviously starts at 0
  75.     degrees_traveled = 0
  76.  
  77.     # This code will keep looping until the robot has gone 360 degrees around the circle
  78.     while degrees_traveled <= 360:
  79.  
  80.         # driver.curve() won't work until the Mindstorm is updated, but it should drive around the circle
  81.         # one degree at a time.
  82.         driver.curve(pond_radius, 1, then=Stop.COAST, wait=True)
  83.  
  84.         # Read the color detected by the sensor, and if it's green, run the green_fish() function to collect it
  85.         color_two = color_sensor_two()
  86.         wait(100)
  87.         if color_two == Color.GREEN:
  88.             green_fish()
  89.        
  90.         # Add one degree to the variable so it knows how far its gone
  91.         degrees_traveled = degrees_traveled + 1
  92.  
  93.  
  94. # Run the function to circle each pond. The argument is the radius of the pond in mm. The first two are disabled for testing.
  95. #circle_pond(203.2) # medium pond
  96. #circle_pond(152.4) # small pond
  97. circle_pond(254) # large pond
Advertisement
Add Comment
Please, Sign In to add comment