Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If the Filter is used by the MCU for several times, the output is abnormal #3

Closed
allrobot opened this issue Apr 14, 2022 · 2 comments · May be fixed by #4
Closed

If the Filter is used by the MCU for several times, the output is abnormal #3

allrobot opened this issue Apr 14, 2022 · 2 comments · May be fixed by #4

Comments

@allrobot
Copy link

allrobot commented Apr 14, 2022

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

I use two MCUS, both raise the same abnormal.

How do I allow multiple filters to output normally?

@edgar-bonet
Copy link

The problem is that the state of the EMGFilters filters is stored in the global variables LPF, HPF and AHF. These being globals means that all instances of EMGFilters share their internal state.

The filter state should be stored as instance data. Otherwise all instances effectively share the same filter.

@edgar-bonet
Copy link

@allrobot: I don't understand why you closed the issue. It is still relevant and has not been fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants