Skip to content

Commit

Permalink
Nonblocking calibration implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
gorbit99 committed Jan 5, 2025
1 parent cee6961 commit ac1beae
Show file tree
Hide file tree
Showing 15 changed files with 407 additions and 221 deletions.
4 changes: 3 additions & 1 deletion src/configuration/SensorConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ enum class SensorConfigType {
MPU9250,
ICM20948,
SFUSION,
BNO0XX
BNO0XX,
NONBLOCKING,
};

const char* calibrationConfigTypeToString(SensorConfigType type);
Expand All @@ -172,6 +173,7 @@ struct SensorConfig {
MPU9250SensorConfig mpu9250;
ICM20948SensorConfig icm20948;
BNO0XXSensorConfig bno0XX;
NonBlockingSensorConfig nonblocking;
} data;
};

Expand Down
1 change: 0 additions & 1 deletion src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@
#define FIRMWARE_VERSION "UNKNOWN"
#endif

#define USE_NONBLOCKING_CALIBRATION false
#ifndef USE_NONBLOCKING_CALIBRATION
#define USE_NONBLOCKING_CALIBRATION true
#endif
Expand Down
2 changes: 2 additions & 0 deletions src/sensors/SensorManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "mpu6050sensor.h"
#include "mpu9250sensor.h"
#include "sensors/softfusion/SoftfusionCalibration.h"
#include "sensors/softfusion/nonblockingcalibration/NonBlockingCalibration.h"
#include "softfusion/drivers/bmi270.h"
#include "softfusion/drivers/icm42688.h"
#include "softfusion/drivers/icm45605.h"
Expand All @@ -49,6 +50,7 @@
#endif

#if USE_NONBLOCKING_CALIBRATION
#define SFCALIBRATOR SlimeVR::Sensors::NonBlockingCalibration::NonBlockingCalibrator
#else
#define SFCALIBRATOR SlimeVR::Sensor::SoftfusionCalibrator
#endif
Expand Down
117 changes: 117 additions & 0 deletions src/sensors/softfusion/CalibrationBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
SlimeVR Code is placed under the MIT license
Copyright (c) 2025 Gorbit99 & SlimeVR Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#pragma once

#include <cstddef>
#include <cstdint>
#include <functional>

#include "configuration/SensorConfig.h"
#include "motionprocessing/types.h"
#include "sensors/SensorFusionRestDetect.h"

namespace SlimeVR::Sensor {

template <
typename IMU,
float TempTs,
double AScale,
double GScale,
typename RawSensorT,
typename RawVectorT>
class CalibrationBase {
public:
CalibrationBase(
SlimeVR::Sensors::SensorFusionRestDetect& fusion,
IMU& sensor,
uint8_t sensorId,
SlimeVR::Logging::Logger& logger
)
: fusion{fusion}
, sensor{sensor}
, sensorId{sensorId}
, logger{logger} {}

static constexpr bool HasMotionlessCalib
= requires(IMU& i) { typename IMU::MotionlessCalibrationData; };
static constexpr size_t MotionlessCalibDataSize() {
if constexpr (HasMotionlessCalib) {
return sizeof(typename IMU::MotionlessCalibrationData);
} else {
return 0;
}
}

using EatSamplesFn = std::function<void(const uint32_t)>;
using ReturnLastFn
= std::function<std::tuple<RawVectorT, RawVectorT, int16_t>(const uint32_t)>;

virtual void startCalibration(
int calibrationType,
const EatSamplesFn& eatSamplesForSeconds,
const ReturnLastFn& eatSamplesReturnLast
){};

virtual bool calibrationMatches(
const SlimeVR::Configuration::SensorConfig& sensorCalibration
) = 0;

virtual void assignCalibration(const Configuration::SensorConfig& sensorCalibration)
= 0;

virtual void begin() {}

virtual void tick() {}

virtual void scaleAccelSample(sensor_real_t accelSample[3]) = 0;
virtual float getAccelTimestep() = 0;

virtual void scaleGyroSample(sensor_real_t gyroSample[3]) = 0;
virtual float getGyroTimestep() = 0;

virtual float getTempTimestep() = 0;

virtual const uint8_t* getMotionlessCalibrationData() = 0;

virtual void provideAccelSample(const RawSensorT accelSample[3]) {}
virtual void provideGyroSample(const RawSensorT gyroSample[3]) {}
virtual void provideTempSample(float tempSample) {}

protected:
void recalcFusion() {
fusion = Sensors::SensorFusionRestDetect(
IMU::SensorVQFParams,
getGyroTimestep(),
getAccelTimestep(),
getTempTimestep()
);
}

Sensors::SensorFusionRestDetect& fusion;
IMU& sensor;
SlimeVR::Logging::Logger& logger;
uint8_t sensorId;
};

} // namespace SlimeVR::Sensor
Loading

0 comments on commit ac1beae

Please sign in to comment.