This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
float ReefwingLSM9DS1::readTemp(TempScale scale) { | |
uint8_t OUT_TEMP_L = readByte(LSM9DS1AG_ADDRESS, LSM9DS1AG_OUT_TEMP_L); | |
uint8_t OUT_TEMP_H = readByte(LSM9DS1AG_ADDRESS, LSM9DS1AG_OUT_TEMP_H); | |
uint16_t count = (OUT_TEMP_H << 8) | (OUT_TEMP_L & 0xff); | |
int16_t val = (int16_t)count; | |
float result = ((float)val)/16.0f + _config.temp.offset; // In Celsius | |
switch(scale) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool ReefwingLSM9DS1::gyroAvailable() { | |
uint8_t STATUS_REG = readByte(LSM9DS1AG_ADDRESS, LSM9DS1AG_STATUS_REG); | |
return (STATUS_REG & 0x02); | |
} | |
bool ReefwingLSM9DS1::accelAvailable() { | |
uint8_t STATUS_REG = readByte(LSM9DS1AG_ADDRESS, LSM9DS1AG_STATUS_REG); | |
return (STATUS_REG & 0x01); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void ReefwingLSM9DS1::start() { | |
// Initialise the Gyro - assumes configuration is done | |
// Control Register 4 - enable gyro axis output bits (5, 4 & 3) | |
// The other bits default to 0. CTRL_REG4 = 0b00111000 = 0x38 | |
writeByte(LSM9DS1AG_ADDRESS, LSM9DS1AG_CTRL_REG4, 0x38); | |
// Initialise the Accelerometer | |
// Control Register 5 XL - enable accel axis output bits (5, 4 & 3) | |
// The other bits default to 0. CTRL_REG5_XL = 0b00111000 = 0x38 | |
writeByte(LSM9DS1AG_ADDRESS, LSM9DS1AG_CTRL_REG5_XL, 0x38); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void ReefwingLSM9DS1::setAccelBandwidth(AccelBW bandwidth) { | |
if (!_config.accel.autoBandwidthEnable) { | |
uint8_t CTRL_REG6_XL = readByte(LSM9DS1AG_ADDRESS, LSM9DS1AG_CTRL_REG6_XL); | |
// Clear the BW_XL bits (1 & 0), maintain the rest | |
// 0xFC = 0b11111100, BW_XL = 00, bandwidth = 408 Hz (default) | |
CTRL_REG6_XL &= 0xFC; | |
switch(bandwidth) { | |
case AccelBW::BW_50Hz: // BW_XL = 11 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void ReefwingLSM9DS1::setGyroBandwidth(GyroBW bandwidth) { | |
uint8_t CTRL_REG1_G = readByte(LSM9DS1AG_ADDRESS, LSM9DS1AG_CTRL_REG1_G); | |
// Clear the BW_G bits (0 & 1), maintain the rest | |
// 0xFC = 0b11111100, BW_G = 00, bandwidth = LOW (default) | |
CTRL_REG1_G &= 0xFC; | |
switch(bandwidth) { | |
case GyroBW::MIDDLE: // BW_G = 01 | |
CTRL_REG1_G |= 0x01; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void ReefwingLSM9DS1::setMagODR(MagODR rate) { | |
if (rate > ODR_80Hz) { | |
enableFastODR(true); | |
switch(rate) { | |
case MagODR::ODR_155Hz: // OM = 11, mode = ULTRA | |
setMagOperatingMode(ULTRA); | |
break; | |
case MagODR::ODR_300Hz: // OM = 10, mode = HIGH |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void ReefwingLSM9DS1::setGyroODR(GyroODR rate) { | |
uint8_t CTRL_REG1_G = readByte(LSM9DS1AG_ADDRESS, LSM9DS1AG_CTRL_REG1_G); | |
// Clear the ODR_G bits (7, 6 & 5), maintain the rest | |
// 0x1F = 0b00011111, ODR_G = 000, mode = power-down | |
CTRL_REG1_G &= 0x1F; | |
_config.gyro.powerDown = false; | |
_config.gyroAccelOpMode = NORMAL; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void ReefwingLSM9DS1::setGyroScale(GyroScale scale) { | |
uint8_t CTRL_REG1_G = readByte(LSM9DS1AG_ADDRESS, LSM9DS1AG_CTRL_REG1_G); | |
// Clear the FS bits (3 & 4), maintain the rest | |
// 0xE7 = 0b11100111, FS = 00, scale = 245 DPS | |
CTRL_REG1_G &= 0xE7; | |
switch(scale) { | |
case GyroScale::FS_500DPS: | |
CTRL_REG1_G |= (0x01 << 3); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void ReefwingLSM9DS1::reset() { | |
writeByte(LSM9DS1AG_ADDRESS, LSM9DS1AG_CTRL_REG8, 0x05); | |
writeByte(LSM9DS1M_ADDRESS, LSM9DS1M_CTRL_REG2_M, 0x0c); | |
delay(20); | |
// Check that chip boot up is complete - bit 3 is BOOT_STATUS | |
// Mask the STATUS_REG with 0b00001000 = 0x08 | |
while ((readByte(LSM9DS1AG_ADDRESS, LSM9DS1AG_STATUS_REG) & 0x08) != 0) { | |
yield(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include <Adafruit_Sensor.h> | |
#include <Adafruit_LSM9DS1.h> | |
Adafruit_LSM9DS1 lsm; | |
void setup() { | |
Serial.begin(9600); | |
if (!lsm.begin()) { | |
Serial.println("Failed to initialize LSM9DS1!"); |