• 安装星瞳实验室APP,快速收到回复。扫描二维码或者点击 https://singtown.com/app/
  • 必看:玩转星瞳论坛了解一下图片上传,代码格式等问题。
  • 一个提问,一个帖子,标题为问题的介绍
  • 请贴出具体的代码,与报错提示。
  • 代码一定要让别人可以运行的文本,不要贴图片
  • openmv程序运行卡,导致部分程序不执行



    • openmv运行时会卡顿,有些程序会不执行,直接跳过,该怎么解决???



    • 请提供具体代码,现象



    • @kidswong999 c = 't'
      send(0x00, c)
      time.sleep(1000)

                                  c = 'j'
                                  send(0x01, c)
                                  time.sleep(4000)
      
                                  s1.pulse_width(790)
                                  time.sleep(1000)
      
                                  c = 'l'
                                  send(0x01, c)
                                  time.sleep(1000)
      
                                  s1.pulse_width(2500)
                                  time.sleep(2000)
      
                                  c = 'k'
                                  send(0x01, c)
                                  time.sleep(1000)


    • 你的代码运行不起来的,需要可以运行的代码



    • @kidswong999 每次收到的字母里边,缺少的部分都不一样,而且多级也会卡,不按照命令执行



    • 你需要提供可以复现问题的代码,我需要能够运行你的错误,才能知道是什么问题。



    • @2475887818
      def find_target(scan_threshold): #寻找目标物
      global angle, c, ROI, flag0, flag2, flag4, flag6, turn, max_blob, second_blob
      img = sensor.snapshot() # Take a picture and return the image.

      blobs = img.find_blobs([scan_threshold], roi = ROI, x_stride = 10, y_stride = 8, pixels_threshold = 50)
      flag4 = 0       #让镜头变化后的下一次数据有效
      if blobs:
          #turn = 0
          max_blob = blobs[0]
          second_blob = blobs[0]
          target_blob = choose_target(blobs)
          #if (flag2 == 1) & (target_blob.cy() >= 60) :
          #    c = 'k'
          #    send(0x01, c)
          #print(target_blob.density())
          print(target_blob.pixels())
          #print(target_blob.cy())
          img.draw_rectangle(target_blob.rect())
          img.draw_cross(target_blob.cx(), target_blob.cy())
          img.draw_cross(second_blob.cx(), second_blob.cy())
          #print(max_blob.pixels())
          if target_blob.cy() < 5 :
              c = 'w'
              send(0x00, c)
              time.sleep(10)
          else :
              if (angle == 200) & (target_blob.cx() <= 95):
                  c = 'a'
                  send(0x00, c)
                  #time.sleep(1000)
              elif (angle == 100) & (target_blob.cx() >= 145) :
                  c = 'd'
                  send(0x00, c)
                  #time.sleep(1000)
              elif (angle >= 100) & (angle <= 200) :
                  if (target_blob.cx() <= 95) :
                      angle = angle + 1
                      if angle > 200 :
                          angle = 200
                      s1.pulse_width(500 + 10*angle)
                      #print(angle)
                  elif (target_blob.cx() >= 145) :
                      angle = angle - 1
                      if angle < 100 :
                          angle = 100
                      s1.pulse_width(500 + 10*angle)
                      #print(angle)
                  elif (target_blob.cx() > 95) & (target_blob.cx() < 145) :
                      if angle < 168 :
                          c = 'd'
                          send(0x00, c)
                      elif angle > 174 :
                          c = 'a'
                          send(0x00, c)
      
                      elif (angle >= 168) & (angle <= 174) :
                          c = 'c'
                          send(0x00, c)
                          time.sleep(10)
                          #ROI = (60, 0, 120, 160)
                          if (flag2 == 1) :
                              if (target_blob.cy() >= 115) :
                                  c = 't'
                                  send(0x00, c)
                                  time.sleep(10)
                                  c = 'k'
                                  send(0x01, c)
                                  flag2 = 0
                                  flag4 = 1
                                  time.sleep(1000)
      
                              elif target_blob.cy() < 115 :
                                  c = 'w'
                                  send(0x00, c)
      
                          elif (flag2 == 0) & (flag4 == 0) :
                              if target_blob.cy() < 105 :
                                  c = 'w'
                                  send(0x00, c)
                              elif target_blob.cy() > 115 :
                                  c = 's'
                                  send(0x00, c)
      
                              elif (target_blob.cy() >= 105) & (target_blob.cy() <= 115) :
                                  c = 't'
                                  send(0x00, c)
                                  time.sleep(1000)
      
                                  c = 'j'
                                  send(0x01, c)
                                  time.sleep(4000)
      
                                  s1.pulse_width(790)
                                  time.sleep(1000)
      
                                  c = 'l'
                                  send(0x01, c)
                                  time.sleep(1000)
      
                                  s1.pulse_width(2500)
                                  time.sleep(2000)
      
                                  c = 'k'
                                  send(0x01, c)
                                  time.sleep(1000)
      
                                  flag0 = 0
                                  flag1 = 0
                                  flag2 = 0
                                  flag3 = 1
                                  flag4 = 0
                                  turn = 0
                                  angle = 200
                                  #ROI = (0, 0, 240, 160)
      
      
      else :
          #pass
          if turn >= 4 :
              flag6 = 1
          elif turn < 4 :
             c = 't'
             send(0x00, c)
             scan_target()


    • 再说一遍,要可以运行,并且能够复现出你的错误的代码。

      你的代码运行不了,连import都没有。

      起码提问之前应该检查一下吧



    • 这是一个做电赛题目的程序,通过按键来决定做的题

      import sensor, image, time, pyb, json, pyb
      from pyb import Servo, Pin, LED, I2C, UART

      ##################### 定义变量 ##########################
      i2c = I2C(2, I2C.MASTER) #定义I2C为主机

      s1 = Servo(1) # P7 #定义舵机

      p0_in = Pin('P0', Pin.IN, Pin.PULL_UP) #定义开关引脚
      p1_in = Pin('P1', Pin.IN, Pin.PULL_UP)
      p2_in = Pin('P2', Pin.IN, Pin.PULL_UP)

      start = pyb.millis()

      led1 = pyb.LED(1) #定义led
      led2 = pyb.LED(2)
      led3 = pyb.LED(3)

      angle = 200 #初始化全局变量
      flag0 = 0 #扫描时是否复位(0——未复位)
      flag1 = 0 #扫描方向(0——向右,1——向左)
      flag2 = 0 #扫描镜头高度(0——低镜头,1——高镜头)
      flag3 = 1 #判断是否为开始第一个扫描循环(1——是开始第一个,不转向;0——不是开始第一个,完成后转向)
      flag4 = 0 #镜头是否进行了由高变低动作(0——未进行,本次cy()值可用;1——应进行,本次cy()不可用)
      flag5 = 0 #机械臂是否转到后方(0——未转到,1——已转到)
      flag6 = 0 #是否完成夹取任务
      flag7 = 0 #是否到达B位置(0——未到达,1——已到达)
      flag8 = 0 #是否到达D位置(0——未到达,1——已到达)
      flag9 = 0 #是否完成倒垃圾
      flag10 = 0
      turn = 0 #搜索目标物时的转动次数
      #turn3 = 0 #搜索目标区域时的转动次数
      c = 't' #要发送的I2C变量
      r = 'z'
      #k=3380 #测距常数
      ROI = (0, 0, 240, 160)

      yellow_threshold = (52, 83, 5, 47, 20, 59)
      #yellow_threshold = (56, 77, 12, 43, 25, 67) #定义阈值
      red_threshold = (44, 84, 27, 76, 16, 56)
      black_threshold = (5, 51, -11, 24, -12, 19)
      #black_threshold = (0, 0, 0, 0, 0, 0)
      scan_threshold = ( 0, 0, 0, 0, 0, 0)

      def choose_target(blobs): #选择目标物

      global turn, max_blob, second_blob
      max_size = 0
      second_size = 0
      
      for blob in blobs:
          if blob.pixels() > max_size:
              second_blob = max_blob
              second_size = max_size
              max_blob = blob
              max_size = blob.pixels()
          elif blob.pixels() > second_size :
              second_blob = blob
              second_size = blob.pixels()
          elif blob.pixels() < second_size :
              pass
          if flag6 == 0 :                           #如果进入搜索目标区域,则关闭对目标区域的排除
              not_goal_area()
          else :
              pass
      
      return max_blob
      

      def not_goal_area() : #排除目标区域
      global max_blob, second_blob
      if max_blob.pixels() >= 1350: #目标大于物块的最大面积,定为区域
      max_blob = second_blob
      elif max_blob.pixels() <= 100: #目标小于区域的最小面积,定为物块
      pass
      #elif max_blob.cy() > 40 :

      elif flag2 == 0 :
          pass
      elif flag2 == 1 :
          pass
      

      def scan_target() : #扫描场地
      global angle, flag0, flag1, flag2, flag3, turn

      if flag2 == 0 :
          c = 'k'
          send(0x01, c)
      elif flag2 == 1 :
          c = 'i'
          send(0x01, c)
      if flag0 == 0 :
          angle = 200
          s1.pulse_width(500 + 10*angle)          #扫描前复位
          flag0 = 1
      else :
          pass
      
      if flag1 == 0 :                             #向右扫描
          angle = angle - 1
          if angle < 100 :
              flag1 = 1
              angle = 100
      
      elif flag1 == 1 :                           #向左扫描
          angle = angle + 1
          if angle > 200 :
              angle = 200
              flag0 = 0
              flag1 = 0
      
              if flag2 == 0 :                     #变换高度扫描
                  flag2 = 1
                  if flag3 == 1 :
                      flag3 = 0
              elif flag2 == 1 :
                  flag2 = 0
                  if flag3 == 0 :
                      turn = turn + 1
                      c = 'a'
                      send(0x00, c)
                      time.sleep(3000)
      
      s1.pulse_width(500 + 10*angle)
      

      def send(addr, char): #发送命令
      try:
      i2c.send(char, addr, timeout = 200) # write 3 bytes to slave with 7-bit address 42
      except OSError as err:
      pass
      return r

      def receive(addr, data): #发送命令
      global r
      try:
      r = i2c.recv(data, addr, timeout = 200) # read 4 bytes from slave with 7-bit address 42
      except OSError as err:
      pass
      return r

      def find_target(scan_threshold): #寻找目标物
      global angle, c, ROI, flag0, flag2, flag4, flag6, turn, max_blob, second_blob
      img = sensor.snapshot() # Take a picture and return the image.

      blobs = img.find_blobs([scan_threshold], roi = ROI, x_stride = 10, y_stride = 8, pixels_threshold = 50)
      flag4 = 0       #让镜头变化后的下一次数据有效
      if blobs:
          #turn = 0
          max_blob = blobs[0]
          second_blob = blobs[0]
          target_blob = choose_target(blobs)
          #if (flag2 == 1) & (target_blob.cy() >= 60) :
          #    c = 'k'
          #    send(0x01, c)
          #print(target_blob.density())
          print(target_blob.pixels())
          #print(target_blob.cy())
          img.draw_rectangle(target_blob.rect())
          img.draw_cross(target_blob.cx(), target_blob.cy())
          img.draw_cross(second_blob.cx(), second_blob.cy())
          #print(max_blob.pixels())
          if target_blob.cy() < 5 :
              c = 'w'
              send(0x00, c)
              time.sleep(10)
          else :
              if (angle == 200) & (target_blob.cx() <= 95):
                  c = 'a'
                  send(0x00, c)
                  #time.sleep(1000)
              elif (angle == 100) & (target_blob.cx() >= 145) :
                  c = 'd'
                  send(0x00, c)
                  #time.sleep(1000)
              elif (angle >= 100) & (angle <= 200) :
                  if (target_blob.cx() <= 95) :
                      angle = angle + 1
                      if angle > 200 :
                          angle = 200
                      s1.pulse_width(500 + 10*angle)
                      #print(angle)
                  elif (target_blob.cx() >= 145) :
                      angle = angle - 1
                      if angle < 100 :
                          angle = 100
                      s1.pulse_width(500 + 10*angle)
                      #print(angle)
                  elif (target_blob.cx() > 95) & (target_blob.cx() < 145) :
                      if angle < 168 :
                          c = 'd'
                          send(0x00, c)
                      elif angle > 174 :
                          c = 'a'
                          send(0x00, c)
      
                      elif (angle >= 168) & (angle <= 174) :
                          c = 'c'
                          send(0x00, c)
                          time.sleep(10)
                          #ROI = (60, 0, 120, 160)
                          if (flag2 == 1) :
                              if (target_blob.cy() >= 115) :
                                  c = 't'
                                  send(0x00, c)
                                  time.sleep(10)
                                  c = 'k'
                                  send(0x01, c)
                                  flag2 = 0
                                  flag4 = 1
                                  time.sleep(1000)
      
                              elif target_blob.cy() < 115 :
                                  c = 'w'
                                  send(0x00, c)
      
                          elif (flag2 == 0) & (flag4 == 0) :
                              if target_blob.cy() < 105 :
                                  c = 'w'
                                  send(0x00, c)
                              elif target_blob.cy() > 115 :
                                  c = 's'
                                  send(0x00, c)
      
                              elif (target_blob.cy() >= 105) & (target_blob.cy() <= 115) :
                                  c = 't'
                                  send(0x00, c)
                                  time.sleep(1000)
      
                                  c = 'j'
                                  send(0x01, c)
                                  time.sleep(4000)
      
                                  s1.pulse_width(790)
                                  time.sleep(1000)
      
                                  c = 'l'
                                  send(0x01, c)
                                  time.sleep(1000)
      
                                  s1.pulse_width(2500)
                                  time.sleep(2000)
      
                                  c = 'k'
                                  send(0x01, c)
                                  time.sleep(1000)
      
                                  flag0 = 0
                                  flag1 = 0
                                  flag2 = 0
                                  flag3 = 1
                                  flag4 = 0
                                  turn = 0
                                  angle = 200
                                  #ROI = (0, 0, 240, 160)
      
      
      else :
          #pass
          if turn >= 4 :
              flag6 = 1
          elif turn < 4 :
             c = 't'
             send(0x00, c)
             scan_target()
      

      def move_to_B(scan_threshold) :

      global angle, c, flag0, flag2, flag4, flag7, turn, max_blob, second_blob
      img = sensor.snapshot() # Take a picture and return the image.
      
      blobs = img.find_blobs([scan_threshold], roi = ROI, x_stride = 10, y_stride = 8, pixels_threshold = 50)
      #flag4 = 0
      if blobs:
          max_blob = blobs[0]
          second_blob = blobs[0]
          target_blob = choose_target(blobs)
          #if (flag2 == 1) & (target_blob.cy() >= 60) :
          #    c = 'k'
          #    send(0x01, c)
          #print(target_blob.density())
          print(target_blob.pixels())
          #print(target_blob.cy())
          img.draw_rectangle(target_blob.rect())
          img.draw_cross(target_blob.cx(), target_blob.cy())
          #print(max_blob.pixels())
          if target_blob.cy() < 5 :
              c = 'w'
              send(0x00, c)
              time.sleep(10)
          else :
              if angle == 200 :
                  if target_blob.cx() <= 90 :
                      c = 'a'
                      send(0x00, c)
                      #time.sleep(1000)
              elif angle == 100 :
                  if target_blob.cx() >= 150 :
                      c = 'd'
                      send(0x00, c)
                      #time.sleep(1000)
              if (target_blob.cx() <= 90) :
                  angle = angle + 1
                  if angle > 200 :
                      angle = 200
                  s1.pulse_width(500 + 10*angle)
                  #print(angle)
              elif (target_blob.cx() >= 150) :
                  angle = angle - 1
                  if angle < 100 :
                      angle = 100
                  s1.pulse_width(500 + 10*angle)
                  #print(angle)
              elif (target_blob.cx() > 90) & (target_blob.cx() < 150) :
                  if angle < 166 :
                      c = 'd'
                      send(0x00, c)
                  elif angle > 176 :
                      c = 'a'
                      send(0x00, c)
      
                  elif (angle >= 166) & (angle <= 176) :
                      if target_blob.cy() < 120 :
                          c = 'w'
                          send(0x00, c)
                      elif target_blob.cy() > 140 :
                          c = 's'
                          send(0x00, c)
      
                      elif (target_blob.cy() >= 120) & (target_blob.cy() <= 140) :
                          c = 't'
                          send(0x00, c)
                          time.sleep(10)
      
                          flag0 = 0
                          flag1 = 0
                          flag2 = 0
                          flag3 = 1
                          flag4 = 0
                          flag7 = 1
                          #turn = 0
      
      
      else :
          c = 'i'
          send(0x01, c)
          time.sleep(10)
          c = 'd'
          send(0x00, c)
          time.sleep(100)
          c = 't'
          send(0x00, c)
      

      def move_to_D() :
      global c, r, flag8
      c = 'e'
      send(0x00, c)
      time.sleep(10)
      if receive(0x00, 1) == b'x' :
      flag0 = 0
      flag1 = 0
      flag2 = 0
      flag3 = 1
      flag4 = 0
      flag8 = 1
      print('@')
      else :
      pass

      #def spotting(scan_threshold) : #测点定位

      def find_goal_area(scan_threshold) :
      global angle, c, flag0, flag2, flag4, flag5, flag9, max_blob, second_blob
      if flag5 == 0 :
      time.sleep(2000)
      c = 't'
      send(0x00, c)
      time.sleep(1000)
      c = 'm'
      send(0x01, c)
      time.sleep(1000)
      s1.pulse_width(790)
      time.sleep(1000)
      c = 'n'
      send(0x01, c)
      time.sleep(500)
      flag5 = 1
      else :
      pass

      img = sensor.snapshot() # Take a picture and return the image.
      
      blobs = img.find_blobs([scan_threshold], roi = ROI, x_stride = 10, y_stride = 8, pixels_threshold = 300)
      
      #flag4 = 0
      
      if blobs:
          max_blob = blobs[0]
          second_blob = blobs[0]
          target_blob = choose_target(blobs)
      
          print(target_blob.density())
          #print(target_blob.pixels())
          #print(target_blob.cy())
          img.draw_rectangle(target_blob.rect())
          img.draw_cross(target_blob.cx(), target_blob.cy())
      
      
          if target_blob.cx() <= 80 :
              c = 'a'
              send(0x00, c)
              time.sleep(200)
      
          elif target_blob.cx() >= 160 :
              c = 'd'
              send(0x00, c)
              time.sleep(200)
      
          elif (target_blob.cx() > 80) & (target_blob.cx() < 160) :
      
              if target_blob.cy() < 145 :
                  c = 's'
                  send(0x00, c)
              elif target_blob.cy() > 150 :
                  c = 'w'
                  send(0x00, c)
      
              elif (target_blob.cy() >= 145) & (target_blob.cy() <= 150) :
                  c = 'c'
                  send(0x00, c)
      
                  c = 't'
                  send(0x00, c)
                  time.sleep(1000)
      
                  c = 'm'
                  send(0x01, c)
                  time.sleep(1000)
      
                  s1.pulse_width(2210)
                  time.sleep(1000)
      
                  c = 'o'
                  send(0x01, c)
                  time.sleep(2000)
      
                  c = 's'
                  send(0x00, c)
                  time.sleep(2000)
                  c = 't'
                  send(0x00, c)
                  time.sleep(1000)
                  flag0 = 0
                  flag1 = 0
                  flag9 = 1
                  #flag4 = 0
      
      else :
          #pass
          c = 'd'
          send(0x00, c)
          time.sleep(1000)
          c = 't'
          send(0x00, c)
      

      #def sport() :

      ################### 初始化 ###############################
      sensor.reset() # Initialize the camera sensor.
      sensor.set_pixformat(sensor.RGB565) # use RGB565.
      sensor.set_framesize(sensor.HQVGA) # use QQVGA for speed.
      sensor.skip_frames(10) # Let new settings take affect.
      sensor.set_auto_whitebal(False) # turn this off.
      clock = time.clock() # Tracks FPS.

      s1.pulse_width(2500)
      c = 't'
      send(0x00, c)
      time.sleep(10)
      c = 'k'
      send(0x01, c)
      time.sleep(1000)
      flag10 = 0

      while (True) :
      #print(flag6)
      #print(angle)
      print(pyb.elapsed_millis(start))
      print(turn)
      if (p1_in.value() == 0) & (p0_in.value() == 0) :
      led1.on()
      if flag10 == 0 :
      c = 'w'
      send(0x00, c)
      time.sleep(4000)
      c = 's'
      send(0x00, c)
      time.sleep(3000)
      c = 't'
      send(0x00, c)
      time.sleep(1000)
      flag10 = 1
      elif flag10 == 1 :
      if (flag6 == 0) & (pyb.elapsed_millis(start) < 150000) :
      find_target(black_threshold)
      print('a')
      else :
      flag6 = 1
      find_goal_area(black_threshold)
      print('b')
      elif (p1_in.value() == 0) & (p0_in.value() == 1) :
      led2.on()
      if (flag6 == 0) & (pyb.elapsed_millis(start) < 60000) :
      find_target(black_threshold)
      print('a')
      else :
      flag6 = 1
      if flag7 == 0 :
      move_to_B(black_threshold)
      print('b')
      elif flag7 == 1 :
      if flag8 == 0 :
      move_to_D()
      print('c')
      elif flag8 == 1 :
      if flag9 == 0 :
      find_goal_area(red_threshold)
      print('d')
      elif flag9 == 1 :
      c = 't'
      send(0x00, c)
      time.sleep(100)
      elif (p1_in.value() == 1) & (p0_in.value() == 0) :
      led3.on()
      if flag6 == 0 :
      find_target(yellow_threshold)
      print('a')
      elif flag6 == 1 :
      if flag7 == 0 :
      move_to_B(black_threshold)
      print('b')
      elif flag7 == 1 :
      if flag8 == 0 :
      move_to_D()
      print('c')
      elif flag8 == 1 :
      if flag9 == 0 :
      find_goal_area(red_threshold)
      print('d')
      elif flag9 == 1 :
      c = 't'
      send(0x00, c)
      time.sleep(100)

      elif (p1_in.value() == 1) & (p0_in.value() == 1) :
          c = 't'
          send(0x00, c)


    • 所以有什么问题?

      • 想要的现象,想实现的功能
      • 哪里有问题,有什么现象
      • 自己尝试过什么解决办法


    • 现在不能正常的夹目标,find_target()里边的最后部分不能完整顺利的执行,有时候会越过一部分的动作,导致夹目标动作出现问题
      就以下的这些动作出问题(延时是给机械臂执行动作的时间):
      c = 't'
      send(0x00, c)
      time.sleep(1000)

                              c = 'j'
                              send(0x01, c)
                              time.sleep(4000)
      
                              s1.pulse_width(790)
                              time.sleep(1000)
      
                              c = 'l'
                              send(0x01, c)
                              time.sleep(1000)
      
                              s1.pulse_width(2500)
                              time.sleep(2000)
      
                              c = 'k'
                              send(0x01, c)
                              time.sleep(1000)


    • 是数据没有发出去?还是执行器有问题?

      有没有用示波器看波形?

      起码你应该print一下,看一下是否是逻辑的问题。



    • @kidswong999 单个没问题,把各部分集成到一块就会出错,而且不一定出错,有时候会,有时候不会



    • 还是我上面的话,你根本也没回答我的问题……

      是数据没有发出去?还是执行器有问题?

      有没有用示波器看波形?

      起码你应该print一下,看一下是否是逻辑的问题。



    • 数据发送的时候有一部分会丢失,导致执行部分不能执行完整的动作,没有示波器,看不了波形,单个运行的时候都是正常的,说明这一部分的逻辑应该是没问题的。