这是一个非常简单的代码,用于学习可以,但是实际应用并不是很顺畅。
具体解析如下:
/*
DIY Gimbal - MPU6050 Arduino Tutorial
by Dejan, www.HowToMechatronics.com
Code based on the MPU6050_DMP6 example from the i2cdevlib library by Jeff Rowberg:
https://github.com/jrowberg/i2cdevlib
*/
// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
//#include "MPU6050.h" // not necessary if using MotionApps include file 如果使用MotionApps包含文件,则没有必要
// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
// is used in I2Cdev.h
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif
#include <Servo.h>
// class default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here 特定的I2C地址可以作为参数在这里传递
// AD0 low = 0x68 (default for SparkFun breakout and InvenSense evaluation board)
// AD0 high = 0x69
MPU6050 mpu;
//MPU6050 mpu(0x69); // <-- use for AD0 high
// Define the 3 servo motors 定义3个伺服电机
Servo servo0;
Servo servo1;
Servo servo2;
float correct;
int j = 0;
#define OUTPUT_READABLE_YAWPITCHROLL
#define INTERRUPT_PIN 2 // use pin 2 on Arduino Uno & most boards 定义中断引脚,UNO和大多数板子是pin 2
bool blinkState = false;
// MPU control/status vars 微处理机控制/状态变量
bool dmpReady = false; // set true if DMP init was successful 如果DMP 初始化成功,则设置为true
uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU 保持从MPU读取实际的中断状态
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error) 返回每个设备操作后状态
uint16_t packetSize; // expected DMP packet size (default is 42 bytes) 预期的DMP包大小(默认为42字节)
uint16_t fifoCount; // count of all bytes currently in FIFO 当前FIFO中所有字节的计数
uint8_t fifoBuffer[64]; // FIFO storage buffer FIFO存储缓冲区
// orientation/motion vars 方向/运动变量
Quaternion q; // [w, x, y, z] quaternion container 四元数的容器
VectorInt16 aa; // [x, y, z] accel sensor measurements 加速度传感器测量
VectorInt16 aaReal; // [x, y, z] gravity-free accel sensor measurements 无重力加速度传感器测量
VectorInt16 aaWorld; // [x, y, z] world-frame accel sensor measurements 世界框架加速度传感器测量
VectorFloat gravity; // [x, y, z] gravity vector 重力向量
float euler[3]; // [psi, theta, phi] Euler angle container 欧拉角容器
float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector 偏航/俯仰/滚动容器和重力矢量
// packet structure for InvenSense teapot demo 用于InvenSense茶壶演示包结构
uint8_t teapotPacket[14] = { '$', 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0x00, 0x00, '\r', '\n' };
// ================================================================
// === INTERRUPT DETECTION ROUTINE 中断检测程序 ===
// ================================================================
volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high 指示MPU中断引脚是否高
void dmpDataReady() {
mpuInterrupt = true;
}
// ===========================