-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_project_537_demo_host.txt
More file actions
109 lines (88 loc) · 2.85 KB
/
Copy pathfinal_project_537_demo_host.txt
File metadata and controls
109 lines (88 loc) · 2.85 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
#include <LSM6DS3.h>
#include <Wire.h>
#include <lstm_48_2_q.h>
LSM6DS3 myIMU(I2C_MODE, 0x6A); // I2C address for LSM6DS3
#define CONVERT_G_TO_MS2 9.80665f
// === Adjustable parameters ===
const int SAMPLE_RATE_HZ = 60; // Samples per second
const int NUM_SAMPLES = 60; // Total samples to record (1 second)
const float SAMPLE_INTERVAL_MS = 1000.0 / SAMPLE_RATE_HZ;
float meanX = 0.0f, meanY = 0.0f, meanZ = 0.0f;
const float MEAN_ALPHA = 0.02f; // smoothing factor
const float b0 = 0.13110644f;
const float b1 = 0.26221288f;
const float b2 = 0.13110644f;
const float a1 = -0.74778918f;
const float a2 = 0.27221494f;
// delay states per axis (DF2T)
float s1x = 0, s2x = 0;
float s1y = 0, s2y = 0;
float s1z = 0, s2z = 0;
// ==============================
inline float butterworth_filter(float x, float &s1, float &s2) {
float y = b0 * x + s1;
s1 = b1 * x - a1 * y + s2;
s2 = b2 * x - a2 * y;
return y;
}
void setup() {
Serial.begin(115200);
while (!Serial)
;
if (myIMU.begin() != 0) {
Serial.println("Device error");
while (1);
} else {
Serial.println("Device OK!");
}
Serial.println("Ready");
}
void loop() {
// Wait for serial command 's' to start recording
if (Serial.available()) {
char command = Serial.read();
if (command == 's' || command == 'S') {
recordMotion();
}
}
}
// ===========================================
// Function to record accelerometer data
// ===========================================
void recordMotion() {
Serial.println("Recording");
unsigned long lastSampleTime = millis();
float ax, ay, az;
for (int i = 0; i < NUM_SAMPLES; i++) {
ax = myIMU.readFloatAccelX() * CONVERT_G_TO_MS2;
ay = myIMU.readFloatAccelY() * CONVERT_G_TO_MS2;
az = myIMU.readFloatAccelZ() * CONVERT_G_TO_MS2;
// ---------------------------------------------------------
// 1) Exponential running mean (online DC removal)
// ---------------------------------------------------------
meanX += MEAN_ALPHA * (ax - meanX);
meanY += MEAN_ALPHA * (ay - meanY);
meanZ += MEAN_ALPHA * (az - meanZ);
float ax_demean = ax - meanX;
float ay_demean = ay - meanY;
float az_demean = az - meanZ;
// ---------------------------------------------------------
// 2) Apply 2nd-order Butterworth filter
// ---------------------------------------------------------
float fx = butterworth_filter(ax_demean, s1x, s2x);
float fy = butterworth_filter(ay_demean, s1y, s2y);
float fz = butterworth_filter(az_demean, s1z, s2z);
Serial.print(fx, 6);
Serial.print('\t');
Serial.print(fy, 6);
Serial.print('\t');
Serial.println(fz, 6);
// Wait for next sample
while (millis() - lastSampleTime < SAMPLE_INTERVAL_MS) {
// passive wait
}
lastSampleTime += SAMPLE_INTERVAL_MS;
}
Serial.println("Done");
Serial.println("Ready");
}