mirror of
https://github.com/brendanhaines/RasPi.git
synced 2024-11-09 16:44:40 -07:00
Add MPU6050dmp example code
This commit is contained in:
parent
a3460720a5
commit
6276473f88
427
IMU/I2Cdev.cpp
Normal file
427
IMU/I2Cdev.cpp
Normal file
|
@ -0,0 +1,427 @@
|
||||||
|
// I2Cdev library collection - Main I2C device class
|
||||||
|
// Abstracts bit and byte I2C R/W functions into a convenient class
|
||||||
|
// 6/9/2012 by Jeff Rowberg <jeff@rowberg.net>
|
||||||
|
//
|
||||||
|
// Changelog:
|
||||||
|
// 2012-06-09 - fix major issue with reading > 32 bytes at a time with Arduino Wire
|
||||||
|
// - add compiler warnings when using outdated or IDE or limited I2Cdev implementation
|
||||||
|
// 2011-11-01 - fix write*Bits mask calculation (thanks sasquatch @ Arduino forums)
|
||||||
|
// 2011-10-03 - added automatic Arduino version detection for ease of use
|
||||||
|
// 2011-10-02 - added Gene Knight's NBWire TwoWire class implementation with small modifications
|
||||||
|
// 2011-08-31 - added support for Arduino 1.0 Wire library (methods are different from 0.x)
|
||||||
|
// 2011-08-03 - added optional timeout parameter to read* methods to easily change from default
|
||||||
|
// 2011-08-02 - added support for 16-bit registers
|
||||||
|
// - fixed incorrect Doxygen comments on some methods
|
||||||
|
// - added timeout value for read operations (thanks mem @ Arduino forums)
|
||||||
|
// 2011-07-30 - changed read/write function structures to return success or byte counts
|
||||||
|
// - made all methods static for multi-device memory savings
|
||||||
|
// 2011-07-28 - initial release
|
||||||
|
|
||||||
|
/* ============================================
|
||||||
|
I2Cdev device library code is placed under the MIT license
|
||||||
|
Copyright (c) 2012 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.
|
||||||
|
===============================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <linux/i2c-dev.h>
|
||||||
|
#include "I2Cdev.h"
|
||||||
|
|
||||||
|
/** Default constructor.
|
||||||
|
*/
|
||||||
|
I2Cdev::I2Cdev() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Read a single bit from an 8-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr Register regAddr to read from
|
||||||
|
* @param bitNum Bit position to read (0-7)
|
||||||
|
* @param data Container for single bit value
|
||||||
|
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
|
||||||
|
* @return Status of read operation (true = success)
|
||||||
|
*/
|
||||||
|
int8_t I2Cdev::readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, uint16_t timeout) {
|
||||||
|
uint8_t b;
|
||||||
|
uint8_t count = readByte(devAddr, regAddr, &b, timeout);
|
||||||
|
*data = b & (1 << bitNum);
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Read a single bit from a 16-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr Register regAddr to read from
|
||||||
|
* @param bitNum Bit position to read (0-15)
|
||||||
|
* @param data Container for single bit value
|
||||||
|
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
|
||||||
|
* @return Status of read operation (true = success)
|
||||||
|
*/
|
||||||
|
int8_t I2Cdev::readBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t *data, uint16_t timeout) {
|
||||||
|
uint16_t b;
|
||||||
|
uint8_t count = readWord(devAddr, regAddr, &b, timeout);
|
||||||
|
*data = b & (1 << bitNum);
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Read multiple bits from an 8-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr Register regAddr to read from
|
||||||
|
* @param bitStart First bit position to read (0-7)
|
||||||
|
* @param length Number of bits to read (not more than 8)
|
||||||
|
* @param data Container for right-aligned value (i.e. '101' read from any bitStart position will equal 0x05)
|
||||||
|
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
|
||||||
|
* @return Status of read operation (true = success)
|
||||||
|
*/
|
||||||
|
int8_t I2Cdev::readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data, uint16_t timeout) {
|
||||||
|
// 01101001 read byte
|
||||||
|
// 76543210 bit numbers
|
||||||
|
// xxx args: bitStart=4, length=3
|
||||||
|
// 010 masked
|
||||||
|
// -> 010 shifted
|
||||||
|
uint8_t count, b;
|
||||||
|
if ((count = readByte(devAddr, regAddr, &b, timeout)) != 0) {
|
||||||
|
uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
|
||||||
|
b &= mask;
|
||||||
|
b >>= (bitStart - length + 1);
|
||||||
|
*data = b;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Read multiple bits from a 16-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr Register regAddr to read from
|
||||||
|
* @param bitStart First bit position to read (0-15)
|
||||||
|
* @param length Number of bits to read (not more than 16)
|
||||||
|
* @param data Container for right-aligned value (i.e. '101' read from any bitStart position will equal 0x05)
|
||||||
|
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
|
||||||
|
* @return Status of read operation (1 = success, 0 = failure, -1 = timeout)
|
||||||
|
*/
|
||||||
|
int8_t I2Cdev::readBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t *data, uint16_t timeout) {
|
||||||
|
// 1101011001101001 read byte
|
||||||
|
// fedcba9876543210 bit numbers
|
||||||
|
// xxx args: bitStart=12, length=3
|
||||||
|
// 010 masked
|
||||||
|
// -> 010 shifted
|
||||||
|
uint8_t count;
|
||||||
|
uint16_t w;
|
||||||
|
if ((count = readWord(devAddr, regAddr, &w, timeout)) != 0) {
|
||||||
|
uint16_t mask = ((1 << length) - 1) << (bitStart - length + 1);
|
||||||
|
w &= mask;
|
||||||
|
w >>= (bitStart - length + 1);
|
||||||
|
*data = w;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Read single byte from an 8-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr Register regAddr to read from
|
||||||
|
* @param data Container for byte value read from device
|
||||||
|
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
|
||||||
|
* @return Status of read operation (true = success)
|
||||||
|
*/
|
||||||
|
int8_t I2Cdev::readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint16_t timeout) {
|
||||||
|
return readBytes(devAddr, regAddr, 1, data, timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Read single word from a 16-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr Register regAddr to read from
|
||||||
|
* @param data Container for word value read from device
|
||||||
|
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
|
||||||
|
* @return Status of read operation (true = success)
|
||||||
|
*/
|
||||||
|
int8_t I2Cdev::readWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data, uint16_t timeout) {
|
||||||
|
return readWords(devAddr, regAddr, 1, data, timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Read multiple bytes from an 8-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr First register regAddr to read from
|
||||||
|
* @param length Number of bytes to read
|
||||||
|
* @param data Buffer to store read data in
|
||||||
|
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
|
||||||
|
* @return Number of bytes read (-1 indicates failure)
|
||||||
|
*/
|
||||||
|
int8_t I2Cdev::readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout) {
|
||||||
|
int8_t count = 0;
|
||||||
|
int fd = open("/dev/i2c-1", O_RDWR);
|
||||||
|
|
||||||
|
if (fd < 0) {
|
||||||
|
fprintf(stderr, "Failed to open device: %s\n", strerror(errno));
|
||||||
|
return(-1);
|
||||||
|
}
|
||||||
|
if (ioctl(fd, I2C_SLAVE, devAddr) < 0) {
|
||||||
|
fprintf(stderr, "Failed to select device: %s\n", strerror(errno));
|
||||||
|
close(fd);
|
||||||
|
return(-1);
|
||||||
|
}
|
||||||
|
if (write(fd, ®Addr, 1) != 1) {
|
||||||
|
fprintf(stderr, "Failed to write reg: %s\n", strerror(errno));
|
||||||
|
close(fd);
|
||||||
|
return(-1);
|
||||||
|
}
|
||||||
|
count = read(fd, data, length);
|
||||||
|
if (count < 0) {
|
||||||
|
fprintf(stderr, "Failed to read device(%d): %s\n", count, ::strerror(errno));
|
||||||
|
close(fd);
|
||||||
|
return(-1);
|
||||||
|
} else if (count != length) {
|
||||||
|
fprintf(stderr, "Short read from device, expected %d, got %d\n", length, count);
|
||||||
|
close(fd);
|
||||||
|
return(-1);
|
||||||
|
}
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Read multiple words from a 16-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr First register regAddr to read from
|
||||||
|
* @param length Number of words to read
|
||||||
|
* @param data Buffer to store read data in
|
||||||
|
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
|
||||||
|
* @return Number of words read (0 indicates failure)
|
||||||
|
*/
|
||||||
|
int8_t I2Cdev::readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data, uint16_t timeout) {
|
||||||
|
int8_t count = 0;
|
||||||
|
|
||||||
|
printf("ReadWords() not implemented\n");
|
||||||
|
// Use readBytes() and potential byteswap
|
||||||
|
*data = 0; // keep the compiler quiet
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** write a single bit in an 8-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr Register regAddr to write to
|
||||||
|
* @param bitNum Bit position to write (0-7)
|
||||||
|
* @param value New bit value to write
|
||||||
|
* @return Status of operation (true = success)
|
||||||
|
*/
|
||||||
|
bool I2Cdev::writeBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data) {
|
||||||
|
uint8_t b;
|
||||||
|
readByte(devAddr, regAddr, &b);
|
||||||
|
b = (data != 0) ? (b | (1 << bitNum)) : (b & ~(1 << bitNum));
|
||||||
|
return writeByte(devAddr, regAddr, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** write a single bit in a 16-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr Register regAddr to write to
|
||||||
|
* @param bitNum Bit position to write (0-15)
|
||||||
|
* @param value New bit value to write
|
||||||
|
* @return Status of operation (true = success)
|
||||||
|
*/
|
||||||
|
bool I2Cdev::writeBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t data) {
|
||||||
|
uint16_t w;
|
||||||
|
readWord(devAddr, regAddr, &w);
|
||||||
|
w = (data != 0) ? (w | (1 << bitNum)) : (w & ~(1 << bitNum));
|
||||||
|
return writeWord(devAddr, regAddr, w);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Write multiple bits in an 8-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr Register regAddr to write to
|
||||||
|
* @param bitStart First bit position to write (0-7)
|
||||||
|
* @param length Number of bits to write (not more than 8)
|
||||||
|
* @param data Right-aligned value to write
|
||||||
|
* @return Status of operation (true = success)
|
||||||
|
*/
|
||||||
|
bool I2Cdev::writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data) {
|
||||||
|
// 010 value to write
|
||||||
|
// 76543210 bit numbers
|
||||||
|
// xxx args: bitStart=4, length=3
|
||||||
|
// 00011100 mask byte
|
||||||
|
// 10101111 original value (sample)
|
||||||
|
// 10100011 original & ~mask
|
||||||
|
// 10101011 masked | value
|
||||||
|
uint8_t b;
|
||||||
|
if (readByte(devAddr, regAddr, &b) != 0) {
|
||||||
|
uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
|
||||||
|
data <<= (bitStart - length + 1); // shift data into correct position
|
||||||
|
data &= mask; // zero all non-important bits in data
|
||||||
|
b &= ~(mask); // zero all important bits in existing byte
|
||||||
|
b |= data; // combine data with existing byte
|
||||||
|
return writeByte(devAddr, regAddr, b);
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Write multiple bits in a 16-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr Register regAddr to write to
|
||||||
|
* @param bitStart First bit position to write (0-15)
|
||||||
|
* @param length Number of bits to write (not more than 16)
|
||||||
|
* @param data Right-aligned value to write
|
||||||
|
* @return Status of operation (true = success)
|
||||||
|
*/
|
||||||
|
bool I2Cdev::writeBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t data) {
|
||||||
|
// 010 value to write
|
||||||
|
// fedcba9876543210 bit numbers
|
||||||
|
// xxx args: bitStart=12, length=3
|
||||||
|
// 0001110000000000 mask byte
|
||||||
|
// 1010111110010110 original value (sample)
|
||||||
|
// 1010001110010110 original & ~mask
|
||||||
|
// 1010101110010110 masked | value
|
||||||
|
uint16_t w;
|
||||||
|
if (readWord(devAddr, regAddr, &w) != 0) {
|
||||||
|
uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
|
||||||
|
data <<= (bitStart - length + 1); // shift data into correct position
|
||||||
|
data &= mask; // zero all non-important bits in data
|
||||||
|
w &= ~(mask); // zero all important bits in existing word
|
||||||
|
w |= data; // combine data with existing word
|
||||||
|
return writeWord(devAddr, regAddr, w);
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Write single byte to an 8-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr Register address to write to
|
||||||
|
* @param data New byte value to write
|
||||||
|
* @return Status of operation (true = success)
|
||||||
|
*/
|
||||||
|
bool I2Cdev::writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data) {
|
||||||
|
return writeBytes(devAddr, regAddr, 1, &data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Write single word to a 16-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr Register address to write to
|
||||||
|
* @param data New word value to write
|
||||||
|
* @return Status of operation (true = success)
|
||||||
|
*/
|
||||||
|
bool I2Cdev::writeWord(uint8_t devAddr, uint8_t regAddr, uint16_t data) {
|
||||||
|
return writeWords(devAddr, regAddr, 1, &data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Write multiple bytes to an 8-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr First register address to write to
|
||||||
|
* @param length Number of bytes to write
|
||||||
|
* @param data Buffer to copy new data from
|
||||||
|
* @return Status of operation (true = success)
|
||||||
|
*/
|
||||||
|
bool I2Cdev::writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t* data) {
|
||||||
|
int8_t count = 0;
|
||||||
|
uint8_t buf[128];
|
||||||
|
int fd;
|
||||||
|
|
||||||
|
if (length > 127) {
|
||||||
|
fprintf(stderr, "Byte write count (%d) > 127\n", length);
|
||||||
|
return(FALSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
fd = open("/dev/i2c-1", O_RDWR);
|
||||||
|
if (fd < 0) {
|
||||||
|
fprintf(stderr, "Failed to open device: %s\n", strerror(errno));
|
||||||
|
return(FALSE);
|
||||||
|
}
|
||||||
|
if (ioctl(fd, I2C_SLAVE, devAddr) < 0) {
|
||||||
|
fprintf(stderr, "Failed to select device: %s\n", strerror(errno));
|
||||||
|
close(fd);
|
||||||
|
return(FALSE);
|
||||||
|
}
|
||||||
|
buf[0] = regAddr;
|
||||||
|
memcpy(buf+1,data,length);
|
||||||
|
count = write(fd, buf, length+1);
|
||||||
|
if (count < 0) {
|
||||||
|
fprintf(stderr, "Failed to write device(%d): %s\n", count, ::strerror(errno));
|
||||||
|
close(fd);
|
||||||
|
return(FALSE);
|
||||||
|
} else if (count != length+1) {
|
||||||
|
fprintf(stderr, "Short write to device, expected %d, got %d\n", length+1, count);
|
||||||
|
close(fd);
|
||||||
|
return(FALSE);
|
||||||
|
}
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Write multiple words to a 16-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr First register address to write to
|
||||||
|
* @param length Number of words to write
|
||||||
|
* @param data Buffer to copy new data from
|
||||||
|
* @return Status of operation (true = success)
|
||||||
|
*/
|
||||||
|
bool I2Cdev::writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t* data) {
|
||||||
|
int8_t count = 0;
|
||||||
|
uint8_t buf[128];
|
||||||
|
int i, fd;
|
||||||
|
|
||||||
|
// Should do potential byteswap and call writeBytes() really, but that
|
||||||
|
// messes with the callers buffer
|
||||||
|
|
||||||
|
if (length > 63) {
|
||||||
|
fprintf(stderr, "Word write count (%d) > 63\n", length);
|
||||||
|
return(FALSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
fd = open("/dev/i2c-1", O_RDWR);
|
||||||
|
if (fd < 0) {
|
||||||
|
fprintf(stderr, "Failed to open device: %s\n", strerror(errno));
|
||||||
|
return(FALSE);
|
||||||
|
}
|
||||||
|
if (ioctl(fd, I2C_SLAVE, devAddr) < 0) {
|
||||||
|
fprintf(stderr, "Failed to select device: %s\n", strerror(errno));
|
||||||
|
close(fd);
|
||||||
|
return(FALSE);
|
||||||
|
}
|
||||||
|
buf[0] = regAddr;
|
||||||
|
for (i = 0; i < length; i++) {
|
||||||
|
buf[i*2+1] = data[i] >> 8;
|
||||||
|
buf[i*2+2] = data[i];
|
||||||
|
}
|
||||||
|
count = write(fd, buf, length*2+1);
|
||||||
|
if (count < 0) {
|
||||||
|
fprintf(stderr, "Failed to write device(%d): %s\n", count, ::strerror(errno));
|
||||||
|
close(fd);
|
||||||
|
return(FALSE);
|
||||||
|
} else if (count != length*2+1) {
|
||||||
|
fprintf(stderr, "Short write to device, expected %d, got %d\n", length+1, count);
|
||||||
|
close(fd);
|
||||||
|
return(FALSE);
|
||||||
|
}
|
||||||
|
close(fd);
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Default timeout value for read operations.
|
||||||
|
* Set this to 0 to disable timeout detection.
|
||||||
|
*/
|
||||||
|
uint16_t I2Cdev::readTimeout = 0;
|
||||||
|
|
427
IMU/I2Cdev.h
Normal file
427
IMU/I2Cdev.h
Normal file
|
@ -0,0 +1,427 @@
|
||||||
|
// I2Cdev library collection - Main I2C device class
|
||||||
|
// Abstracts bit and byte I2C R/W functions into a convenient class
|
||||||
|
// 6/9/2012 by Jeff Rowberg <jeff@rowberg.net>
|
||||||
|
//
|
||||||
|
// Changelog:
|
||||||
|
// 2012-06-09 - fix major issue with reading > 32 bytes at a time with Arduino Wire
|
||||||
|
// - add compiler warnings when using outdated or IDE or limited I2Cdev implementation
|
||||||
|
// 2011-11-01 - fix write*Bits mask calculation (thanks sasquatch @ Arduino forums)
|
||||||
|
// 2011-10-03 - added automatic Arduino version detection for ease of use
|
||||||
|
// 2011-10-02 - added Gene Knight's NBWire TwoWire class implementation with small modifications
|
||||||
|
// 2011-08-31 - added support for Arduino 1.0 Wire library (methods are different from 0.x)
|
||||||
|
// 2011-08-03 - added optional timeout parameter to read* methods to easily change from default
|
||||||
|
// 2011-08-02 - added support for 16-bit registers
|
||||||
|
// - fixed incorrect Doxygen comments on some methods
|
||||||
|
// - added timeout value for read operations (thanks mem @ Arduino forums)
|
||||||
|
// 2011-07-30 - changed read/write function structures to return success or byte counts
|
||||||
|
// - made all methods static for multi-device memory savings
|
||||||
|
// 2011-07-28 - initial release
|
||||||
|
|
||||||
|
/* ============================================
|
||||||
|
I2Cdev device library code is placed under the MIT license
|
||||||
|
Copyright (c) 2012 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.
|
||||||
|
===============================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <linux/i2c-dev.h>
|
||||||
|
#include "I2Cdev.h"
|
||||||
|
|
||||||
|
/** Default constructor.
|
||||||
|
*/
|
||||||
|
I2Cdev::I2Cdev() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Read a single bit from an 8-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr Register regAddr to read from
|
||||||
|
* @param bitNum Bit position to read (0-7)
|
||||||
|
* @param data Container for single bit value
|
||||||
|
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
|
||||||
|
* @return Status of read operation (true = success)
|
||||||
|
*/
|
||||||
|
int8_t I2Cdev::readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, uint16_t timeout) {
|
||||||
|
uint8_t b;
|
||||||
|
uint8_t count = readByte(devAddr, regAddr, &b, timeout);
|
||||||
|
*data = b & (1 << bitNum);
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Read a single bit from a 16-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr Register regAddr to read from
|
||||||
|
* @param bitNum Bit position to read (0-15)
|
||||||
|
* @param data Container for single bit value
|
||||||
|
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
|
||||||
|
* @return Status of read operation (true = success)
|
||||||
|
*/
|
||||||
|
int8_t I2Cdev::readBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t *data, uint16_t timeout) {
|
||||||
|
uint16_t b;
|
||||||
|
uint8_t count = readWord(devAddr, regAddr, &b, timeout);
|
||||||
|
*data = b & (1 << bitNum);
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Read multiple bits from an 8-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr Register regAddr to read from
|
||||||
|
* @param bitStart First bit position to read (0-7)
|
||||||
|
* @param length Number of bits to read (not more than 8)
|
||||||
|
* @param data Container for right-aligned value (i.e. '101' read from any bitStart position will equal 0x05)
|
||||||
|
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
|
||||||
|
* @return Status of read operation (true = success)
|
||||||
|
*/
|
||||||
|
int8_t I2Cdev::readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data, uint16_t timeout) {
|
||||||
|
// 01101001 read byte
|
||||||
|
// 76543210 bit numbers
|
||||||
|
// xxx args: bitStart=4, length=3
|
||||||
|
// 010 masked
|
||||||
|
// -> 010 shifted
|
||||||
|
uint8_t count, b;
|
||||||
|
if ((count = readByte(devAddr, regAddr, &b, timeout)) != 0) {
|
||||||
|
uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
|
||||||
|
b &= mask;
|
||||||
|
b >>= (bitStart - length + 1);
|
||||||
|
*data = b;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Read multiple bits from a 16-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr Register regAddr to read from
|
||||||
|
* @param bitStart First bit position to read (0-15)
|
||||||
|
* @param length Number of bits to read (not more than 16)
|
||||||
|
* @param data Container for right-aligned value (i.e. '101' read from any bitStart position will equal 0x05)
|
||||||
|
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
|
||||||
|
* @return Status of read operation (1 = success, 0 = failure, -1 = timeout)
|
||||||
|
*/
|
||||||
|
int8_t I2Cdev::readBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t *data, uint16_t timeout) {
|
||||||
|
// 1101011001101001 read byte
|
||||||
|
// fedcba9876543210 bit numbers
|
||||||
|
// xxx args: bitStart=12, length=3
|
||||||
|
// 010 masked
|
||||||
|
// -> 010 shifted
|
||||||
|
uint8_t count;
|
||||||
|
uint16_t w;
|
||||||
|
if ((count = readWord(devAddr, regAddr, &w, timeout)) != 0) {
|
||||||
|
uint16_t mask = ((1 << length) - 1) << (bitStart - length + 1);
|
||||||
|
w &= mask;
|
||||||
|
w >>= (bitStart - length + 1);
|
||||||
|
*data = w;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Read single byte from an 8-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr Register regAddr to read from
|
||||||
|
* @param data Container for byte value read from device
|
||||||
|
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
|
||||||
|
* @return Status of read operation (true = success)
|
||||||
|
*/
|
||||||
|
int8_t I2Cdev::readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint16_t timeout) {
|
||||||
|
return readBytes(devAddr, regAddr, 1, data, timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Read single word from a 16-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr Register regAddr to read from
|
||||||
|
* @param data Container for word value read from device
|
||||||
|
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
|
||||||
|
* @return Status of read operation (true = success)
|
||||||
|
*/
|
||||||
|
int8_t I2Cdev::readWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data, uint16_t timeout) {
|
||||||
|
return readWords(devAddr, regAddr, 1, data, timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Read multiple bytes from an 8-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr First register regAddr to read from
|
||||||
|
* @param length Number of bytes to read
|
||||||
|
* @param data Buffer to store read data in
|
||||||
|
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
|
||||||
|
* @return Number of bytes read (-1 indicates failure)
|
||||||
|
*/
|
||||||
|
int8_t I2Cdev::readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout) {
|
||||||
|
int8_t count = 0;
|
||||||
|
int fd = open("/dev/i2c-1", O_RDWR);
|
||||||
|
|
||||||
|
if (fd < 0) {
|
||||||
|
fprintf(stderr, "Failed to open device: %s\n", strerror(errno));
|
||||||
|
return(-1);
|
||||||
|
}
|
||||||
|
if (ioctl(fd, I2C_SLAVE, devAddr) < 0) {
|
||||||
|
fprintf(stderr, "Failed to select device: %s\n", strerror(errno));
|
||||||
|
close(fd);
|
||||||
|
return(-1);
|
||||||
|
}
|
||||||
|
if (write(fd, ®Addr, 1) != 1) {
|
||||||
|
fprintf(stderr, "Failed to write reg: %s\n", strerror(errno));
|
||||||
|
close(fd);
|
||||||
|
return(-1);
|
||||||
|
}
|
||||||
|
count = read(fd, data, length);
|
||||||
|
if (count < 0) {
|
||||||
|
fprintf(stderr, "Failed to read device(%d): %s\n", count, ::strerror(errno));
|
||||||
|
close(fd);
|
||||||
|
return(-1);
|
||||||
|
} else if (count != length) {
|
||||||
|
fprintf(stderr, "Short read from device, expected %d, got %d\n", length, count);
|
||||||
|
close(fd);
|
||||||
|
return(-1);
|
||||||
|
}
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Read multiple words from a 16-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr First register regAddr to read from
|
||||||
|
* @param length Number of words to read
|
||||||
|
* @param data Buffer to store read data in
|
||||||
|
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
|
||||||
|
* @return Number of words read (0 indicates failure)
|
||||||
|
*/
|
||||||
|
int8_t I2Cdev::readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data, uint16_t timeout) {
|
||||||
|
int8_t count = 0;
|
||||||
|
|
||||||
|
printf("ReadWords() not implemented\n");
|
||||||
|
// Use readBytes() and potential byteswap
|
||||||
|
*data = 0; // keep the compiler quiet
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** write a single bit in an 8-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr Register regAddr to write to
|
||||||
|
* @param bitNum Bit position to write (0-7)
|
||||||
|
* @param value New bit value to write
|
||||||
|
* @return Status of operation (true = success)
|
||||||
|
*/
|
||||||
|
bool I2Cdev::writeBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data) {
|
||||||
|
uint8_t b;
|
||||||
|
readByte(devAddr, regAddr, &b);
|
||||||
|
b = (data != 0) ? (b | (1 << bitNum)) : (b & ~(1 << bitNum));
|
||||||
|
return writeByte(devAddr, regAddr, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** write a single bit in a 16-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr Register regAddr to write to
|
||||||
|
* @param bitNum Bit position to write (0-15)
|
||||||
|
* @param value New bit value to write
|
||||||
|
* @return Status of operation (true = success)
|
||||||
|
*/
|
||||||
|
bool I2Cdev::writeBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t data) {
|
||||||
|
uint16_t w;
|
||||||
|
readWord(devAddr, regAddr, &w);
|
||||||
|
w = (data != 0) ? (w | (1 << bitNum)) : (w & ~(1 << bitNum));
|
||||||
|
return writeWord(devAddr, regAddr, w);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Write multiple bits in an 8-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr Register regAddr to write to
|
||||||
|
* @param bitStart First bit position to write (0-7)
|
||||||
|
* @param length Number of bits to write (not more than 8)
|
||||||
|
* @param data Right-aligned value to write
|
||||||
|
* @return Status of operation (true = success)
|
||||||
|
*/
|
||||||
|
bool I2Cdev::writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data) {
|
||||||
|
// 010 value to write
|
||||||
|
// 76543210 bit numbers
|
||||||
|
// xxx args: bitStart=4, length=3
|
||||||
|
// 00011100 mask byte
|
||||||
|
// 10101111 original value (sample)
|
||||||
|
// 10100011 original & ~mask
|
||||||
|
// 10101011 masked | value
|
||||||
|
uint8_t b;
|
||||||
|
if (readByte(devAddr, regAddr, &b) != 0) {
|
||||||
|
uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
|
||||||
|
data <<= (bitStart - length + 1); // shift data into correct position
|
||||||
|
data &= mask; // zero all non-important bits in data
|
||||||
|
b &= ~(mask); // zero all important bits in existing byte
|
||||||
|
b |= data; // combine data with existing byte
|
||||||
|
return writeByte(devAddr, regAddr, b);
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Write multiple bits in a 16-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr Register regAddr to write to
|
||||||
|
* @param bitStart First bit position to write (0-15)
|
||||||
|
* @param length Number of bits to write (not more than 16)
|
||||||
|
* @param data Right-aligned value to write
|
||||||
|
* @return Status of operation (true = success)
|
||||||
|
*/
|
||||||
|
bool I2Cdev::writeBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t data) {
|
||||||
|
// 010 value to write
|
||||||
|
// fedcba9876543210 bit numbers
|
||||||
|
// xxx args: bitStart=12, length=3
|
||||||
|
// 0001110000000000 mask byte
|
||||||
|
// 1010111110010110 original value (sample)
|
||||||
|
// 1010001110010110 original & ~mask
|
||||||
|
// 1010101110010110 masked | value
|
||||||
|
uint16_t w;
|
||||||
|
if (readWord(devAddr, regAddr, &w) != 0) {
|
||||||
|
uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
|
||||||
|
data <<= (bitStart - length + 1); // shift data into correct position
|
||||||
|
data &= mask; // zero all non-important bits in data
|
||||||
|
w &= ~(mask); // zero all important bits in existing word
|
||||||
|
w |= data; // combine data with existing word
|
||||||
|
return writeWord(devAddr, regAddr, w);
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Write single byte to an 8-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr Register address to write to
|
||||||
|
* @param data New byte value to write
|
||||||
|
* @return Status of operation (true = success)
|
||||||
|
*/
|
||||||
|
bool I2Cdev::writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data) {
|
||||||
|
return writeBytes(devAddr, regAddr, 1, &data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Write single word to a 16-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr Register address to write to
|
||||||
|
* @param data New word value to write
|
||||||
|
* @return Status of operation (true = success)
|
||||||
|
*/
|
||||||
|
bool I2Cdev::writeWord(uint8_t devAddr, uint8_t regAddr, uint16_t data) {
|
||||||
|
return writeWords(devAddr, regAddr, 1, &data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Write multiple bytes to an 8-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr First register address to write to
|
||||||
|
* @param length Number of bytes to write
|
||||||
|
* @param data Buffer to copy new data from
|
||||||
|
* @return Status of operation (true = success)
|
||||||
|
*/
|
||||||
|
bool I2Cdev::writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t* data) {
|
||||||
|
int8_t count = 0;
|
||||||
|
uint8_t buf[128];
|
||||||
|
int fd;
|
||||||
|
|
||||||
|
if (length > 127) {
|
||||||
|
fprintf(stderr, "Byte write count (%d) > 127\n", length);
|
||||||
|
return(FALSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
fd = open("/dev/i2c-1", O_RDWR);
|
||||||
|
if (fd < 0) {
|
||||||
|
fprintf(stderr, "Failed to open device: %s\n", strerror(errno));
|
||||||
|
return(FALSE);
|
||||||
|
}
|
||||||
|
if (ioctl(fd, I2C_SLAVE, devAddr) < 0) {
|
||||||
|
fprintf(stderr, "Failed to select device: %s\n", strerror(errno));
|
||||||
|
close(fd);
|
||||||
|
return(FALSE);
|
||||||
|
}
|
||||||
|
buf[0] = regAddr;
|
||||||
|
memcpy(buf+1,data,length);
|
||||||
|
count = write(fd, buf, length+1);
|
||||||
|
if (count < 0) {
|
||||||
|
fprintf(stderr, "Failed to write device(%d): %s\n", count, ::strerror(errno));
|
||||||
|
close(fd);
|
||||||
|
return(FALSE);
|
||||||
|
} else if (count != length+1) {
|
||||||
|
fprintf(stderr, "Short write to device, expected %d, got %d\n", length+1, count);
|
||||||
|
close(fd);
|
||||||
|
return(FALSE);
|
||||||
|
}
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Write multiple words to a 16-bit device register.
|
||||||
|
* @param devAddr I2C slave device address
|
||||||
|
* @param regAddr First register address to write to
|
||||||
|
* @param length Number of words to write
|
||||||
|
* @param data Buffer to copy new data from
|
||||||
|
* @return Status of operation (true = success)
|
||||||
|
*/
|
||||||
|
bool I2Cdev::writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t* data) {
|
||||||
|
int8_t count = 0;
|
||||||
|
uint8_t buf[128];
|
||||||
|
int i, fd;
|
||||||
|
|
||||||
|
// Should do potential byteswap and call writeBytes() really, but that
|
||||||
|
// messes with the callers buffer
|
||||||
|
|
||||||
|
if (length > 63) {
|
||||||
|
fprintf(stderr, "Word write count (%d) > 63\n", length);
|
||||||
|
return(FALSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
fd = open("/dev/i2c-1", O_RDWR);
|
||||||
|
if (fd < 0) {
|
||||||
|
fprintf(stderr, "Failed to open device: %s\n", strerror(errno));
|
||||||
|
return(FALSE);
|
||||||
|
}
|
||||||
|
if (ioctl(fd, I2C_SLAVE, devAddr) < 0) {
|
||||||
|
fprintf(stderr, "Failed to select device: %s\n", strerror(errno));
|
||||||
|
close(fd);
|
||||||
|
return(FALSE);
|
||||||
|
}
|
||||||
|
buf[0] = regAddr;
|
||||||
|
for (i = 0; i < length; i++) {
|
||||||
|
buf[i*2+1] = data[i] >> 8;
|
||||||
|
buf[i*2+2] = data[i];
|
||||||
|
}
|
||||||
|
count = write(fd, buf, length*2+1);
|
||||||
|
if (count < 0) {
|
||||||
|
fprintf(stderr, "Failed to write device(%d): %s\n", count, ::strerror(errno));
|
||||||
|
close(fd);
|
||||||
|
return(FALSE);
|
||||||
|
} else if (count != length*2+1) {
|
||||||
|
fprintf(stderr, "Short write to device, expected %d, got %d\n", length+1, count);
|
||||||
|
close(fd);
|
||||||
|
return(FALSE);
|
||||||
|
}
|
||||||
|
close(fd);
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Default timeout value for read operations.
|
||||||
|
* Set this to 0 to disable timeout detection.
|
||||||
|
*/
|
||||||
|
uint16_t I2Cdev::readTimeout = 0;
|
||||||
|
|
3147
IMU/MPU6050.cpp
Normal file
3147
IMU/MPU6050.cpp
Normal file
File diff suppressed because it is too large
Load Diff
988
IMU/MPU6050.h
Normal file
988
IMU/MPU6050.h
Normal file
|
@ -0,0 +1,988 @@
|
||||||
|
// I2Cdev library collection - MPU6050 I2C device class
|
||||||
|
// Based on InvenSense MPU-6050 register map document rev. 2.0, 5/19/2011 (RM-MPU-6000A-00)
|
||||||
|
// 10/3/2011 by Jeff Rowberg <jeff@rowberg.net>
|
||||||
|
// Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
|
||||||
|
//
|
||||||
|
// Changelog:
|
||||||
|
// ... - ongoing debug release
|
||||||
|
|
||||||
|
// NOTE: THIS IS ONLY A PARIAL RELEASE. THIS DEVICE CLASS IS CURRENTLY UNDERGOING ACTIVE
|
||||||
|
// DEVELOPMENT AND IS STILL MISSING SOME IMPORTANT FEATURES. PLEASE KEEP THIS IN MIND IF
|
||||||
|
// YOU DECIDE TO USE THIS PARTICULAR CODE FOR ANYTHING.
|
||||||
|
|
||||||
|
/* ============================================
|
||||||
|
I2Cdev device library code is placed under the MIT license
|
||||||
|
Copyright (c) 2012 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.
|
||||||
|
===============================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _MPU6050_H_
|
||||||
|
#define _MPU6050_H_
|
||||||
|
|
||||||
|
#include "I2Cdev.h"
|
||||||
|
//#include <avr/pgmspace.h>
|
||||||
|
|
||||||
|
#define pgm_read_byte(p) (*(uint8_t *)(p))
|
||||||
|
|
||||||
|
|
||||||
|
#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
|
||||||
|
|
||||||
|
#define MPU6050_RA_XG_OFFS_TC 0x00 //[7] PWR_MODE, [6:1] XG_OFFS_TC, [0] OTP_BNK_VLD
|
||||||
|
#define MPU6050_RA_YG_OFFS_TC 0x01 //[7] PWR_MODE, [6:1] YG_OFFS_TC, [0] OTP_BNK_VLD
|
||||||
|
#define MPU6050_RA_ZG_OFFS_TC 0x02 //[7] PWR_MODE, [6:1] ZG_OFFS_TC, [0] OTP_BNK_VLD
|
||||||
|
#define MPU6050_RA_X_FINE_GAIN 0x03 //[7:0] X_FINE_GAIN
|
||||||
|
#define MPU6050_RA_Y_FINE_GAIN 0x04 //[7:0] Y_FINE_GAIN
|
||||||
|
#define MPU6050_RA_Z_FINE_GAIN 0x05 //[7:0] Z_FINE_GAIN
|
||||||
|
#define MPU6050_RA_XA_OFFS_H 0x06 //[15:0] XA_OFFS
|
||||||
|
#define MPU6050_RA_XA_OFFS_L_TC 0x07
|
||||||
|
#define MPU6050_RA_YA_OFFS_H 0x08 //[15:0] YA_OFFS
|
||||||
|
#define MPU6050_RA_YA_OFFS_L_TC 0x09
|
||||||
|
#define MPU6050_RA_ZA_OFFS_H 0x0A //[15:0] ZA_OFFS
|
||||||
|
#define MPU6050_RA_ZA_OFFS_L_TC 0x0B
|
||||||
|
#define MPU6050_RA_XG_OFFS_USRH 0x13 //[15:0] XG_OFFS_USR
|
||||||
|
#define MPU6050_RA_XG_OFFS_USRL 0x14
|
||||||
|
#define MPU6050_RA_YG_OFFS_USRH 0x15 //[15:0] YG_OFFS_USR
|
||||||
|
#define MPU6050_RA_YG_OFFS_USRL 0x16
|
||||||
|
#define MPU6050_RA_ZG_OFFS_USRH 0x17 //[15:0] ZG_OFFS_USR
|
||||||
|
#define MPU6050_RA_ZG_OFFS_USRL 0x18
|
||||||
|
#define MPU6050_RA_SMPLRT_DIV 0x19
|
||||||
|
#define MPU6050_RA_CONFIG 0x1A
|
||||||
|
#define MPU6050_RA_GYRO_CONFIG 0x1B
|
||||||
|
#define MPU6050_RA_ACCEL_CONFIG 0x1C
|
||||||
|
#define MPU6050_RA_FF_THR 0x1D
|
||||||
|
#define MPU6050_RA_FF_DUR 0x1E
|
||||||
|
#define MPU6050_RA_MOT_THR 0x1F
|
||||||
|
#define MPU6050_RA_MOT_DUR 0x20
|
||||||
|
#define MPU6050_RA_ZRMOT_THR 0x21
|
||||||
|
#define MPU6050_RA_ZRMOT_DUR 0x22
|
||||||
|
#define MPU6050_RA_FIFO_EN 0x23
|
||||||
|
#define MPU6050_RA_I2C_MST_CTRL 0x24
|
||||||
|
#define MPU6050_RA_I2C_SLV0_ADDR 0x25
|
||||||
|
#define MPU6050_RA_I2C_SLV0_REG 0x26
|
||||||
|
#define MPU6050_RA_I2C_SLV0_CTRL 0x27
|
||||||
|
#define MPU6050_RA_I2C_SLV1_ADDR 0x28
|
||||||
|
#define MPU6050_RA_I2C_SLV1_REG 0x29
|
||||||
|
#define MPU6050_RA_I2C_SLV1_CTRL 0x2A
|
||||||
|
#define MPU6050_RA_I2C_SLV2_ADDR 0x2B
|
||||||
|
#define MPU6050_RA_I2C_SLV2_REG 0x2C
|
||||||
|
#define MPU6050_RA_I2C_SLV2_CTRL 0x2D
|
||||||
|
#define MPU6050_RA_I2C_SLV3_ADDR 0x2E
|
||||||
|
#define MPU6050_RA_I2C_SLV3_REG 0x2F
|
||||||
|
#define MPU6050_RA_I2C_SLV3_CTRL 0x30
|
||||||
|
#define MPU6050_RA_I2C_SLV4_ADDR 0x31
|
||||||
|
#define MPU6050_RA_I2C_SLV4_REG 0x32
|
||||||
|
#define MPU6050_RA_I2C_SLV4_DO 0x33
|
||||||
|
#define MPU6050_RA_I2C_SLV4_CTRL 0x34
|
||||||
|
#define MPU6050_RA_I2C_SLV4_DI 0x35
|
||||||
|
#define MPU6050_RA_I2C_MST_STATUS 0x36
|
||||||
|
#define MPU6050_RA_INT_PIN_CFG 0x37
|
||||||
|
#define MPU6050_RA_INT_ENABLE 0x38
|
||||||
|
#define MPU6050_RA_DMP_INT_STATUS 0x39
|
||||||
|
#define MPU6050_RA_INT_STATUS 0x3A
|
||||||
|
#define MPU6050_RA_ACCEL_XOUT_H 0x3B
|
||||||
|
#define MPU6050_RA_ACCEL_XOUT_L 0x3C
|
||||||
|
#define MPU6050_RA_ACCEL_YOUT_H 0x3D
|
||||||
|
#define MPU6050_RA_ACCEL_YOUT_L 0x3E
|
||||||
|
#define MPU6050_RA_ACCEL_ZOUT_H 0x3F
|
||||||
|
#define MPU6050_RA_ACCEL_ZOUT_L 0x40
|
||||||
|
#define MPU6050_RA_TEMP_OUT_H 0x41
|
||||||
|
#define MPU6050_RA_TEMP_OUT_L 0x42
|
||||||
|
#define MPU6050_RA_GYRO_XOUT_H 0x43
|
||||||
|
#define MPU6050_RA_GYRO_XOUT_L 0x44
|
||||||
|
#define MPU6050_RA_GYRO_YOUT_H 0x45
|
||||||
|
#define MPU6050_RA_GYRO_YOUT_L 0x46
|
||||||
|
#define MPU6050_RA_GYRO_ZOUT_H 0x47
|
||||||
|
#define MPU6050_RA_GYRO_ZOUT_L 0x48
|
||||||
|
#define MPU6050_RA_EXT_SENS_DATA_00 0x49
|
||||||
|
#define MPU6050_RA_EXT_SENS_DATA_01 0x4A
|
||||||
|
#define MPU6050_RA_EXT_SENS_DATA_02 0x4B
|
||||||
|
#define MPU6050_RA_EXT_SENS_DATA_03 0x4C
|
||||||
|
#define MPU6050_RA_EXT_SENS_DATA_04 0x4D
|
||||||
|
#define MPU6050_RA_EXT_SENS_DATA_05 0x4E
|
||||||
|
#define MPU6050_RA_EXT_SENS_DATA_06 0x4F
|
||||||
|
#define MPU6050_RA_EXT_SENS_DATA_07 0x50
|
||||||
|
#define MPU6050_RA_EXT_SENS_DATA_08 0x51
|
||||||
|
#define MPU6050_RA_EXT_SENS_DATA_09 0x52
|
||||||
|
#define MPU6050_RA_EXT_SENS_DATA_10 0x53
|
||||||
|
#define MPU6050_RA_EXT_SENS_DATA_11 0x54
|
||||||
|
#define MPU6050_RA_EXT_SENS_DATA_12 0x55
|
||||||
|
#define MPU6050_RA_EXT_SENS_DATA_13 0x56
|
||||||
|
#define MPU6050_RA_EXT_SENS_DATA_14 0x57
|
||||||
|
#define MPU6050_RA_EXT_SENS_DATA_15 0x58
|
||||||
|
#define MPU6050_RA_EXT_SENS_DATA_16 0x59
|
||||||
|
#define MPU6050_RA_EXT_SENS_DATA_17 0x5A
|
||||||
|
#define MPU6050_RA_EXT_SENS_DATA_18 0x5B
|
||||||
|
#define MPU6050_RA_EXT_SENS_DATA_19 0x5C
|
||||||
|
#define MPU6050_RA_EXT_SENS_DATA_20 0x5D
|
||||||
|
#define MPU6050_RA_EXT_SENS_DATA_21 0x5E
|
||||||
|
#define MPU6050_RA_EXT_SENS_DATA_22 0x5F
|
||||||
|
#define MPU6050_RA_EXT_SENS_DATA_23 0x60
|
||||||
|
#define MPU6050_RA_MOT_DETECT_STATUS 0x61
|
||||||
|
#define MPU6050_RA_I2C_SLV0_DO 0x63
|
||||||
|
#define MPU6050_RA_I2C_SLV1_DO 0x64
|
||||||
|
#define MPU6050_RA_I2C_SLV2_DO 0x65
|
||||||
|
#define MPU6050_RA_I2C_SLV3_DO 0x66
|
||||||
|
#define MPU6050_RA_I2C_MST_DELAY_CTRL 0x67
|
||||||
|
#define MPU6050_RA_SIGNAL_PATH_RESET 0x68
|
||||||
|
#define MPU6050_RA_MOT_DETECT_CTRL 0x69
|
||||||
|
#define MPU6050_RA_USER_CTRL 0x6A
|
||||||
|
#define MPU6050_RA_PWR_MGMT_1 0x6B
|
||||||
|
#define MPU6050_RA_PWR_MGMT_2 0x6C
|
||||||
|
#define MPU6050_RA_BANK_SEL 0x6D
|
||||||
|
#define MPU6050_RA_MEM_START_ADDR 0x6E
|
||||||
|
#define MPU6050_RA_MEM_R_W 0x6F
|
||||||
|
#define MPU6050_RA_DMP_CFG_1 0x70
|
||||||
|
#define MPU6050_RA_DMP_CFG_2 0x71
|
||||||
|
#define MPU6050_RA_FIFO_COUNTH 0x72
|
||||||
|
#define MPU6050_RA_FIFO_COUNTL 0x73
|
||||||
|
#define MPU6050_RA_FIFO_R_W 0x74
|
||||||
|
#define MPU6050_RA_WHO_AM_I 0x75
|
||||||
|
|
||||||
|
#define MPU6050_TC_PWR_MODE_BIT 7
|
||||||
|
#define MPU6050_TC_OFFSET_BIT 6
|
||||||
|
#define MPU6050_TC_OFFSET_LENGTH 6
|
||||||
|
#define MPU6050_TC_OTP_BNK_VLD_BIT 0
|
||||||
|
|
||||||
|
#define MPU6050_VDDIO_LEVEL_VLOGIC 0
|
||||||
|
#define MPU6050_VDDIO_LEVEL_VDD 1
|
||||||
|
|
||||||
|
#define MPU6050_CFG_EXT_SYNC_SET_BIT 5
|
||||||
|
#define MPU6050_CFG_EXT_SYNC_SET_LENGTH 3
|
||||||
|
#define MPU6050_CFG_DLPF_CFG_BIT 2
|
||||||
|
#define MPU6050_CFG_DLPF_CFG_LENGTH 3
|
||||||
|
|
||||||
|
#define MPU6050_EXT_SYNC_DISABLED 0x0
|
||||||
|
#define MPU6050_EXT_SYNC_TEMP_OUT_L 0x1
|
||||||
|
#define MPU6050_EXT_SYNC_GYRO_XOUT_L 0x2
|
||||||
|
#define MPU6050_EXT_SYNC_GYRO_YOUT_L 0x3
|
||||||
|
#define MPU6050_EXT_SYNC_GYRO_ZOUT_L 0x4
|
||||||
|
#define MPU6050_EXT_SYNC_ACCEL_XOUT_L 0x5
|
||||||
|
#define MPU6050_EXT_SYNC_ACCEL_YOUT_L 0x6
|
||||||
|
#define MPU6050_EXT_SYNC_ACCEL_ZOUT_L 0x7
|
||||||
|
|
||||||
|
#define MPU6050_DLPF_BW_256 0x00
|
||||||
|
#define MPU6050_DLPF_BW_188 0x01
|
||||||
|
#define MPU6050_DLPF_BW_98 0x02
|
||||||
|
#define MPU6050_DLPF_BW_42 0x03
|
||||||
|
#define MPU6050_DLPF_BW_20 0x04
|
||||||
|
#define MPU6050_DLPF_BW_10 0x05
|
||||||
|
#define MPU6050_DLPF_BW_5 0x06
|
||||||
|
|
||||||
|
#define MPU6050_GCONFIG_FS_SEL_BIT 4
|
||||||
|
#define MPU6050_GCONFIG_FS_SEL_LENGTH 2
|
||||||
|
|
||||||
|
#define MPU6050_GYRO_FS_250 0x00
|
||||||
|
#define MPU6050_GYRO_FS_500 0x01
|
||||||
|
#define MPU6050_GYRO_FS_1000 0x02
|
||||||
|
#define MPU6050_GYRO_FS_2000 0x03
|
||||||
|
|
||||||
|
#define MPU6050_ACONFIG_XA_ST_BIT 7
|
||||||
|
#define MPU6050_ACONFIG_YA_ST_BIT 6
|
||||||
|
#define MPU6050_ACONFIG_ZA_ST_BIT 5
|
||||||
|
#define MPU6050_ACONFIG_AFS_SEL_BIT 4
|
||||||
|
#define MPU6050_ACONFIG_AFS_SEL_LENGTH 2
|
||||||
|
#define MPU6050_ACONFIG_ACCEL_HPF_BIT 2
|
||||||
|
#define MPU6050_ACONFIG_ACCEL_HPF_LENGTH 3
|
||||||
|
|
||||||
|
#define MPU6050_ACCEL_FS_2 0x00
|
||||||
|
#define MPU6050_ACCEL_FS_4 0x01
|
||||||
|
#define MPU6050_ACCEL_FS_8 0x02
|
||||||
|
#define MPU6050_ACCEL_FS_16 0x03
|
||||||
|
|
||||||
|
#define MPU6050_DHPF_RESET 0x00
|
||||||
|
#define MPU6050_DHPF_5 0x01
|
||||||
|
#define MPU6050_DHPF_2P5 0x02
|
||||||
|
#define MPU6050_DHPF_1P25 0x03
|
||||||
|
#define MPU6050_DHPF_0P63 0x04
|
||||||
|
#define MPU6050_DHPF_HOLD 0x07
|
||||||
|
|
||||||
|
#define MPU6050_TEMP_FIFO_EN_BIT 7
|
||||||
|
#define MPU6050_XG_FIFO_EN_BIT 6
|
||||||
|
#define MPU6050_YG_FIFO_EN_BIT 5
|
||||||
|
#define MPU6050_ZG_FIFO_EN_BIT 4
|
||||||
|
#define MPU6050_ACCEL_FIFO_EN_BIT 3
|
||||||
|
#define MPU6050_SLV2_FIFO_EN_BIT 2
|
||||||
|
#define MPU6050_SLV1_FIFO_EN_BIT 1
|
||||||
|
#define MPU6050_SLV0_FIFO_EN_BIT 0
|
||||||
|
|
||||||
|
#define MPU6050_MULT_MST_EN_BIT 7
|
||||||
|
#define MPU6050_WAIT_FOR_ES_BIT 6
|
||||||
|
#define MPU6050_SLV_3_FIFO_EN_BIT 5
|
||||||
|
#define MPU6050_I2C_MST_P_NSR_BIT 4
|
||||||
|
#define MPU6050_I2C_MST_CLK_BIT 3
|
||||||
|
#define MPU6050_I2C_MST_CLK_LENGTH 4
|
||||||
|
|
||||||
|
#define MPU6050_CLOCK_DIV_348 0x0
|
||||||
|
#define MPU6050_CLOCK_DIV_333 0x1
|
||||||
|
#define MPU6050_CLOCK_DIV_320 0x2
|
||||||
|
#define MPU6050_CLOCK_DIV_308 0x3
|
||||||
|
#define MPU6050_CLOCK_DIV_296 0x4
|
||||||
|
#define MPU6050_CLOCK_DIV_286 0x5
|
||||||
|
#define MPU6050_CLOCK_DIV_276 0x6
|
||||||
|
#define MPU6050_CLOCK_DIV_267 0x7
|
||||||
|
#define MPU6050_CLOCK_DIV_258 0x8
|
||||||
|
#define MPU6050_CLOCK_DIV_500 0x9
|
||||||
|
#define MPU6050_CLOCK_DIV_471 0xA
|
||||||
|
#define MPU6050_CLOCK_DIV_444 0xB
|
||||||
|
#define MPU6050_CLOCK_DIV_421 0xC
|
||||||
|
#define MPU6050_CLOCK_DIV_400 0xD
|
||||||
|
#define MPU6050_CLOCK_DIV_381 0xE
|
||||||
|
#define MPU6050_CLOCK_DIV_364 0xF
|
||||||
|
|
||||||
|
#define MPU6050_I2C_SLV_RW_BIT 7
|
||||||
|
#define MPU6050_I2C_SLV_ADDR_BIT 6
|
||||||
|
#define MPU6050_I2C_SLV_ADDR_LENGTH 7
|
||||||
|
#define MPU6050_I2C_SLV_EN_BIT 7
|
||||||
|
#define MPU6050_I2C_SLV_BYTE_SW_BIT 6
|
||||||
|
#define MPU6050_I2C_SLV_REG_DIS_BIT 5
|
||||||
|
#define MPU6050_I2C_SLV_GRP_BIT 4
|
||||||
|
#define MPU6050_I2C_SLV_LEN_BIT 3
|
||||||
|
#define MPU6050_I2C_SLV_LEN_LENGTH 4
|
||||||
|
|
||||||
|
#define MPU6050_I2C_SLV4_RW_BIT 7
|
||||||
|
#define MPU6050_I2C_SLV4_ADDR_BIT 6
|
||||||
|
#define MPU6050_I2C_SLV4_ADDR_LENGTH 7
|
||||||
|
#define MPU6050_I2C_SLV4_EN_BIT 7
|
||||||
|
#define MPU6050_I2C_SLV4_INT_EN_BIT 6
|
||||||
|
#define MPU6050_I2C_SLV4_REG_DIS_BIT 5
|
||||||
|
#define MPU6050_I2C_SLV4_MST_DLY_BIT 4
|
||||||
|
#define MPU6050_I2C_SLV4_MST_DLY_LENGTH 5
|
||||||
|
|
||||||
|
#define MPU6050_MST_PASS_THROUGH_BIT 7
|
||||||
|
#define MPU6050_MST_I2C_SLV4_DONE_BIT 6
|
||||||
|
#define MPU6050_MST_I2C_LOST_ARB_BIT 5
|
||||||
|
#define MPU6050_MST_I2C_SLV4_NACK_BIT 4
|
||||||
|
#define MPU6050_MST_I2C_SLV3_NACK_BIT 3
|
||||||
|
#define MPU6050_MST_I2C_SLV2_NACK_BIT 2
|
||||||
|
#define MPU6050_MST_I2C_SLV1_NACK_BIT 1
|
||||||
|
#define MPU6050_MST_I2C_SLV0_NACK_BIT 0
|
||||||
|
|
||||||
|
#define MPU6050_INTCFG_INT_LEVEL_BIT 7
|
||||||
|
#define MPU6050_INTCFG_INT_OPEN_BIT 6
|
||||||
|
#define MPU6050_INTCFG_LATCH_INT_EN_BIT 5
|
||||||
|
#define MPU6050_INTCFG_INT_RD_CLEAR_BIT 4
|
||||||
|
#define MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT 3
|
||||||
|
#define MPU6050_INTCFG_FSYNC_INT_EN_BIT 2
|
||||||
|
#define MPU6050_INTCFG_I2C_BYPASS_EN_BIT 1
|
||||||
|
#define MPU6050_INTCFG_CLKOUT_EN_BIT 0
|
||||||
|
|
||||||
|
#define MPU6050_INTMODE_ACTIVEHIGH 0x00
|
||||||
|
#define MPU6050_INTMODE_ACTIVELOW 0x01
|
||||||
|
|
||||||
|
#define MPU6050_INTDRV_PUSHPULL 0x00
|
||||||
|
#define MPU6050_INTDRV_OPENDRAIN 0x01
|
||||||
|
|
||||||
|
#define MPU6050_INTLATCH_50USPULSE 0x00
|
||||||
|
#define MPU6050_INTLATCH_WAITCLEAR 0x01
|
||||||
|
|
||||||
|
#define MPU6050_INTCLEAR_STATUSREAD 0x00
|
||||||
|
#define MPU6050_INTCLEAR_ANYREAD 0x01
|
||||||
|
|
||||||
|
#define MPU6050_INTERRUPT_FF_BIT 7
|
||||||
|
#define MPU6050_INTERRUPT_MOT_BIT 6
|
||||||
|
#define MPU6050_INTERRUPT_ZMOT_BIT 5
|
||||||
|
#define MPU6050_INTERRUPT_FIFO_OFLOW_BIT 4
|
||||||
|
#define MPU6050_INTERRUPT_I2C_MST_INT_BIT 3
|
||||||
|
#define MPU6050_INTERRUPT_PLL_RDY_INT_BIT 2
|
||||||
|
#define MPU6050_INTERRUPT_DMP_INT_BIT 1
|
||||||
|
#define MPU6050_INTERRUPT_DATA_RDY_BIT 0
|
||||||
|
|
||||||
|
// TODO: figure out what these actually do
|
||||||
|
// UMPL source code is not very obivous
|
||||||
|
#define MPU6050_DMPINT_5_BIT 5
|
||||||
|
#define MPU6050_DMPINT_4_BIT 4
|
||||||
|
#define MPU6050_DMPINT_3_BIT 3
|
||||||
|
#define MPU6050_DMPINT_2_BIT 2
|
||||||
|
#define MPU6050_DMPINT_1_BIT 1
|
||||||
|
#define MPU6050_DMPINT_0_BIT 0
|
||||||
|
|
||||||
|
#define MPU6050_MOTION_MOT_XNEG_BIT 7
|
||||||
|
#define MPU6050_MOTION_MOT_XPOS_BIT 6
|
||||||
|
#define MPU6050_MOTION_MOT_YNEG_BIT 5
|
||||||
|
#define MPU6050_MOTION_MOT_YPOS_BIT 4
|
||||||
|
#define MPU6050_MOTION_MOT_ZNEG_BIT 3
|
||||||
|
#define MPU6050_MOTION_MOT_ZPOS_BIT 2
|
||||||
|
#define MPU6050_MOTION_MOT_ZRMOT_BIT 0
|
||||||
|
|
||||||
|
#define MPU6050_DELAYCTRL_DELAY_ES_SHADOW_BIT 7
|
||||||
|
#define MPU6050_DELAYCTRL_I2C_SLV4_DLY_EN_BIT 4
|
||||||
|
#define MPU6050_DELAYCTRL_I2C_SLV3_DLY_EN_BIT 3
|
||||||
|
#define MPU6050_DELAYCTRL_I2C_SLV2_DLY_EN_BIT 2
|
||||||
|
#define MPU6050_DELAYCTRL_I2C_SLV1_DLY_EN_BIT 1
|
||||||
|
#define MPU6050_DELAYCTRL_I2C_SLV0_DLY_EN_BIT 0
|
||||||
|
|
||||||
|
#define MPU6050_PATHRESET_GYRO_RESET_BIT 2
|
||||||
|
#define MPU6050_PATHRESET_ACCEL_RESET_BIT 1
|
||||||
|
#define MPU6050_PATHRESET_TEMP_RESET_BIT 0
|
||||||
|
|
||||||
|
#define MPU6050_DETECT_ACCEL_ON_DELAY_BIT 5
|
||||||
|
#define MPU6050_DETECT_ACCEL_ON_DELAY_LENGTH 2
|
||||||
|
#define MPU6050_DETECT_FF_COUNT_BIT 3
|
||||||
|
#define MPU6050_DETECT_FF_COUNT_LENGTH 2
|
||||||
|
#define MPU6050_DETECT_MOT_COUNT_BIT 1
|
||||||
|
#define MPU6050_DETECT_MOT_COUNT_LENGTH 2
|
||||||
|
|
||||||
|
#define MPU6050_DETECT_DECREMENT_RESET 0x0
|
||||||
|
#define MPU6050_DETECT_DECREMENT_1 0x1
|
||||||
|
#define MPU6050_DETECT_DECREMENT_2 0x2
|
||||||
|
#define MPU6050_DETECT_DECREMENT_4 0x3
|
||||||
|
|
||||||
|
#define MPU6050_USERCTRL_DMP_EN_BIT 7
|
||||||
|
#define MPU6050_USERCTRL_FIFO_EN_BIT 6
|
||||||
|
#define MPU6050_USERCTRL_I2C_MST_EN_BIT 5
|
||||||
|
#define MPU6050_USERCTRL_I2C_IF_DIS_BIT 4
|
||||||
|
#define MPU6050_USERCTRL_DMP_RESET_BIT 3
|
||||||
|
#define MPU6050_USERCTRL_FIFO_RESET_BIT 2
|
||||||
|
#define MPU6050_USERCTRL_I2C_MST_RESET_BIT 1
|
||||||
|
#define MPU6050_USERCTRL_SIG_COND_RESET_BIT 0
|
||||||
|
|
||||||
|
#define MPU6050_PWR1_DEVICE_RESET_BIT 7
|
||||||
|
#define MPU6050_PWR1_SLEEP_BIT 6
|
||||||
|
#define MPU6050_PWR1_CYCLE_BIT 5
|
||||||
|
#define MPU6050_PWR1_TEMP_DIS_BIT 3
|
||||||
|
#define MPU6050_PWR1_CLKSEL_BIT 2
|
||||||
|
#define MPU6050_PWR1_CLKSEL_LENGTH 3
|
||||||
|
|
||||||
|
#define MPU6050_CLOCK_INTERNAL 0x00
|
||||||
|
#define MPU6050_CLOCK_PLL_XGYRO 0x01
|
||||||
|
#define MPU6050_CLOCK_PLL_YGYRO 0x02
|
||||||
|
#define MPU6050_CLOCK_PLL_ZGYRO 0x03
|
||||||
|
#define MPU6050_CLOCK_PLL_EXT32K 0x04
|
||||||
|
#define MPU6050_CLOCK_PLL_EXT19M 0x05
|
||||||
|
#define MPU6050_CLOCK_KEEP_RESET 0x07
|
||||||
|
|
||||||
|
#define MPU6050_PWR2_LP_WAKE_CTRL_BIT 7
|
||||||
|
#define MPU6050_PWR2_LP_WAKE_CTRL_LENGTH 2
|
||||||
|
#define MPU6050_PWR2_STBY_XA_BIT 5
|
||||||
|
#define MPU6050_PWR2_STBY_YA_BIT 4
|
||||||
|
#define MPU6050_PWR2_STBY_ZA_BIT 3
|
||||||
|
#define MPU6050_PWR2_STBY_XG_BIT 2
|
||||||
|
#define MPU6050_PWR2_STBY_YG_BIT 1
|
||||||
|
#define MPU6050_PWR2_STBY_ZG_BIT 0
|
||||||
|
|
||||||
|
#define MPU6050_WAKE_FREQ_1P25 0x0
|
||||||
|
#define MPU6050_WAKE_FREQ_2P5 0x1
|
||||||
|
#define MPU6050_WAKE_FREQ_5 0x2
|
||||||
|
#define MPU6050_WAKE_FREQ_10 0x3
|
||||||
|
|
||||||
|
#define MPU6050_BANKSEL_PRFTCH_EN_BIT 6
|
||||||
|
#define MPU6050_BANKSEL_CFG_USER_BANK_BIT 5
|
||||||
|
#define MPU6050_BANKSEL_MEM_SEL_BIT 4
|
||||||
|
#define MPU6050_BANKSEL_MEM_SEL_LENGTH 5
|
||||||
|
|
||||||
|
#define MPU6050_WHO_AM_I_BIT 6
|
||||||
|
#define MPU6050_WHO_AM_I_LENGTH 6
|
||||||
|
|
||||||
|
#define MPU6050_DMP_MEMORY_BANKS 8
|
||||||
|
#define MPU6050_DMP_MEMORY_BANK_SIZE 256
|
||||||
|
#define MPU6050_DMP_MEMORY_CHUNK_SIZE 16
|
||||||
|
|
||||||
|
// note: DMP code memory blocks defined at end of header file
|
||||||
|
|
||||||
|
class MPU6050 {
|
||||||
|
public:
|
||||||
|
MPU6050();
|
||||||
|
MPU6050(uint8_t address);
|
||||||
|
|
||||||
|
void initialize();
|
||||||
|
bool testConnection();
|
||||||
|
|
||||||
|
// AUX_VDDIO register
|
||||||
|
uint8_t getAuxVDDIOLevel();
|
||||||
|
void setAuxVDDIOLevel(uint8_t level);
|
||||||
|
|
||||||
|
// SMPLRT_DIV register
|
||||||
|
uint8_t getRate();
|
||||||
|
void setRate(uint8_t rate);
|
||||||
|
|
||||||
|
// CONFIG register
|
||||||
|
uint8_t getExternalFrameSync();
|
||||||
|
void setExternalFrameSync(uint8_t sync);
|
||||||
|
uint8_t getDLPFMode();
|
||||||
|
void setDLPFMode(uint8_t bandwidth);
|
||||||
|
|
||||||
|
// GYRO_CONFIG register
|
||||||
|
uint8_t getFullScaleGyroRange();
|
||||||
|
void setFullScaleGyroRange(uint8_t range);
|
||||||
|
|
||||||
|
// ACCEL_CONFIG register
|
||||||
|
bool getAccelXSelfTest();
|
||||||
|
void setAccelXSelfTest(bool enabled);
|
||||||
|
bool getAccelYSelfTest();
|
||||||
|
void setAccelYSelfTest(bool enabled);
|
||||||
|
bool getAccelZSelfTest();
|
||||||
|
void setAccelZSelfTest(bool enabled);
|
||||||
|
uint8_t getFullScaleAccelRange();
|
||||||
|
void setFullScaleAccelRange(uint8_t range);
|
||||||
|
uint8_t getDHPFMode();
|
||||||
|
void setDHPFMode(uint8_t mode);
|
||||||
|
|
||||||
|
// FF_THR register
|
||||||
|
uint8_t getFreefallDetectionThreshold();
|
||||||
|
void setFreefallDetectionThreshold(uint8_t threshold);
|
||||||
|
|
||||||
|
// FF_DUR register
|
||||||
|
uint8_t getFreefallDetectionDuration();
|
||||||
|
void setFreefallDetectionDuration(uint8_t duration);
|
||||||
|
|
||||||
|
// MOT_THR register
|
||||||
|
uint8_t getMotionDetectionThreshold();
|
||||||
|
void setMotionDetectionThreshold(uint8_t threshold);
|
||||||
|
|
||||||
|
// MOT_DUR register
|
||||||
|
uint8_t getMotionDetectionDuration();
|
||||||
|
void setMotionDetectionDuration(uint8_t duration);
|
||||||
|
|
||||||
|
// ZRMOT_THR register
|
||||||
|
uint8_t getZeroMotionDetectionThreshold();
|
||||||
|
void setZeroMotionDetectionThreshold(uint8_t threshold);
|
||||||
|
|
||||||
|
// ZRMOT_DUR register
|
||||||
|
uint8_t getZeroMotionDetectionDuration();
|
||||||
|
void setZeroMotionDetectionDuration(uint8_t duration);
|
||||||
|
|
||||||
|
// FIFO_EN register
|
||||||
|
bool getTempFIFOEnabled();
|
||||||
|
void setTempFIFOEnabled(bool enabled);
|
||||||
|
bool getXGyroFIFOEnabled();
|
||||||
|
void setXGyroFIFOEnabled(bool enabled);
|
||||||
|
bool getYGyroFIFOEnabled();
|
||||||
|
void setYGyroFIFOEnabled(bool enabled);
|
||||||
|
bool getZGyroFIFOEnabled();
|
||||||
|
void setZGyroFIFOEnabled(bool enabled);
|
||||||
|
bool getAccelFIFOEnabled();
|
||||||
|
void setAccelFIFOEnabled(bool enabled);
|
||||||
|
bool getSlave2FIFOEnabled();
|
||||||
|
void setSlave2FIFOEnabled(bool enabled);
|
||||||
|
bool getSlave1FIFOEnabled();
|
||||||
|
void setSlave1FIFOEnabled(bool enabled);
|
||||||
|
bool getSlave0FIFOEnabled();
|
||||||
|
void setSlave0FIFOEnabled(bool enabled);
|
||||||
|
|
||||||
|
// I2C_MST_CTRL register
|
||||||
|
bool getMultiMasterEnabled();
|
||||||
|
void setMultiMasterEnabled(bool enabled);
|
||||||
|
bool getWaitForExternalSensorEnabled();
|
||||||
|
void setWaitForExternalSensorEnabled(bool enabled);
|
||||||
|
bool getSlave3FIFOEnabled();
|
||||||
|
void setSlave3FIFOEnabled(bool enabled);
|
||||||
|
bool getSlaveReadWriteTransitionEnabled();
|
||||||
|
void setSlaveReadWriteTransitionEnabled(bool enabled);
|
||||||
|
uint8_t getMasterClockSpeed();
|
||||||
|
void setMasterClockSpeed(uint8_t speed);
|
||||||
|
|
||||||
|
// I2C_SLV* registers (Slave 0-3)
|
||||||
|
uint8_t getSlaveAddress(uint8_t num);
|
||||||
|
void setSlaveAddress(uint8_t num, uint8_t address);
|
||||||
|
uint8_t getSlaveRegister(uint8_t num);
|
||||||
|
void setSlaveRegister(uint8_t num, uint8_t reg);
|
||||||
|
bool getSlaveEnabled(uint8_t num);
|
||||||
|
void setSlaveEnabled(uint8_t num, bool enabled);
|
||||||
|
bool getSlaveWordByteSwap(uint8_t num);
|
||||||
|
void setSlaveWordByteSwap(uint8_t num, bool enabled);
|
||||||
|
bool getSlaveWriteMode(uint8_t num);
|
||||||
|
void setSlaveWriteMode(uint8_t num, bool mode);
|
||||||
|
bool getSlaveWordGroupOffset(uint8_t num);
|
||||||
|
void setSlaveWordGroupOffset(uint8_t num, bool enabled);
|
||||||
|
uint8_t getSlaveDataLength(uint8_t num);
|
||||||
|
void setSlaveDataLength(uint8_t num, uint8_t length);
|
||||||
|
|
||||||
|
// I2C_SLV* registers (Slave 4)
|
||||||
|
uint8_t getSlave4Address();
|
||||||
|
void setSlave4Address(uint8_t address);
|
||||||
|
uint8_t getSlave4Register();
|
||||||
|
void setSlave4Register(uint8_t reg);
|
||||||
|
void setSlave4OutputByte(uint8_t data);
|
||||||
|
bool getSlave4Enabled();
|
||||||
|
void setSlave4Enabled(bool enabled);
|
||||||
|
bool getSlave4InterruptEnabled();
|
||||||
|
void setSlave4InterruptEnabled(bool enabled);
|
||||||
|
bool getSlave4WriteMode();
|
||||||
|
void setSlave4WriteMode(bool mode);
|
||||||
|
uint8_t getSlave4MasterDelay();
|
||||||
|
void setSlave4MasterDelay(uint8_t delay);
|
||||||
|
uint8_t getSlate4InputByte();
|
||||||
|
|
||||||
|
// I2C_MST_STATUS register
|
||||||
|
bool getPassthroughStatus();
|
||||||
|
bool getSlave4IsDone();
|
||||||
|
bool getLostArbitration();
|
||||||
|
bool getSlave4Nack();
|
||||||
|
bool getSlave3Nack();
|
||||||
|
bool getSlave2Nack();
|
||||||
|
bool getSlave1Nack();
|
||||||
|
bool getSlave0Nack();
|
||||||
|
|
||||||
|
// INT_PIN_CFG register
|
||||||
|
bool getInterruptMode();
|
||||||
|
void setInterruptMode(bool mode);
|
||||||
|
bool getInterruptDrive();
|
||||||
|
void setInterruptDrive(bool drive);
|
||||||
|
bool getInterruptLatch();
|
||||||
|
void setInterruptLatch(bool latch);
|
||||||
|
bool getInterruptLatchClear();
|
||||||
|
void setInterruptLatchClear(bool clear);
|
||||||
|
bool getFSyncInterruptLevel();
|
||||||
|
void setFSyncInterruptLevel(bool level);
|
||||||
|
bool getFSyncInterruptEnabled();
|
||||||
|
void setFSyncInterruptEnabled(bool enabled);
|
||||||
|
bool getI2CBypassEnabled();
|
||||||
|
void setI2CBypassEnabled(bool enabled);
|
||||||
|
bool getClockOutputEnabled();
|
||||||
|
void setClockOutputEnabled(bool enabled);
|
||||||
|
|
||||||
|
// INT_ENABLE register
|
||||||
|
uint8_t getIntEnabled();
|
||||||
|
void setIntEnabled(uint8_t enabled);
|
||||||
|
bool getIntFreefallEnabled();
|
||||||
|
void setIntFreefallEnabled(bool enabled);
|
||||||
|
bool getIntMotionEnabled();
|
||||||
|
void setIntMotionEnabled(bool enabled);
|
||||||
|
bool getIntZeroMotionEnabled();
|
||||||
|
void setIntZeroMotionEnabled(bool enabled);
|
||||||
|
bool getIntFIFOBufferOverflowEnabled();
|
||||||
|
void setIntFIFOBufferOverflowEnabled(bool enabled);
|
||||||
|
bool getIntI2CMasterEnabled();
|
||||||
|
void setIntI2CMasterEnabled(bool enabled);
|
||||||
|
bool getIntDataReadyEnabled();
|
||||||
|
void setIntDataReadyEnabled(bool enabled);
|
||||||
|
|
||||||
|
// INT_STATUS register
|
||||||
|
uint8_t getIntStatus();
|
||||||
|
bool getIntFreefallStatus();
|
||||||
|
bool getIntMotionStatus();
|
||||||
|
bool getIntZeroMotionStatus();
|
||||||
|
bool getIntFIFOBufferOverflowStatus();
|
||||||
|
bool getIntI2CMasterStatus();
|
||||||
|
bool getIntDataReadyStatus();
|
||||||
|
|
||||||
|
// ACCEL_*OUT_* registers
|
||||||
|
void getMotion9(int16_t* ax, int16_t* ay, int16_t* az, int16_t* gx, int16_t* gy, int16_t* gz, int16_t* mx, int16_t* my, int16_t* mz);
|
||||||
|
void getMotion6(int16_t* ax, int16_t* ay, int16_t* az, int16_t* gx, int16_t* gy, int16_t* gz);
|
||||||
|
void getAcceleration(int16_t* x, int16_t* y, int16_t* z);
|
||||||
|
int16_t getAccelerationX();
|
||||||
|
int16_t getAccelerationY();
|
||||||
|
int16_t getAccelerationZ();
|
||||||
|
|
||||||
|
// TEMP_OUT_* registers
|
||||||
|
int16_t getTemperature();
|
||||||
|
|
||||||
|
// GYRO_*OUT_* registers
|
||||||
|
void getRotation(int16_t* x, int16_t* y, int16_t* z);
|
||||||
|
int16_t getRotationX();
|
||||||
|
int16_t getRotationY();
|
||||||
|
int16_t getRotationZ();
|
||||||
|
|
||||||
|
// EXT_SENS_DATA_* registers
|
||||||
|
uint8_t getExternalSensorByte(int position);
|
||||||
|
uint16_t getExternalSensorWord(int position);
|
||||||
|
uint32_t getExternalSensorDWord(int position);
|
||||||
|
|
||||||
|
// MOT_DETECT_STATUS register
|
||||||
|
bool getXNegMotionDetected();
|
||||||
|
bool getXPosMotionDetected();
|
||||||
|
bool getYNegMotionDetected();
|
||||||
|
bool getYPosMotionDetected();
|
||||||
|
bool getZNegMotionDetected();
|
||||||
|
bool getZPosMotionDetected();
|
||||||
|
bool getZeroMotionDetected();
|
||||||
|
|
||||||
|
// I2C_SLV*_DO register
|
||||||
|
void setSlaveOutputByte(uint8_t num, uint8_t data);
|
||||||
|
|
||||||
|
// I2C_MST_DELAY_CTRL register
|
||||||
|
bool getExternalShadowDelayEnabled();
|
||||||
|
void setExternalShadowDelayEnabled(bool enabled);
|
||||||
|
bool getSlaveDelayEnabled(uint8_t num);
|
||||||
|
void setSlaveDelayEnabled(uint8_t num, bool enabled);
|
||||||
|
|
||||||
|
// SIGNAL_PATH_RESET register
|
||||||
|
void resetGyroscopePath();
|
||||||
|
void resetAccelerometerPath();
|
||||||
|
void resetTemperaturePath();
|
||||||
|
|
||||||
|
// MOT_DETECT_CTRL register
|
||||||
|
uint8_t getAccelerometerPowerOnDelay();
|
||||||
|
void setAccelerometerPowerOnDelay(uint8_t delay);
|
||||||
|
uint8_t getFreefallDetectionCounterDecrement();
|
||||||
|
void setFreefallDetectionCounterDecrement(uint8_t decrement);
|
||||||
|
uint8_t getMotionDetectionCounterDecrement();
|
||||||
|
void setMotionDetectionCounterDecrement(uint8_t decrement);
|
||||||
|
|
||||||
|
// USER_CTRL register
|
||||||
|
bool getFIFOEnabled();
|
||||||
|
void setFIFOEnabled(bool enabled);
|
||||||
|
bool getI2CMasterModeEnabled();
|
||||||
|
void setI2CMasterModeEnabled(bool enabled);
|
||||||
|
void switchSPIEnabled(bool enabled);
|
||||||
|
void resetFIFO();
|
||||||
|
void resetI2CMaster();
|
||||||
|
void resetSensors();
|
||||||
|
|
||||||
|
// PWR_MGMT_1 register
|
||||||
|
void reset();
|
||||||
|
bool getSleepEnabled();
|
||||||
|
void setSleepEnabled(bool enabled);
|
||||||
|
bool getWakeCycleEnabled();
|
||||||
|
void setWakeCycleEnabled(bool enabled);
|
||||||
|
bool getTempSensorEnabled();
|
||||||
|
void setTempSensorEnabled(bool enabled);
|
||||||
|
uint8_t getClockSource();
|
||||||
|
void setClockSource(uint8_t source);
|
||||||
|
|
||||||
|
// PWR_MGMT_2 register
|
||||||
|
uint8_t getWakeFrequency();
|
||||||
|
void setWakeFrequency(uint8_t frequency);
|
||||||
|
bool getStandbyXAccelEnabled();
|
||||||
|
void setStandbyXAccelEnabled(bool enabled);
|
||||||
|
bool getStandbyYAccelEnabled();
|
||||||
|
void setStandbyYAccelEnabled(bool enabled);
|
||||||
|
bool getStandbyZAccelEnabled();
|
||||||
|
void setStandbyZAccelEnabled(bool enabled);
|
||||||
|
bool getStandbyXGyroEnabled();
|
||||||
|
void setStandbyXGyroEnabled(bool enabled);
|
||||||
|
bool getStandbyYGyroEnabled();
|
||||||
|
void setStandbyYGyroEnabled(bool enabled);
|
||||||
|
bool getStandbyZGyroEnabled();
|
||||||
|
void setStandbyZGyroEnabled(bool enabled);
|
||||||
|
|
||||||
|
// FIFO_COUNT_* registers
|
||||||
|
uint16_t getFIFOCount();
|
||||||
|
|
||||||
|
// FIFO_R_W register
|
||||||
|
uint8_t getFIFOByte();
|
||||||
|
void setFIFOByte(uint8_t data);
|
||||||
|
void getFIFOBytes(uint8_t *data, uint8_t length);
|
||||||
|
|
||||||
|
// WHO_AM_I register
|
||||||
|
uint8_t getDeviceID();
|
||||||
|
void setDeviceID(uint8_t id);
|
||||||
|
|
||||||
|
// ======== UNDOCUMENTED/DMP REGISTERS/METHODS ========
|
||||||
|
|
||||||
|
// XG_OFFS_TC register
|
||||||
|
uint8_t getOTPBankValid();
|
||||||
|
void setOTPBankValid(bool enabled);
|
||||||
|
int8_t getXGyroOffset();
|
||||||
|
void setXGyroOffset(int8_t offset);
|
||||||
|
|
||||||
|
// YG_OFFS_TC register
|
||||||
|
int8_t getYGyroOffset();
|
||||||
|
void setYGyroOffset(int8_t offset);
|
||||||
|
|
||||||
|
// ZG_OFFS_TC register
|
||||||
|
int8_t getZGyroOffset();
|
||||||
|
void setZGyroOffset(int8_t offset);
|
||||||
|
|
||||||
|
// X_FINE_GAIN register
|
||||||
|
int8_t getXFineGain();
|
||||||
|
void setXFineGain(int8_t gain);
|
||||||
|
|
||||||
|
// Y_FINE_GAIN register
|
||||||
|
int8_t getYFineGain();
|
||||||
|
void setYFineGain(int8_t gain);
|
||||||
|
|
||||||
|
// Z_FINE_GAIN register
|
||||||
|
int8_t getZFineGain();
|
||||||
|
void setZFineGain(int8_t gain);
|
||||||
|
|
||||||
|
// XA_OFFS_* registers
|
||||||
|
int16_t getXAccelOffset();
|
||||||
|
void setXAccelOffset(int16_t offset);
|
||||||
|
|
||||||
|
// YA_OFFS_* register
|
||||||
|
int16_t getYAccelOffset();
|
||||||
|
void setYAccelOffset(int16_t offset);
|
||||||
|
|
||||||
|
// ZA_OFFS_* register
|
||||||
|
int16_t getZAccelOffset();
|
||||||
|
void setZAccelOffset(int16_t offset);
|
||||||
|
|
||||||
|
// XG_OFFS_USR* registers
|
||||||
|
int16_t getXGyroOffsetUser();
|
||||||
|
void setXGyroOffsetUser(int16_t offset);
|
||||||
|
|
||||||
|
// YG_OFFS_USR* register
|
||||||
|
int16_t getYGyroOffsetUser();
|
||||||
|
void setYGyroOffsetUser(int16_t offset);
|
||||||
|
|
||||||
|
// ZG_OFFS_USR* register
|
||||||
|
int16_t getZGyroOffsetUser();
|
||||||
|
void setZGyroOffsetUser(int16_t offset);
|
||||||
|
|
||||||
|
// INT_ENABLE register (DMP functions)
|
||||||
|
bool getIntPLLReadyEnabled();
|
||||||
|
void setIntPLLReadyEnabled(bool enabled);
|
||||||
|
bool getIntDMPEnabled();
|
||||||
|
void setIntDMPEnabled(bool enabled);
|
||||||
|
|
||||||
|
// DMP_INT_STATUS
|
||||||
|
bool getDMPInt5Status();
|
||||||
|
bool getDMPInt4Status();
|
||||||
|
bool getDMPInt3Status();
|
||||||
|
bool getDMPInt2Status();
|
||||||
|
bool getDMPInt1Status();
|
||||||
|
bool getDMPInt0Status();
|
||||||
|
|
||||||
|
// INT_STATUS register (DMP functions)
|
||||||
|
bool getIntPLLReadyStatus();
|
||||||
|
bool getIntDMPStatus();
|
||||||
|
|
||||||
|
// USER_CTRL register (DMP functions)
|
||||||
|
bool getDMPEnabled();
|
||||||
|
void setDMPEnabled(bool enabled);
|
||||||
|
void resetDMP();
|
||||||
|
|
||||||
|
// BANK_SEL register
|
||||||
|
void setMemoryBank(uint8_t bank, bool prefetchEnabled=false, bool userBank=false);
|
||||||
|
|
||||||
|
// MEM_START_ADDR register
|
||||||
|
void setMemoryStartAddress(uint8_t address);
|
||||||
|
|
||||||
|
// MEM_R_W register
|
||||||
|
uint8_t readMemoryByte();
|
||||||
|
void writeMemoryByte(uint8_t data);
|
||||||
|
void readMemoryBlock(uint8_t *data, uint16_t dataSize, uint8_t bank=0, uint8_t address=0);
|
||||||
|
bool writeMemoryBlock(const uint8_t *data, uint16_t dataSize, uint8_t bank=0, uint8_t address=0, bool verify=true, bool useProgMem=false);
|
||||||
|
bool writeProgMemoryBlock(const uint8_t *data, uint16_t dataSize, uint8_t bank=0, uint8_t address=0, bool verify=true);
|
||||||
|
|
||||||
|
bool writeDMPConfigurationSet(const uint8_t *data, uint16_t dataSize, bool useProgMem=false);
|
||||||
|
bool writeProgDMPConfigurationSet(const uint8_t *data, uint16_t dataSize);
|
||||||
|
|
||||||
|
// DMP_CFG_1 register
|
||||||
|
uint8_t getDMPConfig1();
|
||||||
|
void setDMPConfig1(uint8_t config);
|
||||||
|
|
||||||
|
// DMP_CFG_2 register
|
||||||
|
uint8_t getDMPConfig2();
|
||||||
|
void setDMPConfig2(uint8_t config);
|
||||||
|
|
||||||
|
// special methods for MotionApps 2.0 implementation
|
||||||
|
#ifdef MPU6050_INCLUDE_DMP_MOTIONAPPS20
|
||||||
|
uint8_t *dmpPacketBuffer;
|
||||||
|
uint16_t dmpPacketSize;
|
||||||
|
|
||||||
|
uint8_t dmpInitialize();
|
||||||
|
bool dmpPacketAvailable();
|
||||||
|
|
||||||
|
uint8_t dmpSetFIFORate(uint8_t fifoRate);
|
||||||
|
uint8_t dmpGetFIFORate();
|
||||||
|
uint8_t dmpGetSampleStepSizeMS();
|
||||||
|
uint8_t dmpGetSampleFrequency();
|
||||||
|
int32_t dmpDecodeTemperature(int8_t tempReg);
|
||||||
|
|
||||||
|
// Register callbacks after a packet of FIFO data is processed
|
||||||
|
//uint8_t dmpRegisterFIFORateProcess(inv_obj_func func, int16_t priority);
|
||||||
|
//uint8_t dmpUnregisterFIFORateProcess(inv_obj_func func);
|
||||||
|
uint8_t dmpRunFIFORateProcesses();
|
||||||
|
|
||||||
|
// Setup FIFO for various output
|
||||||
|
uint8_t dmpSendQuaternion(uint_fast16_t accuracy);
|
||||||
|
uint8_t dmpSendGyro(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
uint8_t dmpSendAccel(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
uint8_t dmpSendLinearAccel(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
uint8_t dmpSendLinearAccelInWorld(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
uint8_t dmpSendControlData(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
uint8_t dmpSendSensorData(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
uint8_t dmpSendExternalSensorData(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
uint8_t dmpSendGravity(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
uint8_t dmpSendPacketNumber(uint_fast16_t accuracy);
|
||||||
|
uint8_t dmpSendQuantizedAccel(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
uint8_t dmpSendEIS(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
|
||||||
|
// Get Fixed Point data from FIFO
|
||||||
|
uint8_t dmpGetAccel(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetAccel(int16_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetAccel(VectorInt16 *v, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetQuaternion(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetQuaternion(int16_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetQuaternion(Quaternion *q, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGet6AxisQuaternion(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGet6AxisQuaternion(int16_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGet6AxisQuaternion(Quaternion *q, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetRelativeQuaternion(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetRelativeQuaternion(int16_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetRelativeQuaternion(Quaternion *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetGyro(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetGyro(int16_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetGyro(VectorInt16 *v, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpSetLinearAccelFilterCoefficient(float coef);
|
||||||
|
uint8_t dmpGetLinearAccel(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetLinearAccel(int16_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetLinearAccel(VectorInt16 *v, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetLinearAccel(VectorInt16 *v, VectorInt16 *vRaw, VectorFloat *gravity);
|
||||||
|
uint8_t dmpGetLinearAccelInWorld(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetLinearAccelInWorld(int16_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetLinearAccelInWorld(VectorInt16 *v, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetLinearAccelInWorld(VectorInt16 *v, VectorInt16 *vReal, Quaternion *q);
|
||||||
|
uint8_t dmpGetGyroAndAccelSensor(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetGyroAndAccelSensor(int16_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetGyroAndAccelSensor(VectorInt16 *g, VectorInt16 *a, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetGyroSensor(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetGyroSensor(int16_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetGyroSensor(VectorInt16 *v, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetControlData(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetTemperature(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetGravity(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetGravity(int16_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetGravity(VectorInt16 *v, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetGravity(VectorFloat *v, Quaternion *q);
|
||||||
|
uint8_t dmpGetUnquantizedAccel(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetUnquantizedAccel(int16_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetUnquantizedAccel(VectorInt16 *v, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetQuantizedAccel(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetQuantizedAccel(int16_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetQuantizedAccel(VectorInt16 *v, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetExternalSensorData(int32_t *data, uint16_t size, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetEIS(int32_t *data, const uint8_t* packet=0);
|
||||||
|
|
||||||
|
uint8_t dmpGetEuler(float *data, Quaternion *q);
|
||||||
|
uint8_t dmpGetYawPitchRoll(float *data, Quaternion *q, VectorFloat *gravity);
|
||||||
|
|
||||||
|
// Get Floating Point data from FIFO
|
||||||
|
uint8_t dmpGetAccelFloat(float *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetQuaternionFloat(float *data, const uint8_t* packet=0);
|
||||||
|
|
||||||
|
uint8_t dmpProcessFIFOPacket(const unsigned char *dmpData);
|
||||||
|
uint8_t dmpReadAndProcessFIFOPacket(uint8_t numPackets, uint8_t *processed=NULL);
|
||||||
|
|
||||||
|
uint8_t dmpSetFIFOProcessedCallback(void (*func) (void));
|
||||||
|
|
||||||
|
uint8_t dmpInitFIFOParam();
|
||||||
|
uint8_t dmpCloseFIFO();
|
||||||
|
uint8_t dmpSetGyroDataSource(uint8_t source);
|
||||||
|
uint8_t dmpDecodeQuantizedAccel();
|
||||||
|
uint32_t dmpGetGyroSumOfSquare();
|
||||||
|
uint32_t dmpGetAccelSumOfSquare();
|
||||||
|
void dmpOverrideQuaternion(long *q);
|
||||||
|
uint16_t dmpGetFIFOPacketSize();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// special methods for MotionApps 4.1 implementation
|
||||||
|
#ifdef MPU6050_INCLUDE_DMP_MOTIONAPPS41
|
||||||
|
uint8_t *dmpPacketBuffer;
|
||||||
|
uint16_t dmpPacketSize;
|
||||||
|
|
||||||
|
uint8_t dmpInitialize();
|
||||||
|
bool dmpPacketAvailable();
|
||||||
|
|
||||||
|
uint8_t dmpSetFIFORate(uint8_t fifoRate);
|
||||||
|
uint8_t dmpGetFIFORate();
|
||||||
|
uint8_t dmpGetSampleStepSizeMS();
|
||||||
|
uint8_t dmpGetSampleFrequency();
|
||||||
|
int32_t dmpDecodeTemperature(int8_t tempReg);
|
||||||
|
|
||||||
|
// Register callbacks after a packet of FIFO data is processed
|
||||||
|
//uint8_t dmpRegisterFIFORateProcess(inv_obj_func func, int16_t priority);
|
||||||
|
//uint8_t dmpUnregisterFIFORateProcess(inv_obj_func func);
|
||||||
|
uint8_t dmpRunFIFORateProcesses();
|
||||||
|
|
||||||
|
// Setup FIFO for various output
|
||||||
|
uint8_t dmpSendQuaternion(uint_fast16_t accuracy);
|
||||||
|
uint8_t dmpSendGyro(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
uint8_t dmpSendAccel(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
uint8_t dmpSendLinearAccel(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
uint8_t dmpSendLinearAccelInWorld(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
uint8_t dmpSendControlData(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
uint8_t dmpSendSensorData(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
uint8_t dmpSendExternalSensorData(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
uint8_t dmpSendGravity(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
uint8_t dmpSendPacketNumber(uint_fast16_t accuracy);
|
||||||
|
uint8_t dmpSendQuantizedAccel(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
uint8_t dmpSendEIS(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
|
||||||
|
// Get Fixed Point data from FIFO
|
||||||
|
uint8_t dmpGetAccel(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetAccel(int16_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetAccel(VectorInt16 *v, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetQuaternion(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetQuaternion(int16_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetQuaternion(Quaternion *q, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGet6AxisQuaternion(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGet6AxisQuaternion(int16_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGet6AxisQuaternion(Quaternion *q, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetRelativeQuaternion(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetRelativeQuaternion(int16_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetRelativeQuaternion(Quaternion *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetGyro(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetGyro(int16_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetGyro(VectorInt16 *v, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetMag(int16_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpSetLinearAccelFilterCoefficient(float coef);
|
||||||
|
uint8_t dmpGetLinearAccel(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetLinearAccel(int16_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetLinearAccel(VectorInt16 *v, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetLinearAccel(VectorInt16 *v, VectorInt16 *vRaw, VectorFloat *gravity);
|
||||||
|
uint8_t dmpGetLinearAccelInWorld(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetLinearAccelInWorld(int16_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetLinearAccelInWorld(VectorInt16 *v, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetLinearAccelInWorld(VectorInt16 *v, VectorInt16 *vReal, Quaternion *q);
|
||||||
|
uint8_t dmpGetGyroAndAccelSensor(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetGyroAndAccelSensor(int16_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetGyroAndAccelSensor(VectorInt16 *g, VectorInt16 *a, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetGyroSensor(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetGyroSensor(int16_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetGyroSensor(VectorInt16 *v, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetControlData(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetTemperature(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetGravity(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetGravity(int16_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetGravity(VectorInt16 *v, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetGravity(VectorFloat *v, Quaternion *q);
|
||||||
|
uint8_t dmpGetUnquantizedAccel(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetUnquantizedAccel(int16_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetUnquantizedAccel(VectorInt16 *v, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetQuantizedAccel(int32_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetQuantizedAccel(int16_t *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetQuantizedAccel(VectorInt16 *v, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetExternalSensorData(int32_t *data, uint16_t size, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetEIS(int32_t *data, const uint8_t* packet=0);
|
||||||
|
|
||||||
|
uint8_t dmpGetEuler(float *data, Quaternion *q);
|
||||||
|
uint8_t dmpGetYawPitchRoll(float *data, Quaternion *q, VectorFloat *gravity);
|
||||||
|
|
||||||
|
// Get Floating Point data from FIFO
|
||||||
|
uint8_t dmpGetAccelFloat(float *data, const uint8_t* packet=0);
|
||||||
|
uint8_t dmpGetQuaternionFloat(float *data, const uint8_t* packet=0);
|
||||||
|
|
||||||
|
uint8_t dmpProcessFIFOPacket(const unsigned char *dmpData);
|
||||||
|
uint8_t dmpReadAndProcessFIFOPacket(uint8_t numPackets, uint8_t *processed=NULL);
|
||||||
|
|
||||||
|
uint8_t dmpSetFIFOProcessedCallback(void (*func) (void));
|
||||||
|
|
||||||
|
uint8_t dmpInitFIFOParam();
|
||||||
|
uint8_t dmpCloseFIFO();
|
||||||
|
uint8_t dmpSetGyroDataSource(uint8_t source);
|
||||||
|
uint8_t dmpDecodeQuantizedAccel();
|
||||||
|
uint32_t dmpGetGyroSumOfSquare();
|
||||||
|
uint32_t dmpGetAccelSumOfSquare();
|
||||||
|
void dmpOverrideQuaternion(long *q);
|
||||||
|
uint16_t dmpGetFIFOPacketSize();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
private:
|
||||||
|
uint8_t devAddr;
|
||||||
|
uint8_t buffer[14];
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* _MPU6050_H_ */
|
709
IMU/MPU6050_6Axis_MotionApps20.h
Normal file
709
IMU/MPU6050_6Axis_MotionApps20.h
Normal file
|
@ -0,0 +1,709 @@
|
||||||
|
// I2Cdev library collection - MPU6050 I2C device class, 6-axis MotionApps 2.0 implementation
|
||||||
|
// Based on InvenSense MPU-6050 register map document rev. 2.0, 5/19/2011 (RM-MPU-6000A-00)
|
||||||
|
// 6/18/2012 by Jeff Rowberg <jeff@rowberg.net>
|
||||||
|
// Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
|
||||||
|
//
|
||||||
|
// Changelog:
|
||||||
|
// ... - ongoing debug release
|
||||||
|
|
||||||
|
/* ============================================
|
||||||
|
I2Cdev device library code is placed under the MIT license
|
||||||
|
Copyright (c) 2012 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.
|
||||||
|
===============================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _MPU6050_6AXIS_MOTIONAPPS20_H_
|
||||||
|
#define _MPU6050_6AXIS_MOTIONAPPS20_H_
|
||||||
|
|
||||||
|
#include "I2Cdev.h"
|
||||||
|
#include "helper_3dmath.h"
|
||||||
|
|
||||||
|
// MotionApps 2.0 DMP implementation, built using the MPU-6050EVB evaluation board
|
||||||
|
#define MPU6050_INCLUDE_DMP_MOTIONAPPS20
|
||||||
|
|
||||||
|
#include "MPU6050.h"
|
||||||
|
//#include <avr/pgmspace.h>
|
||||||
|
|
||||||
|
/* Source is from the InvenSense MotionApps v2 demo code. Original source is
|
||||||
|
* unavailable, unless you happen to be amazing as decompiling binary by
|
||||||
|
* hand (in which case, please contact me, and I'm totally serious).
|
||||||
|
*
|
||||||
|
* Also, I'd like to offer many, many thanks to Noah Zerkin for all of the
|
||||||
|
* DMP reverse-engineering he did to help make this bit of wizardry
|
||||||
|
* possible.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// NOTE! Enabling DEBUG adds about 3.3kB to the flash program size.
|
||||||
|
// Debug output is now working even on ATMega328P MCUs (e.g. Arduino Uno)
|
||||||
|
// after moving string constants to flash memory storage using the F()
|
||||||
|
// compiler macro (Arduino IDE 1.0+ required).
|
||||||
|
|
||||||
|
//#define DEBUG
|
||||||
|
#ifdef DEBUG
|
||||||
|
#define DEBUG_PRINT(x) Serial.print(x)
|
||||||
|
#define DEBUG_PRINTF(x, y) Serial.print(x, y)
|
||||||
|
#define DEBUG_PRINTLN(x) Serial.println(x)
|
||||||
|
#define DEBUG_PRINTLNF(x, y) Serial.println(x, y)
|
||||||
|
#else
|
||||||
|
#define DEBUG_PRINT(x)
|
||||||
|
#define DEBUG_PRINTF(x, y)
|
||||||
|
#define DEBUG_PRINTLN(x)
|
||||||
|
#define DEBUG_PRINTLNF(x, y)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define MPU6050_DMP_CODE_SIZE 1929 // dmpMemory[]
|
||||||
|
#define MPU6050_DMP_CONFIG_SIZE 192 // dmpConfig[]
|
||||||
|
#define MPU6050_DMP_UPDATES_SIZE 47 // dmpUpdates[]
|
||||||
|
|
||||||
|
/* ================================================================================================ *
|
||||||
|
| Default MotionApps v2.0 42-byte FIFO packet structure: |
|
||||||
|
| |
|
||||||
|
| [QUAT W][ ][QUAT X][ ][QUAT Y][ ][QUAT Z][ ][GYRO X][ ][GYRO Y][ ] |
|
||||||
|
| 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
||||||
|
| |
|
||||||
|
| [GYRO Z][ ][ACC X ][ ][ACC Y ][ ][ACC Z ][ ][ ] |
|
||||||
|
| 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
||||||
|
* ================================================================================================ */
|
||||||
|
|
||||||
|
#define prog_uchar uint8_t
|
||||||
|
#define PROGMEM
|
||||||
|
|
||||||
|
|
||||||
|
// this block of memory gets written to the MPU on start-up, and it seems
|
||||||
|
// to be volatile memory, so it has to be done each time (it only takes ~1
|
||||||
|
// second though)
|
||||||
|
const prog_uchar dmpMemory[MPU6050_DMP_CODE_SIZE] PROGMEM = {
|
||||||
|
// bank 0, 256 bytes
|
||||||
|
0xFB, 0x00, 0x00, 0x3E, 0x00, 0x0B, 0x00, 0x36, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00,
|
||||||
|
0x00, 0x65, 0x00, 0x54, 0xFF, 0xEF, 0x00, 0x00, 0xFA, 0x80, 0x00, 0x0B, 0x12, 0x82, 0x00, 0x01,
|
||||||
|
0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x28, 0x00, 0x00, 0xFF, 0xFF, 0x45, 0x81, 0xFF, 0xFF, 0xFA, 0x72, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x03, 0xE8, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x7F, 0xFF, 0xFF, 0xFE, 0x80, 0x01,
|
||||||
|
0x00, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x3E, 0x03, 0x30, 0x40, 0x00, 0x00, 0x00, 0x02, 0xCA, 0xE3, 0x09, 0x3E, 0x80, 0x00, 0x00,
|
||||||
|
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00,
|
||||||
|
0x41, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x0B, 0x2A, 0x00, 0x00, 0x16, 0x55, 0x00, 0x00, 0x21, 0x82,
|
||||||
|
0xFD, 0x87, 0x26, 0x50, 0xFD, 0x80, 0x00, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x05, 0x80, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00,
|
||||||
|
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x6F, 0x00, 0x02, 0x65, 0x32, 0x00, 0x00, 0x5E, 0xC0,
|
||||||
|
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0xFB, 0x8C, 0x6F, 0x5D, 0xFD, 0x5D, 0x08, 0xD9, 0x00, 0x7C, 0x73, 0x3B, 0x00, 0x6C, 0x12, 0xCC,
|
||||||
|
0x32, 0x00, 0x13, 0x9D, 0x32, 0x00, 0xD0, 0xD6, 0x32, 0x00, 0x08, 0x00, 0x40, 0x00, 0x01, 0xF4,
|
||||||
|
0xFF, 0xE6, 0x80, 0x79, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xD0, 0xD6, 0x00, 0x00, 0x27, 0x10,
|
||||||
|
|
||||||
|
// bank 1, 256 bytes
|
||||||
|
0xFB, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0xFA, 0x36, 0xFF, 0xBC, 0x30, 0x8E, 0x00, 0x05, 0xFB, 0xF0, 0xFF, 0xD9, 0x5B, 0xC8,
|
||||||
|
0xFF, 0xD0, 0x9A, 0xBE, 0x00, 0x00, 0x10, 0xA9, 0xFF, 0xF4, 0x1E, 0xB2, 0x00, 0xCE, 0xBB, 0xF7,
|
||||||
|
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x02, 0x02, 0x00, 0x00, 0x0C,
|
||||||
|
0xFF, 0xC2, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0xCF, 0x80, 0x00, 0x40, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x14,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x03, 0x3F, 0x68, 0xB6, 0x79, 0x35, 0x28, 0xBC, 0xC6, 0x7E, 0xD1, 0x6C,
|
||||||
|
0x80, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0xB2, 0x6A, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0xF0, 0x00, 0x00, 0x00, 0x30,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x25, 0x4D, 0x00, 0x2F, 0x70, 0x6D, 0x00, 0x00, 0x05, 0xAE, 0x00, 0x0C, 0x02, 0xD0,
|
||||||
|
|
||||||
|
// bank 2, 256 bytes
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x65, 0x00, 0x54, 0xFF, 0xEF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x01, 0x00, 0x00, 0x44, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x01, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x54, 0x00, 0x00, 0xFF, 0xEF, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
|
||||||
|
// bank 3, 256 bytes
|
||||||
|
0xD8, 0xDC, 0xBA, 0xA2, 0xF1, 0xDE, 0xB2, 0xB8, 0xB4, 0xA8, 0x81, 0x91, 0xF7, 0x4A, 0x90, 0x7F,
|
||||||
|
0x91, 0x6A, 0xF3, 0xF9, 0xDB, 0xA8, 0xF9, 0xB0, 0xBA, 0xA0, 0x80, 0xF2, 0xCE, 0x81, 0xF3, 0xC2,
|
||||||
|
0xF1, 0xC1, 0xF2, 0xC3, 0xF3, 0xCC, 0xA2, 0xB2, 0x80, 0xF1, 0xC6, 0xD8, 0x80, 0xBA, 0xA7, 0xDF,
|
||||||
|
0xDF, 0xDF, 0xF2, 0xA7, 0xC3, 0xCB, 0xC5, 0xB6, 0xF0, 0x87, 0xA2, 0x94, 0x24, 0x48, 0x70, 0x3C,
|
||||||
|
0x95, 0x40, 0x68, 0x34, 0x58, 0x9B, 0x78, 0xA2, 0xF1, 0x83, 0x92, 0x2D, 0x55, 0x7D, 0xD8, 0xB1,
|
||||||
|
0xB4, 0xB8, 0xA1, 0xD0, 0x91, 0x80, 0xF2, 0x70, 0xF3, 0x70, 0xF2, 0x7C, 0x80, 0xA8, 0xF1, 0x01,
|
||||||
|
0xB0, 0x98, 0x87, 0xD9, 0x43, 0xD8, 0x86, 0xC9, 0x88, 0xBA, 0xA1, 0xF2, 0x0E, 0xB8, 0x97, 0x80,
|
||||||
|
0xF1, 0xA9, 0xDF, 0xDF, 0xDF, 0xAA, 0xDF, 0xDF, 0xDF, 0xF2, 0xAA, 0xC5, 0xCD, 0xC7, 0xA9, 0x0C,
|
||||||
|
0xC9, 0x2C, 0x97, 0x97, 0x97, 0x97, 0xF1, 0xA9, 0x89, 0x26, 0x46, 0x66, 0xB0, 0xB4, 0xBA, 0x80,
|
||||||
|
0xAC, 0xDE, 0xF2, 0xCA, 0xF1, 0xB2, 0x8C, 0x02, 0xA9, 0xB6, 0x98, 0x00, 0x89, 0x0E, 0x16, 0x1E,
|
||||||
|
0xB8, 0xA9, 0xB4, 0x99, 0x2C, 0x54, 0x7C, 0xB0, 0x8A, 0xA8, 0x96, 0x36, 0x56, 0x76, 0xF1, 0xB9,
|
||||||
|
0xAF, 0xB4, 0xB0, 0x83, 0xC0, 0xB8, 0xA8, 0x97, 0x11, 0xB1, 0x8F, 0x98, 0xB9, 0xAF, 0xF0, 0x24,
|
||||||
|
0x08, 0x44, 0x10, 0x64, 0x18, 0xF1, 0xA3, 0x29, 0x55, 0x7D, 0xAF, 0x83, 0xB5, 0x93, 0xAF, 0xF0,
|
||||||
|
0x00, 0x28, 0x50, 0xF1, 0xA3, 0x86, 0x9F, 0x61, 0xA6, 0xDA, 0xDE, 0xDF, 0xD9, 0xFA, 0xA3, 0x86,
|
||||||
|
0x96, 0xDB, 0x31, 0xA6, 0xD9, 0xF8, 0xDF, 0xBA, 0xA6, 0x8F, 0xC2, 0xC5, 0xC7, 0xB2, 0x8C, 0xC1,
|
||||||
|
0xB8, 0xA2, 0xDF, 0xDF, 0xDF, 0xA3, 0xDF, 0xDF, 0xDF, 0xD8, 0xD8, 0xF1, 0xB8, 0xA8, 0xB2, 0x86,
|
||||||
|
|
||||||
|
// bank 4, 256 bytes
|
||||||
|
0xB4, 0x98, 0x0D, 0x35, 0x5D, 0xB8, 0xAA, 0x98, 0xB0, 0x87, 0x2D, 0x35, 0x3D, 0xB2, 0xB6, 0xBA,
|
||||||
|
0xAF, 0x8C, 0x96, 0x19, 0x8F, 0x9F, 0xA7, 0x0E, 0x16, 0x1E, 0xB4, 0x9A, 0xB8, 0xAA, 0x87, 0x2C,
|
||||||
|
0x54, 0x7C, 0xB9, 0xA3, 0xDE, 0xDF, 0xDF, 0xA3, 0xB1, 0x80, 0xF2, 0xC4, 0xCD, 0xC9, 0xF1, 0xB8,
|
||||||
|
0xA9, 0xB4, 0x99, 0x83, 0x0D, 0x35, 0x5D, 0x89, 0xB9, 0xA3, 0x2D, 0x55, 0x7D, 0xB5, 0x93, 0xA3,
|
||||||
|
0x0E, 0x16, 0x1E, 0xA9, 0x2C, 0x54, 0x7C, 0xB8, 0xB4, 0xB0, 0xF1, 0x97, 0x83, 0xA8, 0x11, 0x84,
|
||||||
|
0xA5, 0x09, 0x98, 0xA3, 0x83, 0xF0, 0xDA, 0x24, 0x08, 0x44, 0x10, 0x64, 0x18, 0xD8, 0xF1, 0xA5,
|
||||||
|
0x29, 0x55, 0x7D, 0xA5, 0x85, 0x95, 0x02, 0x1A, 0x2E, 0x3A, 0x56, 0x5A, 0x40, 0x48, 0xF9, 0xF3,
|
||||||
|
0xA3, 0xD9, 0xF8, 0xF0, 0x98, 0x83, 0x24, 0x08, 0x44, 0x10, 0x64, 0x18, 0x97, 0x82, 0xA8, 0xF1,
|
||||||
|
0x11, 0xF0, 0x98, 0xA2, 0x24, 0x08, 0x44, 0x10, 0x64, 0x18, 0xDA, 0xF3, 0xDE, 0xD8, 0x83, 0xA5,
|
||||||
|
0x94, 0x01, 0xD9, 0xA3, 0x02, 0xF1, 0xA2, 0xC3, 0xC5, 0xC7, 0xD8, 0xF1, 0x84, 0x92, 0xA2, 0x4D,
|
||||||
|
0xDA, 0x2A, 0xD8, 0x48, 0x69, 0xD9, 0x2A, 0xD8, 0x68, 0x55, 0xDA, 0x32, 0xD8, 0x50, 0x71, 0xD9,
|
||||||
|
0x32, 0xD8, 0x70, 0x5D, 0xDA, 0x3A, 0xD8, 0x58, 0x79, 0xD9, 0x3A, 0xD8, 0x78, 0x93, 0xA3, 0x4D,
|
||||||
|
0xDA, 0x2A, 0xD8, 0x48, 0x69, 0xD9, 0x2A, 0xD8, 0x68, 0x55, 0xDA, 0x32, 0xD8, 0x50, 0x71, 0xD9,
|
||||||
|
0x32, 0xD8, 0x70, 0x5D, 0xDA, 0x3A, 0xD8, 0x58, 0x79, 0xD9, 0x3A, 0xD8, 0x78, 0xA8, 0x8A, 0x9A,
|
||||||
|
0xF0, 0x28, 0x50, 0x78, 0x9E, 0xF3, 0x88, 0x18, 0xF1, 0x9F, 0x1D, 0x98, 0xA8, 0xD9, 0x08, 0xD8,
|
||||||
|
0xC8, 0x9F, 0x12, 0x9E, 0xF3, 0x15, 0xA8, 0xDA, 0x12, 0x10, 0xD8, 0xF1, 0xAF, 0xC8, 0x97, 0x87,
|
||||||
|
|
||||||
|
// bank 5, 256 bytes
|
||||||
|
0x34, 0xB5, 0xB9, 0x94, 0xA4, 0x21, 0xF3, 0xD9, 0x22, 0xD8, 0xF2, 0x2D, 0xF3, 0xD9, 0x2A, 0xD8,
|
||||||
|
0xF2, 0x35, 0xF3, 0xD9, 0x32, 0xD8, 0x81, 0xA4, 0x60, 0x60, 0x61, 0xD9, 0x61, 0xD8, 0x6C, 0x68,
|
||||||
|
0x69, 0xD9, 0x69, 0xD8, 0x74, 0x70, 0x71, 0xD9, 0x71, 0xD8, 0xB1, 0xA3, 0x84, 0x19, 0x3D, 0x5D,
|
||||||
|
0xA3, 0x83, 0x1A, 0x3E, 0x5E, 0x93, 0x10, 0x30, 0x81, 0x10, 0x11, 0xB8, 0xB0, 0xAF, 0x8F, 0x94,
|
||||||
|
0xF2, 0xDA, 0x3E, 0xD8, 0xB4, 0x9A, 0xA8, 0x87, 0x29, 0xDA, 0xF8, 0xD8, 0x87, 0x9A, 0x35, 0xDA,
|
||||||
|
0xF8, 0xD8, 0x87, 0x9A, 0x3D, 0xDA, 0xF8, 0xD8, 0xB1, 0xB9, 0xA4, 0x98, 0x85, 0x02, 0x2E, 0x56,
|
||||||
|
0xA5, 0x81, 0x00, 0x0C, 0x14, 0xA3, 0x97, 0xB0, 0x8A, 0xF1, 0x2D, 0xD9, 0x28, 0xD8, 0x4D, 0xD9,
|
||||||
|
0x48, 0xD8, 0x6D, 0xD9, 0x68, 0xD8, 0xB1, 0x84, 0x0D, 0xDA, 0x0E, 0xD8, 0xA3, 0x29, 0x83, 0xDA,
|
||||||
|
0x2C, 0x0E, 0xD8, 0xA3, 0x84, 0x49, 0x83, 0xDA, 0x2C, 0x4C, 0x0E, 0xD8, 0xB8, 0xB0, 0xA8, 0x8A,
|
||||||
|
0x9A, 0xF5, 0x20, 0xAA, 0xDA, 0xDF, 0xD8, 0xA8, 0x40, 0xAA, 0xD0, 0xDA, 0xDE, 0xD8, 0xA8, 0x60,
|
||||||
|
0xAA, 0xDA, 0xD0, 0xDF, 0xD8, 0xF1, 0x97, 0x86, 0xA8, 0x31, 0x9B, 0x06, 0x99, 0x07, 0xAB, 0x97,
|
||||||
|
0x28, 0x88, 0x9B, 0xF0, 0x0C, 0x20, 0x14, 0x40, 0xB8, 0xB0, 0xB4, 0xA8, 0x8C, 0x9C, 0xF0, 0x04,
|
||||||
|
0x28, 0x51, 0x79, 0x1D, 0x30, 0x14, 0x38, 0xB2, 0x82, 0xAB, 0xD0, 0x98, 0x2C, 0x50, 0x50, 0x78,
|
||||||
|
0x78, 0x9B, 0xF1, 0x1A, 0xB0, 0xF0, 0x8A, 0x9C, 0xA8, 0x29, 0x51, 0x79, 0x8B, 0x29, 0x51, 0x79,
|
||||||
|
0x8A, 0x24, 0x70, 0x59, 0x8B, 0x20, 0x58, 0x71, 0x8A, 0x44, 0x69, 0x38, 0x8B, 0x39, 0x40, 0x68,
|
||||||
|
0x8A, 0x64, 0x48, 0x31, 0x8B, 0x30, 0x49, 0x60, 0xA5, 0x88, 0x20, 0x09, 0x71, 0x58, 0x44, 0x68,
|
||||||
|
|
||||||
|
// bank 6, 256 bytes
|
||||||
|
0x11, 0x39, 0x64, 0x49, 0x30, 0x19, 0xF1, 0xAC, 0x00, 0x2C, 0x54, 0x7C, 0xF0, 0x8C, 0xA8, 0x04,
|
||||||
|
0x28, 0x50, 0x78, 0xF1, 0x88, 0x97, 0x26, 0xA8, 0x59, 0x98, 0xAC, 0x8C, 0x02, 0x26, 0x46, 0x66,
|
||||||
|
0xF0, 0x89, 0x9C, 0xA8, 0x29, 0x51, 0x79, 0x24, 0x70, 0x59, 0x44, 0x69, 0x38, 0x64, 0x48, 0x31,
|
||||||
|
0xA9, 0x88, 0x09, 0x20, 0x59, 0x70, 0xAB, 0x11, 0x38, 0x40, 0x69, 0xA8, 0x19, 0x31, 0x48, 0x60,
|
||||||
|
0x8C, 0xA8, 0x3C, 0x41, 0x5C, 0x20, 0x7C, 0x00, 0xF1, 0x87, 0x98, 0x19, 0x86, 0xA8, 0x6E, 0x76,
|
||||||
|
0x7E, 0xA9, 0x99, 0x88, 0x2D, 0x55, 0x7D, 0x9E, 0xB9, 0xA3, 0x8A, 0x22, 0x8A, 0x6E, 0x8A, 0x56,
|
||||||
|
0x8A, 0x5E, 0x9F, 0xB1, 0x83, 0x06, 0x26, 0x46, 0x66, 0x0E, 0x2E, 0x4E, 0x6E, 0x9D, 0xB8, 0xAD,
|
||||||
|
0x00, 0x2C, 0x54, 0x7C, 0xF2, 0xB1, 0x8C, 0xB4, 0x99, 0xB9, 0xA3, 0x2D, 0x55, 0x7D, 0x81, 0x91,
|
||||||
|
0xAC, 0x38, 0xAD, 0x3A, 0xB5, 0x83, 0x91, 0xAC, 0x2D, 0xD9, 0x28, 0xD8, 0x4D, 0xD9, 0x48, 0xD8,
|
||||||
|
0x6D, 0xD9, 0x68, 0xD8, 0x8C, 0x9D, 0xAE, 0x29, 0xD9, 0x04, 0xAE, 0xD8, 0x51, 0xD9, 0x04, 0xAE,
|
||||||
|
0xD8, 0x79, 0xD9, 0x04, 0xD8, 0x81, 0xF3, 0x9D, 0xAD, 0x00, 0x8D, 0xAE, 0x19, 0x81, 0xAD, 0xD9,
|
||||||
|
0x01, 0xD8, 0xF2, 0xAE, 0xDA, 0x26, 0xD8, 0x8E, 0x91, 0x29, 0x83, 0xA7, 0xD9, 0xAD, 0xAD, 0xAD,
|
||||||
|
0xAD, 0xF3, 0x2A, 0xD8, 0xD8, 0xF1, 0xB0, 0xAC, 0x89, 0x91, 0x3E, 0x5E, 0x76, 0xF3, 0xAC, 0x2E,
|
||||||
|
0x2E, 0xF1, 0xB1, 0x8C, 0x5A, 0x9C, 0xAC, 0x2C, 0x28, 0x28, 0x28, 0x9C, 0xAC, 0x30, 0x18, 0xA8,
|
||||||
|
0x98, 0x81, 0x28, 0x34, 0x3C, 0x97, 0x24, 0xA7, 0x28, 0x34, 0x3C, 0x9C, 0x24, 0xF2, 0xB0, 0x89,
|
||||||
|
0xAC, 0x91, 0x2C, 0x4C, 0x6C, 0x8A, 0x9B, 0x2D, 0xD9, 0xD8, 0xD8, 0x51, 0xD9, 0xD8, 0xD8, 0x79,
|
||||||
|
|
||||||
|
// bank 7, 138 bytes (remainder)
|
||||||
|
0xD9, 0xD8, 0xD8, 0xF1, 0x9E, 0x88, 0xA3, 0x31, 0xDA, 0xD8, 0xD8, 0x91, 0x2D, 0xD9, 0x28, 0xD8,
|
||||||
|
0x4D, 0xD9, 0x48, 0xD8, 0x6D, 0xD9, 0x68, 0xD8, 0xB1, 0x83, 0x93, 0x35, 0x3D, 0x80, 0x25, 0xDA,
|
||||||
|
0xD8, 0xD8, 0x85, 0x69, 0xDA, 0xD8, 0xD8, 0xB4, 0x93, 0x81, 0xA3, 0x28, 0x34, 0x3C, 0xF3, 0xAB,
|
||||||
|
0x8B, 0xF8, 0xA3, 0x91, 0xB6, 0x09, 0xB4, 0xD9, 0xAB, 0xDE, 0xFA, 0xB0, 0x87, 0x9C, 0xB9, 0xA3,
|
||||||
|
0xDD, 0xF1, 0xA3, 0xA3, 0xA3, 0xA3, 0x95, 0xF1, 0xA3, 0xA3, 0xA3, 0x9D, 0xF1, 0xA3, 0xA3, 0xA3,
|
||||||
|
0xA3, 0xF2, 0xA3, 0xB4, 0x90, 0x80, 0xF2, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
|
||||||
|
0xA3, 0xB2, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xB0, 0x87, 0xB5, 0x99, 0xF1, 0xA3, 0xA3, 0xA3,
|
||||||
|
0x98, 0xF1, 0xA3, 0xA3, 0xA3, 0xA3, 0x97, 0xA3, 0xA3, 0xA3, 0xA3, 0xF3, 0x9B, 0xA3, 0xA3, 0xDC,
|
||||||
|
0xB9, 0xA7, 0xF1, 0x26, 0x26, 0x26, 0xD8, 0xD8, 0xFF
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// DMP FIFO update rate: 0x09 drops the FIFO rate down to 20 Hz. 0x07 is 25 Hz,
|
||||||
|
// 0x01 is 100Hz. Going faster than 100Hz (0x00=200Hz) tends to result in very
|
||||||
|
// noisy data. DMP output frequency is calculated easily using this equation:
|
||||||
|
// (200Hz / (1 + value))
|
||||||
|
|
||||||
|
// It is important to make sure the host processor can keep up with reading and
|
||||||
|
// processing the FIFO output at the desired rate. Handling FIFO overflow
|
||||||
|
// cleanly is also a good idea. thanks to Noah Zerkin for piecing this stuff
|
||||||
|
// together!
|
||||||
|
|
||||||
|
#ifndef DMP_FIFO_RATE
|
||||||
|
#define DMP_FIFO_RATE 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
const prog_uchar dmpConfig[MPU6050_DMP_CONFIG_SIZE] PROGMEM = {
|
||||||
|
// BANK OFFSET LENGTH [DATA]
|
||||||
|
0x03, 0x7B, 0x03, 0x4C, 0xCD, 0x6C, // FCFG_1 inv_set_gyro_calibration
|
||||||
|
0x03, 0xAB, 0x03, 0x36, 0x56, 0x76, // FCFG_3 inv_set_gyro_calibration
|
||||||
|
0x00, 0x68, 0x04, 0x02, 0xCB, 0x47, 0xA2, // D_0_104 inv_set_gyro_calibration
|
||||||
|
0x02, 0x18, 0x04, 0x00, 0x05, 0x8B, 0xC1, // D_0_24 inv_set_gyro_calibration
|
||||||
|
0x01, 0x0C, 0x04, 0x00, 0x00, 0x00, 0x00, // D_1_152 inv_set_accel_calibration
|
||||||
|
0x03, 0x7F, 0x06, 0x0C, 0xC9, 0x2C, 0x97, 0x97, 0x97, // FCFG_2 inv_set_accel_calibration
|
||||||
|
0x03, 0x89, 0x03, 0x26, 0x46, 0x66, // FCFG_7 inv_set_accel_calibration
|
||||||
|
0x00, 0x6C, 0x02, 0x20, 0x00, // D_0_108 inv_set_accel_calibration
|
||||||
|
0x02, 0x40, 0x04, 0x00, 0x00, 0x00, 0x00, // CPASS_MTX_00 inv_set_compass_calibration
|
||||||
|
0x02, 0x44, 0x04, 0x00, 0x00, 0x00, 0x00, // CPASS_MTX_01
|
||||||
|
0x02, 0x48, 0x04, 0x00, 0x00, 0x00, 0x00, // CPASS_MTX_02
|
||||||
|
0x02, 0x4C, 0x04, 0x00, 0x00, 0x00, 0x00, // CPASS_MTX_10
|
||||||
|
0x02, 0x50, 0x04, 0x00, 0x00, 0x00, 0x00, // CPASS_MTX_11
|
||||||
|
0x02, 0x54, 0x04, 0x00, 0x00, 0x00, 0x00, // CPASS_MTX_12
|
||||||
|
0x02, 0x58, 0x04, 0x00, 0x00, 0x00, 0x00, // CPASS_MTX_20
|
||||||
|
0x02, 0x5C, 0x04, 0x00, 0x00, 0x00, 0x00, // CPASS_MTX_21
|
||||||
|
0x02, 0xBC, 0x04, 0x00, 0x00, 0x00, 0x00, // CPASS_MTX_22
|
||||||
|
0x01, 0xEC, 0x04, 0x00, 0x00, 0x40, 0x00, // D_1_236 inv_apply_endian_accel
|
||||||
|
0x03, 0x7F, 0x06, 0x0C, 0xC9, 0x2C, 0x97, 0x97, 0x97, // FCFG_2 inv_set_mpu_sensors
|
||||||
|
0x04, 0x02, 0x03, 0x0D, 0x35, 0x5D, // CFG_MOTION_BIAS inv_turn_on_bias_from_no_motion
|
||||||
|
0x04, 0x09, 0x04, 0x87, 0x2D, 0x35, 0x3D, // FCFG_5 inv_set_bias_update
|
||||||
|
0x00, 0xA3, 0x01, 0x00, // D_0_163 inv_set_dead_zone
|
||||||
|
// SPECIAL 0x01 = enable interrupts
|
||||||
|
0x00, 0x00, 0x00, 0x01, // SET INT_ENABLE at i=22, SPECIAL INSTRUCTION
|
||||||
|
0x07, 0x86, 0x01, 0xFE, // CFG_6 inv_set_fifo_interupt
|
||||||
|
0x07, 0x41, 0x05, 0xF1, 0x20, 0x28, 0x30, 0x38, // CFG_8 inv_send_quaternion
|
||||||
|
0x07, 0x7E, 0x01, 0x30, // CFG_16 inv_set_footer
|
||||||
|
0x07, 0x46, 0x01, 0x9A, // CFG_GYRO_SOURCE inv_send_gyro
|
||||||
|
0x07, 0x47, 0x04, 0xF1, 0x28, 0x30, 0x38, // CFG_9 inv_send_gyro -> inv_construct3_fifo
|
||||||
|
0x07, 0x6C, 0x04, 0xF1, 0x28, 0x30, 0x38, // CFG_12 inv_send_accel -> inv_construct3_fifo
|
||||||
|
0x02, 0x16, 0x02, 0x00, DMP_FIFO_RATE // D_0_22 inv_set_fifo_rate
|
||||||
|
};
|
||||||
|
|
||||||
|
const prog_uchar dmpUpdates[MPU6050_DMP_UPDATES_SIZE] PROGMEM = {
|
||||||
|
0x01, 0xB2, 0x02, 0xFF, 0xFF,
|
||||||
|
0x01, 0x90, 0x04, 0x09, 0x23, 0xA1, 0x35,
|
||||||
|
0x01, 0x6A, 0x02, 0x06, 0x00,
|
||||||
|
0x01, 0x60, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x60, 0x04, 0x40, 0x00, 0x00, 0x00,
|
||||||
|
0x01, 0x62, 0x02, 0x00, 0x00,
|
||||||
|
0x00, 0x60, 0x04, 0x00, 0x40, 0x00, 0x00
|
||||||
|
};
|
||||||
|
|
||||||
|
uint8_t MPU6050::dmpInitialize() {
|
||||||
|
// reset device
|
||||||
|
DEBUG_PRINTLN(F("\n\nResetting MPU6050..."));
|
||||||
|
reset();
|
||||||
|
usleep(30000); // wait after reset
|
||||||
|
|
||||||
|
// enable sleep mode and wake cycle
|
||||||
|
/*Serial.println(F("Enabling sleep mode..."));
|
||||||
|
setSleepEnabled(true);
|
||||||
|
Serial.println(F("Enabling wake cycle..."));
|
||||||
|
setWakeCycleEnabled(true);*/
|
||||||
|
|
||||||
|
// disable sleep mode
|
||||||
|
DEBUG_PRINTLN(F("Disabling sleep mode..."));
|
||||||
|
setSleepEnabled(false);
|
||||||
|
|
||||||
|
// get MPU hardware revision
|
||||||
|
DEBUG_PRINTLN(F("Selecting user bank 16..."));
|
||||||
|
setMemoryBank(0x10, true, true);
|
||||||
|
DEBUG_PRINTLN(F("Selecting memory byte 6..."));
|
||||||
|
setMemoryStartAddress(0x06);
|
||||||
|
DEBUG_PRINTLN(F("Checking hardware revision..."));
|
||||||
|
uint8_t hwRevision __attribute__((__unused__)) = readMemoryByte();
|
||||||
|
DEBUG_PRINT(F("Revision @ user[16][6] = "));
|
||||||
|
DEBUG_PRINTLNF(hwRevision, HEX);
|
||||||
|
DEBUG_PRINTLN(F("Resetting memory bank selection to 0..."));
|
||||||
|
setMemoryBank(0, false, false);
|
||||||
|
|
||||||
|
// check OTP bank valid
|
||||||
|
DEBUG_PRINTLN(F("Reading OTP bank valid flag..."));
|
||||||
|
uint8_t otpValid __attribute__((__unused__)) = getOTPBankValid();
|
||||||
|
DEBUG_PRINT(F("OTP bank is "));
|
||||||
|
DEBUG_PRINTLN(otpValid ? F("valid!") : F("invalid!"));
|
||||||
|
|
||||||
|
// get X/Y/Z gyro offsets
|
||||||
|
DEBUG_PRINTLN(F("Reading gyro offset values..."));
|
||||||
|
int8_t xgOffset = getXGyroOffset();
|
||||||
|
int8_t ygOffset = getYGyroOffset();
|
||||||
|
int8_t zgOffset = getZGyroOffset();
|
||||||
|
DEBUG_PRINT(F("X gyro offset = "));
|
||||||
|
DEBUG_PRINTLN(xgOffset);
|
||||||
|
DEBUG_PRINT(F("Y gyro offset = "));
|
||||||
|
DEBUG_PRINTLN(ygOffset);
|
||||||
|
DEBUG_PRINT(F("Z gyro offset = "));
|
||||||
|
DEBUG_PRINTLN(zgOffset);
|
||||||
|
|
||||||
|
// setup weird slave stuff (?)
|
||||||
|
DEBUG_PRINTLN(F("Setting slave 0 address to 0x7F..."));
|
||||||
|
setSlaveAddress(0, 0x7F);
|
||||||
|
DEBUG_PRINTLN(F("Disabling I2C Master mode..."));
|
||||||
|
setI2CMasterModeEnabled(false);
|
||||||
|
DEBUG_PRINTLN(F("Setting slave 0 address to 0x68 (self)..."));
|
||||||
|
setSlaveAddress(0, 0x68);
|
||||||
|
DEBUG_PRINTLN(F("Resetting I2C Master control..."));
|
||||||
|
resetI2CMaster();
|
||||||
|
usleep(20000);
|
||||||
|
|
||||||
|
// load DMP code into memory banks
|
||||||
|
DEBUG_PRINT(F("Writing DMP code to MPU memory banks ("));
|
||||||
|
DEBUG_PRINT(MPU6050_DMP_CODE_SIZE);
|
||||||
|
DEBUG_PRINTLN(F(" bytes)"));
|
||||||
|
if (writeProgMemoryBlock(dmpMemory, MPU6050_DMP_CODE_SIZE)) {
|
||||||
|
printf("Success! DMP code written and verified.\n");
|
||||||
|
|
||||||
|
// write DMP configuration
|
||||||
|
DEBUG_PRINT(F("Writing DMP configuration to MPU memory banks ("));
|
||||||
|
DEBUG_PRINT(MPU6050_DMP_CONFIG_SIZE);
|
||||||
|
DEBUG_PRINTLN(F(" bytes in config def)"));
|
||||||
|
if (writeProgDMPConfigurationSet(dmpConfig, MPU6050_DMP_CONFIG_SIZE)) {
|
||||||
|
printf("Success! DMP configuration written and verified.\n");
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Setting clock source to Z Gyro..."));
|
||||||
|
setClockSource(MPU6050_CLOCK_PLL_ZGYRO);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Setting DMP and FIFO_OFLOW interrupts enabled..."));
|
||||||
|
setIntEnabled(0x12);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Setting sample rate to 200Hz..."));
|
||||||
|
setRate(4); // 1khz / (1 + 4) = 200 Hz
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Setting external frame sync to TEMP_OUT_L[0]..."));
|
||||||
|
setExternalFrameSync(MPU6050_EXT_SYNC_TEMP_OUT_L);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Setting DLPF bandwidth to 42Hz..."));
|
||||||
|
setDLPFMode(MPU6050_DLPF_BW_42);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Setting gyro sensitivity to +/- 2000 deg/sec..."));
|
||||||
|
setFullScaleGyroRange(MPU6050_GYRO_FS_2000);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Setting DMP configuration bytes (function unknown)..."));
|
||||||
|
setDMPConfig1(0x03);
|
||||||
|
setDMPConfig2(0x00);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Clearing OTP Bank flag..."));
|
||||||
|
setOTPBankValid(false);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Setting X/Y/Z gyro offsets to previous values..."));
|
||||||
|
setXGyroOffset(xgOffset);
|
||||||
|
setYGyroOffset(ygOffset);
|
||||||
|
setZGyroOffset(zgOffset);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Setting X/Y/Z gyro user offsets to zero..."));
|
||||||
|
setXGyroOffsetUser(0);
|
||||||
|
setYGyroOffsetUser(0);
|
||||||
|
setZGyroOffsetUser(0);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Writing final memory update 1/7 (function unknown)..."));
|
||||||
|
uint8_t dmpUpdate[16], j;
|
||||||
|
uint16_t pos = 0;
|
||||||
|
for (j = 0; j < 4 || j < dmpUpdate[2] + 3; j++, pos++) dmpUpdate[j] = pgm_read_byte(&dmpUpdates[pos]);
|
||||||
|
writeMemoryBlock(dmpUpdate + 3, dmpUpdate[2], dmpUpdate[0], dmpUpdate[1]);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Writing final memory update 2/7 (function unknown)..."));
|
||||||
|
for (j = 0; j < 4 || j < dmpUpdate[2] + 3; j++, pos++) dmpUpdate[j] = pgm_read_byte(&dmpUpdates[pos]);
|
||||||
|
writeMemoryBlock(dmpUpdate + 3, dmpUpdate[2], dmpUpdate[0], dmpUpdate[1]);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Resetting FIFO..."));
|
||||||
|
resetFIFO();
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Reading FIFO count..."));
|
||||||
|
uint8_t fifoCount = getFIFOCount();
|
||||||
|
uint8_t fifoBuffer[128];
|
||||||
|
|
||||||
|
printf("Current FIFO count=%d\n", fifoCount);
|
||||||
|
DEBUG_PRINTLN(fifoCount);
|
||||||
|
if (fifoCount > 0)
|
||||||
|
getFIFOBytes(fifoBuffer, fifoCount);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Setting motion detection threshold to 2..."));
|
||||||
|
setMotionDetectionThreshold(2);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Setting zero-motion detection threshold to 156..."));
|
||||||
|
setZeroMotionDetectionThreshold(156);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Setting motion detection duration to 80..."));
|
||||||
|
setMotionDetectionDuration(80);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Setting zero-motion detection duration to 0..."));
|
||||||
|
setZeroMotionDetectionDuration(0);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Resetting FIFO..."));
|
||||||
|
resetFIFO();
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Enabling FIFO..."));
|
||||||
|
setFIFOEnabled(true);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Enabling DMP..."));
|
||||||
|
setDMPEnabled(true);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Resetting DMP..."));
|
||||||
|
resetDMP();
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Writing final memory update 3/7 (function unknown)..."));
|
||||||
|
for (j = 0; j < 4 || j < dmpUpdate[2] + 3; j++, pos++) dmpUpdate[j] = pgm_read_byte(&dmpUpdates[pos]);
|
||||||
|
writeMemoryBlock(dmpUpdate + 3, dmpUpdate[2], dmpUpdate[0], dmpUpdate[1]);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Writing final memory update 4/7 (function unknown)..."));
|
||||||
|
for (j = 0; j < 4 || j < dmpUpdate[2] + 3; j++, pos++) dmpUpdate[j] = pgm_read_byte(&dmpUpdates[pos]);
|
||||||
|
writeMemoryBlock(dmpUpdate + 3, dmpUpdate[2], dmpUpdate[0], dmpUpdate[1]);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Writing final memory update 5/7 (function unknown)..."));
|
||||||
|
for (j = 0; j < 4 || j < dmpUpdate[2] + 3; j++, pos++) dmpUpdate[j] = pgm_read_byte(&dmpUpdates[pos]);
|
||||||
|
writeMemoryBlock(dmpUpdate + 3, dmpUpdate[2], dmpUpdate[0], dmpUpdate[1]);
|
||||||
|
|
||||||
|
printf("Waiting for FIFO count > 2...\n");
|
||||||
|
while ((fifoCount = getFIFOCount()) < 3);
|
||||||
|
|
||||||
|
printf("Current FIFO count=%d",fifoCount);
|
||||||
|
DEBUG_PRINTLN(fifoCount);
|
||||||
|
DEBUG_PRINTLN(F("Reading FIFO data..."));
|
||||||
|
getFIFOBytes(fifoBuffer, fifoCount);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Reading interrupt status..."));
|
||||||
|
uint8_t mpuIntStatus __attribute__((__unused__)) = getIntStatus();
|
||||||
|
|
||||||
|
DEBUG_PRINT(F("Current interrupt status="));
|
||||||
|
DEBUG_PRINTLNF(mpuIntStatus, HEX);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Reading final memory update 6/7 (function unknown)..."));
|
||||||
|
for (j = 0; j < 4 || j < dmpUpdate[2] + 3; j++, pos++) dmpUpdate[j] = pgm_read_byte(&dmpUpdates[pos]);
|
||||||
|
readMemoryBlock(dmpUpdate + 3, dmpUpdate[2], dmpUpdate[0], dmpUpdate[1]);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Waiting for FIFO count > 2..."));
|
||||||
|
while ((fifoCount = getFIFOCount()) < 3);
|
||||||
|
|
||||||
|
DEBUG_PRINT(F("Current FIFO count="));
|
||||||
|
DEBUG_PRINTLN(fifoCount);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Reading FIFO data..."));
|
||||||
|
getFIFOBytes(fifoBuffer, fifoCount);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Reading interrupt status..."));
|
||||||
|
mpuIntStatus = getIntStatus();
|
||||||
|
|
||||||
|
DEBUG_PRINT(F("Current interrupt status="));
|
||||||
|
DEBUG_PRINTLNF(mpuIntStatus, HEX);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Writing final memory update 7/7 (function unknown)..."));
|
||||||
|
for (j = 0; j < 4 || j < dmpUpdate[2] + 3; j++, pos++) dmpUpdate[j] = pgm_read_byte(&dmpUpdates[pos]);
|
||||||
|
writeMemoryBlock(dmpUpdate + 3, dmpUpdate[2], dmpUpdate[0], dmpUpdate[1]);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("DMP is good to go! Finally."));
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Disabling DMP (you turn it on later)..."));
|
||||||
|
setDMPEnabled(false);
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Setting up internal 42-byte (default) DMP packet buffer..."));
|
||||||
|
dmpPacketSize = 42;
|
||||||
|
/*if ((dmpPacketBuffer = (uint8_t *)malloc(42)) == 0) {
|
||||||
|
return 3; // TODO: proper error code for no memory
|
||||||
|
}*/
|
||||||
|
|
||||||
|
DEBUG_PRINTLN(F("Resetting FIFO and clearing INT status one last time..."));
|
||||||
|
resetFIFO();
|
||||||
|
getIntStatus();
|
||||||
|
} else {
|
||||||
|
DEBUG_PRINTLN(F("ERROR! DMP configuration verification failed."));
|
||||||
|
return 2; // configuration block loading failed
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
DEBUG_PRINTLN(F("ERROR! DMP code verification failed."));
|
||||||
|
return 1; // main binary block loading failed
|
||||||
|
}
|
||||||
|
return 0; // success
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MPU6050::dmpPacketAvailable() {
|
||||||
|
return getFIFOCount() >= dmpGetFIFOPacketSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
// uint8_t MPU6050::dmpSetFIFORate(uint8_t fifoRate);
|
||||||
|
// uint8_t MPU6050::dmpGetFIFORate();
|
||||||
|
// uint8_t MPU6050::dmpGetSampleStepSizeMS();
|
||||||
|
// uint8_t MPU6050::dmpGetSampleFrequency();
|
||||||
|
// int32_t MPU6050::dmpDecodeTemperature(int8_t tempReg);
|
||||||
|
|
||||||
|
//uint8_t MPU6050::dmpRegisterFIFORateProcess(inv_obj_func func, int16_t priority);
|
||||||
|
//uint8_t MPU6050::dmpUnregisterFIFORateProcess(inv_obj_func func);
|
||||||
|
//uint8_t MPU6050::dmpRunFIFORateProcesses();
|
||||||
|
|
||||||
|
// uint8_t MPU6050::dmpSendQuaternion(uint_fast16_t accuracy);
|
||||||
|
// uint8_t MPU6050::dmpSendGyro(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
// uint8_t MPU6050::dmpSendAccel(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
// uint8_t MPU6050::dmpSendLinearAccel(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
// uint8_t MPU6050::dmpSendLinearAccelInWorld(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
// uint8_t MPU6050::dmpSendControlData(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
// uint8_t MPU6050::dmpSendSensorData(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
// uint8_t MPU6050::dmpSendExternalSensorData(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
// uint8_t MPU6050::dmpSendGravity(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
// uint8_t MPU6050::dmpSendPacketNumber(uint_fast16_t accuracy);
|
||||||
|
// uint8_t MPU6050::dmpSendQuantizedAccel(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
// uint8_t MPU6050::dmpSendEIS(uint_fast16_t elements, uint_fast16_t accuracy);
|
||||||
|
|
||||||
|
uint8_t MPU6050::dmpGetAccel(int32_t *data, const uint8_t* packet) {
|
||||||
|
// TODO: accommodate different arrangements of sent data (ONLY default supported now)
|
||||||
|
if (packet == 0) packet = dmpPacketBuffer;
|
||||||
|
data[0] = ((packet[28] << 24) + (packet[29] << 16) + (packet[30] << 8) + packet[31]);
|
||||||
|
data[1] = ((packet[32] << 24) + (packet[33] << 16) + (packet[34] << 8) + packet[35]);
|
||||||
|
data[2] = ((packet[36] << 24) + (packet[37] << 16) + (packet[38] << 8) + packet[39]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
uint8_t MPU6050::dmpGetAccel(int16_t *data, const uint8_t* packet) {
|
||||||
|
// TODO: accommodate different arrangements of sent data (ONLY default supported now)
|
||||||
|
if (packet == 0) packet = dmpPacketBuffer;
|
||||||
|
data[0] = (packet[28] << 8) + packet[29];
|
||||||
|
data[1] = (packet[32] << 8) + packet[33];
|
||||||
|
data[2] = (packet[36] << 8) + packet[37];
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
uint8_t MPU6050::dmpGetAccel(VectorInt16 *v, const uint8_t* packet) {
|
||||||
|
// TODO: accommodate different arrangements of sent data (ONLY default supported now)
|
||||||
|
if (packet == 0) packet = dmpPacketBuffer;
|
||||||
|
v -> x = (packet[28] << 8) + packet[29];
|
||||||
|
v -> y = (packet[32] << 8) + packet[33];
|
||||||
|
v -> z = (packet[36] << 8) + packet[37];
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
uint8_t MPU6050::dmpGetQuaternion(int32_t *data, const uint8_t* packet) {
|
||||||
|
// TODO: accommodate different arrangements of sent data (ONLY default supported now)
|
||||||
|
if (packet == 0) packet = dmpPacketBuffer;
|
||||||
|
data[0] = ((packet[0] << 24) + (packet[1] << 16) + (packet[2] << 8) + packet[3]);
|
||||||
|
data[1] = ((packet[4] << 24) + (packet[5] << 16) + (packet[6] << 8) + packet[7]);
|
||||||
|
data[2] = ((packet[8] << 24) + (packet[9] << 16) + (packet[10] << 8) + packet[11]);
|
||||||
|
data[3] = ((packet[12] << 24) + (packet[13] << 16) + (packet[14] << 8) + packet[15]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
uint8_t MPU6050::dmpGetQuaternion(int16_t *data, const uint8_t* packet) {
|
||||||
|
// TODO: accommodate different arrangements of sent data (ONLY default supported now)
|
||||||
|
if (packet == 0) packet = dmpPacketBuffer;
|
||||||
|
data[0] = ((packet[0] << 8) + packet[1]);
|
||||||
|
data[1] = ((packet[4] << 8) + packet[5]);
|
||||||
|
data[2] = ((packet[8] << 8) + packet[9]);
|
||||||
|
data[3] = ((packet[12] << 8) + packet[13]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
uint8_t MPU6050::dmpGetQuaternion(Quaternion *q, const uint8_t* packet) {
|
||||||
|
// TODO: accommodate different arrangements of sent data (ONLY default supported now)
|
||||||
|
int16_t qI[4];
|
||||||
|
uint8_t status = dmpGetQuaternion(qI, packet);
|
||||||
|
if (status == 0) {
|
||||||
|
q -> w = (float)qI[0] / 16384.0f;
|
||||||
|
q -> x = (float)qI[1] / 16384.0f;
|
||||||
|
q -> y = (float)qI[2] / 16384.0f;
|
||||||
|
q -> z = (float)qI[3] / 16384.0f;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return status; // int16 return value, indicates error if this line is reached
|
||||||
|
}
|
||||||
|
// uint8_t MPU6050::dmpGet6AxisQuaternion(long *data, const uint8_t* packet);
|
||||||
|
// uint8_t MPU6050::dmpGetRelativeQuaternion(long *data, const uint8_t* packet);
|
||||||
|
uint8_t MPU6050::dmpGetGyro(int32_t *data, const uint8_t* packet) {
|
||||||
|
// TODO: accommodate different arrangements of sent data (ONLY default supported now)
|
||||||
|
if (packet == 0) packet = dmpPacketBuffer;
|
||||||
|
data[0] = ((packet[16] << 24) + (packet[17] << 16) + (packet[18] << 8) + packet[19]);
|
||||||
|
data[1] = ((packet[20] << 24) + (packet[21] << 16) + (packet[22] << 8) + packet[23]);
|
||||||
|
data[2] = ((packet[24] << 24) + (packet[25] << 16) + (packet[26] << 8) + packet[27]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
uint8_t MPU6050::dmpGetGyro(int16_t *data, const uint8_t* packet) {
|
||||||
|
// TODO: accommodate different arrangements of sent data (ONLY default supported now)
|
||||||
|
if (packet == 0) packet = dmpPacketBuffer;
|
||||||
|
data[0] = (packet[16] << 8) + packet[17];
|
||||||
|
data[1] = (packet[20] << 8) + packet[21];
|
||||||
|
data[2] = (packet[24] << 8) + packet[25];
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
// uint8_t MPU6050::dmpSetLinearAccelFilterCoefficient(float coef);
|
||||||
|
// uint8_t MPU6050::dmpGetLinearAccel(long *data, const uint8_t* packet);
|
||||||
|
uint8_t MPU6050::dmpGetLinearAccel(VectorInt16 *v, VectorInt16 *vRaw, VectorFloat *gravity) {
|
||||||
|
// get rid of the gravity component (+1g = +4096 in standard DMP FIFO packet)
|
||||||
|
v -> x = vRaw -> x - gravity -> x*4096;
|
||||||
|
v -> y = vRaw -> y - gravity -> y*4096;
|
||||||
|
v -> z = vRaw -> z - gravity -> z*4096;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
// uint8_t MPU6050::dmpGetLinearAccelInWorld(long *data, const uint8_t* packet);
|
||||||
|
uint8_t MPU6050::dmpGetLinearAccelInWorld(VectorInt16 *v, VectorInt16 *vReal, Quaternion *q) {
|
||||||
|
// rotate measured 3D acceleration vector into original state
|
||||||
|
// frame of reference based on orientation quaternion
|
||||||
|
memcpy(v, vReal, sizeof(VectorInt16));
|
||||||
|
v -> rotate(q);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
// uint8_t MPU6050::dmpGetGyroAndAccelSensor(long *data, const uint8_t* packet);
|
||||||
|
// uint8_t MPU6050::dmpGetGyroSensor(long *data, const uint8_t* packet);
|
||||||
|
// uint8_t MPU6050::dmpGetControlData(long *data, const uint8_t* packet);
|
||||||
|
// uint8_t MPU6050::dmpGetTemperature(long *data, const uint8_t* packet);
|
||||||
|
// uint8_t MPU6050::dmpGetGravity(long *data, const uint8_t* packet);
|
||||||
|
uint8_t MPU6050::dmpGetGravity(VectorFloat *v, Quaternion *q) {
|
||||||
|
v -> x = 2 * (q -> x*q -> z - q -> w*q -> y);
|
||||||
|
v -> y = 2 * (q -> w*q -> x + q -> y*q -> z);
|
||||||
|
v -> z = q -> w*q -> w - q -> x*q -> x - q -> y*q -> y + q -> z*q -> z;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
// uint8_t MPU6050::dmpGetUnquantizedAccel(long *data, const uint8_t* packet);
|
||||||
|
// uint8_t MPU6050::dmpGetQuantizedAccel(long *data, const uint8_t* packet);
|
||||||
|
// uint8_t MPU6050::dmpGetExternalSensorData(long *data, int size, const uint8_t* packet);
|
||||||
|
// uint8_t MPU6050::dmpGetEIS(long *data, const uint8_t* packet);
|
||||||
|
|
||||||
|
uint8_t MPU6050::dmpGetEuler(float *data, Quaternion *q) {
|
||||||
|
data[0] = atan2(2*q -> x*q -> y - 2*q -> w*q -> z, 2*q -> w*q -> w + 2*q -> x*q -> x - 1); // psi
|
||||||
|
data[1] = -asin(2*q -> x*q -> z + 2*q -> w*q -> y); // theta
|
||||||
|
data[2] = atan2(2*q -> y*q -> z - 2*q -> w*q -> x, 2*q -> w*q -> w + 2*q -> z*q -> z - 1); // phi
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
uint8_t MPU6050::dmpGetYawPitchRoll(float *data, Quaternion *q, VectorFloat *gravity) {
|
||||||
|
// yaw: (about Z axis)
|
||||||
|
data[0] = atan2(2*q -> x*q -> y - 2*q -> w*q -> z, 2*q -> w*q -> w + 2*q -> x*q -> x - 1);
|
||||||
|
// pitch: (nose up/down, about Y axis)
|
||||||
|
data[1] = atan(gravity -> x / sqrt(gravity -> y*gravity -> y + gravity -> z*gravity -> z));
|
||||||
|
// roll: (tilt left/right, about X axis)
|
||||||
|
data[2] = atan(gravity -> y / sqrt(gravity -> x*gravity -> x + gravity -> z*gravity -> z));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// uint8_t MPU6050::dmpGetAccelFloat(float *data, const uint8_t* packet);
|
||||||
|
// uint8_t MPU6050::dmpGetQuaternionFloat(float *data, const uint8_t* packet);
|
||||||
|
|
||||||
|
uint8_t MPU6050::dmpProcessFIFOPacket(const unsigned char *dmpData) {
|
||||||
|
/*for (uint8_t k = 0; k < dmpPacketSize; k++) {
|
||||||
|
if (dmpData[k] < 0x10) Serial.print("0");
|
||||||
|
Serial.print(dmpData[k], HEX);
|
||||||
|
Serial.print(" ");
|
||||||
|
}
|
||||||
|
Serial.print("\n");*/
|
||||||
|
//Serial.println((uint16_t)dmpPacketBuffer);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
uint8_t MPU6050::dmpReadAndProcessFIFOPacket(uint8_t numPackets, uint8_t *processed) {
|
||||||
|
uint8_t status;
|
||||||
|
uint8_t buf[dmpPacketSize];
|
||||||
|
for (uint8_t i = 0; i < numPackets; i++) {
|
||||||
|
// read packet from FIFO
|
||||||
|
getFIFOBytes(buf, dmpPacketSize);
|
||||||
|
|
||||||
|
// process packet
|
||||||
|
if ((status = dmpProcessFIFOPacket(buf)) > 0) return status;
|
||||||
|
|
||||||
|
// increment external process count variable, if supplied
|
||||||
|
if (processed != 0) (*processed)++;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// uint8_t MPU6050::dmpSetFIFOProcessedCallback(void (*func) (void));
|
||||||
|
|
||||||
|
// uint8_t MPU6050::dmpInitFIFOParam();
|
||||||
|
// uint8_t MPU6050::dmpCloseFIFO();
|
||||||
|
// uint8_t MPU6050::dmpSetGyroDataSource(uint_fast8_t source);
|
||||||
|
// uint8_t MPU6050::dmpDecodeQuantizedAccel();
|
||||||
|
// uint32_t MPU6050::dmpGetGyroSumOfSquare();
|
||||||
|
// uint32_t MPU6050::dmpGetAccelSumOfSquare();
|
||||||
|
// void MPU6050::dmpOverrideQuaternion(long *q);
|
||||||
|
uint16_t MPU6050::dmpGetFIFOPacketSize() {
|
||||||
|
return dmpPacketSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* _MPU6050_6AXIS_MOTIONAPPS20_H_ */
|
115
IMU/MPU6050dmp_tester.cpp
Normal file
115
IMU/MPU6050dmp_tester.cpp
Normal file
|
@ -0,0 +1,115 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include "I2Cdev.h"
|
||||||
|
#include "MPU6050_6Axis_MotionApps20.h"
|
||||||
|
#include "wiringPi.h"
|
||||||
|
|
||||||
|
#define INTERRUPT_PIN 7 // silkscreen: 4
|
||||||
|
#define LED_PIN 6 // silkscreen: 25
|
||||||
|
#define SWITCH_PIN // silkscreen:
|
||||||
|
|
||||||
|
MPU6050 mpu; // AD0 pin low
|
||||||
|
//MPU6050 mpu(0x69) // AD0 pin high
|
||||||
|
|
||||||
|
// MPU control/status vars
|
||||||
|
uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
|
||||||
|
uint16_t fifoCount; // count of all bytes currently in FIFO
|
||||||
|
uint8_t fifoBuffer[64]; // FIFO storage buffer
|
||||||
|
float sensorYaw;
|
||||||
|
int32_t
|
||||||
|
|
||||||
|
|
||||||
|
// ================================================================
|
||||||
|
// === INTERRUPT FUNCTION ===
|
||||||
|
// ================================================================
|
||||||
|
|
||||||
|
void dmpDataReady()
|
||||||
|
{
|
||||||
|
fifoCount = mpu.getFIFOCount();
|
||||||
|
|
||||||
|
if (fifoCount == 1024)
|
||||||
|
{
|
||||||
|
// reset so we can continue cleanly
|
||||||
|
mpu.resetFIFO();
|
||||||
|
printf("FIFO overflow!\n");
|
||||||
|
}
|
||||||
|
else if( fifoCount >= 42 )
|
||||||
|
{
|
||||||
|
// 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
|
||||||
|
int32_t[3] accel;
|
||||||
|
//float euler[3]; // [psi, theta, phi] Euler angle container
|
||||||
|
float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector
|
||||||
|
|
||||||
|
mpu.getFIFOBytes(fifoBuffer, packetSize);
|
||||||
|
mpu.dmpGetQuaternion(&q, fifoBuffer);
|
||||||
|
mpu.dmpGetGravity(&gravity, &q);
|
||||||
|
mpu.dmpGetLinearAccelInWorld( , , &q );
|
||||||
|
|
||||||
|
mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
|
||||||
|
printf("ypr %7.2f %7.2f %7.2f ", ypr[0] * 180/M_PI, ypr[1] * 180/M_PI, ypr[2] * 180/M_PI);
|
||||||
|
printf( "\n" );
|
||||||
|
sensorYaw = ypr[0] * 180/M_PI;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================================================================
|
||||||
|
// === INITIAL SETUP ===
|
||||||
|
// ================================================================
|
||||||
|
|
||||||
|
void setup()
|
||||||
|
{
|
||||||
|
// setup led as output
|
||||||
|
pinMode( LED_PIN, OUTPUT );
|
||||||
|
|
||||||
|
printf( "Setting up GPIO...\n" );
|
||||||
|
wiringPiSetup();
|
||||||
|
pinMode( INTERRUPT_PIN, INPUT );
|
||||||
|
|
||||||
|
printf("Initializing I2C devices...\n");
|
||||||
|
mpu.initialize();
|
||||||
|
|
||||||
|
printf("Testing device connections...\n");
|
||||||
|
printf(mpu.testConnection() ? "MPU6050 connection successful\n" : "MPU6050 connection failed\n");
|
||||||
|
|
||||||
|
printf("Initializing DMP...\n");
|
||||||
|
uint8_t devStatus = mpu.dmpInitialize();
|
||||||
|
|
||||||
|
if( devStatus == 0 )
|
||||||
|
{
|
||||||
|
printf("Enabling DMP...\n");
|
||||||
|
mpu.setDMPEnabled(true);
|
||||||
|
|
||||||
|
printf( "Enabling external interrupt on pin %d\n", INTERRUPT_PIN);
|
||||||
|
wiringPiISR( INTERRUPT_PIN, INT_EDGE_RISING, dmpDataReady );
|
||||||
|
|
||||||
|
printf("DMP ready!\n");
|
||||||
|
|
||||||
|
packetSize = mpu.dmpGetFIFOPacketSize();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("DMP Initialization failed (code %d)\n", devStatus);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================================================================
|
||||||
|
// === MAIN PROGRAM LOOP ===
|
||||||
|
// ================================================================
|
||||||
|
|
||||||
|
int main( int argc, char* argv[] )
|
||||||
|
{
|
||||||
|
setup();
|
||||||
|
usleep(100000);
|
||||||
|
while(1)
|
||||||
|
{}
|
||||||
|
return 0;
|
||||||
|
}
|
12
IMU/Makefile
Normal file
12
IMU/Makefile
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
all: MPU6050dmp_tester
|
||||||
|
|
||||||
|
HDRS = helper_3dmath.h I2Cdev.h MPU6050_6Axis_MotionApps20.h MPU6050.h
|
||||||
|
CXXFLAGS = -DDMP_FIFO_RATE=9 -Wall
|
||||||
|
|
||||||
|
I2Cdev.o MPU6050.o MPU6050dmp_tester.o: $(HDRS)
|
||||||
|
|
||||||
|
MPU6050dmp_tester: I2Cdev.o MPU6050.o MPU6050dmp_tester.o
|
||||||
|
$(CXX) $^ -lm `pkg-config gtkmm-3.0 --cflags --libs` -lwiringPi -o $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm I2Cdev.o MPU6050.o MPU6050dmp.o MPU6050dmp_tester.o MPU6050dmp_tester
|
216
IMU/helper_3dmath.h
Normal file
216
IMU/helper_3dmath.h
Normal file
|
@ -0,0 +1,216 @@
|
||||||
|
// I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050 class, 3D math helper
|
||||||
|
// 6/5/2012 by Jeff Rowberg <jeff@rowberg.net>
|
||||||
|
// Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
|
||||||
|
//
|
||||||
|
// Changelog:
|
||||||
|
// 2012-06-05 - add 3D math helper file to DMP6 example sketch
|
||||||
|
|
||||||
|
/* ============================================
|
||||||
|
I2Cdev device library code is placed under the MIT license
|
||||||
|
Copyright (c) 2012 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.
|
||||||
|
===============================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _HELPER_3DMATH_H_
|
||||||
|
#define _HELPER_3DMATH_H_
|
||||||
|
|
||||||
|
class Quaternion {
|
||||||
|
public:
|
||||||
|
float w;
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
float z;
|
||||||
|
|
||||||
|
Quaternion() {
|
||||||
|
w = 1.0f;
|
||||||
|
x = 0.0f;
|
||||||
|
y = 0.0f;
|
||||||
|
z = 0.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
Quaternion(float nw, float nx, float ny, float nz) {
|
||||||
|
w = nw;
|
||||||
|
x = nx;
|
||||||
|
y = ny;
|
||||||
|
z = nz;
|
||||||
|
}
|
||||||
|
|
||||||
|
Quaternion getProduct(Quaternion q) {
|
||||||
|
// Quaternion multiplication is defined by:
|
||||||
|
// (Q1 * Q2).w = (w1w2 - x1x2 - y1y2 - z1z2)
|
||||||
|
// (Q1 * Q2).x = (w1x2 + x1w2 + y1z2 - z1y2)
|
||||||
|
// (Q1 * Q2).y = (w1y2 - x1z2 + y1w2 + z1x2)
|
||||||
|
// (Q1 * Q2).z = (w1z2 + x1y2 - y1x2 + z1w2
|
||||||
|
return Quaternion(
|
||||||
|
w*q.w - x*q.x - y*q.y - z*q.z, // new w
|
||||||
|
w*q.x + x*q.w + y*q.z - z*q.y, // new x
|
||||||
|
w*q.y - x*q.z + y*q.w + z*q.x, // new y
|
||||||
|
w*q.z + x*q.y - y*q.x + z*q.w); // new z
|
||||||
|
}
|
||||||
|
|
||||||
|
Quaternion getConjugate() {
|
||||||
|
return Quaternion(w, -x, -y, -z);
|
||||||
|
}
|
||||||
|
|
||||||
|
float getMagnitude() {
|
||||||
|
return sqrt(w*w + x*x + y*y + z*z);
|
||||||
|
}
|
||||||
|
|
||||||
|
void normalize() {
|
||||||
|
float m = getMagnitude();
|
||||||
|
w /= m;
|
||||||
|
x /= m;
|
||||||
|
y /= m;
|
||||||
|
z /= m;
|
||||||
|
}
|
||||||
|
|
||||||
|
Quaternion getNormalized() {
|
||||||
|
Quaternion r(w, x, y, z);
|
||||||
|
r.normalize();
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class VectorInt16 {
|
||||||
|
public:
|
||||||
|
int16_t x;
|
||||||
|
int16_t y;
|
||||||
|
int16_t z;
|
||||||
|
|
||||||
|
VectorInt16() {
|
||||||
|
x = 0;
|
||||||
|
y = 0;
|
||||||
|
z = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
VectorInt16(int16_t nx, int16_t ny, int16_t nz) {
|
||||||
|
x = nx;
|
||||||
|
y = ny;
|
||||||
|
z = nz;
|
||||||
|
}
|
||||||
|
|
||||||
|
float getMagnitude() {
|
||||||
|
return sqrt(x*x + y*y + z*z);
|
||||||
|
}
|
||||||
|
|
||||||
|
void normalize() {
|
||||||
|
float m = getMagnitude();
|
||||||
|
x /= m;
|
||||||
|
y /= m;
|
||||||
|
z /= m;
|
||||||
|
}
|
||||||
|
|
||||||
|
VectorInt16 getNormalized() {
|
||||||
|
VectorInt16 r(x, y, z);
|
||||||
|
r.normalize();
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
void rotate(Quaternion *q) {
|
||||||
|
// http://www.cprogramming.com/tutorial/3d/quaternions.html
|
||||||
|
// http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/transforms/index.htm
|
||||||
|
// http://content.gpwiki.org/index.php/OpenGL:Tutorials:Using_Quaternions_to_represent_rotation
|
||||||
|
// ^ or: http://webcache.googleusercontent.com/search?q=cache:xgJAp3bDNhQJ:content.gpwiki.org/index.php/OpenGL:Tutorials:Using_Quaternions_to_represent_rotation&hl=en&gl=us&strip=1
|
||||||
|
|
||||||
|
// P_out = q * P_in * conj(q)
|
||||||
|
// - P_out is the output vector
|
||||||
|
// - q is the orientation quaternion
|
||||||
|
// - P_in is the input vector (a*aReal)
|
||||||
|
// - conj(q) is the conjugate of the orientation quaternion (q=[w,x,y,z], q*=[w,-x,-y,-z])
|
||||||
|
Quaternion p(0, x, y, z);
|
||||||
|
|
||||||
|
// quaternion multiplication: q * p, stored back in p
|
||||||
|
p = q -> getProduct(p);
|
||||||
|
|
||||||
|
// quaternion multiplication: p * conj(q), stored back in p
|
||||||
|
p = p.getProduct(q -> getConjugate());
|
||||||
|
|
||||||
|
// p quaternion is now [0, x', y', z']
|
||||||
|
x = p.x;
|
||||||
|
y = p.y;
|
||||||
|
z = p.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
VectorInt16 getRotated(Quaternion *q) {
|
||||||
|
VectorInt16 r(x, y, z);
|
||||||
|
r.rotate(q);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class VectorFloat {
|
||||||
|
public:
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
float z;
|
||||||
|
|
||||||
|
VectorFloat() {
|
||||||
|
x = 0;
|
||||||
|
y = 0;
|
||||||
|
z = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
VectorFloat(float nx, float ny, float nz) {
|
||||||
|
x = nx;
|
||||||
|
y = ny;
|
||||||
|
z = nz;
|
||||||
|
}
|
||||||
|
|
||||||
|
float getMagnitude() {
|
||||||
|
return sqrt(x*x + y*y + z*z);
|
||||||
|
}
|
||||||
|
|
||||||
|
void normalize() {
|
||||||
|
float m = getMagnitude();
|
||||||
|
x /= m;
|
||||||
|
y /= m;
|
||||||
|
z /= m;
|
||||||
|
}
|
||||||
|
|
||||||
|
VectorFloat getNormalized() {
|
||||||
|
VectorFloat r(x, y, z);
|
||||||
|
r.normalize();
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
void rotate(Quaternion *q) {
|
||||||
|
Quaternion p(0, x, y, z);
|
||||||
|
|
||||||
|
// quaternion multiplication: q * p, stored back in p
|
||||||
|
p = q -> getProduct(p);
|
||||||
|
|
||||||
|
// quaternion multiplication: p * conj(q), stored back in p
|
||||||
|
p = p.getProduct(q -> getConjugate());
|
||||||
|
|
||||||
|
// p quaternion is now [0, x', y', z']
|
||||||
|
x = p.x;
|
||||||
|
y = p.y;
|
||||||
|
z = p.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
VectorFloat getRotated(Quaternion *q) {
|
||||||
|
VectorFloat r(x, y, z);
|
||||||
|
r.rotate(q);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* _HELPER_3DMATH_H_ */
|
10
IMU/setup-i2c.sh
Executable file
10
IMU/setup-i2c.sh
Executable file
|
@ -0,0 +1,10 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# This little script ensures that the kernel I2C drivers are loaded, and it
|
||||||
|
# changes permissions on /dev/i2c-0 so you can run the demos as a regular
|
||||||
|
# user
|
||||||
|
|
||||||
|
sudo modprobe i2c-dev
|
||||||
|
sudo modprobe i2c-bcm2708
|
||||||
|
sleep 0.1
|
||||||
|
sudo chmod 666 /dev/i2c-0
|
Loading…
Reference in New Issue
Block a user