-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This updated EMG Envelop code now works for Arduino UNO/NANO, ESP32, and other development boards.
- Loading branch information
1 parent
1a584bb
commit 14cf18d
Showing
1 changed file
with
25 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
// EMG Envelop - EXG Pill | ||
// EMG Envelop - BioAmp EXG Pill | ||
// https://github.com/upsidedownlabs/BioAmp-EXG-Pill | ||
|
||
// Copyright (c) 2021 Upside Down Labs | ||
// Upside Down Labs invests time and resources providing this open source code, | ||
// please support Upside Down Labs and open-source hardware by purchasing | ||
// products from Upside Down Labs! | ||
|
||
// Copyright (c) 2021 Upside Down Labs - [email protected] | ||
|
||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
|
@@ -20,17 +25,17 @@ | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
#define SampleRate 500 | ||
#define BaudRate 115200 | ||
#define inputPin A0 | ||
#define BufferSize 128 | ||
#define SAMPLE_RATE 500 | ||
#define BAUD_RATE 115200 | ||
#define INPUT_PIN A0 | ||
#define BUFFER_SIZE 128 | ||
|
||
int circularBuffer[BufferSize]; | ||
int index, sum; | ||
int circular_buffer[BUFFER_SIZE]; | ||
int data_index, sum; | ||
|
||
void setup() { | ||
// Serial connection begin | ||
Serial.begin(BaudRate); | ||
Serial.begin(BAUD_RATE); | ||
} | ||
|
||
void loop() { | ||
|
@@ -46,23 +51,23 @@ void loop() { | |
|
||
// Sample and get envelop | ||
if(timer < 0) { | ||
timer += 1000000 / SampleRate; | ||
int sensorValue = analogRead(inputPin); | ||
int EMGSignal = EMGFilter(sensorValue); | ||
int envelop = getEnvelop(abs(EMGSignal)); | ||
Serial.print(EMGSignal); | ||
timer += 1000000 / SAMPLE_RATE; | ||
int sensor_value = analogRead(INPUT_PIN); | ||
int signal = EMGFilter(sensor_value); | ||
int envelop = getEnvelop(abs(signal)); | ||
Serial.print(signal); | ||
Serial.print(","); | ||
Serial.println(envelop); | ||
} | ||
} | ||
|
||
// Envelop detection algorithm | ||
int getEnvelop(int absEMG){ | ||
sum -= circularBuffer[index]; | ||
sum += absEMG; | ||
circularBuffer[index] = absEMG; | ||
index = (index + 1) % BufferSize; | ||
return (sum/BufferSize) * 2; | ||
int getEnvelop(int abs_emg){ | ||
sum -= circular_buffer[data_index]; | ||
sum += abs_emg; | ||
circular_buffer[data_index] = abs_emg; | ||
data_index = (data_index + 1) % BUFFER_SIZE; | ||
return (sum/BUFFER_SIZE) * 2; | ||
} | ||
|
||
// Band-Pass Butterworth IIR digital filter, generated using filter_gen.py. | ||
|