|
| 1 | +/*! |
| 2 | + * @file drvLsm6ds3.cpp |
| 3 | + * |
| 4 | + * Driver wrapper for the Adafruit LSM6DS3 6-DoF IMU. |
| 5 | + */ |
| 6 | + |
| 7 | +#include "drvLsm6ds3.h" |
| 8 | + |
| 9 | +#include <math.h> |
| 10 | + |
| 11 | +/******************************************************************************/ |
| 12 | +/*! @brief Destructor */ |
| 13 | +/******************************************************************************/ |
| 14 | +drvLsm6ds3::~drvLsm6ds3() { |
| 15 | + if (_imu) { |
| 16 | + delete _imu; |
| 17 | + _imu = nullptr; |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +/******************************************************************************/ |
| 22 | +bool drvLsm6ds3::begin() { |
| 23 | + if (_imu) { |
| 24 | + delete _imu; |
| 25 | + _imu = nullptr; |
| 26 | + } |
| 27 | + |
| 28 | + _imu = new Adafruit_LSM6DS3(); |
| 29 | + if (!_imu) { |
| 30 | + return false; |
| 31 | + } |
| 32 | + |
| 33 | + uint8_t addr = _address == 0 ? LSM6DS_I2CADDR_DEFAULT : (uint8_t)_address; |
| 34 | + WS_DEBUG_PRINT("[drvLsm6ds3] Initialising @ 0x"); |
| 35 | + WS_DEBUG_PRINTHEX(addr); |
| 36 | + WS_DEBUG_PRINTLN("..."); |
| 37 | + |
| 38 | + if (!_imu->begin_I2C(addr, _i2c)) { |
| 39 | + WS_DEBUG_PRINTLN("[drvLsm6ds3] Failed to initialise sensor"); |
| 40 | + delete _imu; |
| 41 | + _imu = nullptr; |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + _imu->setAccelRange(LSM6DS_ACCEL_RANGE_4_G); |
| 46 | + _imu->setAccelDataRate(LSM6DS_RATE_104_HZ); |
| 47 | + _imu->setGyroRange(LSM6DS_GYRO_RANGE_500_DPS); |
| 48 | + _imu->setGyroDataRate(LSM6DS_RATE_104_HZ); |
| 49 | + _imu->configInt1(false, false, false, false, true); |
| 50 | + _imu->configInt2(false, false, false); |
| 51 | + _imu->enableWakeup(true); |
| 52 | + |
| 53 | + WS_DEBUG_PRINTLN("[drvLsm6ds3] Sensor initialised successfully"); |
| 54 | + return true; |
| 55 | +} |
| 56 | + |
| 57 | +bool drvLsm6ds3::readAllEvents(sensors_event_t *accel, |
| 58 | + sensors_event_t *gyro, |
| 59 | + sensors_event_t *temp) { |
| 60 | + if (!_imu) { |
| 61 | + return false; |
| 62 | + } |
| 63 | + return _imu->getEvent(accel, gyro, temp); |
| 64 | +} |
| 65 | + |
| 66 | +bool drvLsm6ds3::computeAccelMagnitude(float &magnitude) { |
| 67 | + sensors_event_t accel, gyro, temp; |
| 68 | + if (!readAllEvents(&accel, &gyro, &temp)) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + magnitude = sqrtf(accel.acceleration.x * accel.acceleration.x + |
| 72 | + accel.acceleration.y * accel.acceleration.y + |
| 73 | + accel.acceleration.z * accel.acceleration.z); |
| 74 | + return true; |
| 75 | +} |
| 76 | + |
| 77 | +bool drvLsm6ds3::getEventRaw(sensors_event_t *rawEvent) { |
| 78 | + if (!_imu) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + float magnitude = 0.0f; |
| 82 | + if (!computeAccelMagnitude(magnitude)) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + rawEvent->data[0] = magnitude; |
| 86 | + return true; |
| 87 | +} |
| 88 | + |
| 89 | +bool drvLsm6ds3::getEventBoolean(sensors_event_t *booleanEvent) { |
| 90 | + if (!_imu) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + bool shake = _imu->shake(); |
| 94 | + booleanEvent->data[0] = shake ? 1.0f : 0.0f; |
| 95 | + return true; |
| 96 | +} |
| 97 | + |
| 98 | +bool drvLsm6ds3::getEventAccelerometer(sensors_event_t *accelEvent) { |
| 99 | + if (!_imu) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + sensors_event_t gyro, temp; |
| 103 | + return readAllEvents(accelEvent, &gyro, &temp); |
| 104 | +} |
| 105 | + |
| 106 | +bool drvLsm6ds3::getEventGyroscope(sensors_event_t *gyroEvent) { |
| 107 | + if (!_imu) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + sensors_event_t accel, temp; |
| 111 | + return readAllEvents(&accel, gyroEvent, &temp); |
| 112 | +} |
| 113 | + |
| 114 | +void drvLsm6ds3::ConfigureDefaultSensorTypes() { |
| 115 | + _default_sensor_types_count = 1; |
| 116 | + _default_sensor_types[0] = |
| 117 | + wippersnapper_sensor_SensorType_SENSOR_TYPE_ACCELEROMETER; |
| 118 | +} |
0 commit comments