【问题标题】:Issues interfacing Arduino sensor MPU6050 and Processing连接 Arduino 传感器 MPU6050 和处理的问题
【发布时间】:2020-05-28 08:45:42
【问题描述】:

我有一个通过 I2C 协议与 Arduino 连接的 MPU6050 陀螺仪和加速度计。这些传感器使用这些指令(在 arduino IDE 中)将连续的数据流发送到串行端口:

Serial.print(euler[0] * 180/M_PI);
Serial.print(":");
Serial.print(euler[1] * 180/M_PI);
Serial.print(":");
Serial.println(euler[2] * 180/M_PI);

这来自传感器库中包含的示例草图,它只是将偏航/俯仰/滚动的值发送到串行端口,用冒号分隔。

现在是有趣的部分。我一直对可视化数据很着迷,所以我想为这些来自处理中的串行数据构建一个图表(这是一个更大的项目的一部分,其中包括一个超声波传感器,比如一种雷达)。

所以我写了一个关于处理的简短草图,以捕获该数据,以便对其进行分析和可视化。这是草图:

import processing.serial.*;

Serial myPort;

String data; //Angle values

String[] splitted; //Array containing splitted data

float yaw, pitch , roll;



void setup()
{
  myPort = new Serial (this, Serial.list()[0], 115200);

}

void draw()
{

  while (myPort.available() > 0) //data arrived fromm serial
  {

    data = myPort.readStringUntil('\n');

    //Data Parsing
    splitted = data.split(":");

    yaw = float(splitted[0]);
    pitch = float(splitted[1]);
    roll = float(splitted[2]);

    println(yaw + "  " + pitch + "  " + roll);


  }



}

此代码不起作用。有 2 个错误交替出现。其中之一是:

ArrayIndexOutOfBondsException

还有一个:

NullPointerException

指向“拆分”的数组。

我想我有问题。在以前版本的处理草图中,我使用的是:

readString() function

我认为,由于一次将数据发送到 Arduino 草图中的串行端口,处理草图有时只捕获一个或两个偏航、俯仰、滚动值,导致数组索引崩溃或当没有值被添加到数组中时,nullPointerexception。然后,我将 '''readString''' 更改为 '''readStringUntil('\n')''',因为,也许第一个数据包会丢失,但下一个数据包将始终被连接而不会破坏它们(我赶上了整条线)。但是有同样的错误,所以我觉得我的小经验已经不能帮助解决问题了。我需要你的帮助。

对不起,我的英语不好,感谢您的帮助。

【问题讨论】:

    标签: arduino serial-port processing data-visualization sensors


    【解决方案1】:

    你在正确的轨道上。这里有几点建议:

    • 您可以使用try/catch 块,这样草图就不会因为错误而崩溃
    • 您可以使用bufferUntil() 告诉串行库为您缓冲字节,直到遇到新行:它与自动调用的serialEvent() 协同工作(因此您不需要使用@ 987654327@ 循环会阻塞渲染/草图的其余部分)
    • 您可以检查(并且应该)任何可能导致数据出错的内容(空字符串、空字符串、字符串中的值不足等)

    这是你的草图的修改版本:

    import processing.serial.*;
    
    Serial myPort;
    
    float yaw, pitch , roll;
    
    void setup()
    {
    
      String[] portNames = Serial.list();
      // skipp serial setup if there are no ports
      if(portNames.length == 0){
        println("no serial ports found");
        return;
      }
    
      // try to open serial port, handle error
      try
      {
        myPort = new Serial (this, portNames[0], 115200);
        // buffer bytes(characters) until new line is hit
        myPort.bufferUntil('\n');
      }
      catch(Exception e)
      {
        println("error opening port: " + portNames[0]);
        println("double check the port is present and not used by other applications (e.g. SerialMonitor)");
        e.printStackTrace();
      }
    
    }
    
    void draw()
    {
    
      background(0);
      text(String.format("yaw: %.2f \npitch: %.2f \nroll: %.2f", yaw, pitch, roll), 5, 15);
    }
    
    // serialEvent gets called when there's new data: no need an explicit blocking while loop
    void serialEvent(Serial port){
      try
      {
        // read string from serial  
        String rawSerialString = port.readString();
        // exit on null string
        if(rawSerialString == null)
        {
          println("received null string, skipping this serial message");
          return;
        }
        // exit on empty string
        if(rawSerialString.length() == 0)
        {
          println("received empty string, skipping this serial message");
          return;
        }
    
        // trim white space (\r, \n, etc.)
        rawSerialString = rawSerialString.trim();
        // split and convert to float
        float[] rotations = float(rawSerialString.split(":"));
    
        // exit if message got jumbled up and values are missing
        if(rotations.length < 3)
        {
          println("received less than 3 values, skipping this serial message");
          return;
        }
    
        // finally extract values
        yaw   = rotations[0];
        pitch = rotations[1];
        roll  = rotations[2];
    
        println(yaw + "  " + pitch + "  " + roll);
    
      }
      catch(Exception e)
      {
        println("error reading/parsing serial data");
        e.printStackTrace();
      }
    
    }
    

    【讨论】:

    • 非常感谢。不幸的是,我的 MPU6050 已停止发送任何数据,所以我试图用另一个更改它,但我编辑了代码。
    • 很高兴这个答案很有帮助。很遗憾听到 MPU6050 停止发送数据。仔细检查接线/焊接连接是否牢固,尽可能绝缘以避免意外短路并尝试在无静电环境中工作。
    【解决方案2】:

    显然你收到的行少于两个冒号。

    从这里很难说你应该做什么,但检查分割数组的长度无论如何都是第一步。通过ifException

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-30
      • 2018-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多