找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2013|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

openmv機械臂

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:543223 發(fā)表于 2020-2-24 12:29 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式

  1. import sensor, image, time
  2. from pid import PID
  3. from pyb import Servo

  4. pan_servo=Servo(1)
  5. #tilt_servo=Servo(2)
  6. s1 = Servo(3) # P9 Only for OpenMV3 M7
  7. myservo=s1.angle
  8. s2 = Servo(2) # P9 Only for OpenMV3 M7
  9. myservow=s2.angle

  10. red_threshold  =  (90, 30, -8, -43, 21, 65)

  11. time.sleep(800)
  12. myservo(40)
  13. time.sleep(1000)
  14. myservo(-50)
  15. time.sleep(1000)
  16. myservo(10)
  17. time.sleep(1000)

  18. #for i in range(5):
  19. myservow(20)
  20. time.sleep(800)


  21. pan_pid = PID(p=0.09, i=0, imax=90) #脫機運行或者禁用圖像傳輸,使用這個PID
  22. tilt_pid = PID(p=0.05, i=0, imax=90) #脫機運行或者禁用圖像傳輸,使用這個PID

  23. #pan_pid = PID(p=0.1, i=0, imax=90)#在線調(diào)試使用這個PID
  24. #tilt_pid = PID(p=0.1, i=0, imax=90)#在線調(diào)試使用這個PID

  25. sensor.reset() # Initialize the camera sensor.
  26. sensor.set_pixformat(sensor.RGB565) # use RGB565.
  27. sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
  28. sensor.skip_frames(10) # Let new settings take affect.
  29. sensor.set_auto_whitebal(False) # turn this off.
  30. clock = time.clock() # Tracks FPS.

  31. K=913#the value should be measured

  32. def find_max(blobs):
  33.     max_size=0
  34.     for blob in blobs:
  35.         if blob[2]*blob[3] > max_size:
  36.             max_blob=blob
  37.             max_size = blob[2]*blob[3]
  38.     return max_blob

  39. while(True):
  40.     clock.tick() # Track elapsed milliseconds between snapshots().
  41.     img = sensor.snapshot() # Take a picture and return the image.
  42.     blobs = img.find_blobs([red_threshold])
  43.     if blobs:
  44.         max_blob = find_max(blobs)
  45.         pan_error = max_blob.cx()-img.width()/2
  46.         tilt_error = max_blob.cy()-img.height()/2
  47.         #print("pan_error: ", pan_error)
  48.         img.draw_rectangle(max_blob.rect()) # rect
  49.         img.draw_cross(max_blob.cx(), max_blob.cy()) # cx,
  50.         #print(tilt_servo.angle())
  51.         #print(pan_servo.angle())

  52.         pan_output=pan_pid.get_pid(pan_error,1)/2
  53.         tilt_output=tilt_pid.get_pid(tilt_error,1)
  54.         #print("pan_output",pan_output)
  55.         pan_servo.angle(pan_servo.angle()+pan_output)
  56.         #tilt_servo.angle(tilt_servo.angle()-tilt_output)

  57.         if len(blobs) == 1:
  58.             b = blobs[0]
  59.             Lm = (b[2]+b[3])/2
  60.             length = K/Lm
  61.             print(length)
  62.             print(max_blob.cx(),max_blob.cy())

  63.             widtha=(img.width()/2)*11/10
  64.             widthb=(img.width()/2)*9/10
  65.             heighta=(img.height()/2)*11/10
  66.             heightb=(img.height()/2)*9/10
  67.             if widthb<max_blob.cx()< widtha or heightb< max_blob.cy() < heighta:
  68.                 print(231)
  69.                 if length < 15:
  70.                     print (123)
  71.                     for t in range(4):
  72.                         myservow(70)
  73.                         time.sleep(50)

  74.                     if widthb<max_blob.cx()< widtha or heightb< max_blob.cy() < heighta and length < 13 :
  75.                         while(True):
  76.                             myservo(-40)
  77.                             time.sleep(600)


復(fù)制代碼

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

沙發(fā)
ID:582255 發(fā)表于 2021-1-24 14:40 | 只看該作者
這個貌似只有一點啊,能給個完整的不兄弟
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表