|
| 1 | +/*! |
| 2 | + * @file drvLsm9ds1.cpp |
| 3 | + * |
| 4 | + * Driver wrapper for the Adafruit LSM9DS1 9-DOF IMU. |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | +#include "drvLsm9ds1.h" |
| 9 | + |
| 10 | +#include <Adafruit_LSM9DS1.h> |
| 11 | +#include <math.h> |
| 12 | + |
| 13 | +/******************************************************************************/ |
| 14 | +/*! |
| 15 | + @brief Destructor for a LSM9DS1 sensor. |
| 16 | +*/ |
| 17 | +/******************************************************************************/ |
| 18 | +drvLsm9ds1::~drvLsm9ds1() { |
| 19 | + if (_lsm) { |
| 20 | + delete _lsm; |
| 21 | + _lsm = nullptr; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/******************************************************************************/ |
| 26 | +/*! |
| 27 | + @brief Initializes the LSM9DS1 sensor and begins I2C. |
| 28 | + @returns True if initialized successfully, False otherwise. |
| 29 | +*/ |
| 30 | +/******************************************************************************/ |
| 31 | +bool drvLsm9ds1::begin() { |
| 32 | + _lsm = new Adafruit_LSM9DS1(_i2c); |
| 33 | + // Consumes I2C Addresses 0x1E and 0x6B |
| 34 | + if (!_lsm->begin()) { |
| 35 | + WS_DEBUG_PRINTLN("LSM9DS1 failed to initialise!"); |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + // Mirror the configuration used by the reference example |
| 40 | + _lsm->setupAccel(_lsm->LSM9DS1_ACCELRANGE_2G, |
| 41 | + _lsm->LSM9DS1_ACCELDATARATE_10HZ); |
| 42 | + _lsm->setupMag(_lsm->LSM9DS1_MAGGAIN_4GAUSS); |
| 43 | + _lsm->setupGyro(_lsm->LSM9DS1_GYROSCALE_245DPS); |
| 44 | + |
| 45 | + return true; |
| 46 | +} |
| 47 | + |
| 48 | +bool drvLsm9ds1::readAllEvents(sensors_event_t *accel, |
| 49 | + sensors_event_t *mag, |
| 50 | + sensors_event_t *gyro, |
| 51 | + sensors_event_t *temp) { |
| 52 | + if (!_lsm) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + _lsm->read(); |
| 56 | + _lsm->getEvent(accel, mag, gyro, temp); |
| 57 | + return true; |
| 58 | +} |
| 59 | + |
| 60 | +/******************************************************************************/ |
| 61 | +/*! |
| 62 | + @brief Gets the LSM9DS1's raw sensor event. |
| 63 | + @param rawEvent |
| 64 | + Pointer to the sensor event. |
| 65 | + @returns True if the sensor event was obtained successfully, False |
| 66 | + otherwise. |
| 67 | +*/ |
| 68 | +/******************************************************************************/ |
| 69 | +bool drvLsm9ds1::getEventRaw(sensors_event_t *rawEvent) { |
| 70 | + //TODO: Not yet, but eventually migrate to providing the temperatre data here |
| 71 | + WS_DEBUG_PRINTLN("[drvLsm9ds1] Getting raw event..."); |
| 72 | + sensors_event_t accel, mag, gyro, temp; |
| 73 | + if (!readAllEvents(&accel, &mag, &gyro, &temp)) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + float mag_accel = sqrtf(accel.acceleration.x * accel.acceleration.x + |
| 78 | + accel.acceleration.y * accel.acceleration.y + |
| 79 | + accel.acceleration.z * accel.acceleration.z); |
| 80 | + rawEvent->data[0] = mag_accel; |
| 81 | + WS_DEBUG_PRINT("[drvLsm9ds1] Raw magnitude: "); |
| 82 | + WS_DEBUG_PRINTLN(mag_accel); |
| 83 | + return true; |
| 84 | +} |
| 85 | + |
| 86 | +/******************************************************************************/ |
| 87 | +/*! |
| 88 | + @brief Gets the LSM9DS1's boolean sensor event. |
| 89 | + @param booleanEvent |
| 90 | + Pointer to the sensor event. |
| 91 | + @returns True if the sensor event was obtained successfully, False |
| 92 | + otherwise. |
| 93 | +*/ |
| 94 | +/******************************************************************************/ |
| 95 | +bool drvLsm9ds1::getEventBoolean(sensors_event_t *booleanEvent) { |
| 96 | + WS_DEBUG_PRINTLN("[drvLsm9ds1] Checking for tap event..."); |
| 97 | + sensors_event_t accel, mag, gyro, temp; |
| 98 | + if (!readAllEvents(&accel, &mag, &gyro, &temp)) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + float mag_accel = sqrtf(accel.acceleration.x * accel.acceleration.x + |
| 103 | + accel.acceleration.y * accel.acceleration.y + |
| 104 | + accel.acceleration.z * accel.acceleration.z); |
| 105 | + |
| 106 | + bool tap_detected = (mag_accel > LSM9DS1_TAP_THRESHOLD_MSS); |
| 107 | + booleanEvent->data[0] = tap_detected ? 1.0f : 0.0f; |
| 108 | + |
| 109 | + if (tap_detected) { |
| 110 | + WS_DEBUG_PRINTLN("[drvLsm9ds1] Tap event detected!"); |
| 111 | + } |
| 112 | + |
| 113 | + return true; |
| 114 | +} |
| 115 | + |
| 116 | +/******************************************************************************/ |
| 117 | +/*! |
| 118 | + @brief Gets the LSM9DS1's accelerometer sensor event (x,y,z in m/s^2). |
| 119 | + @param accelEvent |
| 120 | + Pointer to the accelerometer sensor event. |
| 121 | + @returns True if the sensor event was obtained successfully, False |
| 122 | + otherwise. |
| 123 | +*/ |
| 124 | +/******************************************************************************/ |
| 125 | +bool drvLsm9ds1::getEventAccelerometer(sensors_event_t *accelEvent) { |
| 126 | + WS_DEBUG_PRINTLN("[drvLsm9ds1] Getting accelerometer event..."); |
| 127 | + sensors_event_t mag, gyro, temp; |
| 128 | + if (!readAllEvents(accelEvent, &mag, &gyro, &temp)) { |
| 129 | + return false; |
| 130 | + } |
| 131 | + return true; |
| 132 | +} |
| 133 | + |
| 134 | +/******************************************************************************/ |
| 135 | +/*! |
| 136 | + @brief Gets the LSM9DS1's gyroscope sensor event (x,y,z in rad/s). |
| 137 | + @param gyroEvent |
| 138 | + Pointer to the gyroscope sensor event. |
| 139 | + @returns True if the sensor event was obtained successfully, False |
| 140 | + otherwise. |
| 141 | +*/ |
| 142 | +/******************************************************************************/ |
| 143 | +bool drvLsm9ds1::getEventGyroscope(sensors_event_t *gyroEvent) { |
| 144 | + WS_DEBUG_PRINTLN("[drvLsm9ds1] Getting gyroscope event..."); |
| 145 | + sensors_event_t accel, mag, temp; |
| 146 | + if (!readAllEvents(&accel, &mag, gyroEvent, &temp)) { |
| 147 | + return false; |
| 148 | + } |
| 149 | + return true; |
| 150 | +} |
| 151 | + |
| 152 | +/******************************************************************************/ |
| 153 | +/*! |
| 154 | + @brief Gets the LSM9DS1's magnetometer sensor event (x,y,z in uT). |
| 155 | + @param magEvent |
| 156 | + Pointer to the magnetometer sensor event. |
| 157 | + @returns True if the sensor event was obtained successfully, False |
| 158 | + otherwise. |
| 159 | +*/ |
| 160 | +/******************************************************************************/ |
| 161 | +bool drvLsm9ds1::getEventMagneticField(sensors_event_t *magEvent) { |
| 162 | + WS_DEBUG_PRINTLN("[drvLsm9ds1] Getting magnetometer event..."); |
| 163 | + sensors_event_t accel, gyro, temp; |
| 164 | + if (!readAllEvents(&accel, magEvent, &gyro, &temp)) { |
| 165 | + return false; |
| 166 | + } |
| 167 | + return true; |
| 168 | +} |
| 169 | + |
| 170 | +void drvLsm9ds1::ConfigureDefaultSensorTypes() { |
| 171 | + _default_sensor_types_count = 1; |
| 172 | + _default_sensor_types[0] = |
| 173 | + wippersnapper_sensor_SensorType_SENSOR_TYPE_ACCELEROMETER; |
| 174 | +} |
0 commit comments