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

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

Open
gen2thomas opened this issue Nov 6, 2024 · 0 comments
Open

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

gen2thomas opened this issue Nov 6, 2024 · 0 comments
Assignees

Comments

@gen2thomas
Copy link
Collaborator

gen2thomas commented Nov 6, 2024

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:

  • DigitalPinsAdaptor.Finalize()
  • pin.Unexport()
  • digitalPinGpiodReconfigure()
  • digitalPinGpiodReconfigureLine()
  • startEdgePolling()
  • call of a go - function with a loop, like this
	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:
@gen2thomas gen2thomas self-assigned this Nov 6, 2024
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

1 participant