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

Throw an exception when running wait_for_edge() in a new thread #28

Closed
frostcza opened this issue Dec 7, 2021 · 7 comments
Closed

Throw an exception when running wait_for_edge() in a new thread #28

frostcza opened this issue Dec 7, 2021 · 7 comments

Comments

@frostcza
Copy link

frostcza commented Dec 7, 2021

Hello, I'm trying to use GPIO::wait_for_edge() to calculate the high level duration of a PWM signal input like this:

` struct timeval start, end;

int mode_pin = 11;
GPIO::setmode(GPIO::BOARD);
GPIO::setup(mode_pin, GPIO::IN);
int high_voltage_time;

while (!end_this_program) 
{
    GPIO::wait_for_edge(mode_pin, GPIO::Edge::RISING);
    gettimeofday(&start,NULL);
    GPIO::wait_for_edge(mode_pin, GPIO::Edge::FALLING);
    gettimeofday(&end,NULL);
    high_voltage_time = end.tv_usec - start.tv_usec;
    printf("%d\n",high_voltage_time);
}`

It works well when executed independently. But I want to check two channels at the same time. And when I create two threads to run the same function, it works well for a while (about 1 minute) and then throws the exception:

[Exception] Failure to open the /sys/class/gpio/gpio{$ch}/edge file (catched from: GPIO::wait_for_edge())
terminate called after throwing an instance of 'std::runtime_error'
what(): Failure to open the /sys/class/gpio/gpio{$ch}/edge file
Aborted (core dumped)

Do you have any clue for this? Thank you.

@pjueon
Copy link
Owner

pjueon commented Dec 8, 2021

@ShimmyShaman Can you check this issue please?

@ShimmyShaman
Copy link
Collaborator

As long as the two channels are different channels the usage is okay. There should've been more error detail (at least to the console) for why the edge file cannot be opened (submitted pull request addresses this).

Try again with that, and at least find out why.

If not then I won't get my hands on my Jetson device for another 2-3 days to check it out.

@frostcza
Copy link
Author

frostcza commented Dec 9, 2021

Thanks for the reply. I added the std::perror() and tried again.
I got an extra message:

sysfs/edge open: Too many open files

And I checked the /proc/$pid/fd. There are hundreds of records like:

/sys/devices/2200000.gpio/gpiochip0/gpio/gpio428/value
and
/sys/devices/2200000.gpio/gpiochip0/gpio/gpio445/value

@frostcza
Copy link
Author

frostcza commented Dec 9, 2021

I found it is irrelevant to multi-threading. If I run it independently on a single channel for a long time, I will get the same error.
The complete code is as below.

#include <chrono>
#include <thread>
#include <signal.h>
#include <sys/time.h>
#include <JetsonGPIO.h>

static bool end_this_program = false;
void signalHandler(int s) { end_this_program = true; }
void delay(int s) { std::this_thread::sleep_for(std::chrono::seconds(s)); }


int main()
{
    signal(SIGINT, signalHandler);
    int pwm_input = 11; 
    GPIO::setmode(GPIO::BOARD);
    GPIO::setup(pwm_input, GPIO::IN);
    struct timeval start, end;
    int high;

    while (!end_this_program) 
    {
        printf("start to listen...\n");
        GPIO::wait_for_edge(pwm_input, GPIO::Edge::RISING);
        gettimeofday(&start,NULL);
        GPIO::wait_for_edge(pwm_input, GPIO::Edge::FALLING);
        gettimeofday(&end,NULL);
        high = end.tv_usec - start.tv_usec;
        printf("high voltage time: %d us\n", high);
        if(high>1500 && high<=2000)
        {
            printf("received save flag\n");
        }
    }

    GPIO::cleanup();
    return 0;
}

@ShimmyShaman ShimmyShaman mentioned this issue Dec 12, 2021
@ShimmyShaman
Copy link
Collaborator

ShimmyShaman commented Dec 12, 2021

Yeap, right on, a file wasn't being closed when it should've been. Good error report.

Added pull request. The files get closed now and the records don't pile up in /proc/$pid/fd

@frostcza
Copy link
Author

Thanks! Now it runs fine.

pjueon added a commit that referenced this issue Dec 12, 2021
bug fix for issue #28 and private/PythonFunctions/format function.
@pjueon
Copy link
Owner

pjueon commented Dec 12, 2021

@frostcza Thank you for confirming and the error report.
@ShimmyShaman Thank you for your help.

Merged the pull request. Closed the issue.

@pjueon pjueon closed this as completed Dec 12, 2021
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

No branches or pull requests

3 participants