Advertisement
Guest User

kriselosning

a guest
Nov 15th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.68 KB | None | 0 0
  1. # Single Color RGB565 Blob Tracking Example
  2. #
  3. # This example shows off single color RGB565 tracking using the OpenMV Cam.
  4.  
  5. import sensor, image, time
  6.  
  7. threshold_index = 1 # 0 for red, 1 for green, 2 for blue
  8.  
  9. # Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
  10. # The below thresholds track in general red/green/blue things. You may wish to tune them...
  11. thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds
  12.               (27, 100, -74, -25, 13, 62), # generic_green_thresholds
  13.               (0, 30, 0, 64, -128, 0)] # generic_blue_thresholds
  14.  
  15. sensor.reset()
  16. sensor.set_pixformat(sensor.RGB565)
  17. sensor.set_framesize(sensor.QVGA)
  18. sensor.skip_frames(time = 2000)
  19. sensor.set_auto_gain(False) # must be turned off for color tracking
  20. sensor.set_auto_whitebal(False) # must be turned off for color tracking
  21. clock = time.clock()
  22.  
  23. # Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are
  24. # returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the
  25. # camera resolution. "merge=True" merges all overlapping blobs in the image.
  26.  
  27. from pyb import Servo
  28.  
  29. x_pos = 1500 # default
  30. y_pos = 1500 # default
  31.  
  32. x_min = 640
  33. x_max = 2500
  34. y_max = 2500
  35. y_min = 640
  36.  
  37. x_gain = -.75 # You have to tweak this value to stablize the control loop.
  38.                # You also may need to invert the value if the system goes
  39.                # in the wrong direction.
  40. y_gain = -.75 # You have to tweak this value to stablize the control loop.
  41.                # You also may need to invert the value if the system goes
  42.                # in the wrong direction.
  43. xServo = Servo(1)
  44. yServo = Servo(2)
  45. xServo.pulse_width(x_pos)
  46. yServo.pulse_width(y_pos)
  47.  
  48. while(True):
  49.     clock.tick()
  50.     img = sensor.snapshot()
  51.     for blob in img.find_blobs([thresholds[threshold_index]], pixels_threshold=200, area_threshold=200, merge=True):
  52.         img.draw_rectangle(blob.rect())
  53.         img.draw_cross(blob.cx(), blob.cy())
  54.  
  55.         x = blob.cx()
  56.         y = blob.cy()
  57.  
  58.         x_error = x - (img.width()/2)
  59.         y_error = y - (img.height()/2)
  60.  
  61.         x_pos += x_error * x_gain
  62.         y_pos += y_error * y_gain
  63.  
  64.         # Clamp output between min and max
  65.         if (x_pos > x_max):
  66.             x_pos = x_max
  67.         if (x_pos < x_min):
  68.             x_pos = x_min
  69.  
  70.         # Clamp output between min and max
  71.         if (y_pos > y_max):
  72.             y_pos = y_max
  73.         if (y_pos < y_min):
  74.             y_pos = y_min
  75.  
  76.         xServo.pulse_width(int(x_pos))
  77.         yServo.pulse_width(int(y_pos))
  78. #        print(x)
  79. #        print(y)
  80.  
  81.     # Draw FPS
  82.     img.draw_string(0, 0, "FPS:%.2f"%(clock.fps()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement