From 452efccb0aed95c90cdbb2a53ddb6c5e53b9e8f8 Mon Sep 17 00:00:00 2001 From: unlogisch04 <98281608+unlogisch04@users.noreply.github.com> Date: Sun, 18 Aug 2024 03:13:04 +0200 Subject: [PATCH] add guards to some defines Should address #334 Make defines.h more powerfull --- src/debug.h | 126 +++++++++++++----- src/defines_bmi160.h | 34 ++++- .../GyroTemperatureCalibrator.h | 28 ++-- src/motionprocessing/RestDetection.h | 18 +-- src/sensors/SensorFusion.h | 10 +- src/sensors/mpu6050sensor.cpp | 8 +- src/sensors/mpu6050sensor.h | 2 +- 7 files changed, 162 insertions(+), 64 deletions(-) diff --git a/src/debug.h b/src/debug.h index b2c4968d5..802c20992 100644 --- a/src/debug.h +++ b/src/debug.h @@ -25,19 +25,46 @@ #include "consts.h" #include "logging/Level.h" -#define IMU_MPU6050_RUNTIME_CALIBRATION // Comment to revert to startup/traditional-calibration -#define BNO_USE_ARVR_STABILIZATION true // Set to false to disable stabilization for BNO085+ IMUs -#define BNO_USE_MAGNETOMETER_CORRECTION false // Set to true to enable magnetometer correction for BNO08x IMUs. Only works with USE_6_AXIS set to true. -#define USE_6_AXIS true // uses 9 DoF (with mag) if false (only for ICM-20948 and BNO0xx currently) -#define LOAD_BIAS true // Loads the bias values from NVS on start -#define SAVE_BIAS true // Periodically saves bias calibration data to NVS -#define BIAS_DEBUG false // Printing BIAS Variables to serial (ICM20948 only) -#define ENABLE_TAP false // monitor accel for (triple) tap events and send them. Uses more cpu, disable if problems. Server does nothing with value so disabled atm -#define SEND_ACCELERATION true // send linear acceleration to the server +#ifndef IMU_MPU6050_RUNTIME_CALIBRATION + #define IMU_MPU6050_RUNTIME_CALIBRATION true // Comment to revert to startup/traditional-calibration +#endif + +#ifndef BNO_USE_ARVR_STABILIZATION + #define BNO_USE_ARVR_STABILIZATION true // Set to false to disable stabilization for BNO085+ IMUs +#endif + +#ifndef BNO_USE_MAGNETOMETER_CORRECTION + #define BNO_USE_MAGNETOMETER_CORRECTION false // Set to true to enable magnetometer correction for BNO08x IMUs. Only works with USE_6_AXIS set to true. +#endif + +#ifndef USE_6_AXIS + #define USE_6_AXIS true // uses 9 DoF (with mag) if false (only for ICM-20948 and BNO0xx currently) +#endif + +#ifndef LOAD_BIAS + #define LOAD_BIAS true // Loads the bias values from NVS on start +#endif + +#ifndef SAVE_BIAS + #define SAVE_BIAS true // Periodically saves bias calibration data to NVS +#endif + +#ifndef BIAS_DEBUG + #define BIAS_DEBUG false // Printing BIAS Variables to serial (ICM20948 only) +#endif + +#ifndef ENABLE_TAP + #define ENABLE_TAP false // monitor accel for (triple) tap events and send them. Uses more cpu, disable if problems. Server does nothing with value so disabled atm +#endif +#ifndef SEND_ACCELERATION + #define SEND_ACCELERATION true // send linear acceleration to the server +#endif //Debug information -#define LOG_LEVEL LOG_LEVEL_DEBUG +#ifndef LOG_LEVEL + #define LOG_LEVEL LOG_LEVEL_DEBUG +#endif #if LOG_LEVEL == LOG_LEVEL_TRACE #define DEBUG_SENSOR @@ -45,50 +72,87 @@ #define DEBUG_CONFIGURATION #endif -#define serialDebug false // Set to true to get Serial output for debugging -#define serialBaudRate 115200 -#define LED_INTERVAL_STANDBY 10000 -#define PRINT_STATE_EVERY_MS 60000 +#ifndef serialDebug + #define serialDebug false // Set to true to get Serial output for debugging +#endif +#ifndef serialBaudRate + #define serialBaudRate 115200 +#endif +#ifndef LED_INTERVAL_STANDBY + #define LED_INTERVAL_STANDBY 10000 +#endif +#ifndef PRINT_STATE_EVERY_MS + #define PRINT_STATE_EVERY_MS 60000 +#endif // Determines how often we sample and send data -#define samplingRateInMillis 10 +#ifndef samplingRateInMillis + #define samplingRateInMillis 10 +#endif // Sleeping options -#define POWERSAVING_MODE POWER_SAVING_LEGACY // Minimum causes sporadic data pauses +#ifndef POWERSAVING_MODE + #define POWERSAVING_MODE POWER_SAVING_LEGACY // Minimum causes sporadic data pauses +#endif #if POWERSAVING_MODE >= POWER_SAVING_MINIMUM #define TARGET_LOOPTIME_MICROS (samplingRateInMillis * 1000) #endif // Packet bundling/aggregation -#define PACKET_BUNDLING PACKET_BUNDLING_BUFFERED +#ifndef PACKET_BUNDLING + #define PACKET_BUNDLING PACKET_BUNDLING_BUFFERED +#endif // Extra tunable for PACKET_BUNDLING_BUFFERED (10000us = 10ms timeout, 100hz target) -#define PACKET_BUNDLING_BUFFER_SIZE_MICROS 10000 - +#ifndef PACKET_BUNDLING_BUFFER_SIZE_MICROS + #define PACKET_BUNDLING_BUFFER_SIZE_MICROS 10000 +#endif // Setup for the Magnetometer -#define useFullCalibrationMatrix true - +#ifndef useFullCalibrationMatrix + #define useFullCalibrationMatrix true +#endif // Battery configuration -#define batterySampleRate 10000 -#define BATTERY_LOW_VOLTAGE_DEEP_SLEEP false -#define BATTERY_LOW_POWER_VOLTAGE 3.3f // Voltage to raise error +#ifndef batterySampleRate + #define batterySampleRate 10000 +#endif +#ifndef BATTERY_LOW_VOLTAGE_DEEP_SLEEP + #define BATTERY_LOW_VOLTAGE_DEEP_SLEEP false +#endif +#ifndef BATTERY_LOW_POWER_VOLTAGE + #define BATTERY_LOW_POWER_VOLTAGE 3.3f // Voltage to raise error +#endif // Send updates over network only when changes are substantial // If "false" updates are sent at the sensor update rate (usually 100 TPS) // If "true" updates will be less frequent in the time of little motion // Experimental -#define OPTIMIZE_UPDATES true +#ifndef OPTIMIZE_UPDATES + #define OPTIMIZE_UPDATES true +#endif -#define I2C_SPEED 400000 +#ifndef I2C_SPEED + #define I2C_SPEED 400000 +#endif + +#ifndef COMPLIANCE_MODE + #define COMPLIANCE_MODE true +#endif -#define COMPLIANCE_MODE true #define USE_ATTENUATION COMPLIANCE_MODE && ESP8266 -#define ATTENUATION_N 10.0 / 4.0 -#define ATTENUATION_G 14.0 / 4.0 -#define ATTENUATION_B 40.0 / 4.0 +#ifndef ATTENUATION_N + #define ATTENUATION_N 10.0 / 4.0 +#endif +#ifndef ATTENUATION_G + #define ATTENUATION_G 14.0 / 4.0 +#endif +#ifndef ATTENUATION_B + #define ATTENUATION_B 40.0 / 4.0 +#endif // Send inspection packets over the network to a profiler // Not recommended for production -#define ENABLE_INSPECTION false +#ifndef ENABLE_INSPECTION + #define ENABLE_INSPECTION false +#endif #define FIRMWARE_BUILD_NUMBER 17 #define FIRMWARE_VERSION "0.4.0" diff --git a/src/defines_bmi160.h b/src/defines_bmi160.h index 9eb9eea91..059e468d9 100644 --- a/src/defines_bmi160.h +++ b/src/defines_bmi160.h @@ -27,24 +27,34 @@ // If only 1 out of 2 trackers has a mag, tracker without a mag should still function normally. // NOT USED if USE_6_AXIS == true // Pick one: -#define BMI160_MAG_TYPE BMI160_MAG_TYPE_HMC +#ifndef BMI160_MAG_TYPE + #define BMI160_MAG_TYPE BMI160_MAG_TYPE_HMC +#endif // #define BMI160_MAG_TYPE BMI160_MAG_TYPE_QMC // Use VQF instead of mahony sensor fusion. // Features: rest bias estimation, magnetic distortion rejection. -#define BMI160_USE_VQF true +#ifndef BMI160_USE_VQF + #define BMI160_USE_VQF true +#endif // Use BasicVQF instead of VQF (if BMI160_USE_VQF == true). // Disables the features above. -#define BMI160_USE_BASIC_VQF false +#ifndef BMI160_USE_BASIC_VQF + #define BMI160_USE_BASIC_VQF false +#endif // Use temperature calibration. -#define BMI160_USE_TEMPCAL true +#ifndef BMI160_USE_TEMPCAL + #define BMI160_USE_TEMPCAL true +#endif // How long to run gyro calibration for. // Disables this calibration step if value is 0. // Default: 5 -#define BMI160_CALIBRATION_GYRO_SECONDS 5 +#ifndef BMI160_CALIBRATION_GYRO_SECONDS + #define BMI160_CALIBRATION_GYRO_SECONDS 5 +#endif // Calibration method options: // - Skip: disable this calibration step; @@ -53,24 +63,34 @@ // Default: ACCEL_CALIBRATION_METHOD_6POINT // #define BMI160_ACCEL_CALIBRATION_METHOD ACCEL_CALIBRATION_METHOD_SKIP // #define BMI160_ACCEL_CALIBRATION_METHOD ACCEL_CALIBRATION_METHOD_ROTATION -#define BMI160_ACCEL_CALIBRATION_METHOD ACCEL_CALIBRATION_METHOD_6POINT +#ifndef BMI160_ACCEL_CALIBRATION_METHOD + #define BMI160_ACCEL_CALIBRATION_METHOD ACCEL_CALIBRATION_METHOD_6POINT +#endif // How long to run magnetometer calibration for, if enabled and you have added a magnetometer. // Magnetometer not be used until you calibrate it. // Disables this calibration step if value is 0. // NOT USED if USE_6_AXIS == true // Default: 20 +#ifndef BMI160_CALIBRATION_MAG_SECONDS #define BMI160_CALIBRATION_MAG_SECONDS 20 +#endif // Send temperature to the server as AXXYY, // where XX is calibration progress from 0 to 60, and YY is temperature, // A is 1: not in calibration mode or 2: calibration in progress. +#ifndef BMI160_TEMPCAL_DEBUG #define BMI160_TEMPCAL_DEBUG false +#endif // Print debug info every second. +#ifndef BMI160_DEBUG #define BMI160_DEBUG false +#endif // Use sensitivity calibration. +#ifndef BMI160_USE_SENSCAL #define BMI160_USE_SENSCAL true +#endif -#endif \ No newline at end of file +#endif diff --git a/src/motionprocessing/GyroTemperatureCalibrator.h b/src/motionprocessing/GyroTemperatureCalibrator.h index eb27f7fb9..75780ccf4 100644 --- a/src/motionprocessing/GyroTemperatureCalibrator.h +++ b/src/motionprocessing/GyroTemperatureCalibrator.h @@ -34,24 +34,34 @@ // Degrees C // default: 15.0f -#define TEMP_CALIBRATION_MIN 15.0f +#ifndef TEMP_CALIBRATION_MIN + #define TEMP_CALIBRATION_MIN 15.0f +#endif // Degrees C // default: 45.0f -#define TEMP_CALIBRATION_MAX 45.0f +#ifndef TEMP_CALIBRATION_MAX + #define TEMP_CALIBRATION_MAX 45.0f +#endif // Snap calibration to every 1/2 of degree: 20.00, 20.50, 21.00, etc // default: 0.5f -#define TEMP_CALIBRATION_STEP 0.5f +#ifndef TEMP_CALIBRATION_STEP + #define TEMP_CALIBRATION_STEP 0.5f +#endif // Record debug samples if current temperature is off by no more than this value; // if snapping point is 20.00 - samples will be recorded in range of 19.80 - 20.20 // default: 0.2f -#define TEMP_CALIBRATION_MAX_DEVIATION_FROM_STEP 0.2f +#ifndef TEMP_CALIBRATION_MAX_DEVIATION_FROM_STEP + #define TEMP_CALIBRATION_MAX_DEVIATION_FROM_STEP 0.2f +#endif // How long to average gyro samples for before saving a data point // default: 0.2f -#define TEMP_CALIBRATION_SECONDS_PER_STEP 0.2f +#ifndef TEMP_CALIBRATION_SECONDS_PER_STEP + #define TEMP_CALIBRATION_SECONDS_PER_STEP 0.2f +#endif #if IMU == IMU_ICM20948 // 16 bit 333 lsb/K, ~0.00508 degrees per bit @@ -121,7 +131,7 @@ struct GyroTemperatureCalibrationConfig { minTemperatureRange(1000), maxTemperatureRange(-1000) { } - + bool hasData() { return minTemperatureRange != 1000; } @@ -164,7 +174,7 @@ class GyroTemperatureCalibrator { public: uint8_t sensorId; GyroTemperatureCalibrationConfig config; - + // set when config is fully calibrated is saved OR on startup when loaded config is fully calibrated; // left unset when sending saving command over serial so it can continue calibration and autosave later bool configSaved = false; @@ -200,7 +210,7 @@ class GyroTemperatureCalibrator { GyroTemperatureCalibrationState state; uint32_t samplesPerStep; SlimeVR::Logging::Logger m_Logger; - + float lastApproximatedTemperature = 0.0f; float lastApproximatedOffsets[3]; @@ -216,4 +226,4 @@ class GyroTemperatureCalibrator { void resetCurrentTemperatureState(); }; -#endif \ No newline at end of file +#endif diff --git a/src/motionprocessing/RestDetection.h b/src/motionprocessing/RestDetection.h index 94d754532..768194595 100644 --- a/src/motionprocessing/RestDetection.h +++ b/src/motionprocessing/RestDetection.h @@ -8,7 +8,9 @@ #ifndef REST_DETECTION_H #define REST_DETECTION_H -// #define REST_DETECTION_DISABLE_LPF +#ifndef REST_DETECTION_DISABLE_LPF + #define REST_DETECTION_DISABLE_LPF false +#endif #include #include @@ -50,7 +52,7 @@ class RestDetection { setup(); } -#ifndef REST_DETECTION_DISABLE_LPF +#if !REST_DETECTION_DISABLE_LPF void filterInitialState(sensor_real_t x0, const double b[3], const double a[2], double out[]) { // initial state for steady state (equivalent to scipy.signal.lfilter_zi, obtained by setting y=x=x0 in the filter @@ -104,7 +106,7 @@ class RestDetection { #endif void updateGyr(const sensor_real_t gyr[3]) { -#ifdef REST_DETECTION_DISABLE_LPF +#if REST_DETECTION_DISABLE_LPF gyrLastSquaredDeviation = square(gyr[0] - lastSample.gyr[0]) + square(gyr[1] - lastSample.gyr[1]) + @@ -145,7 +147,7 @@ class RestDetection { return; } -#ifdef REST_DETECTION_DISABLE_LPF +#if REST_DETECTION_DISABLE_LPF accLastSquaredDeviation = square(acc[0] - lastSample.acc[0]) + square(acc[1] - lastSample.acc[1]) + @@ -189,7 +191,7 @@ class RestDetection { return restDetected; } -#ifndef REST_DETECTION_DISABLE_LPF +#if !REST_DETECTION_DISABLE_LPF void resetState() { restDetected = false; @@ -220,7 +222,7 @@ class RestDetection { #endif void setup() { -#ifndef REST_DETECTION_DISABLE_LPF +#if !REST_DETECTION_DISABLE_LPF assert(gyrTs > 0); assert(accTs > 0); @@ -241,7 +243,7 @@ class RestDetection { sensor_real_t gyrTs; sensor_real_t accTs; -#ifndef REST_DETECTION_DISABLE_LPF +#if !REST_DETECTION_DISABLE_LPF sensor_real_t restLastGyrLp[3]; double restGyrLpState[3*2]; double restGyrLpB[3]; @@ -259,4 +261,4 @@ class RestDetection { }; -#endif \ No newline at end of file +#endif diff --git a/src/sensors/SensorFusion.h b/src/sensors/SensorFusion.h index cc0116205..b9fca7eb4 100644 --- a/src/sensors/SensorFusion.h +++ b/src/sensors/SensorFusion.h @@ -6,7 +6,9 @@ #define SENSOR_DOUBLE_PRECISION 0 -#define SENSOR_FUSION_TYPE SENSOR_FUSION_VQF +#ifndef SENSOR_FUSION_TYPE + #define SENSOR_FUSION_TYPE SENSOR_FUSION_VQF +#endif #define SENSOR_FUSION_MAHONY 1 #define SENSOR_FUSION_MADGWICK 2 @@ -58,8 +60,8 @@ namespace SlimeVR { public: SensorFusion(sensor_real_t gyrTs, sensor_real_t accTs=-1.0, sensor_real_t magTs=-1.0) - : gyrTs(gyrTs), - accTs( (accTs<0) ? gyrTs : accTs ), + : gyrTs(gyrTs), + accTs( (accTs<0) ? gyrTs : accTs ), magTs( (magTs<0) ? gyrTs : magTs ) #if SENSOR_USE_MAHONY #elif SENSOR_USE_MADGWICK @@ -67,7 +69,7 @@ namespace SlimeVR , basicvqf(gyrTs, ((accTs<0) ? gyrTs : accTs), ((magTs<0) ? gyrTs : magTs)) #elif SENSOR_USE_VQF - , vqf(vqfParams, gyrTs, ((accTs<0) ? gyrTs : accTs), + , vqf(vqfParams, gyrTs, ((accTs<0) ? gyrTs : accTs), ((magTs<0) ? gyrTs : magTs)) #endif {} diff --git a/src/sensors/mpu6050sensor.cpp b/src/sensors/mpu6050sensor.cpp index 44ac31591..fcb87b7e5 100644 --- a/src/sensors/mpu6050sensor.cpp +++ b/src/sensors/mpu6050sensor.cpp @@ -23,7 +23,7 @@ #include "globals.h" -#ifdef IMU_MPU6050_RUNTIME_CALIBRATION +#if IMU_MPU6050_RUNTIME_CALIBRATION #include "MPU6050_6Axis_MotionApps_V6_12.h" #else #include "MPU6050_6Axis_MotionApps20.h" @@ -50,7 +50,7 @@ void MPU6050Sensor::motionSetup() m_Logger.info("Connected to %s (reported device ID 0x%02x) at address 0x%02x", getIMUNameByType(sensorType), imu.getDeviceID(), addr); -#ifndef IMU_MPU6050_RUNTIME_CALIBRATION +#if !IMU_MPU6050_RUNTIME_CALIBRATION // Initialize the configuration { SlimeVR::Configuration::CalibrationConfig sensorCalibration = configuration.getCalibration(sensorId); @@ -76,7 +76,7 @@ void MPU6050Sensor::motionSetup() if (devStatus == 0) { -#ifdef IMU_MPU6050_RUNTIME_CALIBRATION +#if IMU_MPU6050_RUNTIME_CALIBRATION // We don't have to manually calibrate if we are using the dmp's automatic calibration #else // IMU_MPU6050_RUNTIME_CALIBRATION @@ -160,7 +160,7 @@ void MPU6050Sensor::motionLoop() void MPU6050Sensor::startCalibration(int calibrationType) { ledManager.on(); -#ifdef IMU_MPU6050_RUNTIME_CALIBRATION +#if IMU_MPU6050_RUNTIME_CALIBRATION m_Logger.info("MPU is using automatic runtime calibration. Place down the device and it should automatically calibrate after a few seconds"); #else //!IMU_MPU6050_RUNTIME_CALIBRATION m_Logger.info("Put down the device and wait for baseline gyro reading calibration"); diff --git a/src/sensors/mpu6050sensor.h b/src/sensors/mpu6050sensor.h index c5667798f..5edc511d5 100644 --- a/src/sensors/mpu6050sensor.h +++ b/src/sensors/mpu6050sensor.h @@ -55,7 +55,7 @@ class MPU6050Sensor : public Sensor SlimeVR::Sensors::SensorFusionDMP sfusion; -#ifndef IMU_MPU6050_RUNTIME_CALIBRATION +#if !IMU_MPU6050_RUNTIME_CALIBRATION SlimeVR::Configuration::MPU6050CalibrationConfig m_Calibration = {}; #endif };