-
-
Notifications
You must be signed in to change notification settings - Fork 368
Expand file tree
/
Copy pathSensorBuilder.h
More file actions
370 lines (326 loc) · 10.4 KB
/
SensorBuilder.h
File metadata and controls
370 lines (326 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
SlimeVR Code is placed under the MIT license
Copyright (c) 2025 Eiren Rain & 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 <map>
#include <memory>
#include <optional>
#include <type_traits>
#include "EmptySensor.h"
#include "ErroneousSensor.h"
#include "PinInterface.h"
#include "SensorManager.h"
#include "bno055sensor.h"
#include "bno080sensor.h"
#include "consts.h"
#include "globals.h"
#include "icm20948sensor.h"
#include "logging/Logger.h"
#include "mpu6050sensor.h"
#include "mpu9250sensor.h"
#include "sensor.h"
#include "sensorinterface/DirectPinInterface.h"
#include "sensorinterface/DirectSPIInterface.h"
#include "sensorinterface/I2CPCAInterface.h"
#include "sensorinterface/I2CWireSensorInterface.h"
#include "sensorinterface/MCP23X17PinInterface.h"
#include "sensorinterface/RegisterInterface.h"
#include "sensorinterface/SPIImpl.h"
#include "sensorinterface/SensorInterface.h"
#include "sensorinterface/SensorInterfaceManager.h"
#include "sensorinterface/i2cimpl.h"
#include "softfusion/drivers/bmi160.h"
#include "softfusion/drivers/bmi270.h"
#include "softfusion/drivers/icm42688.h"
#include "softfusion/drivers/icm45605.h"
#include "softfusion/drivers/icm45686.h"
#include "softfusion/drivers/icm55686.h"
#include "softfusion/drivers/lsm6ds3trc.h"
#include "softfusion/drivers/lsm6dso.h"
#include "softfusion/drivers/lsm6dsr.h"
#include "softfusion/drivers/lsm6dsv.h"
#include "softfusion/drivers/mpu6050.h"
#include "softfusion/softfusionsensor.h"
#ifndef PRIMARY_IMU_ADDRESS_ONE
#define PRIMARY_IMU_ADDRESS_ONE false
#endif
#ifndef SECONDARY_IMU_ADDRESS_TWO
#define SECONDARY_IMU_ADDRESS_TWO true
#endif
#if USE_RUNTIME_CALIBRATION
#include "sensors/softfusion/runtimecalibration/RuntimeCalibration.h"
#define SFCALIBRATOR RuntimeCalibration::RuntimeCalibrator
#else
#include "sensors/softfusion/SoftfusionCalibration.h"
#define SFCALIBRATOR SoftfusionCalibrator
#endif
#ifdef ESP32
#include "driver/i2c.h"
#endif
namespace SlimeVR::Sensors {
using SoftFusionLSM6DS3TRC
= SoftFusionSensor<SoftFusion::Drivers::LSM6DS3TRC, SFCALIBRATOR>;
using SoftFusionICM42688
= SoftFusionSensor<SoftFusion::Drivers::ICM42688, SFCALIBRATOR>;
using SoftFusionBMI270 = SoftFusionSensor<SoftFusion::Drivers::BMI270, SFCALIBRATOR>;
using SoftFusionLSM6DSV = SoftFusionSensor<SoftFusion::Drivers::LSM6DSV, SFCALIBRATOR>;
using SoftFusionLSM6DSO = SoftFusionSensor<SoftFusion::Drivers::LSM6DSO, SFCALIBRATOR>;
using SoftFusionLSM6DSR = SoftFusionSensor<SoftFusion::Drivers::LSM6DSR, SFCALIBRATOR>;
using SoftFusionMPU6050 = SoftFusionSensor<SoftFusion::Drivers::MPU6050, SFCALIBRATOR>;
using SoftFusionICM45686
= SoftFusionSensor<SoftFusion::Drivers::ICM45686, SFCALIBRATOR>;
using SoftFusionICM45605
= SoftFusionSensor<SoftFusion::Drivers::ICM45605, SFCALIBRATOR>;
using SoftFusionICM55686
= SoftFusionSensor<SoftFusion::Drivers::ICM55686, SFCALIBRATOR>;
using SoftFusionBMI160 = SoftFusionSensor<SoftFusion::Drivers::BMI160, SFCALIBRATOR>;
class SensorAuto {};
struct SensorBuilder {
private:
struct SensorDefinition {
uint8_t sensorID;
RegisterInterface& imuInterface;
float rotation;
SensorInterface* sensorInterface;
bool optional;
PinInterface* intPin;
int extraParam;
};
public:
SensorManager* m_Manager;
explicit SensorBuilder(SensorManager* sensorManager);
uint8_t buildAllSensors();
std::unique_ptr<::Sensor>
buildSensorDynamically(SensorTypeID type, SensorDefinition sensorDef);
template <typename Sensor, typename AccessInterface>
std::optional<std::pair<SensorTypeID, RegisterInterface*>> checkSensorPresent(
uint8_t sensorId,
SensorInterface* sensorInterface,
AccessInterface accessInterface
) {
auto* registerInterface
= getRegisterInterface<Sensor>(sensorId, sensorInterface, accessInterface);
if (!registerInterface->hasSensorOnBus()) {
return {};
}
if constexpr (requires {
{
Sensor::checkPresent(*registerInterface)
} -> std::same_as<SensorTypeID>;
}) {
auto type = Sensor::checkPresent(*registerInterface);
if (type == SensorTypeID::Unknown) {
return {};
}
return std::make_pair(type, registerInterface);
} else {
if (!Sensor::checkPresent(*registerInterface)) {
return {};
}
}
return std::make_pair(Sensor::TypeID, registerInterface);
}
template <typename AccessInterface>
inline std::optional<std::pair<SensorTypeID, RegisterInterface*>>
checkSensorsPresent(uint8_t, SensorInterface*, AccessInterface) {
return std::nullopt;
}
template <typename AccessInterface, typename Sensor, typename... Rest>
inline std::optional<std::pair<SensorTypeID, RegisterInterface*>>
checkSensorsPresent(
uint8_t sensorId,
SensorInterface* sensorInterface,
AccessInterface accessInterface
) {
auto result
= checkSensorPresent<Sensor>(sensorId, sensorInterface, accessInterface);
if (result) {
return result;
}
return checkSensorsPresent<AccessInterface, Rest...>(
sensorId,
sensorInterface,
accessInterface
);
}
template <typename Sensor, typename AccessInterface>
RegisterInterface* getRegisterInterface(
uint8_t sensorId,
SensorInterface* interface,
AccessInterface access
) {
if constexpr (std::is_base_of_v<
PinInterface,
std::remove_pointer_t<AccessInterface>>) {
return interfaceManager.spiImpl().get(
static_cast<DirectSPIInterface*>(interface),
access
);
} else if constexpr (std::is_same_v<AccessInterface, bool>) {
uint8_t addressIncrement = access ? 1 : 0;
return interfaceManager.i2cImpl().get(Sensor::Address + addressIncrement);
} else if constexpr (std::is_integral_v<AccessInterface>) {
return interfaceManager.i2cImpl().get(access);
}
return &EmptyRegisterInterface::instance;
}
template <typename AccessInterface>
std::optional<std::pair<SensorTypeID, RegisterInterface*>> findSensorType(
uint8_t sensorID,
SensorInterface* sensorInterface,
AccessInterface accessInterface
) {
sensorInterface->init();
sensorInterface->swapIn();
return checkSensorsPresent<
AccessInterface,
// SoftFusionLSM6DS3TRC,
// SoftFusionICM42688,
SoftFusionBMI270,
SoftFusionLSM6DSV,
SoftFusionLSM6DSO,
SoftFusionLSM6DSR,
// SoftFusionMPU6050,
SoftFusionICM45686,
// SoftFusionICM45605,
SoftFusionICM55686,
BNO085Sensor>(sensorID, sensorInterface, accessInterface);
}
template <typename SensorType, typename AccessInterface>
bool sensorDescEntry(
uint8_t sensorID,
AccessInterface accessInterface,
float rotation,
SensorInterface* sensorInterface,
bool optional = false,
PinInterface* intPin = nullptr,
int extraParam = 0
) {
std::unique_ptr<::Sensor> sensor;
if constexpr (std::is_same<SensorType, SensorAuto>::value) {
auto result = findSensorType(sensorID, sensorInterface, accessInterface);
if (!result) {
m_Manager->m_Logger.error(
"Can't find sensor type for sensor %d",
sensorID
);
return false;
}
auto sensorType = result->first;
auto& regInterface = *(result->second);
m_Manager->m_Logger.info(
"Sensor %d automatically detected with %s",
sensorID,
getIMUNameByType(sensorType)
);
sensor = buildSensorDynamically(
sensorType,
{
sensorID,
regInterface,
rotation,
sensorInterface,
optional,
intPin,
extraParam,
}
);
} else {
auto& regInterface = *getRegisterInterface<SensorType>(
sensorID,
sensorInterface,
accessInterface
);
sensor = buildSensor<SensorType>({
sensorID,
regInterface,
rotation,
sensorInterface,
optional,
intPin,
extraParam,
});
}
bool working = sensor->isWorking();
m_Manager->m_Sensors.push_back(std::move(sensor));
if (!working) {
return false;
}
m_Manager->m_Logger.info("Sensor %d configured", sensorID);
return true;
}
template <typename ImuType>
std::unique_ptr<::Sensor> buildSensor(SensorDefinition sensorDef) {
m_Manager->m_Logger.trace(
"Building IMU with: id=%d,\n\
address=%s, rotation=%f,\n\
interface=%s, int=%s, extraParam=%d, optional=%d",
sensorDef.sensorID,
sensorDef.imuInterface.toString().c_str(),
sensorDef.rotation,
sensorDef.sensorInterface->toString().c_str(),
sensorDef.intPin ? sensorDef.intPin->toString().c_str() : "None",
sensorDef.extraParam,
sensorDef.optional
);
// Now start detecting and building the IMU
std::unique_ptr<::Sensor> sensor;
// Init I2C bus for each sensor upon startup
sensorDef.sensorInterface->init();
sensorDef.sensorInterface->swapIn();
if (!sensorDef.imuInterface.hasSensorOnBus()) {
if (!sensorDef.optional) {
m_Manager->m_Logger.error(
"Mandatory sensor %d not found at address %s",
sensorDef.sensorID + 1,
sensorDef.imuInterface.toString().c_str()
);
return std::make_unique<ErroneousSensor>(
sensorDef.sensorID,
ImuType::TypeID
);
} else {
m_Manager->m_Logger.debug(
"Optional sensor %d not found at address %s",
sensorDef.sensorID + 1,
sensorDef.imuInterface.toString().c_str()
);
return std::make_unique<EmptySensor>(sensorDef.sensorID);
}
}
m_Manager->m_Logger.trace(
"Sensor %d found at address %s",
sensorDef.sensorID + 1,
sensorDef.imuInterface.toString().c_str()
);
sensor = std::make_unique<ImuType>(
sensorDef.sensorID,
sensorDef.imuInterface,
sensorDef.rotation,
sensorDef.sensorInterface,
sensorDef.intPin,
sensorDef.extraParam
);
sensor->motionSetup();
return sensor;
}
private:
SensorInterfaceManager interfaceManager;
};
} // namespace SlimeVR::Sensors