cps_microros_imu/firmware/lib/imu/ITG3200.cpp
2023-07-10 11:54:53 +02:00

522 lines
19 KiB
C++

// I2Cdev library collection - ITG3200 I2C device class
// Based on InvenSense ITG-3200 datasheet rev. 1.4, 3/30/2010 (PS-ITG-3200A-00-01.4)
// 7/31/2011 by Jeff Rowberg <jeff@rowberg.net>
// Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
//
// Changelog:
// 2011-07-31 - initial release
/* ============================================
I2Cdev device library code is placed under the MIT license
Copyright (c) 2011 Jeff Rowberg
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/
#include "ITG3200.h"
/** Default constructor, uses default I2C address.
* @see ITG3200_DEFAULT_ADDRESS
*/
ITG3200::ITG3200() {
devAddr = ITG3200_DEFAULT_ADDRESS;
}
/** Specific address constructor.
* @param address I2C address
* @see ITG3200_DEFAULT_ADDRESS
* @see ITG3200_ADDRESS_AD0_LOW
* @see ITG3200_ADDRESS_AD0_HIGH
*/
ITG3200::ITG3200(uint8_t address) {
devAddr = address;
}
/** Power on and prepare for general usage.
* This will activate the gyroscope, so be sure to adjust the power settings
* after you call this method if you want it to enter standby mode, or another
* less demanding mode of operation. This also sets the gyroscope to use the
* X-axis gyro for a clock source. Note that it doesn't have any delays in the
* routine, which means you might want to add ~50ms to be safe if you happen
* to need to read gyro data immediately after initialization. The data will
* flow in either case, but the first reports may have higher error offsets.
*/
void ITG3200::initialize() {
setFullScaleRange(ITG3200_FULLSCALE_2000);
setClockSource(ITG3200_CLOCK_PLL_XGYRO);
}
/** Verify the I2C connection.
* Make sure the device is connected and responds as expected.
* @return True if connection is valid, false otherwise
*/
bool ITG3200::testConnection() {
return getDeviceID() == 0b110100;
}
// WHO_AM_I register
/** Get Device ID.
* This register is used to verify the identity of the device (0b110100).
* @return Device ID (should be 0x34, 52 dec, 64 oct)
* @see ITG3200_RA_WHO_AM_I
* @see ITG3200_RA_DEVID_BIT
* @see ITG3200_RA_DEVID_LENGTH
*/
uint8_t ITG3200::getDeviceID() {
I2Cdev::readBits(devAddr, ITG3200_RA_WHO_AM_I, ITG3200_DEVID_BIT, ITG3200_DEVID_LENGTH, buffer);
return buffer[0];
}
/** Set Device ID.
* Write a new ID into the WHO_AM_I register (no idea why this should ever be
* necessary though).
* @param id New device ID to set.
* @see getDeviceID()
* @see ITG3200_RA_WHO_AM_I
* @see ITG3200_RA_DEVID_BIT
* @see ITG3200_RA_DEVID_LENGTH
*/
void ITG3200::setDeviceID(uint8_t id) {
I2Cdev::writeBits(devAddr, ITG3200_RA_WHO_AM_I, ITG3200_DEVID_BIT, ITG3200_DEVID_LENGTH, id);
}
// SMPLRT_DIV register
/** Get sample rate.
* This register determines the sample rate of the ITG-3200 gyros. The gyros'
* outputs are sampled internally at either 1kHz or 8kHz, determined by the
* DLPF_CFG setting (see register 22). This sampling is then filtered digitally
* and delivered into the sensor registers after the number of cycles determined
* by this register. The sample rate is given by the following formula:
*
* F_sample = F_internal / (divider+1), where F_internal is either 1kHz or 8kHz
*
* As an example, if the internal sampling is at 1kHz, then setting this
* register to 7 would give the following:
*
* F_sample = 1kHz / (7 + 1) = 125Hz, or 8ms per sample
*
* @return Current sample rate
* @see setDLPFBandwidth()
* @see ITG3200_RA_SMPLRT_DIV
*/
uint8_t ITG3200::getRate() {
I2Cdev::readByte(devAddr, ITG3200_RA_SMPLRT_DIV, buffer);
return buffer[0];
}
/** Set sample rate.
* @param rate New sample rate
* @see getRate()
* @see setDLPFBandwidth()
* @see ITG3200_RA_SMPLRT_DIV
*/
void ITG3200::setRate(uint8_t rate) {
I2Cdev::writeByte(devAddr, ITG3200_RA_SMPLRT_DIV, rate);
}
// DLPF_FS register
/** Set full-scale range.
* The FS_SEL parameter allows setting the full-scale range of the gyro sensors,
* as described in the table below. The power-on-reset value of FS_SEL is 00h.
* Set to 03h for proper operation.
*
* 0 = Reserved
* 1 = Reserved
* 2 = Reserved
* 3 = +/- 2000 degrees/sec
*
* @return Current full-scale range setting
* @see ITG3200_FULLSCALE_2000
* @see ITG3200_RA_DLPF_FS
* @see ITG3200_DF_FS_SEL_BIT
* @see ITG3200_DF_FS_SEL_LENGTH
*/
uint8_t ITG3200::getFullScaleRange() {
I2Cdev::readBits(devAddr, ITG3200_RA_DLPF_FS, ITG3200_DF_FS_SEL_BIT, ITG3200_DF_FS_SEL_LENGTH, buffer);
return buffer[0];
}
/** Set full-scale range setting.
* @param range New full-scale range value
* @see getFullScaleRange()
* @see ITG3200_FULLSCALE_2000
* @see ITG3200_RA_DLPF_FS
* @see ITG3200_DF_FS_SEL_BIT
* @see ITG3200_DF_FS_SEL_LENGTH
*/
void ITG3200::setFullScaleRange(uint8_t range) {
I2Cdev::writeBits(devAddr, ITG3200_RA_DLPF_FS, ITG3200_DF_FS_SEL_BIT, ITG3200_DF_FS_SEL_LENGTH, range);
}
/** Get digital low-pass filter bandwidth.
* The DLPF_CFG parameter sets the digital low pass filter configuration. It
* also determines the internal sampling rate used by the device as shown in
* the table below.
*
* DLPF_CFG | Low-Pass Filter Bandwidth | Internal Sample Rate
* ---------+---------------------------+---------------------
* 0 | 256Hz | 8kHz
* 1 | 188Hz | 1kHz
* 2 | 98Hz | 1kHz
* 3 | 42Hz | 1kHz
* 4 | 20Hz | 1kHz
* 5 | 10Hz | 1kHz
* 6 | 5Hz | 1kHz
* 7 | Reserved | Reserved
*
* @return DLFP bandwidth setting
* @see ITG3200_RA_DLPF_FS
* @see ITG3200_DF_DLPF_CFG_BIT
* @see ITG3200_DF_DLPF_CFG_LENGTH
*/
uint8_t ITG3200::getDLPFBandwidth() {
I2Cdev::readBits(devAddr, ITG3200_RA_DLPF_FS, ITG3200_DF_DLPF_CFG_BIT, ITG3200_DF_DLPF_CFG_LENGTH, buffer);
return buffer[0];
}
/** Set digital low-pass filter bandwidth.
* @param bandwidth New DLFP bandwidth setting
* @see getDLPFBandwidth()
* @see ITG3200_DLPF_BW_256
* @see ITG3200_RA_DLPF_FS
* @see ITG3200_DF_DLPF_CFG_BIT
* @see ITG3200_DF_DLPF_CFG_LENGTH
*/
void ITG3200::setDLPFBandwidth(uint8_t bandwidth) {
I2Cdev::writeBits(devAddr, ITG3200_RA_DLPF_FS, ITG3200_DF_DLPF_CFG_BIT, ITG3200_DF_DLPF_CFG_LENGTH, bandwidth);
}
// INT_CFG register
/** Get interrupt logic level mode.
* Will be set 0 for active-high, 1 for active-low.
* @return Current interrupt mode (0=active-high, 1=active-low)
* @see ITG3200_RA_INT_CFG
* @see ITG3200_INTCFG_ACTL_BIT
*/
bool ITG3200::getInterruptMode() {
I2Cdev::readBit(devAddr, ITG3200_RA_INT_CFG, ITG3200_INTCFG_ACTL_BIT, buffer);
return buffer[0];
}
/** Set interrupt logic level mode.
* @param mode New interrupt mode (0=active-high, 1=active-low)
* @see getInterruptMode()
* @see ITG3200_RA_INT_CFG
* @see ITG3200_INTCFG_ACTL_BIT
*/
void ITG3200::setInterruptMode(bool mode) {
I2Cdev::writeBit(devAddr, ITG3200_RA_INT_CFG, ITG3200_INTCFG_ACTL_BIT, mode);
}
/** Get interrupt drive mode.
* Will be set 0 for push-pull, 1 for open-drain.
* @return Current interrupt drive mode (0=push-pull, 1=open-drain)
* @see ITG3200_RA_INT_CFG
* @see ITG3200_INTCFG_OPEN_BIT
*/
bool ITG3200::getInterruptDrive() {
I2Cdev::readBit(devAddr, ITG3200_RA_INT_CFG, ITG3200_INTCFG_OPEN_BIT, buffer);
return buffer[0];
}
/** Set interrupt drive mode.
* @param drive New interrupt drive mode (0=push-pull, 1=open-drain)
* @see getInterruptDrive()
* @see ITG3200_RA_INT_CFG
* @see ITG3200_INTCFG_OPEN_BIT
*/
void ITG3200::setInterruptDrive(bool drive) {
I2Cdev::writeBit(devAddr, ITG3200_RA_INT_CFG, ITG3200_INTCFG_OPEN_BIT, drive);
}
/** Get interrupt latch mode.
* Will be set 0 for 50us-pulse, 1 for latch-until-int-cleared.
* @return Current latch mode (0=50us-pulse, 1=latch-until-int-cleared)
* @see ITG3200_RA_INT_CFG
* @see ITG3200_INTCFG_LATCH_INT_EN_BIT
*/
bool ITG3200::getInterruptLatch() {
I2Cdev::readBit(devAddr, ITG3200_RA_INT_CFG, ITG3200_INTCFG_LATCH_INT_EN_BIT, buffer);
return buffer[0];
}
/** Set interrupt latch mode.
* @param latch New latch mode (0=50us-pulse, 1=latch-until-int-cleared)
* @see getInterruptLatch()
* @see ITG3200_RA_INT_CFG
* @see ITG3200_INTCFG_LATCH_INT_EN_BIT
*/
void ITG3200::setInterruptLatch(bool latch) {
I2Cdev::writeBit(devAddr, ITG3200_RA_INT_CFG, ITG3200_INTCFG_LATCH_INT_EN_BIT, latch);
}
/** Get interrupt latch clear mode.
* Will be set 0 for status-read-only, 1 for any-register-read.
* @return Current latch clear mode (0=status-read-only, 1=any-register-read)
* @see ITG3200_RA_INT_CFG
* @see ITG3200_INTCFG_INT_ANYRD_2CLEAR_BIT
*/
bool ITG3200::getInterruptLatchClear() {
I2Cdev::readBit(devAddr, ITG3200_RA_INT_CFG, ITG3200_INTCFG_INT_ANYRD_2CLEAR_BIT, buffer);
return buffer[0];
}
/** Set interrupt latch clear mode.
* @param clear New latch clear mode (0=status-read-only, 1=any-register-read)
* @see getInterruptLatchClear()
* @see ITG3200_RA_INT_CFG
* @see ITG3200_INTCFG_INT_ANYRD_2CLEAR_BIT
*/
void ITG3200::setInterruptLatchClear(bool clear) {
I2Cdev::writeBit(devAddr, ITG3200_RA_INT_CFG, ITG3200_INTCFG_INT_ANYRD_2CLEAR_BIT, clear);
}
/** Get "device ready" interrupt enabled setting.
* Will be set 0 for disabled, 1 for enabled.
* @return Current interrupt enabled setting
* @see ITG3200_RA_INT_CFG
* @see ITG3200_INTCFG_ITG_RDY_EN_BIT
*/
bool ITG3200::getIntDeviceReadyEnabled() {
I2Cdev::readBit(devAddr, ITG3200_RA_INT_CFG, ITG3200_INTCFG_ITG_RDY_EN_BIT, buffer);
return buffer[0];
}
/** Set "device ready" interrupt enabled setting.
* @param enabled New interrupt enabled setting
* @see getIntDeviceReadyEnabled()
* @see ITG3200_RA_INT_CFG
* @see ITG3200_INTCFG_ITG_RDY_EN_BIT
*/
void ITG3200::setIntDeviceReadyEnabled(bool enabled) {
I2Cdev::writeBit(devAddr, ITG3200_RA_INT_CFG, ITG3200_INTCFG_ITG_RDY_EN_BIT, enabled);
}
/** Get "data ready" interrupt enabled setting.
* Will be set 0 for disabled, 1 for enabled.
* @return Current interrupt enabled setting
* @see ITG3200_RA_INT_CFG
* @see ITG3200_INTCFG_RAW_RDY_EN_BIT
*/
bool ITG3200::getIntDataReadyEnabled() {
I2Cdev::readBit(devAddr, ITG3200_RA_INT_CFG, ITG3200_INTCFG_RAW_RDY_EN_BIT, buffer);
return buffer[0];
}
/** Set "data ready" interrupt enabled setting.
* @param enabled New interrupt enabled setting
* @see getIntDataReadyEnabled()
* @see ITG3200_RA_INT_CFG
* @see ITG3200_INTCFG_RAW_RDY_EN_BIT
*/
void ITG3200::setIntDataReadyEnabled(bool enabled) {
I2Cdev::writeBit(devAddr, ITG3200_RA_INT_CFG, ITG3200_INTCFG_RAW_RDY_EN_BIT, enabled);
}
// INT_STATUS register
/** Get Device Ready interrupt status.
* The ITG_RDY interrupt indicates that the PLL is ready and gyroscopic data can
* be read.
* @return Device Ready interrupt status
* @see ITG3200_RA_INT_STATUS
* @see ITG3200_INTSTAT_RAW_DATA_READY_BIT
*/
bool ITG3200::getIntDeviceReadyStatus() {
I2Cdev::readBit(devAddr, ITG3200_RA_INT_STATUS, ITG3200_INTSTAT_ITG_RDY_BIT, buffer);
return buffer[0];
}
/** Get Data Ready interrupt status.
* In normal use, the RAW_DATA_RDY interrupt is used to determine when new
* sensor data is available in and of the sensor registers (27 to 32).
* @return Data Ready interrupt status
* @see ITG3200_RA_INT_STATUS
* @see ITG3200_INTSTAT_RAW_DATA_READY_BIT
*/
bool ITG3200::getIntDataReadyStatus() {
I2Cdev::readBit(devAddr, ITG3200_RA_INT_STATUS, ITG3200_INTSTAT_RAW_DATA_READY_BIT, buffer);
return buffer[0];
}
// TEMP_OUT_* registers
/** Get current internal temperature.
* @return Temperature reading in 16-bit 2's complement format
* @see ITG3200_RA_TEMP_OUT_H
*/
int16_t ITG3200::getTemperature() {
I2Cdev::readBytes(devAddr, ITG3200_RA_TEMP_OUT_H, 2, buffer);
return (((int16_t)buffer[0]) << 8) | buffer[1];
}
// GYRO_*OUT_* registers
/** Get 3-axis gyroscope readings.
* @param x 16-bit signed integer container for X-axis rotation
* @param y 16-bit signed integer container for Y-axis rotation
* @param z 16-bit signed integer container for Z-axis rotation
* @see ITG3200_RA_GYRO_XOUT_H
*/
void ITG3200::getRotation(int16_t* x, int16_t* y, int16_t* z) {
I2Cdev::readBytes(devAddr, ITG3200_RA_GYRO_XOUT_H, 6, buffer);
*x = (((int16_t)buffer[0]) << 8) | buffer[1];
*y = (((int16_t)buffer[2]) << 8) | buffer[3];
*z = (((int16_t)buffer[4]) << 8) | buffer[5];
}
/** Get X-axis gyroscope reading.
* @return X-axis rotation measurement in 16-bit 2's complement format
* @see ITG3200_RA_GYRO_XOUT_H
*/
int16_t ITG3200::getRotationX() {
I2Cdev::readBytes(devAddr, ITG3200_RA_GYRO_XOUT_H, 2, buffer);
return (((int16_t)buffer[0]) << 8) | buffer[1];
}
/** Get Y-axis gyroscope reading.
* @return Y-axis rotation measurement in 16-bit 2's complement format
* @see ITG3200_RA_GYRO_YOUT_H
*/
int16_t ITG3200::getRotationY() {
I2Cdev::readBytes(devAddr, ITG3200_RA_GYRO_YOUT_H, 2, buffer);
return (((int16_t)buffer[0]) << 8) | buffer[1];
}
/** Get Z-axis gyroscope reading.
* @return Z-axis rotation measurement in 16-bit 2's complement format
* @see ITG3200_RA_GYRO_ZOUT_H
*/
int16_t ITG3200::getRotationZ() {
I2Cdev::readBytes(devAddr, ITG3200_RA_GYRO_ZOUT_H, 2, buffer);
return (((int16_t)buffer[0]) << 8) | buffer[1];
}
// PWR_MGM register
/** Trigger a full device reset.
* A small delay of ~50ms may be desirable after triggering a reset.
* @see ITG3200_RA_PWR_MGM
* @see ITG3200_PWR_H_RESET_BIT
*/
void ITG3200::reset() {
I2Cdev::writeBit(devAddr, ITG3200_RA_PWR_MGM, ITG3200_PWR_H_RESET_BIT, true);
}
/** Get sleep mode status.
* Setting the SLEEP bit in the register puts the device into very low power
* sleep mode. In this mode, only the serial interface and internal registers
* remain active, allowing for a very low standby current. Clearing this bit
* puts the device back into normal mode. To save power, the individual standby
* selections for each of the gyros should be used if any gyro axis is not used
* by the application.
* @return Current sleep mode enabled status
* @see ITG3200_RA_PWR_MGM
* @see ITG3200_PWR_SLEEP_BIT
*/
bool ITG3200::getSleepEnabled() {
I2Cdev::readBit(devAddr, ITG3200_RA_PWR_MGM, ITG3200_PWR_SLEEP_BIT, buffer);
return buffer[0];
}
/** Set sleep mode status.
* @param enabled New sleep mode enabled status
* @see getSleepEnabled()
* @see ITG3200_RA_PWR_MGM
* @see ITG3200_PWR_SLEEP_BIT
*/
void ITG3200::setSleepEnabled(bool enabled) {
I2Cdev::writeBit(devAddr, ITG3200_RA_PWR_MGM, ITG3200_PWR_SLEEP_BIT, enabled);
}
/** Get X-axis standby enabled status.
* If enabled, the X-axis will not gather or report data (or use power).
* @return Current X-axis standby enabled status
* @see ITG3200_RA_PWR_MGM
* @see ITG3200_PWR_STBY_XG_BIT
*/
bool ITG3200::getStandbyXEnabled() {
I2Cdev::readBit(devAddr, ITG3200_RA_PWR_MGM, ITG3200_PWR_STBY_XG_BIT, buffer);
return buffer[0];
}
/** Set X-axis standby enabled status.
* @param New X-axis standby enabled status
* @see getStandbyXEnabled()
* @see ITG3200_RA_PWR_MGM
* @see ITG3200_PWR_STBY_XG_BIT
*/
void ITG3200::setStandbyXEnabled(bool enabled) {
I2Cdev::writeBit(devAddr, ITG3200_RA_PWR_MGM, ITG3200_PWR_STBY_XG_BIT, enabled);
}
/** Get Y-axis standby enabled status.
* If enabled, the Y-axis will not gather or report data (or use power).
* @return Current Y-axis standby enabled status
* @see ITG3200_RA_PWR_MGM
* @see ITG3200_PWR_STBY_YG_BIT
*/
bool ITG3200::getStandbyYEnabled() {
I2Cdev::readBit(devAddr, ITG3200_RA_PWR_MGM, ITG3200_PWR_STBY_YG_BIT, buffer);
return buffer[0];
}
/** Set Y-axis standby enabled status.
* @param New Y-axis standby enabled status
* @see getStandbyYEnabled()
* @see ITG3200_RA_PWR_MGM
* @see ITG3200_PWR_STBY_YG_BIT
*/
void ITG3200::setStandbyYEnabled(bool enabled) {
I2Cdev::writeBit(devAddr, ITG3200_RA_PWR_MGM, ITG3200_PWR_STBY_YG_BIT, enabled);
}
/** Get Z-axis standby enabled status.
* If enabled, the Z-axis will not gather or report data (or use power).
* @return Current Z-axis standby enabled status
* @see ITG3200_RA_PWR_MGM
* @see ITG3200_PWR_STBY_ZG_BIT
*/
bool ITG3200::getStandbyZEnabled() {
I2Cdev::readBit(devAddr, ITG3200_RA_PWR_MGM, ITG3200_PWR_STBY_ZG_BIT, buffer);
return buffer[0];
}
/** Set Z-axis standby enabled status.
* @param New Z-axis standby enabled status
* @see getStandbyZEnabled()
* @see ITG3200_RA_PWR_MGM
* @see ITG3200_PWR_STBY_ZG_BIT
*/
void ITG3200::setStandbyZEnabled(bool enabled) {
I2Cdev::writeBit(devAddr, ITG3200_RA_PWR_MGM, ITG3200_PWR_STBY_ZG_BIT, enabled);
}
/** Get clock source setting.
* @return Current clock source setting
* @see ITG3200_RA_PWR_MGM
* @see ITG3200_PWR_CLK_SEL_BIT
* @see ITG3200_PWR_CLK_SEL_LENGTH
*/
uint8_t ITG3200::getClockSource() {
I2Cdev::readBits(devAddr, ITG3200_RA_PWR_MGM, ITG3200_PWR_CLK_SEL_BIT, ITG3200_PWR_CLK_SEL_LENGTH, buffer);
return buffer[0];
}
/** Set clock source setting.
* On power up, the ITG-3200 defaults to the internal oscillator. It is highly recommended that the device is configured to use one of the gyros (or an external clock) as the clock reference, due to the improved stability.
*
* The CLK_SEL setting determines the device clock source as follows:
*
* CLK_SEL | Clock Source
* --------+--------------------------------------
* 0 | Internal oscillator
* 1 | PLL with X Gyro reference
* 2 | PLL with Y Gyro reference
* 3 | PLL with Z Gyro reference
* 4 | PLL with external 32.768kHz reference
* 5 | PLL with external 19.2MHz reference
* 6 | Reserved
* 7 | Reserved
*
* @param source New clock source setting
* @see getClockSource()
* @see ITG3200_RA_PWR_MGM
* @see ITG3200_PWR_CLK_SEL_BIT
* @see ITG3200_PWR_CLK_SEL_LENGTH
*/
void ITG3200::setClockSource(uint8_t source) {
I2Cdev::writeBits(devAddr, ITG3200_RA_PWR_MGM, ITG3200_PWR_CLK_SEL_BIT, ITG3200_PWR_CLK_SEL_LENGTH, source);
}