hybridgroup / gobot

Golang framework for robotics, drones, and the Internet of Things (IoT)
https://gobot.io
Other
8.97k stars 1.04k forks source link

[HCSR04] no clean finish for discrete edge polling with gpiod #1098

Open gen2thomas opened 2 weeks ago

gen2thomas commented 2 weeks ago

If "WithHCSR04UseEdgePolling()" is used, stopping the robot by CTRL-C hangs. This is only tested with gpiod driver, not sysfs.

This is caused by the reconfiguration of the pin with this call hierarchy:

    wg := sync.WaitGroup{}
    wg.Add(1)

    go func() {
...
        var firstLoopDone bool
        for {
            select {
            case <-quitChan:
                return
            default:
...             if !firstLoopDone {
                    wg.Done()
                    firstLoopDone = true
                }
            }
        }
    }()

    wg.Wait()

The wait group ensures, that the first reading is done for the edge detection. But for the "Finalize()" call the quitChan will be closed immediately and the waitgroup is not done. This is more clean and will fix the problem:

for {
            select {
            case <-quitChan:
                if !firstLoopDone {
                    wg.Done()
                }
                return
            default: