Last active
November 2, 2016 06:55
-
-
Save peteroid/2ed61b620d981c3e51f83220cdffd118 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050 class | |
// 10/7/2011 by Jeff Rowberg <[email protected]> | |
// Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib | |
// | |
// Changelog: | |
// 2013-05-08 - added multiple output formats | |
// - added seamless Fastwire support | |
// 2011-10-07 - initial release | |
/* ============================================ | |
I2Cdev device library code is placed under the MIT license | |
Copyright (c) 2011 Jeff Rowberg | |
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. | |
=============================================== | |
*/ | |
// 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" | |
#include "I2Cdev.h" | |
#include "MPU6050_6Axis_MotionApps20.h" | |
// 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 | |
// 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 mpu_1(0x69), mpu_2(0x69), mpu_3(0x69); | |
//MPU6050 mpu_1(0x68), mpu_2(0x68), mpu_3(0x68); | |
typedef struct { | |
MPU6050 mpu; | |
unsigned char SDA; | |
unsigned char SCL; | |
unsigned char muxEnablePin0; | |
unsigned char muxEnablePin1; | |
unsigned char interruptPin; | |
void (*interruptFunc)(void); | |
bool dmpReady; | |
bool isInterrupt; | |
uint16_t packetSize; | |
uint8_t intStatus; | |
} MPUProvider; | |
void mpu1InterruptFunc () {mpusInterruptFunc(0);} | |
void mpu2InterruptFunc () {mpusInterruptFunc(1);} | |
void mpu3InterruptFunc () {mpusInterruptFunc(2);} | |
const int mpuCount = 3; | |
const int mpuSkipCount = 0; | |
MPUProvider mpus[] = { | |
{mpu_1, D5, D6, HIGH, LOW, D1, mpu1InterruptFunc}, | |
{mpu_2, D5, D6, LOW, HIGH, D2, mpu2InterruptFunc}, | |
{mpu_3, D5, D6, HIGH, HIGH, D3, mpu3InterruptFunc} | |
}; | |
int S0 = D7; | |
int S1 = D8; | |
int SDA_PIN = D5; | |
int SCL_PIN = D6; | |
volatile bool mpuInterrupt = false; | |
//#define OUTPUT_TEAPOT_FORMAT | |
// choose one of the output format | |
//#define OUTPUT_INDIVIDUAL_FORMAT | |
#define OUTPUT_BUNDLE_FORMAT | |
uint8_t fifoBuffer[64]; // FIFO storage buffer | |
// packet structure for InvenSense teapot demo | |
#if defined(OUTPUT_INDIVIDUAL_FORMAT) | |
uint8_t teapotPacket[20] = { '$', 0xFF, | |
0,0, 0,0, 0,0, 0,0, | |
0,0, 0,0, 0,0, | |
0x00, 0x00, '\r', '\n' }; | |
#elif defined(OUTPUT_BUNDLE_FORMAT) | |
uint8_t teapotPacket[30] = { '$', 0xFF, | |
0,0, 0,0, 0,0, 0,0, | |
0,0, 0,0, 0,0, 0,0, | |
0,0, 0,0, 0,0, 0,0, | |
0x00, 0x00, '\r', '\n' }; | |
#endif | |
/*#if defined(OUTPUT_INDIVIDUAL_FORMAT) | |
#elif defined(OUTPUT_BUNDLE_FORMAT) | |
#endif*/ | |
#define LED_PIN 13 | |
bool blinkState = false; | |
void mpusInterruptFunc(int target) { | |
mpus[target].isInterrupt = true; | |
} | |
void setup() { | |
// 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(115200); | |
while(!Serial); | |
Serial.println("Start..."); | |
pinMode(S0, OUTPUT); | |
pinMode(S1, OUTPUT); | |
Wire.begin(SDA_PIN, SCL_PIN); | |
Wire.setClock(400000); | |
for (int i = 0; i < mpuCount; i++) { | |
Serial.println("Selecting #" + String(i) + " mpu"); | |
MPUProvider* mpuP = selectMpu(i); | |
delay(50); | |
mpuDMPInitialize(mpuP, i); | |
delay(50); | |
} | |
// configure Arduino LED for | |
pinMode(LED_PIN, OUTPUT); | |
} | |
void loop() { | |
for (int i = 0; i < mpuCount; i++) { | |
MPUProvider* mpuP = selectMpu(i); | |
MPU6050 currentMpu = mpuP->mpu; | |
if (mpuCount <= mpuSkipCount + i) break; | |
delay(1); | |
while (!mpuP->isInterrupt) yield(); | |
mpuP->isInterrupt = false; | |
while (!(currentMpu.getIntStatus() & 0x02)) yield(); | |
#if defined(OUTPUT_INDIVIDUAL_FORMAT) | |
printTeapotFormat(mpuP, i); | |
#elif defined(OUTPUT_BUNDLE_FORMAT) | |
updateTeapotFormat(mpuP, i); | |
if (i == mpuCount - 1) { | |
Serial.write(teapotPacket, 30); | |
teapotPacket[27]++; | |
} | |
#endif | |
} | |
// blink LED to indicate activity | |
blinkState = !blinkState; | |
digitalWrite(LED_PIN, blinkState); | |
} | |
MPUProvider* selectMpu (unsigned char target) { | |
MPUProvider* mpuP = &mpus[target]; | |
digitalWrite(S0, mpuP->muxEnablePin0); | |
digitalWrite(S1, mpuP->muxEnablePin1); | |
return mpuP; | |
} | |
MPUProvider* mpuDMPInitialize (MPUProvider* mpuP, int index) { | |
String indexStr = String(index); | |
Serial.println("Initializing #" + indexStr + " mpu"); | |
MPU6050 mpu = mpuP->mpu; | |
mpu.initialize(); | |
pinMode(mpuP->interruptPin, INPUT); | |
Serial.println("Testing device connections #" + indexStr + " ..."); | |
Serial.println(mpu.testConnection() ? "MPU6050 connection #" + indexStr + " successful" : "MPU6050 connection #" + indexStr + " failed"); | |
Serial.println(F("Initializing DMP...")); | |
uint8_t devStatus = mpu.dmpInitialize(); | |
if (devStatus == 0) { | |
Serial.println("Enabling DMP"); | |
mpu.setDMPEnabled(true); | |
attachInterrupt(digitalPinToInterrupt(mpuP->interruptPin), mpuP->interruptFunc, RISING); | |
mpuP->intStatus = mpu.getIntStatus(); | |
mpuP->packetSize = mpu.dmpGetFIFOPacketSize(); | |
mpuP->dmpReady = true; | |
Serial.println("Packet size: " + mpuP->packetSize); | |
Serial.println("DMP init success"); | |
} else { | |
Serial.print(F("DMP Initialization failed (code ")); | |
Serial.print(devStatus); | |
Serial.println(F(")")); | |
} | |
return mpuP; | |
} | |
void updateTeapotFormat (const MPUProvider* mpuP, char index) { | |
if (!mpuP->dmpReady) { | |
Serial.println("DMP not ready"); | |
return; | |
} | |
MPU6050 mpu = mpuP->mpu; | |
mpu.getFIFOBytes(fifoBuffer, mpuP->packetSize); | |
teapotPacket[2 + 8 * index] = fifoBuffer[0]; | |
teapotPacket[3 + 8 * index] = fifoBuffer[1]; | |
teapotPacket[4 + 8 * index] = fifoBuffer[4]; | |
teapotPacket[5 + 8 * index] = fifoBuffer[5]; | |
teapotPacket[6 + 8 * index] = fifoBuffer[8]; | |
teapotPacket[7 + 8 * index] = fifoBuffer[9]; | |
teapotPacket[8 + 8 * index] = fifoBuffer[12]; | |
teapotPacket[9 + 8 * index] = fifoBuffer[13]; | |
} | |
void printTeapotFormat (const MPUProvider* mpuP, char index) { | |
if (!mpuP->dmpReady) { | |
Serial.println("DMP not ready"); | |
return; | |
} | |
MPU6050 mpu = mpuP->mpu; | |
mpu.getFIFOBytes(fifoBuffer, mpuP->packetSize); | |
// display quaternion values in InvenSense Teapot demo format: | |
teapotPacket[1] = index + '0'; | |
teapotPacket[2] = fifoBuffer[0]; | |
teapotPacket[3] = fifoBuffer[1]; | |
teapotPacket[4] = fifoBuffer[4]; | |
teapotPacket[5] = fifoBuffer[5]; | |
teapotPacket[6] = fifoBuffer[8]; | |
teapotPacket[7] = fifoBuffer[9]; | |
teapotPacket[8] = fifoBuffer[12]; | |
teapotPacket[9] = fifoBuffer[13]; | |
Serial.write(teapotPacket, 20); | |
teapotPacket[17]++; // packetCount, loops at 0xFF on purpose | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment