Last active
November 3, 2016 18:47
-
-
Save peteroid/3012f16b3113f01e746231dc13ca2b2e to your computer and use it in GitHub Desktop.
This file contains 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.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 | |
#define INTERRUPT_PIN 2 | |
// 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_0(0x68); | |
MPU6050 mpu_1(0x68); | |
MPU6050 mpu_2(0x68); | |
typedef struct { | |
MPU6050 mpu; | |
unsigned char muxEnablePin0; | |
unsigned char muxEnablePin1; | |
unsigned char interruptPin; | |
bool dmpReady; | |
uint16_t packetSize; | |
uint8_t intStatus; | |
uint8_t fifoBuffer[64]; | |
Quaternion quat; | |
} MPUProvider; | |
const int mpuCount = 3; | |
const int mpuSkipCount = 0; | |
MPUProvider mpus[] = { | |
{mpu_0, LOW, LOW, 3}, | |
{mpu_1, HIGH, LOW, 2}, | |
{mpu_2, LOW, HIGH, 0x00} | |
}; | |
//MPU6050 mpu_3(0x68); | |
//MPU6050 accelgyro(0x69); // <-- use for AD0 high | |
int16_t ax_0, ay_0, az_0; | |
int16_t gx_0, gy_0, gz_0; | |
volatile bool mpuInterrupt = false; | |
void dmpDataReady() { | |
mpuInterrupt = true; | |
} | |
uint8_t fifoBuffer[64]; // FIFO storage buffer | |
// uncomment "OUTPUT_READABLE_ACCELGYRO" if you want to see a tab-separated | |
// list of the accel X/Y/Z and then gyro X/Y/Z values in decimal. Easy to read, | |
// not so easy to parse, and slow(er) over UART. | |
//#define OUTPUT_READABLE_ACCELGYRO | |
#define OUTPUT_TEAPOT_FORMAT | |
#define OUTPUT_FORMAT_BUNDLE 0x01 | |
#define OUTPUT_FORMAT_INDIVIDUAL 0x02 | |
// output format config | |
int outputFormatPin = 7; | |
uint8_t outputFormat = OUTPUT_FORMAT_BUNDLE; | |
// packet structure for InvenSense teapot demo | |
uint8_t teapotIndividualPacket[20] = { '$', 0xFF, | |
0,0, 0,0, 0,0, 0,0, | |
0,0, 0,0, 0,0, | |
0x00, 0x00, '\r', '\n' }; | |
uint8_t teapotBundlePacket[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' }; | |
//#define LED_PIN 13 | |
bool blinkState = false; | |
//Mux control pins | |
//int s0 = 5; | |
//int s1 = 6; | |
int s0 = D7; | |
int s1 = D8; | |
const int MPU=0x68; | |
long count = 0; | |
void setup() { | |
pinMode(s0, OUTPUT); | |
pinMode(s1, OUTPUT); | |
// pinMode(outputFormatPin, INPUT); | |
// outputFormat = digitalRead(outputFormatPin) == HIGH ? OUTPUT_FORMAT_BUNDLE : OUTPUT_FORMAT_INDIVIDUAL; | |
Serial.print("Using format: " + outputFormat); | |
// 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); | |
// join I2C bus (I2Cdev library doesn't do this automatically) | |
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE | |
// Wire.begin(); | |
Wire.begin(D6, D5); | |
Wire.setClock(400000); | |
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE | |
Fastwire::setup(400, true); | |
#endif | |
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); | |
delay(5); | |
MPU6050 currentMpu = mpuP->mpu; | |
if (mpuCount <= mpuSkipCount + i) break; | |
// while (!mpuInterrupt); | |
// mpuInterrupt = false; | |
while (!(currentMpu.getIntStatus() & 0x02)); | |
#ifdef OUTPUT_READABLE_ACCELGYRO | |
currentMpu.getMotion6(&ax_0, &ay_0, &az_0, &gx_0, &gy_0, &gz_0); | |
Serial.print("#0\ta/g:\t"); | |
Serial.print(ax_0); Serial.print("\t"); | |
Serial.print(ay_0); Serial.print("\t"); | |
Serial.print(az_0); Serial.print("\t"); | |
Serial.print(gx_0); Serial.print("\t"); | |
Serial.print(gy_0); Serial.print("\t"); | |
Serial.println(gz_0); | |
#endif | |
#ifdef OUTPUT_TEAPOT_FORMAT | |
printTeapotFormat(mpuP, i); | |
#endif | |
} | |
// blink LED to indicate activity | |
blinkState = !blinkState; | |
// digitalWrite(LED_PIN, blinkState); | |
count++; | |
} | |
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), dmpDataReady, 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 printTeapotFormat (MPUProvider* mpuP, char index) { | |
if (!mpuP->dmpReady) { | |
Serial.println("DMP not ready"); | |
return; | |
} | |
MPU6050 mpu = mpuP->mpu; | |
if (mpu.getFIFOCount() == 1024) mpu.resetFIFO(); | |
while(mpu.getFIFOCount() < mpuP->packetSize); | |
mpu.getFIFOBytes(fifoBuffer, mpuP->packetSize); | |
// for debugging | |
/*Quaternion* quat = &(mpuP->quat); | |
Quaternion lastQuat = Quaternion(quat->w, quat->x, quat->y, quat->z); | |
mpu.dmpGetQuaternion(quat, fifoBuffer); | |
float diff = getQuatDiff(lastQuat, *quat); | |
if (diff > 1) { | |
Serial.println("skip for diff:" + String(diff)); | |
return; | |
} | |
Serial.print("#" + String(index + '0') + "\t"); | |
Serial.print(mpu.getFIFOCount()); | |
Serial.print("\t"); | |
printQuat(lastQuat); | |
printQuat(*quat); | |
Serial.print(getQuatDiff(lastQuat, *quat)); | |
Serial.print("\t"); | |
if (index == mpuCount - 1) Serial.println(); | |
return;*/ | |
// discard the result if it exceeds the threshold | |
Quaternion* quat = &(mpuP->quat); | |
Quaternion lastQuat = Quaternion(quat->w, quat->x, quat->y, quat->z); | |
mpu.dmpGetQuaternion(quat, fifoBuffer); | |
float diff = getQuatDiff(lastQuat, *quat); | |
if (diff > 1) { | |
Serial.println("skip for diff:" + String(diff)); | |
return; | |
} | |
// display quaternion values in InvenSense Teapot demo format: | |
if (outputFormat == OUTPUT_FORMAT_INDIVIDUAL) { | |
teapotIndividualPacket[1] = index + '0'; | |
teapotIndividualPacket[2] = fifoBuffer[0]; | |
teapotIndividualPacket[3] = fifoBuffer[1]; | |
teapotIndividualPacket[4] = fifoBuffer[4]; | |
teapotIndividualPacket[5] = fifoBuffer[5]; | |
teapotIndividualPacket[6] = fifoBuffer[8]; | |
teapotIndividualPacket[7] = fifoBuffer[9]; | |
teapotIndividualPacket[8] = fifoBuffer[12]; | |
teapotIndividualPacket[9] = fifoBuffer[13]; | |
Serial.write(teapotIndividualPacket, 14); | |
teapotIndividualPacket[17]++; // packetCount, loops at 0xFF on purpose | |
} else if (outputFormat == OUTPUT_FORMAT_BUNDLE) { | |
teapotBundlePacket[2 + index * 8] = fifoBuffer[0]; | |
teapotBundlePacket[3 + index * 8] = fifoBuffer[1]; | |
teapotBundlePacket[4 + index * 8] = fifoBuffer[4]; | |
teapotBundlePacket[5 + index * 8] = fifoBuffer[5]; | |
teapotBundlePacket[6 + index * 8] = fifoBuffer[8]; | |
teapotBundlePacket[7 + index * 8] = fifoBuffer[9]; | |
teapotBundlePacket[8 + index * 8] = fifoBuffer[12]; | |
teapotBundlePacket[9 + index * 8] = fifoBuffer[13]; | |
if (index == mpuCount - 1) Serial.write(teapotBundlePacket, 30); | |
teapotBundlePacket[27]++; // packetCount, loops at 0xFF on purpose | |
} | |
} | |
void printQuat(Quaternion quat) { | |
Serial.print(quat.w); | |
Serial.print("\t"); | |
Serial.print(quat.x); | |
Serial.print("\t"); | |
Serial.print(quat.y); | |
Serial.print("\t"); | |
Serial.print(quat.z); | |
Serial.print("\t"); | |
} | |
float getQuatDiff(Quaternion q1, Quaternion q2) { | |
return (q1.w - q2.w) * (q1.w - q2.w) + | |
(q1.x - q2.x) * (q1.x - q2.x) + | |
(q1.y - q2.y) * (q1.y - q2.y) + | |
(q1.z - q2.z) * (q1.z - q2.z); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment