Nawor3565

Tuesday final version

May 10th, 2022
960
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.60 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 = ColorSensor(Port.S2) # Faces fish
  26. color_sensor_two = NXTColorSensor(Port.S3) # Faces down
  27. wheel_diameter = 43.3
  28. axle_track = 130
  29. #spkr = Sound()
  30.  
  31. POND_PROPORTIONAL_GAIN = 4#3.4
  32. LINE_PROPORTIONAL_GAIN = 1
  33. DRIVE_SPEED = 110#75
  34. #RAD_TO_DEG = 57.29578
  35. HIGH_READING = 53
  36. LOW_READING = 42
  37. line_midpoint = (HIGH_READING + LOW_READING)/2
  38. pond_midpoint = 43#50
  39.  
  40. driver = DriveBase(left_motor, right_motor, wheel_diameter, axle_track)
  41. driver.settings(800, 200, 60, 1200)
  42. fish_collected = 0
  43. amount_per_pond = 4
  44. line_counter = 0
  45. previous_state = False
  46. # Write your program here.
  47. ev3.speaker.beep()
  48. wait(300)
  49.  
  50. def print_on_screen(text):
  51.     ev3.screen.clear()
  52.     ev3.screen.print(str(text))
  53.  
  54. def down_color_percent(color_to_read, return_proportional):
  55.     # Reads the RBG value from the downwards-facing color sensor
  56.     color_reading = color_sensor_two.rgb()
  57.    
  58.     # Separates the tuple into three variables
  59.     read_red_percent = color_reading[0]
  60.     read_green_percent = color_reading[1]
  61.     read_blue_percent = color_reading[2]
  62.  
  63.     # Calculates each color's proportion of the total light returned
  64.     total_color = read_red_percent + read_green_percent + read_blue_percent
  65.     if total_color == 0:
  66.         red_of_total = 0
  67.         green_of_total = 0
  68.         blue_of_total = 0
  69.     else:
  70.         red_of_total = read_red_percent/total_color
  71.         green_of_total = read_green_percent/total_color
  72.         blue_of_total = read_blue_percent/total_color
  73.    
  74.     # Returns the requested value
  75.     if return_proportional:
  76.         if color_to_read == "red":
  77.             return 100*red_of_total
  78.         if color_to_read == "green":
  79.             return 100*green_of_total
  80.         if color_to_read == "blue":
  81.             return 100*blue_of_total
  82.     else:
  83.         if color_to_read == "red":
  84.             return read_red_percent
  85.         if color_to_read == "green":
  86.             return read_green_percent
  87.         if color_to_read == "blue":
  88.             return read_blue_percent
  89.  
  90. def line_counter_check(tape_limit):
  91.     global line_counter
  92.     global previous_state
  93.     # Fucky thing to only set previous_state to False the first time this runs
  94.     #try:
  95.     #    previous_state
  96.     #except NameError:
  97.     #    previous_state = False
  98.     #    line_counter = 0
  99.  
  100.     # Prints the line counter to the screen
  101.     print_on_screen(line_counter)
  102.  
  103.     # Reads value from light sensor, returns integer on scale from 1-100
  104.     line_reading = light_sensor.reflection()
  105.    
  106.     # If the measurement is lower than 35, it must be over a line of tape
  107.     if line_reading < 40:
  108.         over_line = True
  109.     else:
  110.         over_line = False
  111.    
  112.     # If the current state is  different from the last state, so it's only run when something actually changes
  113.     if previous_state != over_line:
  114.         # Only increment the line counter when the over_line variable changes from False to True
  115.         if over_line:
  116.             line_counter = line_counter + 1
  117.        
  118.         # This is used to keep track of what the last reading's result was
  119.         previous_state = over_line
  120.     if line_counter < tape_limit:
  121.         return True
  122.     else:
  123.         line_counter = 0
  124.         return False
  125.  
  126. # function for collecting Color.GREEN fish
  127. def green_fish(backup_dist, forward_dist, turn_degrees):
  128.     # Stops any motion that's in progress
  129.     driver.stop()
  130.  
  131.     # Drive back so axis of rotation lines up with fish, then rotate 90 degrees and drive forward a bit
  132.     driver.straight(0-backup_dist)
  133.     driver.turn(turn_degrees)
  134.     driver.straight(forward_dist)
  135.  
  136.     # Reset angles
  137.     claw.reset_angle(0)
  138.     lift_motor.reset_angle(0)
  139.  
  140.     # Open claw
  141.     #claw.track_target(95)
  142.     wait(200)
  143.     claw.hold()
  144.     #wait(1000)
  145.  
  146.     # Lower tray
  147.     lift_motor.run_angle(100, -145, then=Stop.HOLD, wait=True)
  148.     #wait(1000)
  149.  
  150.     # Close claw
  151.     claw.track_target(-110)
  152.     wait(400)
  153.     claw.hold()
  154.     #wait(1000)
  155.  
  156.     # Lift tray
  157.     #ev3.speaker.play_file("Lift load.wav")
  158.     lift_motor.run_angle(100, 145, then=Stop.COAST, wait=True)
  159.     #wait(200)
  160.     claw.track_target(0)
  161.     #wait(200)
  162.     #lift_motor.run_angle(300, 40, then=Stop.HOLD, wait=True)
  163.     ev3.speaker.play_file("new_audio_loud.wav")
  164.  
  165.     # Drive back and rotate to original heading
  166.     driver.straight(0-forward_dist)
  167.     driver.turn(0-turn_degrees)
  168.     driver.straight(backup_dist)
  169.  
  170. def circle_pond(radius, maxlines, pond_size):
  171.     global line_counter
  172.     # Loops while the line counter is under a set amount
  173.     if pond_size == "small":
  174.         driver.stop()
  175.         driver.turn(-65) # rotate left X degrees once locating the small pond
  176.     if pond_size == "large":
  177.         driver.stop()
  178.         driver.turn(-40)
  179.     while line_counter_check(maxlines):
  180.         # calculates a constant turning rate for a given velocity and radius
  181.         #constant_turn_rate = DRIVE_SPEED/(radius + (0.5 * axle_track))
  182.         #const_turn_rate_degrees = RAD_TO_DEG * (constant_turn_rate)
  183.        
  184.         #print_on_screen(driver.angle())
  185.  
  186.         blue = down_color_percent("blue", True)
  187.         if blue < 30:
  188.             blue = 30
  189.         #green = down_color_percent("green", False)
  190.  
  191.         # Checks for green fish
  192.         if color_sensor.color() == Color.GREEN:
  193.             if pond_size == "large":
  194.                 green_fish(40, 30, 90)
  195.             elif pond_size == "medium":
  196.                 if line_counter == 0 or line_counter == 5:
  197.                     green_fish(42.5, 35, 95) #normally 44.5, 35, 95
  198.                 else:
  199.                     green_fish(44.5, 35, 90)
  200.             elif pond_size == "small":
  201.                 green_fish(48, 30, 85)
  202.             print_on_screen("saw fish!")
  203.            
  204.         #line_count= line_counter_check(maxlines)
  205.         #if line_counter_check(maxlines) == False
  206.            # driver.stop()
  207.        
  208.         else:# (blue - green >= 1 ):
  209.                
  210.            
  211.             #turn_rate = (((8 / radius)*(blue - green))*400) -40
  212.            
  213.             #if turn_rate > 90:
  214.              #   turn_rate = 90
  215.            
  216.        
  217.            
  218.             # Calculate the deviation from the midpoint.
  219.             deviation = pond_midpoint - blue
  220.             # Calculate the turn rate.
  221.             turn_rate = (POND_PROPORTIONAL_GAIN * deviation ) #+ const_turn_rate_degrees
  222.  
  223.         # Set the drive base speed and turn rate.
  224.             driver.drive(DRIVE_SPEED, turn_rate)
  225.             #driver.curve(320, 360)
  226.  
  227.         # You can wait for a short time or do other things in this loop.
  228.         wait(30)
  229.    
  230.     # When the line counter reaches that amount, stop all motion
  231.     driver.stop()
  232.  
  233. def drive_to_next_pond(two_large):
  234.     #print_on_screen("to next pond...")
  235.     # catches the loop until the color sensor is off the initial pond
  236.     #down_red = down_color_percent("red", True)
  237.     #while down_red < 12:
  238.      #   driver.drive(DRIVE_SPEED, 0)
  239.       #  down_red = down_color_percent("red", True)
  240.        # wait(3000)
  241.     driver.drive(DRIVE_SPEED, 0)
  242.     wait(1000)
  243.     if two_large:
  244.        
  245.         driver.turn(25)
  246.         driver.drive(DRIVE_SPEED, 0)
  247.         wait(1000)
  248.         #driver.drive(DRIVE_SPEED, 0)
  249.  
  250.     # Drives forward until blue portion is higher than 55%
  251.     down_blue = down_color_percent("blue", True)
  252.     while down_blue < 45:  
  253.         driver.drive(DRIVE_SPEED, 0)
  254.         print_on_screen(down_blue)
  255.  
  256.         down_blue = down_color_percent("blue", True)
  257.         wait(10)
  258.     #while color_sensor_two.color() != Color.BLUE:
  259.         #driver.drive(DRIVE_SPEED, 0)
  260.    
  261.     # When a pond is reached, stop all motion
  262.     #driver.drive(DRIVE_SPEED, 0)
  263.     #wait(300)
  264.     driver.stop()
  265.     #driver.drive(DRIVE_SPEED, 0)
  266.     #driver.straight(100, then=Stop.HOLD, wait=True)
  267.  
  268. def follow_line_straight():
  269.      # Calculate the deviation from the threshold.
  270.     deviation = light_sensor.reflection() - line_midpoint
  271.  
  272.  
  273.  
  274.     # Calculate the turn rate.
  275.     turn_rate = LINE_PROPORTIONAL_GAIN * deviation
  276.  
  277.     print_on_screen(deviation)
  278.  
  279.     # Set the drive base speed and turn rate.
  280.     driver.drive(0-(DRIVE_SPEED), turn_rate)
  281.  
  282.     # You can wait for a short time or do other things in this loop.
  283.     wait(10)
  284.    
  285. def return_dock():
  286.     driver.reset()
  287.     print_on_screen("to dock...")
  288.     driver.straight(-2500)#-305
  289.    # driver.turn(25)
  290.     #driver.straight(-275)
  291.     #driver.turn(-30)
  292.     #while driver.distance() >= -200000000:
  293.     #    follow_line_straight()
  294.    
  295.     driver.stop()
  296.    
  297.    
  298. drive_to_next_pond(False)
  299. circle_pond(203.2, 11, "medium")
  300. drive_to_next_pond(False)
  301. circle_pond(152.4, 9, "small")
  302. drive_to_next_pond(True)
  303. circle_pond(254, 10, "large")
  304. return_dock()
  305. #ev3.speaker.play_file("new_audio_loud.wav")
Advertisement
Add Comment
Please, Sign In to add comment