1

Problem

If the loop runs only one simulated pin and uses a filter, it will output normally (analog 1600~2200, filter 0~500), but creating an array to store multiple filtered simulated pin values will cause problems, and the output will be abnormal (10000~80000).

2022-04-15--15-00-07_fps=8_scale=720_max_colors=128|690x298

If analog read six sensors, and their value is 1600~2200.

It used the code below.

#include "EMGFilters.h"
#define SERIAL_TX_BUFFER_SIZE 4096 //修改串口接收缓冲区大小为2048
const int SensorInputPin[] = {A0, A1, A2, A3, A4, A5};
EMGFilters myFilter[6];
SAMPLE_FREQUENCY sampleRate = SAMPLE_FREQ_500HZ;
NOTCH_FREQUENCY humFreq = NOTCH_FREQ_50HZ;

void setup()
{

    for (int i = 0; i < 6; i++)
    {
        myFilter[i].init(sampleRate, humFreq, true, true, true);
    }
    Serial.begin(115200);
    Serial1.begin(250000);
    analogReadResolution(12);
}
void loop()
{
    long time = millis();
    int x[6] = {0,0,0,0,0,0};
    int analog[6] = {0,0,0,0,0,0};

    for (int index = 0; index < 6; index++)
    {
        //x[index] = myFilter[index].update(analogRead(SensorInputPin[index]));
        analog[index] = analogRead(SensorInputPin[index]);
    }
    while ((millis() - time) < 2)
    {
        delayMicroseconds(100);
    }
    Serial.printf("filter:%d %d %d %d %d %d analog:%d %d %d %d %d %d\n", x[0], x[1], x[2], x[3], x[4], x[5],analog[0],analog[1],analog[2],analog[3],analog[4],analog[5]);
    Serial1.printf("<%d,%d,%d,%d,%d,%d>", x[0], x[1], x[2], x[3], x[4], x[5]);
}

Change index<6 to index<2, and uncomment \\x[index] = myFilter[index].update(analogRead(SensorInputPin[index]));

It will be a normal output using two inputs and filters.

2022-04-15--15-15-26_fps=8_scale=720_max_colors=128|690x341

But changing index=0;index<2 to index=0;index<6 will be abnormal output. 2022-04-15--15-29-32_fps=8_scale=720_max_colors=28|690x323

If changing to index=3;index<4 or index=2;index<3...etc, it will be normal output: 2022-04-15--15-15-43_fps=8_scale=720_max_colors=128|690x459

Sketch: image|470x500 image|667x500

XIAO MCU schematic in the HTML: https://files.seeedstudio.com/wiki/Seeeduino-XIAO/res/Seeeduino-XIAO-v1.0-SCH-191112.pdf

I don't have a sensor schematic.

Filter code: https://github.com/oymotion/EMGFilters/blob/master/EMGFilters.cpp

I hope each simulation output to look like the first Gif(normal output), what should I do to achieve this?

11
  • 1
    You need one filter per input...?
    – Majenko
    Apr 13, 2022 at 10:34
  • You didn't provide any context on what you are actually measuring, but I second Majenko with the guess, that you need one filter per analog input. Also I don't really understand your problem description. If this is because of the language barrier, maybe someone can help you, who speaks english better, and help with the wording of the question.
    – chrisl
    Apr 13, 2022 at 10:51
  • @Majwnko yes. Is there any way to solve this problem?
    – C.SH.K
    Apr 13, 2022 at 11:29
  • @chrisl I'm sorry, I may not be as fluent in English as my mother tongue. I hope each simulation output to look like the first Gif.
    – C.SH.K
    Apr 13, 2022 at 11:31
  • You currently use only one filter: myFilter. But you can define multiple filters in the same way and put them into an array for use in the for loop. Something like this: EMGFilters myFilters[6]; (define an array of 6 EMGFilters). In setup(): for(int i=0;i<6;i++) myFilters[i].init(sampleRate, humFreq, true, true, true);. And in the for loop: envelops[i * 6 + x] = square(myFilters[x].update(analogRead(SensorInputPin[x])));.
    – chrisl
    Apr 13, 2022 at 12:21

1 Answer 1

2

This is a bug in the filter library you are using. It has already been reported (by you, maybe?).

As Majenko and chrisl suggested in comments, the crux of the problem is that you need one filter per input channel. Using multiple instances of EMGFilters seems like the obvious solution. Alas, the library keeps the filter state in global variables, which essentially means your six filter objects all reference the same actual filter.

6
  • Yes. It's my Issue. How do I prevent referencing global variables of the same filter? Need to reference "EMGFilters1.h","EMGFilters2.h"...etc?
    – C.SH.K
    Apr 15, 2022 at 10:23
  • @C.SH.K: No. You should fix the library, or wait for the authors to fix it. If you want to do it yourself, just turn the filter state into instance data. C.f. my comment to your issue. Apr 15, 2022 at 11:37
  • @C.SH.K: Or you may use the branch from this pull request. Apr 15, 2022 at 13:54
  • Thank you! Solved the immediate problem!
    – C.SH.K
    Apr 15, 2022 at 23:23
  • Image Image2After using your file, some pins have a strange place, some emG output tens of thousands of values, some normal output. Before, with the raw filter code, it would print like thisBefore index=0;index<1...etc
    – C.SH.K
    Apr 16, 2022 at 0:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.