Skip to content

Commit 475b4bf

Browse files
authored
Merge pull request #41 from sensebox/feat/auto-detect-acc-sensor
auto detect acceleration sensor
2 parents 1f7cda5 + ec3450a commit 475b4bf

File tree

3 files changed

+62
-28
lines changed

3 files changed

+62
-28
lines changed

senseBox-bike-atrai-v2/platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ lib_deps =
2323
adafruit/Adafruit MPU6050@^2.2.6
2424
sensebox/SenseBoxBLE@^1.1.0
2525
adafruit/Adafruit NeoPixel@^1.12.2
26-
; tanakamasayuki/TensorFlowLite_ESP32@^1.0.0
26+
invensenseinc/ICM42670P@^1.0.7

senseBox-bike-atrai-v2/src/sensors/AccelerationSensor/AccelerationSensor.cpp

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,34 @@
44
AccelerationSensor::AccelerationSensor() : BaseSensor("accelerationSensorTask", 2048, 0) {}
55

66
String surfaceClassificationUUID = "B944AF10F4954560968F2F0D18CAB521";
7-
// String accUUID = "B944AF10F4954560968F2F0D18CAB522";
87
String anomalyUUID = "B944AF10F4954560968F2F0D18CAB523";
98
int surfaceClassificationCharacteristic = 0;
109
int anomalyCharacteristic = 0;
1110

12-
Adafruit_MPU6050 mpu;
13-
1411
void AccelerationSensor::initSensor()
1512
{
16-
17-
if (!mpu.begin(0x68, &Wire1))
18-
{
19-
Serial.println("MPU6050 Chip wurde nicht gefunden");
20-
return;
21-
}
22-
else
13+
// Try initializing MPU6050 first
14+
if (mpu.begin(0x68, &Wire1))
2315
{
2416
Serial.println("MPU6050 Found!");
2517
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
2618
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
2719
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
28-
delay(100);
29-
};
20+
activeSensor = MPU6050;
21+
}
22+
else if (icm.begin() == 0)
23+
{
24+
// If MPU6050 fails, try ICM42670P
25+
Serial.println("ICM42670P Found!");
26+
icm.startAccel(21, 8); // Accel ODR = 100 Hz, Full Scale Range = 16G
27+
icm.startGyro(21, 500); // Gyro ODR = 100 Hz, Full Scale Range = 2000 dps
28+
activeSensor = ICM42670X;
29+
}
30+
else
31+
{
32+
Serial.println("No compatible acceleration sensor found");
33+
return;
34+
}
3035

3136
surfaceClassificationCharacteristic = BLEModule::createCharacteristic(surfaceClassificationUUID.c_str());
3237
anomalyCharacteristic = BLEModule::createCharacteristic(anomalyUUID.c_str());
@@ -46,25 +51,44 @@ float prevAccTime = millis();
4651
bool AccelerationSensor::readSensorData()
4752
{
4853
bool classified = false;
49-
sensors_event_t a, g, temp;
54+
5055

51-
mpu.getEvent(&a, &g, &temp);
52-
53-
buffer[ix++] = a.acceleration.x;
54-
buffer[ix++] = a.acceleration.y;
55-
buffer[ix++] = a.acceleration.z;
56-
buffer[ix++] = g.gyro.x;
57-
buffer[ix++] = g.gyro.y;
58-
buffer[ix++] = g.gyro.z;
56+
if (activeSensor == MPU6050)
57+
{
58+
sensors_event_t a, g, temp;
59+
mpu.getEvent(&a, &g, &temp);
60+
buffer[ix++] = a.acceleration.x;
61+
buffer[ix++] = a.acceleration.y;
62+
buffer[ix++] = a.acceleration.z;
63+
buffer[ix++] = g.gyro.x;
64+
buffer[ix++] = g.gyro.y;
65+
buffer[ix++] = g.gyro.z;
66+
}
67+
else if (activeSensor == ICM42670X)
68+
{
69+
inv_imu_sensor_event_t imu_event;
70+
icm.getDataFromRegisters(imu_event);
71+
buffer[ix++] = (imu_event.accel[0]*9.81)/4096.0;
72+
buffer[ix++] = (imu_event.accel[1]*9.81)/4096.0;
73+
buffer[ix++] = (imu_event.accel[2]*9.81)/4096.0;
74+
buffer[ix++] = (imu_event.gyro[0]*2.0)/6550.0;
75+
buffer[ix++] = (imu_event.gyro[1]*2.0)/6550.0;
76+
buffer[ix++] = (imu_event.gyro[2]*2.0)/6550.0;
77+
}
78+
else
79+
{
80+
return false; // No sensor active
81+
}
5982

6083
Serial.println(millis() - prevAccTime);
6184
prevAccTime = millis();
6285

63-
// one second inverval
86+
// one second interval
6487
if (EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE <= ix)
6588
{
6689
classified = true;
67-
// Turn the raw buffer in a signal which we can the classify
90+
91+
// Turn the raw buffer in a signal which we can classify
6892
signal_t signal;
6993
int err = numpy::signal_from_buffer(buffer, EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE, &signal);
7094
if (err != 0)
@@ -76,7 +100,6 @@ bool AccelerationSensor::readSensorData()
76100

77101
// Run the classifier
78102
ei_impulse_result_t result = {};
79-
80103
err = run_classifier(&signal, &result, false);
81104
if (err != EI_IMPULSE_OK)
82105
{
@@ -95,12 +118,10 @@ bool AccelerationSensor::readSensorData()
95118

96119
if (sendBLE)
97120
{
98-
99121
notifyBLE(probAsphalt, probCompact, probPaving, probSett, probStanding, anomaly);
100122
}
101123

102124
ix = 0;
103-
104125
buffer[EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE] = {};
105126
}
106127

senseBox-bike-atrai-v2/src/sensors/AccelerationSensor/AccelerationSensor.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "../BaseSensor.h"
55
#include <Adafruit_MPU6050.h>
6+
#include "ICM42670P.h" // Include for ICM42670P
67

78
class AccelerationSensor : public BaseSensor
89
{
@@ -13,6 +14,18 @@ class AccelerationSensor : public BaseSensor
1314
protected:
1415
void initSensor() override;
1516
void notifyBLE(float probAsphalt, float probCompact, float probPaving, float probSett, float probStanding, float anomaly);
17+
18+
private:
19+
enum SensorType
20+
{
21+
NONE,
22+
MPU6050,
23+
ICM42670X // ICM42670X is a placeholder for the ICM42670P
24+
};
25+
26+
SensorType activeSensor = NONE; // Track which sensor is active
27+
Adafruit_MPU6050 mpu;
28+
ICM42670 icm = ICM42670(Wire1, 0); // Instantiate an ICM42670 with LSB address set to 0
1629
};
1730

18-
#endif // ACCELERATION_SENSOR_H
31+
#endif // ACCELERATION_SENSOR_H

0 commit comments

Comments
 (0)