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



    • 就是先从arduino向openmv发送数据,openmv就收到信号后执行程序,然后上传数据给arduino,我能用的语句都用了,还是出错,就是arduino发出的信号openmv无法接受,请教一下

      Untitled - By: xuping - 周日 5月 20 2018

      import sensor, image, time, pyb
      from pyb import UART
      import json
      import math

      black_threshold =(100,59,33,-100,-128,30)

      red_threshold =(0,100,8,127,-128,127)

      sensor.reset() # Initialize the camera sensor.
      sensor.set_pixformat(sensor.RGB565) # use RGB565.
      sensor.set_framesize(sensor.QVGA) # 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.

      uart = UART(3, 9600)
      uart.init(9600, bits=8, parity=None, stop=1)
      while(True):

      if uart.any():
           #判断是否有数据, 有的话就读入
          tmp_data = uart.readall()
          print(tmp_data)
      

      string1="R"
      string2="L"
      string3="U"
      string5="S"
      string4="D"
      #定义点的函数
      class Point:
      def init(self,x=0,y=0):
      self.x=x
      self.y=y
      def getx(self):
      return self.x
      def gety(self):
      return self.y

      class Getlen:
      def init(self,p1,p2):
      self.x=p1.getx()-p2.getx()
      self.y=p1.gety()-p2.gety()
      #用math.sqrt()求平方根
      self.len= math.sqrt((self.x2)+(self.y2))
      #定义得到直线长度的函数
      def getlen(self):
      return self.len

      if tmp_data:
      while(True):
      clock.tick()
      img = sensor.snapshot()
      img.draw_cross(160,120)
      blobs = img.find_blobs([red_threshold], area_threshold=128)
      if blobs:
      print('sum : %d'% len(blobs))
      out_str = '['
      for b in blobs:
      img.draw_rectangle(b[0:4]) # rect
      img.draw_cross(b[5], b[6]) # cx, cy
      x = b[5]
      y = b[6]
      p1=Point(b[5],b[6])
      p2=Point(160,120)
      l=Getlen(p1,p2)
      #获取两点之间直线的长度

                 print(l.getlen())
                 img.get_pixel(x,y)
                 out_str += '(%d,%d)' % (x,y)
      
             out_str += ']'
             print(out_str)
             uart.write(out_str+'\r\n')
             uart.write(l.getlen())
      
             uart = UART(3, 9600)
             for i in (0,360):
                if x<160:
                    uart.write(string1+'\r\n')
                elif x>160:
                    uart.write(string2+'\r\n')
                else:
                    uart.write(string5+'\r\n')
                for j in (0,240):
                    if x<120:
                        uart.write(string3+'\r\n')
                    elif x>120:
                        uart.write(string4+'\r\n')
                    else:
                        uart.write(string5+'\r\n')
      

      else:

      while(True):
          clock.tick()
          img = sensor.snapshot()
          img.draw_cross(160,120)
      
          blobs = img.find_blobs([black_threshold])
          if blobs:
              print('sum : %d'% len(blobs))
              out_str = '['
              for b in blobs:
                  img.draw_rectangle(b[0:4]) # rect
                  img.draw_cross(b[5], b[6]) # cx, cy
                  x = b[5]
                  y = b[6]
                  img.get_pixel(x,y)
                  out_str += '(%d,%d)' % (x,y)
      
              out_str += ']'
              print(out_str)
      
      
              if x<160:
                 text = "right\n"
              elif x>160:
                 text = "left\n"
              else:
                 text = "stop\n"
              for b in blobs:
                  img.draw_rectangle(b[0:4]) # rect
                  img.draw_cross(b[5], b[6]) # cx, cy
                  x = b[5]
                  y = b[6]
                  p1=Point(b[5],b[6])
                  p2=Point(160,120)
                  l=Getlen(p1,p2)
                     #获取两点之间直线的长度
      
                  print(l.getlen())
                  img.get_pixel(x,y)
                  out_str += '(%d,%d)' % (x,y)
      
              out_str += ']'
              print(out_str)
      
              uart.write(out_str+'\r\n')
              uart.write(l.getlen())
      
              uart = UART(3, 9600)
              for i in (0,360):
                 if x<160:
                     uart.write(string1+'\r\n')
                 elif x>160:
                     uart.write(string2+'\r\n')
                 else:
                     uart.write(string5+'\r\n')
                 for j in (0,240):
                     if x<120:
                         uart.write(string3+'\r\n')
                     elif x>120:
                         uart.write(string4+'\r\n')
                     else:
                         uart.write(string5+'\r\n')
      
      
      while(True):
          clock.tick()
          img = sensor.snapshot()
      
          img.draw_cross(160,120)
          blobs = img.find_blobs([black_threshold])
          if blobs:
              print('sum : %d'% len(blobs))
              out_str = '['
              for b in blobs:
                  img.draw_rectangle(b[0:4]) # rect
                  img.draw_cross(b[5], b[6]) # cx, cy
                  x = b[5]
                  y = b[6]
                  a=b[0]+b[1]
                  c=b[1]+b[3]
                  out_str += '(%d,%d)' % (x,y)
              out_str += ']'
              print(out_str)
      
              if b[2]>b[3]:
      
                 if 160<a:
                     uart.write(string1+'\r\n')
                 elif 160>a:
                     uart.write(string2+'\r\n')
                 else:
                     uart.write(string5+'\r\n')
              else:
      
                 if 120<c:
                     uart.write(string4+'\r\n')
                 elif 120>c:
                     uart.write(string3+'\r\n')
                 else:
                     uart.write(string5+'\r\n')
      

      #uart.write(out_str+'\r\n')
      #uart.write()
      #print(out_str)

      arduino程序
      #include <SoftwareSerial.h>

      SoftwareSerial softSerial(10, 11); // RX, TX
      typedef struct
      {
      int data[50][2] = {{0,0}};
      int len = 0;
      }List;
      List list;

      void setup() {
      // put your setup code here, to run once:
      softSerial.begin(9600);
      Serial.begin(9600);
      }

      void loop() {
      //Serial.write(45);
      // int bytesSent = Serial.write("hello");
      if(softSerial.available())
      {
      getList();
      for (int i=0; i<list.len; i++)
      {
      Serial.print(list.data[i][0]);
      Serial.print('\t');
      Serial.println(list.data[i][1]);
      }
      Serial.println("============");
      clearList();
      }

      }

      String detectString()
      {
      while(softSerial.read() != '{');
      return(softSerial.readStringUntil('}'));
      }
      void clearList()
      {
      memset(list.data, sizeof(list.data),0);
      list.len = 0;
      }
      void getList()
      {
      String s = detectString();
      String numStr = "";
      for(int i = 0; i<s.length(); i++)
      {
      if(s[i]=='('){
      numStr = "";
      }
      else if(s[i] == ','){
      list.data[list.len][0] = numStr.toInt();
      numStr = "";
      }
      else if(s[i]==')'){
      list.data[list.len][1] = numStr.toInt();
      numStr = "";
      list.len++;
      }
      else{
      numStr += s[i];
      }
      }
      }