-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathBLE-BCI-Car-Remote.ino
More file actions
469 lines (405 loc) · 15.7 KB
/
BLE-BCI-Car-Remote.ino
File metadata and controls
469 lines (405 loc) · 15.7 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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// Copyright (c) 2025 Aman Maheshwari - Aman@upsidedownlabs.tech
// Copyright (c) 2024 - 2025 Krishnanshu Mittal - krishnanshu@upsidedownlabs.tech
// Copyright (c) 2024 - 2025 Deepak Khatri - deepak@upsidedownlabs.tech
// Copyright (c) 2024 - 2025 Upside Down Labs - contact@upsidedownlabs.tech
// At Upside Down Labs, we create open-source DIY neuroscience hardware and software.
// Our mission is to make neuroscience affordable and accessible for everyone.
// By supporting us with your purchase, you help spread innovation and open science.
// Thank you for being part of this journey with us!
/*
This example is adapted from the client and server code provided by MoThunderz
Firmware: https://github.com/mo-thunderz/Esp32BlePart2
YouTube video: https://www.youtube.com/watch?v=s3yoZa6kzus
*/
#include <Adafruit_NeoPixel.h>
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <Arduino.h>
#include "esp_dsp.h"
#include <vector>
// constants won't change. They're used here to set pin numbers:
// Variables will change:
uint32_t buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
uint32_t bci_val = 0; // EEG-based control value (0=stop, 3=forward)
uint32_t emg1_val1 = 0; // Left EMG control value (0=inactive, 1=left turn)
uint32_t emg2_val2 = 0; // Right EMG control value (0=inactive, 2=right turn)
uint32_t bootback_val = 4; // Button toggle value (4=toggle state)
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
// Initialize all pointers
BLEServer *pServer = NULL; // Pointer to the server
BLECharacteristic *pCharacteristic_1 = NULL; // Pointer to Characteristic 1
BLECharacteristic *pCharacteristic_2 = NULL; // Pointer to Characteristic 2
BLEDescriptor *pDescr_1; // Pointer to Descriptor of Characteristic 1
BLE2902 *pBLE2902_1; // Pointer to BLE2902 of Characteristic 1
BLE2902 *pBLE2902_2; // Pointer to BLE2902 of Characteristic 2
// Some variables to keep track on device connected
bool deviceConnected = false;
bool oldDeviceConnected = false;
Adafruit_NeoPixel pixel(6, 15, NEO_GRB + NEO_KHZ800);
// Variable that will continuously be increased and written to the client
uint32_t value = 0;
uint32_t betaThreshold = 4;
uint32_t emgThreshold = 150;
bool isGoingBackward = false; // Flag to track backward state
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
// UUIDs used in this example:
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID_1 "beb5483e-36e1-4688-b7f5-ea07361b26a8"
#define CHARACTERISTIC_UUID_2 "1c95d5e3-d8f7-413a-bf3d-7a2e5d7be87e"
// Callback function that is called whenever a client is connected or disconnected
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer *pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer *pServer) {
deviceConnected = false;
}
};
// ----------------- USER CONFIGURATION -----------------
#define SAMPLE_RATE 512 // samples per second
#define FFT_SIZE 512 // must be a power of two
#define BAUD_RATE 115200
#define INPUT_PIN1 A0 // EEG input pin
#define INPUT_PIN2 A1 // Left hand EMG input pin
#define INPUT_PIN3 A2 // Right hand EMG input pin
// EEG bands (Hz)
#define DELTA_LOW 0.5f
#define DELTA_HIGH 4.0f
#define THETA_LOW 4.0f
#define THETA_HIGH 8.0f
#define ALPHA_LOW 8.0f
#define ALPHA_HIGH 13.0f
#define BETA_LOW 13.0f
#define BETA_HIGH 30.0f
#define GAMMA_LOW 30.0f
#define GAMMA_HIGH 45.0f
#define SMOOTHING_FACTOR 0.63f
#define EPS 1e-7f
// ----------------- BUFFERS & TYPES -----------------
float inputBuffer[FFT_SIZE];
float powerSpectrum[FFT_SIZE / 2];
// For two-real FFT trick
__attribute__((aligned(16))) float y_cf[FFT_SIZE * 2];
float *y1_cf = &y_cf[0];
typedef struct
{
float delta, theta, alpha, beta, gamma, total;
} BandpowerResults;
BandpowerResults smoothedPowers = { 0, 0, 0, 0, 0, 0 };
// ----------------- NOTCH FILTER CLASSES -----------------
// For 50Hz AC noise removal
// Band-Stop Butterworth IIR digital filter, generated using filter_gen.py.
// Sampling rate: 500.0 Hz, frequency: [48.0, 52.0] Hz.
// Filter is order 2, implemented as second-order sections (biquads).
// Reference: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.butter.html
class NotchFilter {
private:
struct BiquadState {
float z1 = 0;
float z2 = 0;
};
BiquadState state1;
BiquadState state2;
public:
float process(float input) {
float output = input;
// First biquad section
float x = output - (-1.56858163f * state1.z1) - (0.96424138f * state1.z2);
output = 0.96508099f * x + (-1.56202714f * state1.z1) + (0.96508099f * state1.z2);
state1.z2 = state1.z1;
state1.z1 = x;
// Second biquad section
x = output - (-1.61100358f * state2.z1) - (0.96592171f * state2.z2);
output = 1.00000000f * x + (-1.61854514f * state2.z1) + (1.00000000f * state2.z2);
state2.z2 = state2.z1;
state2.z1 = x;
return output;
}
void reset() {
state1.z1 = state1.z2 = 0;
state2.z1 = state2.z2 = 0;
}
};
// ----------------- EMG FILTER CLASSES -----------------
// High-Pass Butterworth IIR digital filter, generated using filter_gen.py.
// Sampling rate: 500.0 Hz, frequency: 70.0 Hz.
// Filter is order 2, implemented as second-order sections (biquads).
// Reference: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.butter.html
class EMGHighPassFilter {
private:
// Filter state for a single channel
double z1 = 0.0;
double z2 = 0.0;
public:
// Process a single sample
double process(double input) {
const double x = input - -0.82523238 * z1 - 0.29463653 * z2;
const double output = 0.52996723 * x + -1.05993445 * z1 + 0.52996723 * z2;
// Update state
z2 = z1;
z1 = x;
return output;
}
// Reset filter state
void reset() {
z1 = 0.0;
z2 = 0.0;
}
};
// Class to calculate EMG Envelope
class EnvelopeFilter {
private:
std::vector<double> circularBuffer;
double sum = 0.0;
int dataIndex = 0;
const int bufferSize;
public:
EnvelopeFilter(int bufferSize)
: bufferSize(bufferSize) {
circularBuffer.resize(bufferSize, 0.0);
}
double getEnvelope(double absEmg) {
sum -= circularBuffer[dataIndex];
sum += absEmg;
circularBuffer[dataIndex] = absEmg;
dataIndex = (dataIndex + 1) % bufferSize;
return (sum / bufferSize);
}
};
// NEW: Low-pass filter for EEG signals
float EEGFilter(float input) {
float output = input;
{
static float z1 = 0, z2 = 0;
float x = output - -1.22465158 * z1 - 0.45044543 * z2;
output = 0.05644846 * x + 0.11289692 * z1 + 0.05644846 * z2;
z2 = z1;
z1 = x;
}
return output;
}
NotchFilter filters[3]; // Notch filters for all 3 input channels
EMGHighPassFilter emgfilters[2]; // High-pass filters for EMG channels
EnvelopeFilter Envelopefilter1(16); // Envelope detector for left EMG
EnvelopeFilter Envelopefilter2(16); // Envelope detector for right EMG
// ----------------- BANDPOWER & SMOOTHING -----------------
BandpowerResults calculateBandpower(float *ps, float binRes, int halfSize) {
BandpowerResults r = { 0, 0, 0, 0, 0, 0 };
for (int i = 1; i < halfSize; i++) {
float freq = i * binRes;
float p = ps[i];
r.total += p;
if (freq >= DELTA_LOW && freq < DELTA_HIGH)
r.delta += p;
else if (freq >= THETA_LOW && freq < THETA_HIGH)
r.theta += p;
else if (freq >= ALPHA_LOW && freq < ALPHA_HIGH)
r.alpha += p;
else if (freq >= BETA_LOW && freq < BETA_HIGH)
r.beta += p;
else if (freq >= GAMMA_LOW && freq < GAMMA_HIGH)
r.gamma += p;
}
return r;
}
void smoothBandpower(const BandpowerResults *raw, BandpowerResults *s) {
s->delta = SMOOTHING_FACTOR * raw->delta + (1 - SMOOTHING_FACTOR) * s->delta;
s->theta = SMOOTHING_FACTOR * raw->theta + (1 - SMOOTHING_FACTOR) * s->theta;
s->alpha = SMOOTHING_FACTOR * raw->alpha + (1 - SMOOTHING_FACTOR) * s->alpha;
s->beta = SMOOTHING_FACTOR * raw->beta + (1 - SMOOTHING_FACTOR) * s->beta;
s->gamma = SMOOTHING_FACTOR * raw->gamma + (1 - SMOOTHING_FACTOR) * s->gamma;
s->total = SMOOTHING_FACTOR * raw->total + (1 - SMOOTHING_FACTOR) * s->total;
}
// ----------------- DSP FFT SETUP -----------------
void initFFT() {
// initialize esp-dsp real-FFT (two-real trick)
esp_err_t err = dsps_fft2r_init_fc32(NULL, FFT_SIZE);
if (err != ESP_OK) {
Serial.println("FFT init failed");
while (1)
delay(10);
}
}
// ----------------- FFT + BANDPOWER + PEAK -----------------
void processFFT() {
// pack real→complex: real=inputBuffer, imag=0
for (int i = 0; i < FFT_SIZE; i++) {
y_cf[2 * i] = inputBuffer[i];
y_cf[2 * i + 1] = 0.0f;
}
// FFT
dsps_fft2r_fc32(y_cf, FFT_SIZE);
dsps_bit_rev_fc32(y_cf, FFT_SIZE);
dsps_cplx2reC_fc32(y_cf, FFT_SIZE);
// magnitude² spectrum
int half = FFT_SIZE / 2;
for (int i = 0; i < half; i++) {
float re = y1_cf[2 * i];
float im = y1_cf[2 * i + 1];
powerSpectrum[i] = re * re + im * im;
}
// detect peak bin (skip i=0)
int maxIdx = 1;
float maxP = powerSpectrum[1];
for (int i = 2; i < half; i++) {
if (powerSpectrum[i] > maxP) {
maxP = powerSpectrum[i];
maxIdx = i;
}
}
float binRes = float(SAMPLE_RATE) / FFT_SIZE;
float peakHz = maxIdx * binRes;
// bandpower & smoothing
BandpowerResults raw = calculateBandpower(powerSpectrum, binRes, half);
smoothBandpower(&raw, &smoothedPowers);
float T = smoothedPowers.total + EPS;
Serial.println(((smoothedPowers.beta / T) * 100)); // for debugging purpose only
// If the power exceeds the threshold (set as 4% of the total power), the threshold value can be adjusted based on your beta parameters.
if (((smoothedPowers.beta / T) * 100) > betaThreshold && !isGoingBackward) {
bci_val = 3;
Serial.println("send 3");
pCharacteristic_1->setValue(bci_val); // for forward moving car
pCharacteristic_1->notify();
digitalWrite(7, HIGH); // Visual feedback
} else {
bci_val = 0;
pCharacteristic_1->setValue(bci_val);
pCharacteristic_1->notify();
digitalWrite(7, LOW);
}
}
void setup() {
// ----- Initialize Neopixel LED -----
pixel.begin();
// Set the Neopixel to red (indicating device turned on)
pixel.setPixelColor(0, pixel.Color(0, 0, 0));
pixel.setPixelColor(2, pixel.Color(0, 0, 0));
pixel.setPixelColor(5, pixel.Color(0, 0, 0));
pixel.show();
Serial.begin(BAUD_RATE);
pinMode(INPUT_PIN1, INPUT);
pinMode(INPUT_PIN2, INPUT);
pinMode(INPUT_PIN3, INPUT);
initFFT();
// Create the BLE Device
BLEDevice::init("ESP32");
// Create the BLE Server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
pCharacteristic_1 = pService->createCharacteristic(
CHARACTERISTIC_UUID_1,
BLECharacteristic::PROPERTY_NOTIFY);
pCharacteristic_2 = pService->createCharacteristic(
CHARACTERISTIC_UUID_2,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY);
// Create a BLE Descriptor
pDescr_1 = new BLEDescriptor((uint16_t)0x2901);
pDescr_1->setValue("A very interesting variable");
pCharacteristic_1->addDescriptor(pDescr_1);
// Add the BLE2902 Descriptor because we are using "PROPERTY_NOTIFY"
pBLE2902_1 = new BLE2902();
pBLE2902_1->setNotifications(true);
pCharacteristic_1->addDescriptor(pBLE2902_1);
pBLE2902_2 = new BLE2902();
pBLE2902_2->setNotifications(true);
pCharacteristic_2->addDescriptor(pBLE2902_2);
// Start the service
pService->start();
// Start advertising
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(false);
pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter
BLEDevice::startAdvertising();
Serial.println("Waiting a client connection to notify...");
}
void loop() {
static uint16_t idx = 0;
static unsigned long lastMicros = micros();
unsigned long now = micros(), dt = now - lastMicros;
lastMicros = now;
pixel.setPixelColor(0, pixel.Color(255, 255, 0)); // Yellow indicating running
// Update NeoPixel based on connection status
if (deviceConnected) {
pixel.setPixelColor(5, pixel.Color(0, 255, 0)); // Green when connected
} else {
pixel.setPixelColor(5, pixel.Color(255, 0, 0)); // Red when disconnected
}
pixel.show();
static long timer = 0;
timer -= dt;
if (timer <= 0) {
timer += 1000000L / SAMPLE_RATE;
int raw1 = analogRead(INPUT_PIN1);
int raw2 = analogRead(INPUT_PIN2);
int raw3 = analogRead(INPUT_PIN3);
float filteeg = EEGFilter(filters[0].process(raw1));
float filtemg1 = emgfilters[0].process(filters[1].process(raw2));
float filtemg2 = emgfilters[1].process(filters[2].process(raw3));
inputBuffer[idx++] = filteeg;
float env1 = Envelopefilter1.getEnvelope(abs(filtemg1));
float env2 = Envelopefilter2.getEnvelope(abs(filtemg2));
// If `env1` exceeds 150, trigger left turn command (send value 1).the threshold value can be adjusted based on your emg parameters.
if (env1 > emgThreshold * 0.8 && env2 > emgThreshold * 0.8) {
bootback_val = 4;
isGoingBackward = true; // Set backward flag
Serial.println("sent 4 - BOTH EMG ACTIVE"); // for debugging purpose only
pCharacteristic_1->setValue(bootback_val); // send value 4
pCharacteristic_1->notify();
} else if (env1 > emgThreshold) {
emg1_val1 = 2;
isGoingBackward = false; // Not going backward anymore
Serial.println("sent 2"); // for debugging purpose only
pCharacteristic_1->setValue(emg1_val1); // for left turn car
pCharacteristic_1->notify();
}
// If `env2` exceeds 150, trigger right turn command (send value 2).the threshold value can be adjusted based on your emg parameters.
else if (env2 > emgThreshold) {
emg2_val2 = 1;
isGoingBackward = false; // Not going backward anymore
Serial.println("sent 1"); // for debugging purpose only
pCharacteristic_1->setValue(emg2_val2); // for right turn car
pCharacteristic_1->notify();
} else {
isGoingBackward = false; // Reset backward flag
}
}
if (idx >= FFT_SIZE) {
processFFT();
idx = 0;
}
// Disconnecting
if (!deviceConnected && oldDeviceConnected) {
delay(500); // give the bluetooth stack the chance to get things ready
pServer->startAdvertising(); // restart advertising
Serial.println("start advertising");
oldDeviceConnected = deviceConnected;
}
// Connecting
if (deviceConnected && !oldDeviceConnected) {
// do stuff here on connecting
oldDeviceConnected = deviceConnected;
}
}