-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaccelerometer.ino
More file actions
62 lines (54 loc) · 2.07 KB
/
accelerometer.ino
File metadata and controls
62 lines (54 loc) · 2.07 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
///////////////////////////////////////////
// ACCELEROMETER MODULE
// LSM6DS3 6-axis IMU on XIAO nRF52840 Sense (onboard, I2C address 0x6A)
// Reads accelerometer X/Y/Z in g-force for CSV logging
// Gyroscope data is available but not used in this version
///////////////////////////////////////////
static unsigned long accelLastReadMs = 0;
static const unsigned long ACCEL_READ_INTERVAL_MS = 20; // 50 Hz — plenty for motorsports logging
/**
* Accelerometer setup - initialize the onboard LSM6DS3 IMU
*
* Configured for ±16g range to capture hard braking and crash events
* without clipping. Resolution at 16g is 0.488 mg/LSB — still far
* finer than needed for motorsports data.
*
* Graceful degradation: if the IMU is not present (non-Sense board
* or hardware fault), accelAvailable stays false and accel values
* remain at 0.0 — logged as "0.000" in CSV with no other side effects.
*/
void ACCEL_SETUP() {
accelIMU.settings.accelRange = 16; // ±16g (captures crashes without clipping)
if (accelIMU.begin() != 0) {
debugln(F("Accelerometer not detected (non-Sense board?)"));
accelAvailable = false;
return;
}
accelAvailable = true;
debugln(F("Accelerometer ready (LSM6DS3, 16g range)"));
}
/**
* Accelerometer main loop - read raw g-force values
*
* Rate-limited to 50 Hz to avoid hammering the I2C bus every loop
* iteration (~250 Hz). 50 Hz is 2x the GPS logging rate and more
* than sufficient for motorsports g-force analysis.
*
* No filtering applied — raw g-force is the standard unit for
* motorsports data analysis (lateral G, braking G, etc.).
*/
void ACCEL_LOOP() {
if (!accelAvailable) return;
unsigned long now = millis();
if (now - accelLastReadMs < ACCEL_READ_INTERVAL_MS) return;
accelLastReadMs = now;
accelX = accelIMU.readFloatAccelX();
accelY = accelIMU.readFloatAccelY();
accelZ = accelIMU.readFloatAccelZ();
// debug(F("ACCEL: X="));
// debug(accelX, 3);
// debug(F(" Y="));
// debug(accelY, 3);
// debug(F(" Z="));
// debugln(accelZ, 3);
}