Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cd8210f

Browse files
+ MicromlGestureIdentificationExample
1 parent 1509c9a commit cd8210f

File tree

4 files changed

+143
-5
lines changed

4 files changed

+143
-5
lines changed

‎.idea/workspace.xml‎

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include "imu.h"
2+
//#include "model.h"
3+
4+
#define NUM_SAMPLES 30
5+
#define NUM_AXES 3
6+
#define TRUNCATE 20
7+
#define ACCEL_THRESHOLD 5
8+
#define INTERVAL 30
9+
10+
double baseline[NUM_AXES];
11+
double features[NUM_SAMPLES * NUM_AXES];
12+
13+
14+
void setup() {
15+
Serial.begin(115200);
16+
imu_setup();
17+
calibrate();
18+
}
19+
20+
void loop() {
21+
float ax, ay, az;
22+
23+
imu_read(&ax, &ay, &az);
24+
25+
ax = constrain(ax - baseline[0], -TRUNCATE, TRUNCATE);
26+
ay = constrain(ay - baseline[1], -TRUNCATE, TRUNCATE);
27+
az = constrain(az - baseline[2], -TRUNCATE, TRUNCATE);
28+
29+
if (!motionDetected(ax, ay, az)) {
30+
delay(10);
31+
return;
32+
}
33+
34+
recordIMU();
35+
printFeatures();
36+
// un-comment to run classification
37+
// classify();
38+
delay(2000);
39+
}
40+
41+
/**
42+
* "Zero" the readings
43+
*/
44+
void calibrate() {
45+
float ax, ay, az;
46+
47+
for (int i = 0; i < 10; i++) {
48+
imu_read(&ax, &ay, &az);
49+
delay(100);
50+
}
51+
52+
baseline[0] = ax;
53+
baseline[1] = ay;
54+
baseline[2] = az;
55+
}
56+
57+
/**
58+
* Detect if motion is happening
59+
* @return
60+
*/
61+
bool motionDetected(float ax, float ay, float az) {
62+
return (abs(ax) + abs(ay) + abs(az)) > ACCEL_THRESHOLD;
63+
}
64+
65+
/**
66+
* Fill the feature vector
67+
*/
68+
void recordIMU() {
69+
float ax, ay, az;
70+
71+
for (int i = 0; i < NUM_SAMPLES; i++) {
72+
imu_read(&ax, &ay, &az);
73+
74+
ax = constrain(ax - baseline[0], -TRUNCATE, TRUNCATE);
75+
ay = constrain(ay - baseline[1], -TRUNCATE, TRUNCATE);
76+
az = constrain(az - baseline[2], -TRUNCATE, TRUNCATE);
77+
78+
features[i * NUM_AXES + 0] = ax;
79+
features[i * NUM_AXES + 1] = ay;
80+
features[i * NUM_AXES + 2] = az;
81+
82+
delay(INTERVAL);
83+
}
84+
}
85+
86+
/**
87+
* Dump the feature vector to Serial monitor
88+
*/
89+
void printFeatures() {
90+
const uint16_t numFeatures = sizeof(features) / sizeof(double);
91+
92+
for (int i = 0; i < numFeatures; i++) {
93+
Serial.print(features[i]);
94+
Serial.print(i == numFeatures - 1 ? '\n' : ',');
95+
}
96+
}
97+
98+
/**
99+
*
100+
*/
101+
void classify() {
102+
Serial.print("Detected gesture: ");
103+
Serial.println(classIdxToName(predict(features)));
104+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <Wire.h>
2+
// library from https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/MPU6050
3+
#include <MPU6050.h>
4+
#define OUTPUT_READABLE_ACCELGYRO
5+
6+
MPU6050 imu;
7+
8+
void imu_setup() {
9+
Wire.begin();
10+
imu.initialize();
11+
}
12+
13+
void imu_read(float *ax, float *ay, float *az) {
14+
float gx, gy, gz;
15+
16+
imu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <Wire.h>
2+
// library from https://github.com/bolderflight/MPU9250
3+
#include <MPU9250.h>
4+
5+
MPU9250 imu(Wire, 0x68);
6+
7+
void imu_setup() {
8+
Wire.begin();
9+
imu.begin();
10+
}
11+
12+
void imu_read(float *ax, float *ay, float *az) {
13+
imu.readSensor();
14+
15+
*ax = imu.getAccelX_mss();
16+
*ay = imu.getAccelY_mss();
17+
*az = imu.getAccelZ_mss();
18+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /