Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report rest calibration #384

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/network/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ void Connection::sendSensorInfo(Sensor& sensor) {
MUST(sendByte(static_cast<uint8_t>(sensor.getSensorState())));
MUST(sendByte(static_cast<uint8_t>(sensor.getSensorType())));
MUST(sendShort(sensor.getSensorConfigData()));
MUST(sendByte(sensor.completedRestCalibration()));

MUST(endPacket());
}
Expand Down Expand Up @@ -533,7 +534,7 @@ void Connection::updateSensorState(std::vector<std::unique_ptr<Sensor>>& sensors
m_LastSensorInfoPacketTimestamp = millis();

for (int i = 0; i < (int)sensors.size(); i++) {
if (m_AckedSensorState[i] != sensors[i]->getSensorState()) {
if (isSensorStateUpdated(i, sensors[i])) {
sendSensorInfo(*sensors[i]);
}
}
Expand All @@ -553,6 +554,11 @@ void Connection::maybeRequestFeatureFlags() {
m_FeatureFlagsRequestAttempts++;
}

bool Connection::isSensorStateUpdated(int i, std::unique_ptr<Sensor>& sensor) {
return m_AckedSensorState[i] != sensor->getSensorState()
|| m_AckedSensorCalibration[i] != sensor->completedRestCalibration();
}

void Connection::searchForServer() {
while (true) {
int packetSize = m_UDP.parsePacket();
Expand Down Expand Up @@ -624,6 +630,11 @@ void Connection::reset() {
m_AckedSensorState + MAX_IMU_COUNT,
SensorStatus::SENSOR_OFFLINE
);
std::fill(
m_AckedSensorCalibration,
m_AckedSensorCalibration + MAX_IMU_COUNT,
false
);

m_UDP.begin(m_ServerPort);

Expand All @@ -650,6 +661,11 @@ void Connection::update() {
m_AckedSensorState + MAX_IMU_COUNT,
SensorStatus::SENSOR_OFFLINE
);
std::fill(
m_AckedSensorCalibration,
m_AckedSensorCalibration + MAX_IMU_COUNT,
false
);
m_Logger.warn("Connection to server timed out");

return;
Expand Down Expand Up @@ -707,6 +723,7 @@ void Connection::update() {
for (int i = 0; i < (int)sensors.size(); i++) {
if (m_Packet[4] == sensors[i]->getSensorId()) {
m_AckedSensorState[i] = (SensorStatus)m_Packet[5];
m_AckedSensorCalibration[i] = (bool)m_Packet[9];
break;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/network/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class Connection {
private:
void updateSensorState(std::vector<std::unique_ptr<Sensor>>& sensors);
void maybeRequestFeatureFlags();
bool isSensorStateUpdated(int i, std::unique_ptr<Sensor>& sensor);

bool beginPacket();
bool endPacket();
Expand Down Expand Up @@ -171,6 +172,7 @@ class Connection {
unsigned long m_LastPacketTimestamp;

SensorStatus m_AckedSensorState[MAX_IMU_COUNT] = {SensorStatus::SENSOR_OFFLINE};
bool m_AckedSensorCalibration[MAX_IMU_COUNT] = {false};
unsigned long m_LastSensorInfoPacketTimestamp = 0;

uint8_t m_FeatureFlagsRequestAttempts = 0;
Expand Down
53 changes: 53 additions & 0 deletions src/sensors/RestCalibrationDetector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
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.
*/

#include "RestCalibrationDetector.h"

namespace SlimeVR::Sensors {

bool RestCalibrationDetector::update(SensorFusionRestDetect& fusion) {
if (state == CalibrationState::Done) {
return false;
}

if (!fusion.getRestDetected()) {
state = CalibrationState::NoRest;
return false;
}

if (state == CalibrationState::NoRest) {
state = CalibrationState::Calibrating;
lastRestStartedMillis = millis();
return false;
}

uint32_t elapsed = millis() - lastRestStartedMillis;
if (elapsed < static_cast<uint32_t>(restCalibrationSeconds * 1e3)) {
return false;
}

state = CalibrationState::Done;
return true;
}

} // namespace SlimeVR::Sensors
49 changes: 49 additions & 0 deletions src/sensors/RestCalibrationDetector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
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 <cstdint>

#include "SensorFusionRestDetect.h"

namespace SlimeVR::Sensors {

class RestCalibrationDetector {
public:
bool update(SensorFusionRestDetect& fusion);

private:
static constexpr float restCalibrationSeconds = 3.0f;

enum class CalibrationState {
NoRest,
Calibrating,
Done,
};

CalibrationState state = CalibrationState::NoRest;
uint32_t lastRestStartedMillis = 0;
};

} // namespace SlimeVR::Sensors
4 changes: 4 additions & 0 deletions src/sensors/bmi160sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ void BMI160Sensor::motionLoop() {
}
#endif

if (calibrationDetector.update(sfusion)) {
markRestCalibrationComplete();
}

{
uint32_t now = micros();
constexpr uint32_t BMI160_TARGET_SYNC_INTERVAL_MICROS = 25000;
Expand Down
3 changes: 3 additions & 0 deletions src/sensors/bmi160sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "../motionprocessing/GyroTemperatureCalibrator.h"
#include "../motionprocessing/RestDetection.h"
#include "../motionprocessing/types.h"
#include "RestCalibrationDetector.h"
#include "SensorFusionRestDetect.h"
#include "magneto1.4.h"
#include "sensor.h"
Expand Down Expand Up @@ -265,6 +266,8 @@ class BMI160Sensor : public Sensor {
bool isMagCalibrated = false;

SlimeVR::Configuration::BMI160SensorConfig m_Config = {};

SlimeVR::Sensors::RestCalibrationDetector calibrationDetector;
};

#endif
8 changes: 8 additions & 0 deletions src/sensors/bno055sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/
#include "bno055sensor.h"

#include <cstdint>

#include "GlobalVars.h"
#include "globals.h"

Expand Down Expand Up @@ -68,6 +70,12 @@ void BNO055Sensor::motionLoop() {
}
#endif

uint8_t gyroCalibrationState;
imu.getCalibration(nullptr, &gyroCalibrationState, nullptr, nullptr);
if (gyroCalibrationState == 3) {
markRestCalibrationComplete();
}

// TODO Optimize a bit with setting rawQuat directly
setFusedRotation(imu.getQuat());
hadData = true;
Expand Down
6 changes: 6 additions & 0 deletions src/sensors/bno080sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ void BNO080Sensor::motionSetup() {
imu.enableRawMagnetometer(10);
#endif

imu.enableStabilityClassifier(500);

lastReset = 0;
lastData = millis();
working = true;
Expand Down Expand Up @@ -238,6 +240,10 @@ void BNO080Sensor::motionLoop() {
lastError.error_code
);
}

if (imu.getStabilityClassifier() == 1) {
markRestCalibrationComplete();
}
}

SensorStatus BNO080Sensor::getSensorState() {
Expand Down
6 changes: 6 additions & 0 deletions src/sensors/sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,9 @@ const char* getIMUNameByType(ImuID imuType) {
}
return "Unknown";
}

bool Sensor::completedRestCalibration() { return restCalibrationComplete; }

void Sensor::markRestCalibrationComplete(bool completed) {
restCalibrationComplete = completed;
}
5 changes: 5 additions & 0 deletions src/sensors/sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class Sensor {
const Vector3& getAcceleration() { return acceleration; };
const Quat& getFusedRotation() { return fusedRotation; };
bool hasNewDataToSend() { return newFusedRotation || newAcceleration; };
bool completedRestCalibration();
gorbit99 marked this conversation as resolved.
Show resolved Hide resolved

protected:
uint8_t addr = 0;
Expand All @@ -114,6 +115,8 @@ class Sensor {
bool newAcceleration = false;
Vector3 acceleration{};

void markRestCalibrationComplete(bool completed = true);

mutable SlimeVR::Logging::Logger m_Logger;

public:
Expand All @@ -122,6 +125,8 @@ class Sensor {

private:
void printTemperatureCalibrationUnsupported();

bool restCalibrationComplete;
};

const char* getIMUNameByType(ImuID imuType);
Expand Down
7 changes: 7 additions & 0 deletions src/sensors/softfusion/softfusionsensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#pragma once

#include "../RestCalibrationDetector.h"
#include "../SensorFusionRestDetect.h"
#include "../sensor.h"
#include "GlobalVars.h"
Expand Down Expand Up @@ -236,6 +237,10 @@ class SoftFusionSensor : public Sensor {
setAcceleration(m_fusion.getLinearAccVec());
optimistic_yield(100);
}

if (calibrationDetector.update(m_fusion)) {
markRestCalibrationComplete();
}
}

void motionSetup() override final {
Expand Down Expand Up @@ -617,6 +622,8 @@ class SoftFusionSensor : public Sensor {
uint32_t m_lastPollTime = micros();
uint32_t m_lastRotationPacketSent = 0;
uint32_t m_lastTemperaturePacketSent = 0;

RestCalibrationDetector calibrationDetector;
};

} // namespace SlimeVR::Sensors