-
Notifications
You must be signed in to change notification settings - Fork 1
/
fitness_classify.ino
212 lines (170 loc) · 5.73 KB
/
fitness_classify.ino
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
/*
* Machine learning model to classify the actions - walking, sitting, standing
* The model uses data from the built in IMU of the Arduino Nano 33 BLE Sense and
* uses the model to predict which action is being done. Using this inference it
* calculates the number of steps taken, the walking time, sitting time, and standing
* time. Bluetooth Low Energy(BLE) is used to send the results to the web interface.
*
* Code written by Ushan Fernando June 2021
*/
#include <ArduinoBLE.h>
#include <Arduino_LSM9DS1.h>
#include <TensorFlowLite.h>
#include <tensorflow/lite/micro/all_ops_resolver.h>
#include <tensorflow/lite/micro/micro_error_reporter.h>
#include <tensorflow/lite/micro/micro_interpreter.h>
#include <tensorflow/lite/schema/schema_generated.h>
#include <tensorflow/lite/version.h>
#include "model.h"
#define MOTION_THRESHOLD 0.08
#define CAPTURE_DELAY 200
#define NUM_SAMPLES 50
BLEService fitnessService("b7fb8e6c-0000-4ee6-9dc8-9c45b99a0356");
BLEStringCharacteristic modelCharacteristic("b7fb8e6c-8000-4ee6-9dc8-9c45b99a0356", BLENotify, 255);
const char *GESTURES[] = {
"walking", "sitting", "standing"
};
#define NUM_GESTURES (sizeof(GESTURES) / sizeof(GESTURES[0]))
bool isCapturing = false;
int numSamplesRead = 0;
float aX, aY, aZ, gX, gY, gZ;
unsigned int stepCount = 0;
unsigned int prevStepCount = 0;
String myValue = "unknown";
unsigned long curTime = 0;
unsigned long prevTime = 0;
unsigned int totalTime = 0;
unsigned int sittingTime = 0;
unsigned int standingTime = 0;
unsigned int walkingTime = 0;
bool isStepsUpdated = false;
tflite::MicroErrorReporter tflErrorReporter;
tflite::AllOpsResolver tflOpsResolver;
const tflite::Model* tflModel = nullptr;
tflite::MicroInterpreter* tflInterpreter = nullptr;
TfLiteTensor* tflInputTensor = nullptr;
TfLiteTensor* tflOutputTensor = nullptr;
constexpr int tensorArenaSize = 8 * 1024;
byte tensorArena[tensorArenaSize];
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}
Serial.print("Accelerometer sample rate: ");
Serial.print(IMU.accelerationSampleRate());
Serial.println(" Hz");
Serial.print("Gyroscope sample rate: ");
Serial.print(IMU.gyroscopeSampleRate());
Serial.println(" Hz");
Serial.println();
if (!BLE.begin()){
Serial.println("Failed to initialize BLE!");
while (1);
}
BLE.setLocalName("Fitness_Data");
BLE.setAdvertisedService(fitnessService);
fitnessService.addCharacteristic(modelCharacteristic);
BLE.addService(fitnessService);
BLE.advertise();
tflModel = tflite::GetModel(model);
if (tflModel->version() != TFLITE_SCHEMA_VERSION) {
Serial.println("Model schema mismatch!");
while (1);
}
tflInterpreter = new tflite::MicroInterpreter(tflModel, tflOpsResolver, tensorArena, tensorArenaSize, &tflErrorReporter);
tflInterpreter->AllocateTensors();
tflInputTensor = tflInterpreter->input(0);
tflOutputTensor = tflInterpreter->output(0);
}
void loop() {
BLEDevice central = BLE.central();
if (central){
digitalWrite(LED_BUILTIN, HIGH);
while (central.connected()){
if (modelCharacteristic.subscribed()){
getData();
timer();
}
}
digitalWrite(LED_BUILTIN, LOW);
}
}
void getData(){
if (!isCapturing) {
if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable()) {
IMU.readAcceleration(aX, aY, aZ);
IMU.readGyroscope(gX, gY, gZ);
float average = fabs(aX / 4.0) + fabs(aY / 4.0) + fabs(aZ / 4.0) + fabs(gX/2000.0) + fabs(gY/2000.0) + fabs(gZ/2000.0);
average /= 6;
if (average >= MOTION_THRESHOLD) {
isCapturing = true;
numSamplesRead = 0;
}
else{
return;
}
}
}
if (isCapturing) {
if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable()) {
IMU.readAcceleration(aX, aY, aZ);
IMU.readGyroscope(gX, gY, gZ);
tflInputTensor->data.f[numSamplesRead * 6 + 0] = aX / 4.0;
tflInputTensor->data.f[numSamplesRead * 6 + 1] = aY / 4.0;
tflInputTensor->data.f[numSamplesRead * 6 + 2] = aZ / 4.0;
tflInputTensor->data.f[numSamplesRead * 6 + 3] = gX / 2000.0;
tflInputTensor->data.f[numSamplesRead * 6 + 4] = gY / 2000.0;
tflInputTensor->data.f[numSamplesRead * 6 + 5] = gZ / 2000.0;
numSamplesRead++;
if (numSamplesRead == NUM_SAMPLES) {
isCapturing = false;
TfLiteStatus invokeStatus = tflInterpreter->Invoke();
if (invokeStatus != kTfLiteOk) {
Serial.println("Error: Invoke failed!");
return;
}
int maxIndex = 0;
float maxValue = 0;
for (int i = 0; i < NUM_GESTURES; i++) {
float _value = tflOutputTensor->data.f[i];
if(_value > maxValue){
maxValue = _value;
maxIndex = i;
}
}
if (strcmp(GESTURES[maxIndex], "walking") == 0){
stepCount++;
}
myValue = String(GESTURES[maxIndex]);
}
}
}
}
void timer(){
// Count number of seconds for each action and set value of BLE characteristic every second
curTime = millis();
if(curTime - prevTime >= 1000){
prevTime = curTime;
totalTime++;
Serial.println(totalTime);
isStepsUpdated = stepCount == prevStepCount ? false : true;
prevStepCount = stepCount;
if(myValue == "sitting"){
sittingTime++;
}
else if(myValue == "walking" && isStepsUpdated){
walkingTime++;
}
else{
if(myValue != "unknown"){
standingTime++;
}
}
modelCharacteristic.setValue(String("0")+"x"+myValue+"x"+String(stepCount));
delay(200);
modelCharacteristic.setValue(String("1")+"x"+String(walkingTime)+"x"+String(standingTime)+"x"+String(sittingTime));
}
}