1

我是菜鸟,我来自土木工程师,我提前道歉。

我有一个连接到我的 Arduino Uno 的 MPU6050,我将数据写入 SD。我可以以 20 毫秒的采样率写作。

为什么我有这个默认频率?

这是代码的一部分

// Main:
  void loop()
  {
    // Body of main/loop
    // Check status:
    Time0 = millis();
    Read_Write();
   // LED off and continue:
      digitalWrite(LEDpin, LOW);
      
       }

  void Read_Write()
  // function that reads the MPU and writes the data to the SD.
  {
   // Local variables:
    int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ; // Variables read from MPU6050

    // Read data:
    Wire.beginTransmission(MPU);
    Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
    Wire.endTransmission(false);
    Wire.requestFrom(MPU,14,true);  // request a total of 14 registers
    AcX = Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)     
    AcY = Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
    AcZ = Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
    
    GyX = Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
    GyY = Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
    GyZ = Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
    
    Serial.print(AcX);
    Serial.print(',');
    Serial.print(AcY);
    Serial.print(',');
    Serial.print(AcZ);
    Serial.print(',');
    Serial.print(GyX);
    Serial.print(',');
    Serial.print(GyY);
    Serial.print(',');
    Serial.print(GyZ);
    Serial.print(',');
    
    Serial.println();
    
    
    // Data preparation for file saving:
    String dataString = ""; // string for assembling the data to log:

    // Add time tag:
    dataString += String(Time0); dataString += ",";

    // Append the MPU6050 data to the string:
    dataString += String(AcX); dataString += ",";
    dataString += String(AcY); dataString += ",";
    dataString += String(AcZ); dataString += ",";
    dataString += String(GyX); dataString += ",";
    dataString += String(GyY); dataString += ",";
    dataString += String(GyZ);
    
    // Open the file in append mode:
    File dataFile = SD.open("datalog.txt", FILE_WRITE);

    // If the file is available, write to it:
    if (dataFile) 
    {
    dataFile.println(dataString);
    dataFile.close();
    if (Serial_plus_SD)
      Serial.println(dataString);
    }
    // if the file does not open, pop up an error:
    else 
    errorFW();
    
    return;
  }

为什么记录时间不是恒定的,但有时是 40 毫秒(见图)? 在此处输入图像描述

对我来说很重要,因为我的目标是有一个恒定频率来应用 FFT 来研究噪声并确定最佳低通滤波器。

如何修改频率?我想在我的代码上放一个delay() 。

 void loop()
      {
        // Body of main/loop
        // Check status:
        Time0 = millis();
        Read_Write();
       // LED off and continue:
          digitalWrite(LEDpin, LOW);
          delay(80)
           }
4

0 回答 0