Nawor3565

Drive between ponds

May 2nd, 2022
1,015
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.61 KB | None | 0 0
  1. from pybricks.hubs import EV3Brick
  2. from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor,
  3.                                  InfraredSensor, UltrasonicSensor, GyroSensor)
  4. from pybricks.parameters import Port, Stop, Direction, Button, Color
  5. from pybricks.tools import wait, StopWatch, DataLog
  6. from pybricks.robotics import DriveBase
  7. from pybricks.media.ev3dev import SoundFile, ImageFile
  8.  
  9.  
  10. # This program requires LEGO EV3 MicroPython v2.0 or higher.
  11. # Click "Open user guide" on the EV3 extension tab for more information.
  12.  
  13.  
  14. # Create your objects here.
  15. ev3 = EV3Brick()
  16. right_motor = Motor(Port.B)
  17. left_motor = Motor(Port.C)
  18. lift_motor = Motor(Port.A)
  19. claw = Motor(Port.D)
  20. light_sensor = LightSensor(Port.S1)
  21. color_sensor = ColorSensor(Port.S2)
  22. color_sensor_two = ColorSensor(Port.S3)
  23. wheel_diameter = 43.3
  24. axle_track = 130
  25.  
  26. PROPORTIONAL_GAIN = 1.2
  27. DRIVE_SPEED = 75
  28. #RAD_TO_DEG = 57.29578
  29. HIGH_READING = 40
  30. LOW_READING = 30
  31. midpoint = (HIGH_READING + LOW_READING)/2
  32.  
  33. driver = DriveBase(left_motor, right_motor, wheel_diameter, axle_track)
  34. driver.settings(800, 200, 30, 1200)
  35. fish_collected = 0
  36. amount_per_pond = 4
  37. line_counter = 0
  38.  
  39. # Write your program here.
  40. ev3.speaker.beep()
  41. wait(300)
  42.  
  43. def down_color_percent(color_to_read):
  44.     # Reads the RBG value from the downwards-facing color sensor
  45.     reading = color_sensor_two.rgb()
  46.    
  47.     # Separates the tuple into three variables
  48.     read_red_percent = str(color_reading[0])
  49.     read_green_percent = str(color_reading[1])
  50.     read_blue_percent = str(color_reading[2])
  51.    
  52.     # Returns the requested value
  53.     if color_to_read == "red":
  54.         return read_red_percent
  55.     if color_to_read == "green":
  56.         return read_green_percent
  57.     if color_to_read == "blue":
  58.         return read_blue_percent
  59.  
  60. def line_counter_check():
  61.     # Reads value from light sensor, returns integer on scale from 1-100
  62.     line_reading = line_sensor.reflection()
  63.    
  64.     # If the measurement is lower than ___, it must be over a line of tape
  65.     if line_reading < !!!MEASURE:
  66.         over_line = True
  67.     else:
  68.         over_line = False
  69.    
  70.     # If the current state is  different from the last state, so it's only run when something actually changes
  71.     if previous_state != over_line:
  72.         # Only increment the line counter when the over_line variable changes from False to True
  73.         if over_line:
  74.             line_counter = line_counter + 1
  75.        
  76.         # This is used to keep track of what the last reading's result was
  77.         previous_state = over_line
  78.  
  79. def green_fish(): # function for collecting Color.GREEN fish
  80. # Stops any motion that's in progress
  81.     driver.stop()
  82.  
  83. # Drive back so axis of rotation lines up with fish, then rotate 90 degrees and drive forward a bit
  84.     driver.straight(-40)
  85.     driver.turn(90)
  86.     driver.straight(20)
  87.  
  88. # Reset angles
  89.     claw.reset_angle(0)
  90.     lift_motor.reset_angle(0)
  91.  
  92. # Open claw
  93.     claw.track_target(95)
  94.     wait(200)
  95.     claw.hold()
  96.     wait(1000)
  97.  
  98. # Lower tray
  99.     lift_motor.run_angle(100, -180, then=Stop.HOLD, wait=True)
  100.     wait(1000)
  101.  
  102. # Close claw
  103.     claw.track_target(0)
  104.     wait(200)
  105.     claw.hold()
  106.     wait(1000)
  107.  
  108. # Lift tray
  109.     lift_motor.run_angle(100, 180, then=Stop.HOLD, wait=True)
  110.  
  111. # Drive back and rotate to original heading
  112.     driver.straight(-20)
  113.     driver.turn(-90)
  114.  
  115.  
  116. def circle_pond(radius):
  117.     # Loops while the line counter is under a set amount
  118.     while line_counter < !!!TEST
  119.         # calculates a constant turning rate for a given velocity and radius
  120.         #constant_turn_rate = DRIVE_SPEED/(radius + (0.5 * axle_track))
  121.         #const_turn_rate_degrees = RAD_TO_DEG * (constant_turn_rate)
  122.        
  123.         # Checks for green fish
  124.         if  color_sensor.color() == Color.Green:
  125.             green_fish()
  126.            
  127.         # Checks if there's a line to count
  128.         line_counter_check()
  129.  
  130.         # Calculate the deviation from the midpoint.
  131.         deviation = down_color_percent("blue") - midpoint
  132.  
  133.         # Calculate the turn rate.
  134.         turn_rate = (PROPORTIONAL_GAIN * deviation) #+ const_turn_rate_degrees
  135.  
  136.         # Set the drive base speed and turn rate.
  137.         driver.drive(DRIVE_SPEED, turn_rate)
  138.  
  139.         # You can wait for a short time or do other things in this loop.
  140.         wait(10)
  141.    
  142.     # When the line counter reaches that amount, stop all motion
  143.     driver.stop()
  144.  
  145.  
  146. def drive_to_next_pond():
  147.     # Drives forward until stopped
  148.     driver.drive(DRIVE_SPEED, 0)
  149.    
  150.     while blue_percent < !!!MEASURE:
  151.         # Read RGB values from sensor, returns tuple of (presumably) [red%, green%, blue%]
  152.         blue_percent = down_color_percent("blue")
  153.    
  154.     # When a pond is reached, stop all motion
  155.     driver.stop()
  156.  
  157.  
  158. #drive_to_next_pond()
  159. #circle_pond(203.2) # medium pond
  160.  
  161. #drive_to_next_pond()
  162. #circle_pond(152.4) # small pond
  163.  
  164. drive_to_next_pond()
  165. circle_pond(254) # large pond
  166.  
Advertisement
Add Comment
Please, Sign In to add comment