weixin_39389143
weixin_39389143
采纳率50%
2019-10-31 09:02

MPU-9150九轴传感器,Failed to init IMU: -1 No valid compass calibration data?

200
已采纳

1、用MPU9150九轴传感器采集人体姿态数据时,之前一直都能将九个数据点输出到串口监视器中,但突然有一天总是报错,传感器无法读取数据

2、烧录的代码如下:
#include

#include

////////////////////////////////////////////////////////////////////////////
//
// This file is part of RTIMULib-Arduino
//
// Copyright (c) 2014-2015, richards-tech
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
// Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#include
#include "I2Cdev.h"
#include "RTIMUSettings.h"
#include "RTIMU.h"
#include "RTFusionRTQF.h"
#include "CalLib.h"
#include

RTIMU *imu; // the IMU object
RTFusionRTQF fusion; // the fusion object
RTIMUSettings settings; // the settings object

// DISPLAY_INTERVAL sets the rate at which results are displayed

#define DISPLAY_INTERVAL 0 // interval between pose displays

// SERIAL_PORT_SPEED defines the speed to use for the debug serial port

#define SERIAL_PORT_SPEED 115200

unsigned long lastDisplay;
unsigned long lastRate;
int sampleCount;
RTQuaternion gravity;

void setup()
{
int errcode;

Serial.begin(SERIAL_PORT_SPEED);
Wire.begin();
imu = RTIMU::createIMU(&settings); // create the imu object

Serial.print("ArduinoIMU starting using device "); Serial.println(imu->IMUName());
if ((errcode = imu->IMUInit()) < 0) {
Serial.print("Failed to init IMU: "); Serial.println(errcode);
}

if (imu->getCalibrationValid())
Serial.println("Using compass calibration");
else
Serial.println("No valid compass calibration data");

lastDisplay = lastRate = millis();
sampleCount = 0;

gravity.setScalar(0);
gravity.setX(0);
gravity.setY(0);
gravity.setZ(1);
}

void loop()
{

unsigned long now = millis();
unsigned long timeTemp = millis();
unsigned long delta;
RTVector3 realAccel;
RTVector3 realGyro;
RTVector3 realCompass;
RTQuaternion rotatedGravity;
RTQuaternion fusedConjugate;
RTQuaternion qTemp;
int loopCount = 0;

while (imu->IMURead()) { // get the latest data if ready yet
// this flushes remaining data in case we are falling behind
if (++loopCount >= 10)
continue;
fusion.newIMUData(imu->getGyro(), imu->getAccel(), imu->getCompass(), imu->getTimestamp());

//  do gravity rotation and subtraction

// create the conjugate of the pose

fusedConjugate = fusion.getFusionQPose().conjugate();

// now do the rotation - takes two steps with qTemp as the intermediate variable

qTemp = gravity * fusion.getFusionQPose();
rotatedGravity = fusedConjugate * qTemp;

// now adjust the measured accel and change the signs to make sense

realAccel.setX(-(imu->getAccel().x() - rotatedGravity.x()));
realAccel.setY(-(imu->getAccel().y() - rotatedGravity.y()));
realAccel.setZ(-(imu->getAccel().z() - rotatedGravity.z()));

realGyro.setX(-(imu->getGyro().x()));
realGyro.setY(-(imu->getGyro().y()));
realGyro.setZ(-(imu->getGyro().z()));


realCompass.setX(-(imu->getCompass().x()));
realCompass.setY(-(imu->getCompass().y()));
realCompass.setZ(-(imu->getCompass().z()));

sampleCount++;
if ((delta = now - lastRate) >= 1000) {
  Serial.print("Sample rate: "); Serial.print(sampleCount);
  if (!imu->IMUGyroBiasValid())
    Serial.println(", calculating gyro bias");
  else
    Serial.println();

  sampleCount = 0;
  lastRate = now;
}

/* if ((now - lastDisplay) >= DISPLAY_INTERVAL) {
lastDisplay = now;

  RTMath::display("Accel:", realAccel);
  RTMath::display("Gyro:", realGyro);
  RTMath::display("Compass:", realCompass);
  Serial.println();
}*/
/*////////////////////正常输出//////////////*/
  /*RTMath::display("Accel:", realAccel);
  RTMath::display("Gyro:", realGyro);
  RTMath::display("Compass:", realCompass);
  Serial.println();*/
  /******************串口输出************/
  RTMath::display( "",realAccel);
  RTMath::display("", realGyro);
  RTMath::display("",realCompass);
  Serial.println();
  delay(25);

}
}

3、编译没有报错,但是打开串口监视器的时候出现:
ArduinoIMU starting using device MPU-9250
Failed to init IMU: -1
No valid compass calibration data

4、尝试过的解决方法:
a、更换连接线
b、更换库文件

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

相关推荐