Skip to content

Commit 4a048a4

Browse files
committed
Removed digital filter. Updated everything to do integer math.
1 parent 2c89b60 commit 4a048a4

3 files changed

Lines changed: 30 additions & 50 deletions

File tree

Devlpr.cpp

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#include "Arduino.h"
22
#include "Devlpr.h"
33

4-
Devlpr::Devlpr()
4+
Devlpr::Devlpr(int pin)
55
{
6-
bufInd = BUFSIZE - 1;
7-
emgPin = A0;
6+
emgPin = pin;
87
emgRunningSum = 0;
8+
bufInd = BUFSIZE - 1;
99
}
1010

1111
void Devlpr::tick()
@@ -29,42 +29,21 @@ void Devlpr::tick()
2929
lastTickMicros = currMicros;
3030
}
3131

32-
int Devlpr::lastValue()
32+
unsigned int Devlpr::lastValue()
3333
{
3434
return buf[bufInd];
3535
}
3636

37-
int filt[93] = {24, 23, 100, 109, 130, 128, 81, 68, -39, -41, -181, -140, -280, -170, -294, -109, -225, 14, -124, 134, -61, 184, -87, 136, -205, 14, -365, -115, -491, -176, -523, -132, -450, -4, -320, 140, -215, 218, -204, 181, -306, 43, -479, -128, -637, -244, 9329, -244, -637, -128, -479, 43, -306, 181, -204, 218, -215, 140, -320, -4, -450, -132, -523, -176, -491, -115, -365, 14, -205, 136, -87, 184, -61, 134, -124, 14, -225, -109, -294, -170, -280, -140, -181, -41, -39, 68, 81, 128, 130, 109, 100, 23, 24};
38-
float filtFactor = 10000.0; // the multiplier on the integral filter
39-
float Devlpr::lastValueFiltered()
37+
unsigned int Devlpr::windowAvg()
4038
{
41-
// should help to roughly recenter data (use quick integral math)
42-
long sampAvg = emgRunningSum / BUFSIZE;
43-
// the buffer is the same size as the filter so filtered data
44-
// will be roughly len/2 ticks in the past
45-
long ret = 0;
46-
int currInd = bufInd + 1; // start at the tail
47-
// the filter is also symmetric, so convolve=linear combination
48-
for (int i = 0; i < BUFSIZE; i++) {
49-
// check the current circular buffer index
50-
if (currInd >= BUFSIZE) { // faster than modulo
51-
currInd = 0;
52-
}
53-
// we will need to coerce the ints to longs to avoid overflow
54-
long filtVal = (long)filt[i];
55-
long sampVal = (long)buf[currInd] - sampAvg;
56-
// do the multiply/add
57-
ret = ret + (filtVal * sampVal);
58-
// and update the current buffer index for next
59-
currInd++;
60-
}
61-
// our integral filter is multiplied by some factor, so correct
62-
return ret / filtFactor;
39+
return emgRunningSum / BUFSIZE;
6340
}
6441

65-
float Devlpr::windowAvg()
42+
int Devlpr::lastValueCentered()
6643
{
67-
return emgRunningSum / float(BUFSIZE);
44+
int lastVal = lastValue();
45+
int wAvg = windowAvg();
46+
return lastVal - wAvg;
6847
}
6948

7049
void Devlpr::readEMG()
@@ -73,7 +52,7 @@ void Devlpr::readEMG()
7352
// so update bufInd before the read
7453
bufInd++;
7554
// circular buffer
76-
if (bufInd >= BUFSIZE) { // surprisingly faster than mod
55+
if (bufInd >= BUFSIZE) { // faster than mod
7756
bufInd = 0;
7857
}
7958
// read our new value

Devlpr.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,24 @@
66
class Devlpr
77
{
88
public:
9-
Devlpr();
9+
Devlpr(int pin=A0);
1010
void tick();
11-
int lastValue();
12-
float lastValueFiltered();
13-
float windowAvg();
11+
unsigned int lastValue();
12+
int lastValueCentered();
13+
unsigned int windowAvg();
1414
private:
1515
// general buffer bookkeeping
16-
static const byte BUFSIZE = 93; // size of filter
16+
static const byte BUFSIZE = 32; // power of 2 for integer avg calc
17+
unsigned int buf[BUFSIZE];
1718
byte bufInd;
18-
int buf[BUFSIZE];
19+
// scheduling
20+
unsigned long MICROS_SCHED_EMG = 1000;
1921
unsigned long lastTickMicros = 0;
22+
unsigned long microsSinceEMG = 0;
2023
// emg tracking
21-
unsigned long MICROS_SCHED_EMG = 1000;
22-
unsigned long microsSinceEMG;
23-
unsigned long emgRunningSum;
2424
int emgPin;
25-
int emgVal;
25+
unsigned int emgVal; // ATMEGA boards have 10-bit ADC (0-1023)
26+
unsigned int emgRunningSum; // if BUFSIZE is small, uint is fine
2627
void readEMG();
2728
};
2829

libdevlpr.ino

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@
33
Devlpr devlpr;
44

55
void setup() {
6-
Serial.begin(9600);
6+
Serial.begin(2000000);
77
}
88

9-
unsigned long microsSinceFilteredCall = 0;
9+
unsigned long microsSincePrint = 0;
1010
unsigned long prevLoopMicros = 0;
11-
float result = 0.0;
11+
int result = 0;
1212
void loop() {
1313
// let the DEVLPR library do its job
1414
devlpr.tick();
1515

16-
// try to sample filtered data at 1000Hz
16+
// try to sample and print data at 1000Hz
1717
unsigned long currMicros = micros();
1818
unsigned long microsDelta = currMicros - prevLoopMicros;
19-
microsSinceFilteredCall += microsDelta;
19+
microsSincePrint += microsDelta;
2020
// has 1ms passed
21-
if (microsSinceFilteredCall > 1000) {
21+
if (microsSincePrint > 1000) {
2222
// 1ms has passed, get the value
23-
result = devlpr.lastValueFiltered();
23+
result = devlpr.lastValueCentered();
2424
Serial.println(result);
2525
// update our last filtered call
26-
microsSinceFilteredCall = 0;
26+
microsSincePrint = 0;
2727
}
2828
// update our last loop time
2929
prevLoopMicros = currMicros;

0 commit comments

Comments
 (0)