问题描述
这里我要获取 mpu6050 陀螺仪的数据,并把它写在一个函数中。
我有一个例子,但这不起作用。如果我移动陀螺仪,它会在 10-30 秒后停止运行。
代码如下:
/*
* mpu6050 Arduino mega 2560
* Vcc 3.3v
* gnd GND
* SDA SDA (20)
* SCL SCL (21)
*/
#include <Wire.h>
#include <mpu6050.h>
int16_t ax,ay,az;
int16_t gx,gy,gz;
mpu6050 mpu; // Initiates library.
double mpuDat[6]={0.00,0.00,0.00};//global variable with mpu datas
unsigned long timer = 0;
float timestep = 0;
float pitch = 0;
float roll = 0;
float yaw = 0;
void setup() {
Serial.begin(115200);
while (!mpu.begin(mpu6050_SCALE_2000DPS,mpu6050_RANGE_2G)) {
Serial.println("Could not find a valid mpu6050 sensor,check wiring!");
delay(500);
}
mpu.calibrateGyro();
mpu.setThreshold(3);
}
void loop() {
get_gyro();
//print gyro data
Serial.print(mpuDat[0]);
Serial.print(" ");
Serial.print(mpuDat[1]);
Serial.print(" ");
Serial.print(mpuDat[2]);
Serial.print(" -- ");
//print accel datas
Serial.print(mpuDat[3]);
Serial.print(" ");
Serial.print(mpuDat[4]);
Serial.print(" ");
Serial.println(mpuDat[5]);
//here use I more functions,here in example just a varint delay
delay(random(0,5));
}
void get_gyro() {
timestep=millis() - timer;
timer = millis();
//gyro
Vector norm = mpu.readnormalizeGyro(); //read gyro datas
pitch = pitch + norm.YAxis * timestep / 1000; //convert °/sec to real deg
roll = roll + norm.XAxis * timestep/1000;
yaw = yaw + norm.ZAxis * timestep/1000;
mpuDat[0] = pitch;
mpuDat[1] = roll;
mpuDat[2] = yaw;
//accel
Vector normA=mpu.readnormalizeAccel(); //read accel datas
mpuDat[3]=normA.XAxis;
mpuDat[4]=normA.YAxis;
mpuDat[5]=normA.ZAxis;
}
感谢您的帮助!
我的英语不完美,抱歉。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)