由于项目的需要,需要连接三个MPU9250模块,模块支持I2C接口磁场MPU9250 9DOF 九轴/9轴姿态 加速度 陀螺仪 指南针磁场传感器
由于项目比较大还有许多传感器,所以使用了Arduino Mega2560,使用扩展版可以直接连接I2C接口。
我计划使用这个扩展版
Grove - Mega Shield v1.2 Arduino mega扩展板
[size=18.018px]数字引脚区还包括一些复用的功能:I2C(有三个),硬串口(Uart0 - Uart3),PWM(PWM2 - PWM11)以及ICSP。
[size=18.018px]但是不知道三个MPU9250模块接上去以后 怎么分别获取九轴数据!
MPU9250获取九轴的代码如下:
#include "Wire.h"
// 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.h"
// class default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
MPU6050 accelgyro;
int16_t ax, ay, az;//加速度acceleration
int16_t gx, gy, gz;//陀螺仪显示参数gyroscope
int16_t mx, my, mz;//磁力计magnetometer
#define LED_PIN 13
bool blinkState = false;
void setup() {
// join I2C bus (I2Cdev library doesn't do this automatically)
Wire.begin();
// initialize serial communication
// (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
// it's really up to you depending on your project)
Serial.begin(57600);
// initialize device
Serial.println("Initializing I2C devices...");
accelgyro.initialize();
// verify connection
Serial.println("Testing device connections...");
Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
// configure Arduino LED for
pinMode(LED_PIN, OUTPUT);
}
void loop() {
// read raw accel/gyro measurements from device
accelgyro.getMotion9(&ax, &ay, &az, &gx, &gy, &gz, &mx, &my, &mz);
// these methods (and a few others) are also available
//accelgyro.getAcceleration(&ax, &ay, &az);
//accelgyro.getRotation(&gx, &gy, &gz);
// display tab-separated accel/gyro x/y/z values
Serial.print("a/g/m:\t");
// Serial.print(ax); Serial.print("\t");
// Serial.print(ay); Serial.print("\t");
// Serial.print(az); Serial.print("\t");
Serial.print(gx); Serial.print("\t");
Serial.print(gy); Serial.print("\t");
Serial.print(gz); Serial.print("\t");
//Serial.print(mx); Serial.print("\t");
// Serial.print(my); Serial.print("\t");
// Serial.println(mz);
// Serial.();
delay(500);
// blink LED to indicate activity
blinkState = !blinkState;
digitalWrite(LED_PIN, blinkState);
}
//
在#include "MPU6050.h"头文件里面可以看到
// #define MPU6050_ADDRESS_AD0_LOW 0x68 // address pin low (GND), default for InvenSense evaluation board
#define MPU6050_ADDRESS_AD0_HIGH 0x69 // address pin high (VCC)
#define MPU6050_DEFAULT_ADDRESS MPU6050_ADDRESS_AD0_LOW
在MPU6050.cpp里面可以看到
默认地址使用的是MPU6050_DEFAULT_ADDRESS
代码如下:
#include "MPU6050.h"
/** Default constructor, uses default I2C address.
* @see MPU6050_DEFAULT_ADDRESS
*/
MPU6050::MPU6050() {
devAddr = MPU6050_DEFAULT_ADDRESS;
}
我该如何编写代码,才能分别获取三个九轴传感器的数据呢?
请教各位高手,不吝赐教!非常感谢!
|