• OpenMV VSCode 扩展发布了,在插件市场直接搜索OpenMV就可以安装
  • 如果有产品硬件故障问题,比如无法开机,论坛很难解决。可以直接找售后维修
  • 发帖子之前,请确认看过所有的视频教程,https://singtown.com/learn/ 和所有的上手教程http://book.openmv.cc/
  • 每一个新的提问,单独发一个新帖子
  • 帖子需要目的,你要做什么?
  • 如果涉及代码,需要报错提示全部代码文本,请注意不要贴代码图片
  • 必看:玩转星瞳论坛了解一下图片上传,代码格式等问题。
  • 我这个代码运行这 舵机也在供电 舵机他就不动啊 代码也没问题啊 他连初始化的动作都不做啊



    • ![1_1716033410840_2.jpg](正在上传 100%) ![0_1716033410840_1.jpg](正在上传 100%)

      import time,sensor,image,math,pyb
      from servo import Servos
      #from pyb import Pin#可有可无
      from machine import I2C, Pin
      
      #摄像头初始化函数
      sensor.reset()
      sensor.set_pixformat(sensor.GRAYSCALE)
      sensor.set_framesize(sensor.VGA) # High Res!
      sensor.set_windowing((80, 640)) #该部分是摄像头分辨率调整,条形码建议640x80或反之
      sensor.skip_frames(30)
      sensor.set_auto_gain(False)  # must turn this off to prevent image washout...
      sensor.set_auto_whitebal(False)  # must turn this off to prevent image washout...
      clock = time.clock()
      
      i2c = I2C(sda=Pin('P5'), scl=Pin('P4'))#pca9685初始化,下面预计为舵机使用iic地址配置
      servo = Servos(i2c, address=0x40, freq=50, min_us=500, max_us=2500, degrees=180)
      
      pin1 = Pin('P1', Pin.IN, Pin.PULL_UP)   ##将P1口作为阈值控制口 OUT_PP PULL_NONE
      key1=0
      
      #所有舵机度数为90度时机械臂与地面垂直
      servo.position(0,90)#初始化舵机位置,从下到上6个舵机
      time.sleep_ms(1000)
      servo.position(1,90)#按支架方向度数减小是向前
      time.sleep_ms(1000)
      servo.position(2,90)#该舵机度数增大是向前
      time.sleep_ms(1000)
      servo.position(3,90)#该舵机度数增大是向前申
      time.sleep_ms(1000)
      servo.position(4,90)#旋转机械爪舵机,一般不用动,90度是水平状态
      servo.position(5,80)#机械爪张合舵机(120度完全闭合,减小度数张开)
      time.sleep_ms(2000)
      
      #按键中断函数
      def callback_PIN1(line):
          global key1#全局变量
          key1=1
          pyb.delay(5)#按键消抖,下面内容为外部中断配置
      extint = pyb.ExtInt(pin1, pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_UP, callback_PIN1)
      
      #条形码识别函数(详细可参考星曈官网)
      def barcode_name(code):
          if(code.type() == image.EAN2):
              return "EAN2"
          if(code.type() == image.EAN5):
              return "EAN5"
          if(code.type() == image.EAN8):
              return "EAN8"
          if(code.type() == image.UPCE):
              return "UPCE"
          if(code.type() == image.ISBN10):
              return "ISBN10"
          if(code.type() == image.UPCA):
              return "UPCA"
          if(code.type() == image.EAN13):
              return "EAN13"
          if(code.type() == image.ISBN13):
              return "ISBN13"
          if(code.type() == image.I25):
              return "I25"
          if(code.type() == image.DATABAR):
              return "DATABAR"
          if(code.type() == image.DATABAR_EXP):
              return "DATABAR_EXP"
          if(code.type() == image.CODABAR):
              return "CODABAR"
          if(code.type() == image.CODE39):
              return "CODE39"
          if(code.type() == image.PDF417):
              return "PDF417"
          if(code.type() == image.CODE93):
              return "CODE93"
          if(code.type() == image.CODE128):
              return "CODE128"
      
      #用字典将大中小和左中右位置对应
      barcode_positions = {}
      
      #条形码扫描函数
      def scan_barcode(position_label):
          while True:#死循环,未检测到条形码则一直检测
              clock.tick()
              img = sensor.snapshot()
              codes = img.find_barcodes()
              if codes:#该部分可参考星曈官网示例
                  for code in codes:
                      img.draw_rectangle(code.rect())
                      barcode_type = barcode_name(code)
                      payload = code.payload()
                      barcode_positions[payload] = position_label
                      print("Barcode %s, Payload \"%s\", position %s" % (barcode_type, payload, position_label))
                      return  # Successfully detected a barcode, exit the loop
              else:
                  print("No barcode detected, retrying...")
      
      #扫描右侧函数
      def scan_move_right():
          for pos0 in range(90,80,-2):
              servo.position(0,pos0)
              time.sleep_ms(50)
          time.sleep_ms(500)
      
      #中间扫描函数
      def scan_move_middle():
          for pos1 in range(90,50,-2):
              servo.position(1,pos1)
              time.sleep_ms(50)
          time.sleep_ms(500)
          for pos3 in range(90,160,2):
              servo.position(3,pos3)
              time.sleep_ms(30)
          time.sleep_ms(500)
          for pos2 in range(90,105,2):
              servo.position(2,pos2)
              time.sleep_ms(50)
          time.sleep_ms(500)
      
      #扫描左侧函数
      def scan_move_left():
          for pos0 in range(90,102,2):
              servo.position(0,pos0)#(100度左右适宜,可能略有误查)
              time.sleep_ms(50)
          time.sleep_ms(500)
      
      #扫描后复位到中间
      def scan_move_back():
          for pos0 in range(80,91,2):#扫描完所有条形码后机械臂转回中间
              servo.position(0,pos0)
              time.sleep_ms(50)
          time.sleep_ms(500)
          for pos1 in range(52,90,2):
              servo.position(1,pos1)
              time.sleep_ms(50)
          time.sleep_ms(500)
      #扫描后的抓取函数左中右一次性抓取
      def catch_move(barcode_positions,key):
          if barcode_positions.get(key)=='L':
              for pos0 in range(90,101,2):
                  servo.position(0,pos0)
                  time.sleep_ms(50)
              time.sleep_ms(500)
              for pos1 in range(90,50,-2):
                  servo.position(1,pos1)
                  time.sleep_ms(50)
              time.sleep_ms(500)
              for pos3 in range(90,75,-2):
                  servo.position(3,pos3)
                  time.sleep_ms(50)
              time.sleep_ms(500)
              for pos2 in range(90,150,2):
                  servo.position(2,pos2)
                  time.sleep_ms(50)
              time.sleep_ms(500)
              servo.position(5,110)
              time.sleep_ms(500)
              for pos1 in range(50,90,2):
                  servo.position(1,pos1)
                  time.sleep_ms(50)
              time.sleep_ms(500)
              for pos0 in range(100,160,2):
                  servo.position(0,pos0)
                  time.sleep_ms(50)
              time.sleep_ms(2000)
              #if key1==1 and pin1.value()==0:#按键按下则打开机械爪并旋转复位,否则机械臂不动
                  #servo.position(3,110)#机械爪张开
                  #time.sleep_ms(1000)
                  #servo.position(0,90)
                  #time.sleep_ms(5000)
              #else:
                  #servo.position(0,180)
                  #time.sleep_ms(1000)
          if barcode_positions.get(key)=='M':
              servo.position(0,90)
              time.sleep_ms(500)
              for pos1 in range(90,50,-2):
                  servo.position(1,pos1)
                  time.sleep_ms(50)
              time.sleep_ms(500)
              for pos3 in range(90,75,-2):
                  servo.position(3,pos3)
                  time.sleep_ms(50)
              time.sleep_ms(500)
              for pos2 in range(90,150,2):
                  servo.position(2,pos2)
                  time.sleep_ms(50)
              time.sleep_ms(500)
              servo.position(5,110)
              time.sleep_ms(500)
              for pos1 in range(50,90,2):
                  servo.position(1,pos1)
                  time.sleep_ms(50)
              time.sleep_ms(500)
              for pos0 in range(90,160,2):
                  servo.position(0,pos0)
                  time.sleep_ms(50)
              time.sleep_ms(2000)
      
              #if key1==1 and pin1.value()==0:#按键按下则打开机械爪并旋转复位,否则机械臂不动
                  #servo.position(3,110)#机械爪张开
                  #time.sleep_ms(1000)
                  #servo.position(0,90)
                  #time.sleep_ms(5000)
              #else:
                  #servo.position(0,180)
                  #time.sleep_ms(1000)
          if barcode_positions.get(key)=='R':
              for pos0 in range(90,80,-2):
                  servo.position(0,pos0)
                  time.sleep_ms(50)
              time.sleep_ms(500)
              for pos1 in range(90,50,-2):
                  servo.position(1,pos1)
                  time.sleep_ms(50)
              time.sleep_ms(500)
              for pos3 in range(90,75,-2):
                  servo.position(3,pos3)
                  time.sleep_ms(50)
              time.sleep_ms(500)
              for pos2 in range(90,150,2):
                  servo.position(2,pos2)
                  time.sleep_ms(50)
              time.sleep_ms(500)
              servo.position(5,110)
              time.sleep_ms(500)
              for pos1 in range(50,90,2):
                  servo.position(1,pos1)
                  time.sleep_ms(50)
              time.sleep_ms(500)
              for pos0 in range(80,160,2):
                  servo.position(0,pos0)
                  time.sleep_ms(50)
              time.sleep_ms(2000)
              #if key1==1 and pin1.value()==0:#按键按下则打开机械爪并旋转复位,否则机械臂不动
                  #servo.position(3,110)#机械爪张开
                  #time.sleep_ms(1000)
                  #servo.position(0,90)
                  #time.sleep_ms(5000)
              #else:
                  #servo.position(0,180)
                  #time.sleep_ms(1000)
      #######################################################################
      #以下为本次抓取程序,上述为各部分函数
      
      #检测中间物体的条形码
      scan_move_middle()
      time.sleep_ms(500)
      scan_barcode("M")
      #向左移动
      scan_move_left()
      time.sleep_ms(500)
      #左边识别条形码
      scan_barcode("L")
      #向右移动
      #右边识别条形码
      scan_move_right()
      scan_barcode("R")
      time.sleep_ms(500)
      scan_move_back()
      time.sleep_ms(500)
      #以下为抓取物料顺序,更改引号内内容即可
      catch_move(barcode_positions,"middle")
      if key1==1 and pin1.value()==0:#按键按下则打开机械爪并旋转复位,否则机械臂不动
          servo.position(5,80)#机械爪张开
          time.sleep_ms(1000)
          for i in range(160,90,-2):
              servo.position(0,i)
              time.sleep_ms(50)
          time.sleep_ms(500)
      catch_move(barcode_positions,"small")
      if key1==1 and pin1.value()==0:#按键按下则打开机械爪并旋转复位,否则机械臂不动
          servo.position(5,80)#机械爪张开
          time.sleep_ms(1000)
          for i in range(160,90,-2):
              servo.position(0,i)
              time.sleep_ms(50)
          time.sleep_ms(500)
      catch_move(barcode_positions,"big")
      if key1==1 and pin1.value()==0:#按键按下则打开机械爪并旋转复位,否则机械臂不动
          servo.position(5,80)#机械爪张开
          time.sleep_ms(1000)
          for i in range(160,90,-2):
              servo.position(0,i)
              time.sleep_ms(50)
          time.sleep_ms(500)
      #这样大中小就和方位对应起来了
      #比如定义一个moveto(L/M/R)ifL左移,ifR右移,if不动
      #机械臂移动到中方块处moveto(barcode_positions["middle"])然后拿走
      #回到初始位置
      #移到小方块处moveto(barcode_positions["small"])拿走
      #回到初始位置
      #移到大方块处moveto(barcode_positions["big"])拿走
      #回到初始位置
      
      ![0_1716033374049_IMG_20240518_195556.jpg](正在上传 100%)