Nawor3565

Untitled

May 2nd, 2022
1,088
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.86 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. ev3.speaker.beep()
  33. wait(300)
  34.  
  35. # Code to run when a green fish is detected
  36. def green_fish():
  37.     # Drive back so axis of rotation lines up with fish, then rotate 90 degrees and drive forward a bit
  38.     driver.straight(-40)
  39.     driver.turn(90)
  40.     driver.straight(20)
  41.  
  42.     # Reset angles
  43.     claw.reset_angle(0)
  44.     lift_motor.reset_angle(0)
  45.  
  46.     # Open claw
  47.     claw.track_target(95)
  48.     wait(200)
  49.     claw.hold()
  50.     wait(1000)
  51.  
  52.     # Lower tray
  53.     lift_motor.run_angle(100, -180, then=Stop.HOLD, wait=True)
  54.     wait(1000)
  55.  
  56.     # Close claw
  57.     claw.track_target(0)
  58.     wait(200)
  59.     claw.hold()
  60.     wait(1000)
  61.  
  62.     # Lift tray
  63.     lift_motor.run_angle(100, 180, then=Stop.HOLD, wait=True)
  64.  
  65.     # Drive back and rotate to original heading
  66.     driver.straight(-20)
  67.     driver.turn(-90)
  68.  
  69. # Code to drive around a circle of a given diameter
  70. def circle_pond(pond_radius):
  71.  
  72.     # This variable is used to keep track of how far along the circle the robot has traveled
  73.     degrees_traveled = 0
  74.  
  75.     # This code will keep looping until the robot has gone 360 degrees around the circle
  76.     while degrees_traveled <= 360:
  77.  
  78.         # driver.curve() won't work until the Mindstorm is updated, but it should drive around the circle
  79.         # one degree at a time.
  80.         driver.curve(pond_radius, 1, then=Stop.COAST, wait=True)
  81.  
  82.         # Read the color detected by the sensor, and if it's green, run the green_fish() function to collect it
  83.         color_two = color_sensor_two()
  84.         wait(100)
  85.         if color_two == Color.GREEN:
  86.             green_fish()
  87.        
  88.         # Add one degree to the varible so it knows how far its gone
  89.         degrees_traveled = degrees_traveled + 1
  90.  
  91.  
  92. # while fish_collected <= amount_per_pond:
  93. #     color_one = color_sensor_one()
  94. #     color_two = color_sensor_two()
  95. #     if color_two == Color.GREEN:# The if statements are just checking to see if we are looking ate a Color.GREEN fish
  96. #         green_fish()              # if the sensor is looking at a Color.GREEN fish it will run the drop code function
  97. #     while color_sensor_one() == Color.BLUE:
  98. #         left_motor.run(65)
  99. #         right_motor.run(75)
  100. #         if color_two == Color.GREEN:
  101. #             green_fish()
  102. #         color_one = color-_sensor_one()
  103. #         color_two = color_sensor_two()
  104. #     while color_sensor_one() != Color.BLUE:
  105. #         left_motor.run(80)
  106. #         right_motor.run(60)
  107. #         if color_two == Color.GREEN:
  108. #             green_fish()
  109. #         color_one = color_sensor_one()
  110. #         color_two = color_sensor_two()
  111.  
  112. # Run the function to circle each pond. The argument is the radius of the pond in mm. The first two are disabled for now for testing.
  113. #circle_pond(203.2) # medium pond
  114. #circle_pond(152.4) # small pond
  115. circle_pond(254) # large pond
Advertisement
Add Comment
Please, Sign In to add comment