Nawor3565

First "working" fish collector

May 9th, 2022
978
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.33 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.nxtdevices import ColorSensor as NXTColorSensor
  7. #from pybricks import nxtdevices.ColorSensor
  8. from pybricks.parameters import Port, Stop, Direction, Button, Color
  9. from pybricks.tools import wait, StopWatch, DataLog
  10. from pybricks.robotics import DriveBase
  11. from pybricks.media.ev3dev import SoundFile, ImageFile
  12.  
  13.  
  14. # This program requires LEGO EV3 MicroPython v2.0 or higher.
  15. # Click "Open user guide" on the EV3 extension tab for more information.
  16.  
  17.  
  18. # Create your objects here.
  19. ev3 = EV3Brick()
  20. right_motor = Motor(Port.B)
  21. left_motor = Motor(Port.C)
  22. lift_motor = Motor(Port.A)
  23. claw = Motor(Port.D)
  24. light_sensor = LightSensor(Port.S1)
  25. color_sensor = NXTColorSensor(Port.S2) # Faces fish
  26. color_sensor_two = ColorSensor(Port.S3) # Faces down
  27. wheel_diameter = 43.3
  28. axle_track = 130
  29.  
  30. PROPORTIONAL_GAIN = 1.2
  31. DRIVE_SPEED = 75
  32. #RAD_TO_DEG = 57.29578
  33. #HIGH_READING = 40
  34. #LOW_READING = 30
  35. midpoint = 30
  36.  
  37. driver = DriveBase(left_motor, right_motor, wheel_diameter, axle_track)
  38. driver.settings(400, 200, 30, 1200)
  39. fish_collected = 0
  40. amount_per_pond = 4
  41. line_counter = 0
  42. previous_state = False
  43. # Write your program here.
  44. ev3.speaker.beep()
  45. wait(300)
  46.  
  47. def print_on_screen(text):
  48.     ev3.screen.clear()
  49.     ev3.screen.print(str(text))
  50.  
  51. def down_color_percent(color_to_read, return_proportional):
  52.     # Reads the RBG value from the downwards-facing color sensor
  53.     color_reading = color_sensor_two.rgb()
  54.    
  55.     # Separates the tuple into three variables
  56.     read_red_percent = color_reading[0]
  57.     read_green_percent = color_reading[1]
  58.     read_blue_percent = color_reading[2]
  59.  
  60.     # Calculates each color's proportion of the total light returned
  61.     total_color = read_red_percent + read_green_percent + read_blue_percent
  62.     if total_color == 0:
  63.         red_of_total = 0
  64.         green_of_total = 0
  65.         blue_of_total = 0
  66.     else:
  67.         red_of_total = read_red_percent/total_color
  68.         green_of_total = read_green_percent/total_color
  69.         blue_of_total = read_blue_percent/total_color
  70.    
  71.     # Returns the requested value
  72.     if return_proportional:
  73.         if color_to_read == "red":
  74.             return 100*red_of_total
  75.         if color_to_read == "green":
  76.             return 100*green_of_total
  77.         if color_to_read == "blue":
  78.             return 100*blue_of_total
  79.     else:
  80.         if color_to_read == "red":
  81.             return read_red_percent
  82.         if color_to_read == "green":
  83.             return read_green_percent
  84.         if color_to_read == "blue":
  85.             return read_blue_percent
  86.  
  87. def line_counter_check(tape_limit):
  88.     # Fucky thing to only set previous_state to False the first time this runs
  89.     try:
  90.         previous_state
  91.     except NameError:
  92.         previous_state = False
  93.         line_counter = 0
  94.  
  95.     # Prints the line counter to the screen
  96.     print_on_screen(line_counter)
  97.  
  98.     # Reads value from light sensor, returns integer on scale from 1-100
  99.     line_reading = light_sensor.reflection()
  100.    
  101.     # If the measurement is lower than 35, it must be over a line of tape
  102.     if line_reading < 35:
  103.         over_line = True
  104.     else:
  105.         over_line = False
  106.    
  107.     # If the current state is  different from the last state, so it's only run when something actually changes
  108.     if previous_state != over_line:
  109.         # Only increment the line counter when the over_line variable changes from False to True
  110.         if over_line:
  111.             line_counter = line_counter + 1
  112.        
  113.         # This is used to keep track of what the last reading's result was
  114.         previous_state = over_line
  115.     if line_counter < tape_limit:
  116.         return True
  117.     else:
  118.         line_counter = 0
  119.         return False
  120.  
  121. # function for collecting Color.GREEN fish
  122. def green_fish():
  123.     # Stops any motion that's in progress
  124.     driver.stop()
  125.  
  126.     # Drive back so axis of rotation lines up with fish, then rotate 90 degrees and drive forward a bit
  127.     driver.straight(-40)
  128.     driver.turn(95)
  129.     driver.straight(25)
  130.  
  131.     # Reset angles
  132.     claw.reset_angle(0)
  133.     lift_motor.reset_angle(0)
  134.  
  135.     # Open claw
  136.     #claw.track_target(95)
  137.     wait(200)
  138.     claw.hold()
  139.     wait(1000)
  140.  
  141.     # Lower tray
  142.     lift_motor.run_angle(100, -145, then=Stop.HOLD, wait=True)
  143.     wait(1000)
  144.  
  145.     # Close claw
  146.     claw.track_target(-95)
  147.     wait(200)
  148.     claw.hold()
  149.     wait(1000)
  150.  
  151.     # Lift tray
  152.     lift_motor.run_angle(100, 105, then=Stop.HOLD, wait=True)
  153.     claw.track_target(0)
  154.     wait(200)
  155.     lift_motor.run_angle(300, 40, then=Stop.HOLD, wait=True)
  156.  
  157.     # Drive back and rotate to original heading
  158.     driver.straight(-25)
  159.     driver.turn(-95)
  160.  
  161. def circle_pond(radius, maxlines):
  162.     # Loops while the line counter is under a set amount
  163.    
  164.     while line_counter_check(maxlines):
  165.         # calculates a constant turning rate for a given velocity and radius
  166.         #constant_turn_rate = DRIVE_SPEED/(radius + (0.5 * axle_track))
  167.         #const_turn_rate_degrees = RAD_TO_DEG * (constant_turn_rate)
  168.        
  169.         blue = down_color_percent("blue", False)
  170.         green = down_color_percent("green", False)
  171.  
  172.         # Checks for green fish
  173.         if color_sensor.color() == Color.GREEN:
  174.             green_fish()
  175.             print_on_screen("saw fish!")
  176.            
  177.         #line_count= line_counter_check(maxlines)
  178.         #if line_counter_check(maxlines) == False
  179.            # driver.stop()
  180.        
  181.         elif (blue - green >= 1):
  182.                
  183.            
  184.             turn_rate = (((8 / radius)*(blue - green))*400) -40
  185.            
  186.             #if turn_rate > 90:
  187.              #   turn_rate = 90
  188.            
  189.        
  190.            
  191.             # Calculate the deviation from the midpoint.
  192.             #deviation = color_deviation - midpoint
  193.             # Calculate the turn rate.
  194.             #turn_rate = (PROPORTIONAL_GAIN * deviation ) #+ const_turn_rate_degrees
  195.  
  196.         # Set the drive base speed and turn rate.
  197.             driver.drive(DRIVE_SPEED, turn_rate)
  198.             #driver.curve(320, 360)
  199.  
  200.         # You can wait for a short time or do other things in this loop.
  201.         wait(30)
  202.    
  203.     # When the line counter reaches that amount, stop all motion
  204.     driver.stop()
  205.  
  206.  
  207. def drive_to_next_pond():
  208.     #print_on_screen("to next pond...")
  209.  
  210.     # Drives forward until blue portion is higher than 55%
  211.     down_blue = down_color_percent("blue", True)
  212.     while down_blue < 55:  
  213.         driver.drive(DRIVE_SPEED, 0)
  214.         print_on_screen(down_blue)
  215.  
  216.         down_blue = down_color_percent("blue", True)
  217.         wait(10)
  218.    
  219.     # When a pond is reached, stop all motion
  220.     driver.drive(DRIVE_SPEED, 0)
  221.     wait(300)
  222.     driver.stop()
  223.     #driver.drive(DRIVE_SPEED, 0)
  224.     #driver.straight(100, then=Stop.HOLD, wait=True)
  225.  
  226.    
  227. def return_dock():
  228.     print_on_screen("to dock...")
  229.     driver.turn(-90)
  230.     driver.straight(30)
  231.     driver.turn(-90)
  232.     driver.straight(-300)  
  233.     driver.stop()
  234.    
  235. #drive_to_next_pond()
  236. circle_pond(254, 7)
  237. drive_to_next_pond()
  238. circle_pond(152.4, 4)
  239. drive_to_next_pond()
  240. circle_pond(203.2, 5)
  241. return_dock()
  242.  
Advertisement
Add Comment
Please, Sign In to add comment