-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRehTracker_arduino-code.ino
332 lines (274 loc) · 8.34 KB
/
RehTracker_arduino-code.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// This sketch manages 3 hardware components connected to an Arduino Nano 33 BLE:
// - LED strip
// - EMG muscle sensor
// - accelerometer (built-in)
// Updated values are sent via Bluetooth as BLE characteristic values
#include <Adafruit_NeoPixel.h> // allows to control the LED strip
#include <Arduino_LSM9DS1.h> // allows to use the built-in accelerometer
#include <ArduinoBLE.h> // allows to use the built-in BLE/WiFi module
#include <Wire.h> // allows to communicate to the LED strip via I2C protocol
// define digital pin to write to the LED strip
#define PIN 6
// define analog pin to read from the muscle sensor
#define EMG_SENSOR_FRONT A0
// general constants
const float MAX_CONTRACTION = 900;
const float MUSCLE_THRESHOLD = 600;
const float MIN_THRESHOLD = 200;
const float GYRO_THRESHOLD = 0.25;
const unsigned int LED_DELAY = 50;
const unsigned int GENERAL_DELAY = 50;
float current_EMG_value = 0;
float old_gyro_sign = 0;
bool old_EMG_threshold = true;
bool reached_minimum = true;
int contractionCounter = 0;
int rotationCounter = 0;
// maximum values recorded for one repetition (%)
float gyro_max_perc = 0;
float EMG_max_perc = 0;
// LED strip object declaration
Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, PIN, NEO_GRB + NEO_KHZ800);
// custom colours & colours array
uint16_t maxIndex = 0;
const uint32_t Red = strip.Color(255, 0, 0);
const uint32_t Yellow = strip.Color(255, 255, 0);
const uint32_t Green = strip.Color(0, 255, 0);
const uint32_t Blue = strip.Color(0, 0, 255);
const uint32_t colours[8] = {Red, Red, Yellow, Yellow, Yellow, Green, Green, Blue};
// accelerometer variables
float ax, ay, az;
// false: gyro exercise, true: muscle exercise
bool isMuscleExercise = true;
// false: RehTracker behaviour, true: robotics demo
bool isRoboticsDemo = false;
// BLE objects declarations
BLEService exerciseService("0ccc7966-1399-4c67-9ede-9b05dbea1ba2"); // create service "Physical Activity Monitor"
BLEIntCharacteristic exerciseValueCharacteristic("b964a50a-0001-4d37-97eb-971bf5233a98", BLERead | BLENotify); // create characteristic "Physical Activity Monitor Features"
BLEBoolCharacteristic exerciseTypeCharacteristic("b964a50a-0002-4d37-97eb-971bf5233a98", BLEWrite | BLERead); // create characteristic "Physical Activity Session Descriptor"
BLEBoolCharacteristic isRoboticsDemoCharacteristic("b964a50a-0003-4d37-97eb-971bf5233a98", BLEWrite | BLERead); // create characteristic for toggling the Robotics Demo mode
// --- SETUP
void setup()
{
// --- LED STRIP
// Initialising all pixels to 'off'
strip.setBrightness(10);
strip.begin();
strip.show();
// --- ACCELEROMETER
if (!IMU.begin())
{
Serial.println("Failed to initialise IMU!");
while (1)
;
}
// --- BLUETOOTH
if (!BLE.begin())
{
Serial.println("Failed to start BLE!");
while (1)
;
}
// Setting the local name the peripheral device advertises
BLE.setLocalName("RehTracker - Console");
// Setting the service this peripheral advertises
BLE.setAdvertisedService(exerciseService);
// Adding the characteristics to the service
exerciseService.addCharacteristic(exerciseValueCharacteristic);
exerciseService.addCharacteristic(exerciseTypeCharacteristic);
exerciseService.addCharacteristic(isRoboticsDemoCharacteristic);
BLE.addService(exerciseService);
// Initialising characteristics and advertising the service
exerciseValueCharacteristic.writeValue(0);
exerciseTypeCharacteristic.writeValue(isMuscleExercise);
isRoboticsDemoCharacteristic.writeValue(0);
BLE.advertise();
// Serial.begin(9600);
}
void loop()
{
// --- BLUETOOTH
// Waiting for a BLE central device to connect
BLEDevice central = BLE.central();
if (central)
{
while (central.connected())
{
bool isRoboticsDemoNewValue = isRoboticsDemoCharacteristic.value();
// Deals with the mode change
if (isRoboticsDemo != isRoboticsDemoNewValue)
{
old_EMG_threshold = false;
resetExerciseValue();
}
isRoboticsDemo = isRoboticsDemoNewValue;
// --- ROBOTICS DEMO
if (isRoboticsDemo)
{
handleRoboticsDemo();
}
else
{
bool isMuscleExerciseNewValue = exerciseTypeCharacteristic.value();
// Dealing with the exercise type change
if (isMuscleExercise != isMuscleExerciseNewValue)
{
contractionCounter = 0;
rotationCounter = 0;
resetExerciseValue();
}
isMuscleExercise = isMuscleExerciseNewValue;
// --- MUSCLE EXERCISE
if (isMuscleExercise)
{
handleMuscleExercise();
}
// --- GYRO EXERCISE
else
{
handleGyroExercise();
}
}
// --- LED STRIP
updateMaxIndex();
setColours();
// Delay to make LED-to-LED transition slower
delay(LED_DELAY);
}
// Resetting the LED strip when the central device disconnects
for (uint16_t i = 0; i < 8; i++)
{
strip.setPixelColor(i, strip.Color(0, 0, 0));
}
strip.show();
}
}
void resetExerciseValue()
{
// Resetting counter value
exerciseValueCharacteristic.writeValue(0);
}
void handleRoboticsDemo()
{
readFromMuscleSensor();
// Send value
exerciseValueCharacteristic.writeValue(current_EMG_value / MAX_CONTRACTION * 100);
delay(GENERAL_DELAY);
}
void handleMuscleExercise()
{
readFromMuscleSensor();
if (current_EMG_value > EMG_max_perc)
{
EMG_max_perc = current_EMG_value;
}
// Send value
if (old_EMG_threshold)
{
reached_minimum = (current_EMG_value <= MIN_THRESHOLD);
if(reached_minimum)
{
contractionCounter++;
exerciseValueCharacteristic.writeValue(contractionCounter);
EMG_max_perc = 0;
old_EMG_threshold = false;
}
return;
}
old_EMG_threshold = (current_EMG_value >= MUSCLE_THRESHOLD);
}
void handleGyroExercise()
{
readFromGyroSensor();
float current_gyro_sign = (ay > 0) ? 1 : ((ay < 0) ? -1 : 0);
if (abs(ay) >= GYRO_THRESHOLD)
{
if (abs(ay) > gyro_max_perc)
{
gyro_max_perc = abs(ay);
}
}
// Send value
if ((old_gyro_sign != current_gyro_sign) && (gyro_max_perc >= GYRO_THRESHOLD))
{
rotationCounter++;
exerciseValueCharacteristic.writeValue(rotationCounter);
gyro_max_perc = 0;
}
old_gyro_sign = current_gyro_sign;
// Serial.print(1);
// Serial.print("\t");
// Serial.print(-1);
// Serial.print("\t");
// Serial.print(GYRO_THRESHOLD);
// Serial.print("\t");
// Serial.print(-GYRO_THRESHOLD);
// Serial.print("\t");
// Serial.println(ay);
}
// Updates 'maxIndex' to the new last LED to light up, according to the contraction intensity
void updateMaxIndex()
{
// Keeping the first red LED always on
maxIndex = (uint16_t)1.0;
// --- MUSCLE EXERCISE
if (isMuscleExercise)
{
// Not the first LED
if (current_EMG_value / MAX_CONTRACTION > (1.0 / 8.0))
{
// Getting proper index in the interval (1,8]
maxIndex = (uint16_t)round(current_EMG_value / MAX_CONTRACTION * 8.0);
}
}
// --- GYRO EXERCISE
else
{
if (abs(ay) > 1.0)
{
maxIndex = (uint16_t)8.0;
}
else if (abs(ay) > (1.0 / 8.0))
{
maxIndex = (uint16_t)round(abs(ay) * 8.0);
}
}
}
// Switches off all the LEDs from 'maxIndex' to the last one (blue/max contraction intensity)
void resetToMaxInput()
{
for (uint16_t i = maxIndex; i < 8; i++)
{
strip.setPixelColor(i, strip.Color(0, 0, 0));
}
}
// Sets the colours of the LEDs that should light up, according to the contraction intensity
void setColours()
{
resetToMaxInput();
for (uint16_t i = 0; i < maxIndex; i++)
{
uint32_t c = colours[i];
strip.setPixelColor(i, c);
}
strip.show();
}
// Reads and updates 'current_EMG_value' with the current value from the muscle sensor
void readFromMuscleSensor()
{
current_EMG_value = analogRead(EMG_SENSOR_FRONT);
current_EMG_value = (current_EMG_value > MAX_CONTRACTION ? MAX_CONTRACTION : current_EMG_value);
// Serial.print(MAX_CONTRACTION);
// Serial.print("\t");
// Serial.print(0);
// Serial.print("\t");
// Serial.print(MUSCLE_THRESHOLD);
// Serial.print("\t");
// Serial.println(current_EMG_value);
}
// Reads and updates 'ay' with the current value from the built-in accelerometer
void readFromGyroSensor()
{
// Only 'ay' is actually used
IMU.readAcceleration(ax, ay, az);
}