From 653d6a0fd6f1664c610f82b2937e215e75f50848 Mon Sep 17 00:00:00 2001 From: Alex Brudner <101155592+SFE-Brudnerd@users.noreply.github.com> Date: Mon, 14 Aug 2023 12:29:59 -0600 Subject: [PATCH 1/3] Refactor Data Ranges. Moved conversion calculation from raw data function to var storage. This reduces the number of times this is computed. --- src/SparkFun_BMI270_Arduino_Library.cpp | 60 ++++++++++++++----------- src/SparkFun_BMI270_Arduino_Library.h | 2 +- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/SparkFun_BMI270_Arduino_Library.cpp b/src/SparkFun_BMI270_Arduino_Library.cpp index 4a8331d..80a1a76 100644 --- a/src/SparkFun_BMI270_Arduino_Library.cpp +++ b/src/SparkFun_BMI270_Arduino_Library.cpp @@ -38,8 +38,22 @@ int8_t BMI270::begin() if(err != BMI2_OK) return err; // Store the accelerometer and gyroscope ranges, these are needed elsewhere - accRange = configs[0].cfg.acc.range; - gyrRange = configs[1].cfg.gyr.range; + // config.cfg.acc.range | accRange + // --------------------------- + // BMI2_ACC_RANGE_2G | 2 + // BMI2_ACC_RANGE_4G | 4 + // BMI2_ACC_RANGE_8G | 8 + // BMI2_ACC_RANGE_16G | 16 + accRange = 2 << configs[0].cfg.acc.range; + + // config.cfg.gyr.range | gyrRange + // ------------------------------ + // BMI2_GYR_RANGE_2000 | 2000 + // BMI2_GYR_RANGE_1000 | 1000 + // BMI2_GYR_RANGE_500 | 500 + // BMI2_GYR_RANGE_250 | 250 + // BMI2_GYR_RANGE_125 | 125 + gyrRange = 125 * (1 << (BMI2_GYR_RANGE_125 - configs[1].cfg.gyr.range)); // Done! return BMI2_OK; @@ -135,19 +149,9 @@ int8_t BMI270::remapAxes(bmi2_remap axes) /// @param data Output data void BMI270::convertRawAccelData(bmi2_sens_axes_data* rawData, BMI270_SensorData* data) { - // Compute the current accelerometer range as a number - // - // accRange | gRange - // --------------------------- - // BMI2_ACC_RANGE_2G | 2 - // BMI2_ACC_RANGE_4G | 4 - // BMI2_ACC_RANGE_8G | 8 - // BMI2_ACC_RANGE_16G | 16 - uint8_t gRange = 2 << accRange; - // Compute conversion factor from raw to g's. Raw data are signed 16-bit // integers, so resolution is gRange / 2^15 = 32768 - float rawToGs = gRange / 32768.0; + float rawToGs = accRange / 32768.0; // Convert raw data to g's data->accelX = rawData->x * rawToGs; @@ -160,20 +164,9 @@ void BMI270::convertRawAccelData(bmi2_sens_axes_data* rawData, BMI270_SensorData /// @param data Output data void BMI270::convertRawGyroData(bmi2_sens_axes_data* rawData, BMI270_SensorData* data) { - // Compute the current gyro range as a number - // - // gyrRange | dpsRange - // ------------------------------ - // BMI2_GYR_RANGE_2000 | 2000 - // BMI2_GYR_RANGE_1000 | 1000 - // BMI2_GYR_RANGE_500 | 500 - // BMI2_GYR_RANGE_250 | 250 - // BMI2_GYR_RANGE_125 | 125 - uint16_t dpsRange = 125 * (1 << (BMI2_GYR_RANGE_125 - gyrRange)); - // Compute conversion factor from raw to deg/sec. Raw data are signed 16-bit // integers, so resolution is dpsRange / 2^15 = 32768 - float rawToDegSec = dpsRange / 32768.0; + float rawToDegSec = gyrRange / 32768.0; // Convert raw data to deg/sec data->gyroX = rawData->x * rawToDegSec; @@ -441,12 +434,25 @@ int8_t BMI270::setConfigs(bmi2_sens_config* configs, uint8_t numConfigs) if(configs[i].type == BMI2_ACCEL) { // Update accelerometer range - accRange = configs[i].cfg.acc.range; + // config.cfg.acc.range | accRange + // --------------------------- + // BMI2_ACC_RANGE_2G | 2 + // BMI2_ACC_RANGE_4G | 4 + // BMI2_ACC_RANGE_8G | 8 + // BMI2_ACC_RANGE_16G | 16 + accRange = 2 << configs[i].cfg.acc.range; } else if(configs[i].type == BMI2_GYRO) { // Update gyro range - gyrRange = configs[i].cfg.gyr.range; + // config.cfg.gyr.range | gyrRange + // ------------------------------ + // BMI2_GYR_RANGE_2000 | 2000 + // BMI2_GYR_RANGE_1000 | 1000 + // BMI2_GYR_RANGE_500 | 500 + // BMI2_GYR_RANGE_250 | 250 + // BMI2_GYR_RANGE_125 | 125 + gyrRange = 125 * (1 << (BMI2_GYR_RANGE_125 - configs[i].cfg.gyr.range)); } } diff --git a/src/SparkFun_BMI270_Arduino_Library.h b/src/SparkFun_BMI270_Arduino_Library.h index fd56d77..45e5ac5 100644 --- a/src/SparkFun_BMI270_Arduino_Library.h +++ b/src/SparkFun_BMI270_Arduino_Library.h @@ -261,7 +261,7 @@ class BMI270 // Need to track the range of each sensor for converting raw data uint8_t accRange; - uint8_t gyrRange; + uint16_t gyrRange; // Need to track the FIFO config for some FIFO functions uint16_t fifoConfigFlags; From a6ffc159d471aee46db5d90c5cd1007083be7a9d Mon Sep 17 00:00:00 2001 From: Alex Brudner <101155592+SFE-Brudnerd@users.noreply.github.com> Date: Mon, 14 Aug 2023 12:55:09 -0600 Subject: [PATCH 2/3] Added helper functions. Now doing all conversion at once inside helper functions. --- src/SparkFun_BMI270_Arduino_Library.cpp | 74 ++++++++++++------------- src/SparkFun_BMI270_Arduino_Library.h | 7 ++- 2 files changed, 39 insertions(+), 42 deletions(-) diff --git a/src/SparkFun_BMI270_Arduino_Library.cpp b/src/SparkFun_BMI270_Arduino_Library.cpp index 80a1a76..255c951 100644 --- a/src/SparkFun_BMI270_Arduino_Library.cpp +++ b/src/SparkFun_BMI270_Arduino_Library.cpp @@ -38,22 +38,8 @@ int8_t BMI270::begin() if(err != BMI2_OK) return err; // Store the accelerometer and gyroscope ranges, these are needed elsewhere - // config.cfg.acc.range | accRange - // --------------------------- - // BMI2_ACC_RANGE_2G | 2 - // BMI2_ACC_RANGE_4G | 4 - // BMI2_ACC_RANGE_8G | 8 - // BMI2_ACC_RANGE_16G | 16 - accRange = 2 << configs[0].cfg.acc.range; - - // config.cfg.gyr.range | gyrRange - // ------------------------------ - // BMI2_GYR_RANGE_2000 | 2000 - // BMI2_GYR_RANGE_1000 | 1000 - // BMI2_GYR_RANGE_500 | 500 - // BMI2_GYR_RANGE_250 | 250 - // BMI2_GYR_RANGE_125 | 125 - gyrRange = 125 * (1 << (BMI2_GYR_RANGE_125 - configs[1].cfg.gyr.range)); + rawToGs = convertRawToGsScalar(configs[0].cfg.acc.range); + rawToDegSec = convertRawToDegSecScalar(configs[1].cfg.gyr.range); // Done! return BMI2_OK; @@ -151,9 +137,6 @@ void BMI270::convertRawAccelData(bmi2_sens_axes_data* rawData, BMI270_SensorData { // Compute conversion factor from raw to g's. Raw data are signed 16-bit // integers, so resolution is gRange / 2^15 = 32768 - float rawToGs = accRange / 32768.0; - - // Convert raw data to g's data->accelX = rawData->x * rawToGs; data->accelY = rawData->y * rawToGs; data->accelZ = rawData->z * rawToGs; @@ -166,9 +149,6 @@ void BMI270::convertRawGyroData(bmi2_sens_axes_data* rawData, BMI270_SensorData* { // Compute conversion factor from raw to deg/sec. Raw data are signed 16-bit // integers, so resolution is dpsRange / 2^15 = 32768 - float rawToDegSec = gyrRange / 32768.0; - - // Convert raw data to deg/sec data->gyroX = rawData->x * rawToDegSec; data->gyroY = rawData->y * rawToDegSec; data->gyroZ = rawData->z * rawToDegSec; @@ -433,26 +413,11 @@ int8_t BMI270::setConfigs(bmi2_sens_config* configs, uint8_t numConfigs) { if(configs[i].type == BMI2_ACCEL) { - // Update accelerometer range - // config.cfg.acc.range | accRange - // --------------------------- - // BMI2_ACC_RANGE_2G | 2 - // BMI2_ACC_RANGE_4G | 4 - // BMI2_ACC_RANGE_8G | 8 - // BMI2_ACC_RANGE_16G | 16 - accRange = 2 << configs[i].cfg.acc.range; + rawToGs = convertRawToGsScalar(configs[i].cfg.acc.range); } else if(configs[i].type == BMI2_GYRO) { - // Update gyro range - // config.cfg.gyr.range | gyrRange - // ------------------------------ - // BMI2_GYR_RANGE_2000 | 2000 - // BMI2_GYR_RANGE_1000 | 1000 - // BMI2_GYR_RANGE_500 | 500 - // BMI2_GYR_RANGE_250 | 250 - // BMI2_GYR_RANGE_125 | 125 - gyrRange = 125 * (1 << (BMI2_GYR_RANGE_125 - configs[i].cfg.gyr.range)); + rawToDegSec = convertRawToDegSecScalar(configs[i].cfg.gyr.range); } } @@ -1361,4 +1326,33 @@ BMI2_INTF_RETURN_TYPE BMI270::writeRegistersSPI(uint8_t regAddress, const uint8_ void BMI270::usDelay(uint32_t period, void* interfacePtr) { delayMicroseconds(period); -} \ No newline at end of file +} + +/// @brief Helper function to generate the correct conversion value for accelerometer data +/// @param accRange Accelerometer range enum value +/// @return float conversion scalar +float BMI270::convertRawToGsScalar(uint8_t accRange) +{ + // accRange | return value + // ----------------------------------- + // BMI2_ACC_RANGE_2G | 2 + // BMI2_ACC_RANGE_4G | 4 + // BMI2_ACC_RANGE_8G | 8 + // BMI2_ACC_RANGE_16G | 16 + return ((2 << accRange) / 32768.0); +} + +/// @brief Helper function to generate the correct conversion value for gyro data +/// @param gyrRange Gyro range enum value +/// @return float conversion scalar +float BMI270::convertRawToDegSecScalar(uint8_t gyrRange) +{ + // gyrRange | return value + // ----------------------------------- + // BMI2_GYR_RANGE_2000 | 2000 + // BMI2_GYR_RANGE_1000 | 1000 + // BMI2_GYR_RANGE_500 | 500 + // BMI2_GYR_RANGE_250 | 250 + // BMI2_GYR_RANGE_125 | 125 + return ((125 * (1 << (BMI2_GYR_RANGE_125 - gyrRange))) / 32768.0); +} diff --git a/src/SparkFun_BMI270_Arduino_Library.h b/src/SparkFun_BMI270_Arduino_Library.h index 45e5ac5..505f89c 100644 --- a/src/SparkFun_BMI270_Arduino_Library.h +++ b/src/SparkFun_BMI270_Arduino_Library.h @@ -253,6 +253,9 @@ class BMI270 // Deley helper function static void usDelay(uint32_t period, void* interfacePtr); + float convertRawToGsScalar(uint8_t accRange); + float convertRawToDegSecScalar(uint8_t gyrRange); + // Reference to the sensor struct bmi2_dev sensor; @@ -260,8 +263,8 @@ class BMI270 BMI270_InterfaceData interfaceData; // Need to track the range of each sensor for converting raw data - uint8_t accRange; - uint16_t gyrRange; + float rawToGs; + float rawToDegSec; // Need to track the FIFO config for some FIFO functions uint16_t fifoConfigFlags; From 5f630078680cebb62752d84b47d020bac64fb19e Mon Sep 17 00:00:00 2001 From: Alex Brudner <101155592+SFE-Brudnerd@users.noreply.github.com> Date: Mon, 14 Aug 2023 12:58:28 -0600 Subject: [PATCH 3/3] Update library properties to 1.0.1 --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index d858f9d..e37af02 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SparkFun BMI270 Arduino Library -version=1.0.0 +version=1.0.1 author=SparkFun Electronics maintainer=SparkFun Electronics sentence=A library to drive the Bosch BMI270 6-DoF IMU.