-
参考数据手册:
PS-MPU-6000A
使
用带有
DMP
的最新库函数(
/jro
wberg/i2cdevlib
),程序模板采用
MPU60
50_DMP6
例程。
角度:
DMP
库函数的
dmpGetYawPitchRoll
,
可以得到
pitch(
俯仰),
yaw
(
偏航),
roll
(滚转)角度。<
/p>
角速度:
void getRotation(int16_t* x,
int16_t* y, int16_t* z);
int16_t getRotationX();
int16_t
getRotationY();
int16_t
getRotationZ();
16
位,配置时四个量程可
选:±250,±500,±1000,±2000
度
/s
。
在
dmp
的例程中初始化:
setFul
lScaleGyroRange(MPU6050_GYRO_FS_2000);
说明是选用最高量程±2000º/
s
,则换算系数
=2^16/4000=16.4
LSB/(
度
/s)
加速度库函数:
void getAcceleration(int16_t* x, int16_t* y,
int16_t* z);
int16_t
getAccelerationX();
int16_t
getAccelerationY();
int16_t
getAccelerationZ();
16
位,配置时四个量程可选:±2,±4,±8,±16
g。
在
dmp
的例程中没有找到初始化部分,估计是采用了缺省配置。
通
过观察输出值,采用的是最小量程±2g,则换算系数
=2^16/4=16384
LSB/g
下面是程序,中断方式,需要把
MP
U
的中断接到
arduino
数字
p>
2
脚上。
ARDUINO
代码复制
打印
1.
// Arduino
Wire library is required if I2Cdev
I2CDEV_ARDUINO_WIRE
implementation
2.
// is used in I2Cdev.h
3.
#include
4.
5.
// I2Cdev and MPU6050 must be installed
as
libraries, or else the .cpp/.h
files
6.
//
for
both
classes
must
be
in
the
include
path
of your
project
7.
#include
8.
9.
#include
10.
MPU6050 mpu
(
0x68<
/p>
)
;
11.
12.
// MPU control/status vars
13.
bool
dmpReady
=
false
;
//
set
true
if
DMP
init
was
successful
14.
uint8_t mpuIntStatus;
//
holds actual
interrupt status byte from
MPU
15.
uint8_t devStatus;
//
return status after
each device
operation (0 = success, !0 = error)
16.
uint16_t
packetSize;
// expected DMP packet
size (default is 42 bytes)
17.
uint16_t
fifoCount;
// count of all bytes
currently in FIFO
18.
uint8_t
fifoBuffer
[
64
]
;
//
FIFO
storage
buffer
19.
20.
//
orientation/motion vars
21.
Quaternion q;
// [w, x, y,
z]
quaternion container
22.
VectorFloat
gravity;
// [x, y,
z]
gravity vector
23.
float
ypr
[
3
]
;
// [yaw, pitch,
roll]
yaw/pitch/roll
container
and
gravity
vector
24.
25.
26.
//
=================================================
===============
27.
// ===
INTERRUPT DETECTION
ROUTINE
===
28.
//
=========================
========================
===============
29.
30.
volatile
bool mpuInterrupt =
false
;
//
indicates whether MPU interrupt pin has
gone high
31.
void
dmpDataReady
()
{
32.
mpuInterrupt =
true
;
33.
}
34.
35.
36.
37.
//
=================================================
===============
38.
// ===
INITIAL
SETUP
===
39.
//
=========================
========================
===============
40.
41.
void
setup
()
{
42.
Serial
.
< br>begin
(
115200
)<
/p>
;
// opens serial
port, sets data rate to 9600
bps
43.
44.
//
join
I2C
bus
(I2Cdev
library
doesn't
do
this
automatically)
45.
Wire
.
begin
()
;
46.
47.
// initialize device
48.
Serial
.
println
(
p>
devices...
)
< br>;
49.
mpu.
initialize
()
;
50.
51.
// verify connection
52.
Serial
.
println
(
p>
connections...
)
;