Skip to content

Commit

Permalink
finish feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ImUrX committed Jul 9, 2024
1 parent d2ca08a commit 3da4427
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 32 deletions.
8 changes: 4 additions & 4 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ build_unflags = -Os -std=gnu++11
; If you want to enable OTA Updates, uncomment and set OTA password here and in credentials.h
; You can set upload_port to device's ip after it's set up for the first time
; Use the same password in SlimeVR Server to let it OTA Update the device
;upload_protocol = espota
;upload_port = 192.168.1.49
;upload_flags =
; --auth=SlimeVR-OTA
upload_protocol = espota
upload_port = 192.168.1.79
upload_flags =
--auth=SlimeVR-OTA

; Settings for different boards

Expand Down
12 changes: 10 additions & 2 deletions src/network/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ void Connection::sendSensorInfo(Sensor* sensor) {
MUST(sendByte(sensor->getSensorId()));
MUST(sendByte((uint8_t)sensor->getSensorState()));
MUST(sendByte(sensor->getSensorType()));
MUST(sendByte((uint8_t)sensor->getMagStatus()));

MUST(endPacket());
}
Expand Down Expand Up @@ -738,8 +739,11 @@ void Connection::update() {
uint8_t sensorId = m_Packet[12];
uint16_t flagId = m_Packet[13] << 8 | m_Packet[14];
bool newState = m_Packet[15] > 0;
if(sensorId == 255) {
// Apply the flag to the whole device
if(sensorId == UINT8_MAX) {
std::vector<Sensor *> & sensors = sensorManager.getSensors();
for (Sensor * sensor : sensors) {
sensor->setFlag(flagId, newState);
}
} else {
std::vector<Sensor *> & sensors = sensorManager.getSensors();
if(sensorId < sensors.size()) {
Expand All @@ -748,6 +752,10 @@ void Connection::update() {
}
}
sendAcknowledgeConfigChange(sensorId, flagId);
configuration.save();
// Should not be done always, but for magnetometer we probably prefer restarting
// the whole ESP
EspClass::restart();
break;
}
}
Expand Down
38 changes: 13 additions & 25 deletions src/sensors/bno080sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ void BNO080Sensor::motionSetup()
switch (sensorConfig.type) {
case SlimeVR::Configuration::SensorConfigType::BNO0XX:
m_Config = sensorConfig.data.bno0XX;
m_magEnabled = m_Config.magEnabled;
magStatus = m_Config.magEnabled ? MagnetometerStatus::ENABLED : MagnetometerStatus::DISABLED;
break;
default:
// Ignore lack of config for BNO, byt default use from FW build
magStatus = USE_6_AXIS ? MagnetometerStatus::DISABLED : MagnetometerStatus::ENABLED;
break;
}

if(!m_magEnabled) {
if(!isMagEnabled()) {
if ((sensorType == IMU_BNO085 || sensorType == IMU_BNO086) && BNO_USE_ARVR_STABILIZATION) {
imu.enableARVRStabilizedGameRotationVector(10);
} else {
Expand Down Expand Up @@ -125,7 +126,7 @@ void BNO080Sensor::motionLoop()
lastReset = 0;
lastData = millis();

if(!m_magEnabled) {
if(!isMagEnabled()) {
if (imu.hasNewGameQuat()) // New quaternion if context
{
Quat nRotation;
Expand Down Expand Up @@ -233,11 +234,11 @@ void BNO080Sensor::sendData()
#endif
}

if(m_magEnabled) {
if(isMagEnabled()) {
networkConnection.sendMagnetometerAccuracy(sensorId, magneticAccuracyEstimate);
}

if(BNO_USE_MAGNETOMETER_CORRECTION && !m_magEnabled) {
if(BNO_USE_MAGNETOMETER_CORRECTION && !isMagEnabled()) {
if (newMagData)
{
newMagData = false;
Expand All @@ -257,26 +258,13 @@ void BNO080Sensor::setFlag(uint16_t flagId, bool state)
{
if(flagId == FLAG_SENSOR_BNO0XX_MAG_ENABLED) {
m_Config.magEnabled = state;
m_magEnabled = state;
if(state) {
if ((sensorType == IMU_BNO085 || sensorType == IMU_BNO086) && BNO_USE_ARVR_STABILIZATION) {
imu.enableARVRStabilizedRotationVector(10);
} else {
imu.enableRotationVector(10);
}
} else {
if ((sensorType == IMU_BNO085 || sensorType == IMU_BNO086) && BNO_USE_ARVR_STABILIZATION) {
imu.enableARVRStabilizedGameRotationVector(10);
} else {
imu.enableGameRotationVector(10);
}

#if BNO_USE_MAGNETOMETER_CORRECTION
imu.enableRotationVector(1000);
#endif
}
imu.softReset();
}
magStatus = state ? MagnetometerStatus::ENABLED : MagnetometerStatus::DISABLED;

SlimeVR::Configuration::SensorConfig config;
config.type = SlimeVR::Configuration::SensorConfigType::BNO0XX;
config.data.bno0XX = m_Config;
configuration.setSensor(sensorId, config);
}
}

void BNO080Sensor::startCalibration(int calibrationType)
Expand Down
1 change: 0 additions & 1 deletion src/sensors/bno080sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class BNO080Sensor : public Sensor
unsigned long lastData = 0;
uint8_t lastReset = 0;
BNO080Error lastError{};
bool m_magEnabled = !USE_6_AXIS;
SlimeVR::Configuration::BNO0XXSensorConfig m_Config = {};

// Magnetometer specific members
Expand Down
13 changes: 13 additions & 0 deletions src/sensors/sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ enum class SensorStatus : uint8_t {
SENSOR_ERROR = 2
};

enum class MagnetometerStatus : uint8_t {
NOT_SUPPORTED = 0,
DISABLED = 1,
ENABLED = 2,
};

class Sensor
{
public:
Expand Down Expand Up @@ -73,12 +79,18 @@ class Sensor
bool isValid() {
return sclPin != sdaPin;
};
bool isMagEnabled() {
return magStatus == MagnetometerStatus::ENABLED;
}
uint8_t getSensorId() {
return sensorId;
};
uint8_t getSensorType() {
return sensorType;
};
MagnetometerStatus getMagStatus() {
return magStatus;
};
const Vector3& getAcceleration() {
return acceleration;
};
Expand All @@ -97,6 +109,7 @@ class Sensor
bool configured = false;
bool working = false;
uint8_t calibrationAccuracy = 0;
MagnetometerStatus magStatus = MagnetometerStatus::NOT_SUPPORTED;
Quat sensorOffset;

bool newFusedRotation = false;
Expand Down

0 comments on commit 3da4427

Please sign in to comment.