找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1626|回复: 1
打印 上一主题 下一主题
收起左侧

openmv机械臂

[复制链接]
跳转到指定楼层
楼主
ID:543223 发表于 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)#在线调试使用这个PID
  24. #tilt_pid = PID(p=0.1, i=0, imax=90)#在线调试使用这个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)


复制代码

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:582255 发表于 2021-1-24 14:40 | 只看该作者
这个貌似只有一点啊,能给个完整的不兄弟
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表