From 5f21bb3e600ad2b6fc19a3261e675059ac941b3b Mon Sep 17 00:00:00 2001 From: Juan Ignacio Date: Fri, 21 Oct 2022 19:22:10 +0200 Subject: [PATCH 1/5] Create hcsr04_driver.go Trting to translate HCSR04 from another lib that uses go-rpio. --- drivers/gpio/hcsr04_driver.go | 183 ++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 drivers/gpio/hcsr04_driver.go diff --git a/drivers/gpio/hcsr04_driver.go b/drivers/gpio/hcsr04_driver.go new file mode 100644 index 000000000..81471315e --- /dev/null +++ b/drivers/gpio/hcsr04_driver.go @@ -0,0 +1,183 @@ +package hcsr04 + +import ( + "errors" + "fmt" + "sync" + "time" + + log "github.com/sirupsen/logrus" + "gobot.io/x/gobot" + "gobot.io/x/gobot/drivers/gpio" +) + +const ( + soundSpeed float32 = 343.0 + measurementCycle time.Duration = 60 // 60ms between two measurements + // MonitorUpdate is the time between each monitor update + MonitorUpdate time.Duration = 100 * time.Millisecond +) + +// HCSR04 instance +type HCSR04 struct { + name string + connection gobot.Adaptor + triggerPin *gpio.DirectPinDriver + echoPin *gpio.DirectPinDriver + // triggerPin rpio.Pin + // echoPin rpio.Pin + mux sync.Mutex + Measure float32 // The last measure + distanceMonitorControl chan int + distanceMonitorStarted bool + gobot.Commander +} + +// NewHCSR04 creates a new HCSR04 instance +func NewHCSR04(a gobot.Adaptor, triggerPinID string, echoPinID string) *HCSR04 { + hcsr04 := &HCSR04{ + name: gobot.DefaultName("HCSR04"), + triggerPin: gpio.NewDirectPinDriver(a, triggerPinID), + echoPin: gpio.NewDirectPinDriver(a, echoPinID), + connection: a, + Commander: gobot.NewCommander(), + } + + // hcsr04 := HCSR04{ + // triggerPinID: triggerPinID, + // echoPinID: echoPinID, + // } + // hcsr04.triggerPin = rpio.Pin(hcsr04.triggerPinID) + // hcsr04.triggerPin.Mode(rpio.Output) + + // hcsr04.echoPin = rpio.Pin(hcsr04.echoPinID) + // hcsr04.echoPin.Mode(rpio.Input) + // hcsr04.echoPin.PullDown() + // hcsr04.triggerPin.Low() + return hcsr04 +} + +// MeasureDistance measure the distance in front of sensor in meters +// and returns the measure +// MeasureDistance triggers a distance measurement by the sensor +// +// ! MeasureDistance is not design to work in a fast loop +// For this specific usage, use StartDistanceMonitor associated with GetDistance Instead +func (hcsr04 *HCSR04) MeasureDistance() (float32, error) { + hcsr04.mux.Lock() + defer hcsr04.mux.Unlock() + pulseDuration, err := hcsr04.measurePulse() + if err != nil { + return 0, err + } + hcsr04.Measure = pulseToDistance(pulseDuration) + return hcsr04.Measure, nil +} + +func (hcsr04 *HCSR04) emitTrigger() { + hcsr04.triggerPin.On() + time.Sleep(10 * time.Microsecond) + hcsr04.triggerPin.Off() +} + +func (hcsr04 *HCSR04) measurePulse() (int64, error) { + startChan := make(chan int64) + stopChan := make(chan int64) + startQuit := false + stopQuit := false + var startTime int64 + var stopTime int64 + go getPinStateChangeTime(hcsr04.echoPin, 1, startChan, &startQuit) + hcsr04.emitTrigger() + readedValue, _ := hcsr04.echoPin.DigitalRead() + if readedValue == 1 { + return 0, errors.New("already receiving echo") + } + select { + case t := <-startChan: + startTime = t + case <-time.After(measurementCycle * time.Millisecond): + startQuit = true + return 0, fmt.Errorf("echo not received after %d milliseconds", measurementCycle) + } + go getPinStateChangeTime(hcsr04.echoPin, 0, stopChan, &stopQuit) + select { + case t := <-stopChan: + stopTime = t + case <-time.After(measurementCycle * time.Millisecond): + stopQuit = true + return 0, fmt.Errorf("echo received for more than %d milliseconds", measurementCycle) + } + return stopTime - startTime, nil +} + +func getPinStateChangeTime(pin *gpio.DirectPinDriver, state int, outChan chan int64, quit *bool) { + + for { + // readedValue, _ := pin.DigitalRead() + readedValue := 1 + // fmt.Println("Lectura: ", readedValue, state, *quit) + if readedValue != state && !*quit { + break + } + } + time.Sleep(100) + readedValue, _ := pin.DigitalRead() + if readedValue == state && !*quit { + outChan <- time.Now().UnixNano() + } +} + +func pulseToDistance(pulseDuration int64) float32 { + return float32(pulseDuration) / 1000000000.0 * soundSpeed / 2 +} + +// // GetDistance returns the last distance measured +// // Contrary to MeasureDistance, GetDistance does not trigger a distance measurement +func (hcsr04 *HCSR04) GetDistance() float32 { + return hcsr04.Measure +} + +// // StartDistanceMonitor starts a process which will keep Measure updated +func (hcsr04 *HCSR04) StartDistanceMonitor() error { + hcsr04.distanceMonitorControl = make(chan int) + if hcsr04.distanceMonitorStarted { + return errors.New("monitor already started") + } + go hcsr04.distanceMonitor() + return nil +} + +// // StopDistanceMonitor stop the monitor process +func (hcsr04 *HCSR04) StopDistanceMonitor() { + if hcsr04.distanceMonitorStarted { + hcsr04.distanceMonitorControl <- 1 + } +} + +func (hcsr04 *HCSR04) distanceMonitor() { + for { + select { + case <-hcsr04.distanceMonitorControl: + hcsr04.distanceMonitorStarted = false + return + default: + if _, err := hcsr04.MeasureDistance(); err != nil { + log.WithField("error", err).Error("impossible to measure distance") + } + } + time.Sleep(MonitorUpdate) + } +} + +func (h *HCSR04) Name() string { return h.name } + +func (h *HCSR04) SetName(n string) { h.name = n } + +func (h *HCSR04) Start() (err error) { return } + +func (h *HCSR04) Halt() (err error) { return } + +func (h *HCSR04) Connection() gobot.Connection { + return h.connection.(gobot.Connection) +} From 02685bac6ab6fee73b9e9e77e160499858b80fa1 Mon Sep 17 00:00:00 2001 From: Juan Ignacio Date: Sat, 29 Oct 2022 17:09:00 +0200 Subject: [PATCH 2/5] Fix PR sugestion --- drivers/gpio/hcsr04_driver.go | 368 +++++++++++++++++----------------- 1 file changed, 185 insertions(+), 183 deletions(-) diff --git a/drivers/gpio/hcsr04_driver.go b/drivers/gpio/hcsr04_driver.go index 81471315e..529dbf5e6 100644 --- a/drivers/gpio/hcsr04_driver.go +++ b/drivers/gpio/hcsr04_driver.go @@ -1,183 +1,185 @@ -package hcsr04 - -import ( - "errors" - "fmt" - "sync" - "time" - - log "github.com/sirupsen/logrus" - "gobot.io/x/gobot" - "gobot.io/x/gobot/drivers/gpio" -) - -const ( - soundSpeed float32 = 343.0 - measurementCycle time.Duration = 60 // 60ms between two measurements - // MonitorUpdate is the time between each monitor update - MonitorUpdate time.Duration = 100 * time.Millisecond -) - -// HCSR04 instance -type HCSR04 struct { - name string - connection gobot.Adaptor - triggerPin *gpio.DirectPinDriver - echoPin *gpio.DirectPinDriver - // triggerPin rpio.Pin - // echoPin rpio.Pin - mux sync.Mutex - Measure float32 // The last measure - distanceMonitorControl chan int - distanceMonitorStarted bool - gobot.Commander -} - -// NewHCSR04 creates a new HCSR04 instance -func NewHCSR04(a gobot.Adaptor, triggerPinID string, echoPinID string) *HCSR04 { - hcsr04 := &HCSR04{ - name: gobot.DefaultName("HCSR04"), - triggerPin: gpio.NewDirectPinDriver(a, triggerPinID), - echoPin: gpio.NewDirectPinDriver(a, echoPinID), - connection: a, - Commander: gobot.NewCommander(), - } - - // hcsr04 := HCSR04{ - // triggerPinID: triggerPinID, - // echoPinID: echoPinID, - // } - // hcsr04.triggerPin = rpio.Pin(hcsr04.triggerPinID) - // hcsr04.triggerPin.Mode(rpio.Output) - - // hcsr04.echoPin = rpio.Pin(hcsr04.echoPinID) - // hcsr04.echoPin.Mode(rpio.Input) - // hcsr04.echoPin.PullDown() - // hcsr04.triggerPin.Low() - return hcsr04 -} - -// MeasureDistance measure the distance in front of sensor in meters -// and returns the measure -// MeasureDistance triggers a distance measurement by the sensor -// -// ! MeasureDistance is not design to work in a fast loop -// For this specific usage, use StartDistanceMonitor associated with GetDistance Instead -func (hcsr04 *HCSR04) MeasureDistance() (float32, error) { - hcsr04.mux.Lock() - defer hcsr04.mux.Unlock() - pulseDuration, err := hcsr04.measurePulse() - if err != nil { - return 0, err - } - hcsr04.Measure = pulseToDistance(pulseDuration) - return hcsr04.Measure, nil -} - -func (hcsr04 *HCSR04) emitTrigger() { - hcsr04.triggerPin.On() - time.Sleep(10 * time.Microsecond) - hcsr04.triggerPin.Off() -} - -func (hcsr04 *HCSR04) measurePulse() (int64, error) { - startChan := make(chan int64) - stopChan := make(chan int64) - startQuit := false - stopQuit := false - var startTime int64 - var stopTime int64 - go getPinStateChangeTime(hcsr04.echoPin, 1, startChan, &startQuit) - hcsr04.emitTrigger() - readedValue, _ := hcsr04.echoPin.DigitalRead() - if readedValue == 1 { - return 0, errors.New("already receiving echo") - } - select { - case t := <-startChan: - startTime = t - case <-time.After(measurementCycle * time.Millisecond): - startQuit = true - return 0, fmt.Errorf("echo not received after %d milliseconds", measurementCycle) - } - go getPinStateChangeTime(hcsr04.echoPin, 0, stopChan, &stopQuit) - select { - case t := <-stopChan: - stopTime = t - case <-time.After(measurementCycle * time.Millisecond): - stopQuit = true - return 0, fmt.Errorf("echo received for more than %d milliseconds", measurementCycle) - } - return stopTime - startTime, nil -} - -func getPinStateChangeTime(pin *gpio.DirectPinDriver, state int, outChan chan int64, quit *bool) { - - for { - // readedValue, _ := pin.DigitalRead() - readedValue := 1 - // fmt.Println("Lectura: ", readedValue, state, *quit) - if readedValue != state && !*quit { - break - } - } - time.Sleep(100) - readedValue, _ := pin.DigitalRead() - if readedValue == state && !*quit { - outChan <- time.Now().UnixNano() - } -} - -func pulseToDistance(pulseDuration int64) float32 { - return float32(pulseDuration) / 1000000000.0 * soundSpeed / 2 -} - -// // GetDistance returns the last distance measured -// // Contrary to MeasureDistance, GetDistance does not trigger a distance measurement -func (hcsr04 *HCSR04) GetDistance() float32 { - return hcsr04.Measure -} - -// // StartDistanceMonitor starts a process which will keep Measure updated -func (hcsr04 *HCSR04) StartDistanceMonitor() error { - hcsr04.distanceMonitorControl = make(chan int) - if hcsr04.distanceMonitorStarted { - return errors.New("monitor already started") - } - go hcsr04.distanceMonitor() - return nil -} - -// // StopDistanceMonitor stop the monitor process -func (hcsr04 *HCSR04) StopDistanceMonitor() { - if hcsr04.distanceMonitorStarted { - hcsr04.distanceMonitorControl <- 1 - } -} - -func (hcsr04 *HCSR04) distanceMonitor() { - for { - select { - case <-hcsr04.distanceMonitorControl: - hcsr04.distanceMonitorStarted = false - return - default: - if _, err := hcsr04.MeasureDistance(); err != nil { - log.WithField("error", err).Error("impossible to measure distance") - } - } - time.Sleep(MonitorUpdate) - } -} - -func (h *HCSR04) Name() string { return h.name } - -func (h *HCSR04) SetName(n string) { h.name = n } - -func (h *HCSR04) Start() (err error) { return } - -func (h *HCSR04) Halt() (err error) { return } - -func (h *HCSR04) Connection() gobot.Connection { - return h.connection.(gobot.Connection) -} +package hcsr04 + +import ( + "errors" + "fmt" + "sync" + "time" + + "gobot.io/x/gobot" + "gobot.io/x/gobot/drivers/gpio" +) + +const ( + soundSpeed float32 = 343.0 // in [m/s] + // the device can measure 2cm .. 4m, this means sweep distances between 4cm and 8m + // this cause pulse durations between 0.12ms and 24ms (at 34.3 cm/ms, ~0.03 ms/cm, ~3ms/m) + measurementCycle time.Duration = 60 // 60ms between two measurements + MonitorUpdate time.Duration = 100 * time.Millisecond +) + +// HCSR04 instance +type HCSR04 struct { + name string + connection gobot.Adaptor + triggerPin *gpio.DirectPinDriver + echoPin *gpio.DirectPinDriver + // triggerPin rpio.Pin + // echoPin rpio.Pin + mux sync.Mutex + Measure float32 // The last measure + distanceMonitorControl chan int + distanceMonitorStarted bool + gobot.Commander +} + +// NewHCSR04 creates a new HCSR04 instance +func NewHCSR04(a gobot.Adaptor, triggerPinID string, echoPinID string) *HCSR04 { + hcsr04 := &HCSR04{ + name: gobot.DefaultName("HCSR04"), + triggerPin: gpio.NewDirectPinDriver(a, triggerPinID), + echoPin: gpio.NewDirectPinDriver(a, echoPinID), + connection: a, + Commander: gobot.NewCommander(), + } + + // hcsr04 := HCSR04{ + // triggerPinID: triggerPinID, + // echoPinID: echoPinID, + // } + // hcsr04.triggerPin = rpio.Pin(hcsr04.triggerPinID) + // hcsr04.triggerPin.Mode(rpio.Output) + + // hcsr04.echoPin = rpio.Pin(hcsr04.echoPinID) + // hcsr04.echoPin.Mode(rpio.Input) + // hcsr04.echoPin.PullDown() + // hcsr04.triggerPin.Low() + return hcsr04 +} + +// MeasureDistance measure the distance in front of sensor in meters +// and returns the measure +// MeasureDistance triggers a distance measurement by the sensor +// +// ! MeasureDistance is not design to work in a fast loop +// For this specific usage, use StartDistanceMonitor associated with GetDistance Instead +func (hcsr04 *HCSR04) MeasureDistance() (float32, error) { + hcsr04.mux.Lock() + defer hcsr04.mux.Unlock() + pulseDuration, err := hcsr04.measurePulse() + if err != nil { + return 0, err + } + hcsr04.Measure = pulseToDistance(pulseDuration) + return hcsr04.Measure, nil +} + +func (hcsr04 *HCSR04) emitTrigger() { + hcsr04.triggerPin.On() + time.Sleep(10 * time.Microsecond) + hcsr04.triggerPin.Off() +} + +func (hcsr04 *HCSR04) measurePulse() (int64, error) { + startChan := make(chan int64) + stopChan := make(chan int64) + startQuit := false + stopQuit := false + var startTime int64 + var stopTime int64 + go getPinStateChangeTime(hcsr04.echoPin, 1, startChan, &startQuit) + hcsr04.emitTrigger() + // readedValue, _ := hcsr04.echoPin.DigitalRead() + // if readedValue == 1 { + // return 0, errors.New("already receiving echo") + // } + select { + case t := <-startChan: + startTime = t + case <-time.After(measurementCycle * time.Millisecond): + startQuit = true + return 0, fmt.Errorf("echo not received after %d milliseconds", measurementCycle) + } + go getPinStateChangeTime(hcsr04.echoPin, 0, stopChan, &stopQuit) + select { + case t := <-stopChan: + stopTime = t + case <-time.After(measurementCycle * time.Millisecond): + stopQuit = true + return 0, fmt.Errorf("echo received for more than %d milliseconds", measurementCycle) + } + return stopTime - startTime, nil +} + +func getPinStateChangeTime(pin *gpio.DirectPinDriver, state int, outChan chan int64, quit *bool) { + + for { + readedValue, _ := pin.DigitalRead() + // stop the loop if the state is different or a quit is done + if readedValue != state && !*quit { + break + } + } + time.Sleep(100) + readedValue, err := pin.DigitalRead() + if err != nil { + fmt.Println(err) + } + if readedValue == state && !*quit { + outChan <- time.Now().UnixNano() + } +} + +func pulseToDistance(pulseDuration int64) float32 { + return float32(pulseDuration) / 1000000000.0 * soundSpeed / 2 +} + +// // GetDistance returns the last distance measured +// // Contrary to MeasureDistance, GetDistance does not trigger a distance measurement +func (hcsr04 *HCSR04) GetDistance() float32 { + return hcsr04.Measure +} + +// // StartDistanceMonitor starts a process which will keep Measure updated +func (hcsr04 *HCSR04) StartDistanceMonitor() error { + hcsr04.distanceMonitorControl = make(chan int) + if hcsr04.distanceMonitorStarted { + return errors.New("monitor already started") + } + go hcsr04.distanceMonitor() + return nil +} + +// // StopDistanceMonitor stop the monitor process +func (hcsr04 *HCSR04) StopDistanceMonitor() { + if hcsr04.distanceMonitorStarted { + hcsr04.distanceMonitorControl <- 1 + } +} + +func (hcsr04 *HCSR04) distanceMonitor() { + for { + select { + case <-hcsr04.distanceMonitorControl: + hcsr04.distanceMonitorStarted = false + return + default: + if _, err := hcsr04.MeasureDistance(); err != nil { + fmt.Println("error: impossible to measure distance", err) + } + } + time.Sleep(MonitorUpdate) + } +} + +func (h *HCSR04) Name() string { return h.name } + +func (h *HCSR04) SetName(n string) { h.name = n } + +func (h *HCSR04) Start() (err error) { return } + +func (h *HCSR04) Halt() (err error) { return } + +func (h *HCSR04) Connection() gobot.Connection { + return h.connection.(gobot.Connection) +} From a09f56c34857bc6f6937ab52f01f3434028a42c8 Mon Sep 17 00:00:00 2001 From: Juan Ignacio Date: Sat, 29 Oct 2022 18:58:19 +0200 Subject: [PATCH 3/5] Update hcsr04_driver.go Start working. I thought it needs to be more stable. Good reads. --- drivers/gpio/hcsr04_driver.go | 33 +++++++-------------------------- 1 file changed, 7 insertions(+), 26 deletions(-) diff --git a/drivers/gpio/hcsr04_driver.go b/drivers/gpio/hcsr04_driver.go index 529dbf5e6..1b2ae096e 100644 --- a/drivers/gpio/hcsr04_driver.go +++ b/drivers/gpio/hcsr04_driver.go @@ -1,4 +1,4 @@ -package hcsr04 +package gpio import ( "errors" @@ -20,12 +20,10 @@ const ( // HCSR04 instance type HCSR04 struct { - name string - connection gobot.Adaptor - triggerPin *gpio.DirectPinDriver - echoPin *gpio.DirectPinDriver - // triggerPin rpio.Pin - // echoPin rpio.Pin + name string + connection gobot.Adaptor + triggerPin *gpio.DirectPinDriver + echoPin *gpio.DirectPinDriver mux sync.Mutex Measure float32 // The last measure distanceMonitorControl chan int @@ -35,26 +33,13 @@ type HCSR04 struct { // NewHCSR04 creates a new HCSR04 instance func NewHCSR04(a gobot.Adaptor, triggerPinID string, echoPinID string) *HCSR04 { - hcsr04 := &HCSR04{ + return &HCSR04{ name: gobot.DefaultName("HCSR04"), triggerPin: gpio.NewDirectPinDriver(a, triggerPinID), echoPin: gpio.NewDirectPinDriver(a, echoPinID), connection: a, Commander: gobot.NewCommander(), } - - // hcsr04 := HCSR04{ - // triggerPinID: triggerPinID, - // echoPinID: echoPinID, - // } - // hcsr04.triggerPin = rpio.Pin(hcsr04.triggerPinID) - // hcsr04.triggerPin.Mode(rpio.Output) - - // hcsr04.echoPin = rpio.Pin(hcsr04.echoPinID) - // hcsr04.echoPin.Mode(rpio.Input) - // hcsr04.echoPin.PullDown() - // hcsr04.triggerPin.Low() - return hcsr04 } // MeasureDistance measure the distance in front of sensor in meters @@ -89,10 +74,6 @@ func (hcsr04 *HCSR04) measurePulse() (int64, error) { var stopTime int64 go getPinStateChangeTime(hcsr04.echoPin, 1, startChan, &startQuit) hcsr04.emitTrigger() - // readedValue, _ := hcsr04.echoPin.DigitalRead() - // if readedValue == 1 { - // return 0, errors.New("already receiving echo") - // } select { case t := <-startChan: startTime = t @@ -116,7 +97,7 @@ func getPinStateChangeTime(pin *gpio.DirectPinDriver, state int, outChan chan in for { readedValue, _ := pin.DigitalRead() // stop the loop if the state is different or a quit is done - if readedValue != state && !*quit { + if readedValue == state || *quit { break } } From 9b9be1c0917d31c5681c126ce62202264fcced1e Mon Sep 17 00:00:00 2001 From: Juan Ignacio Date: Sat, 5 Nov 2022 03:07:22 +0100 Subject: [PATCH 4/5] Update hcsr04_driver.go Move private functions to end of file --- drivers/gpio/hcsr04_driver.go | 100 +++++++++++++++++----------------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/drivers/gpio/hcsr04_driver.go b/drivers/gpio/hcsr04_driver.go index 1b2ae096e..e2770b416 100644 --- a/drivers/gpio/hcsr04_driver.go +++ b/drivers/gpio/hcsr04_driver.go @@ -59,6 +59,56 @@ func (hcsr04 *HCSR04) MeasureDistance() (float32, error) { return hcsr04.Measure, nil } +// GetDistance returns the last distance measured +// Contrary to MeasureDistance, GetDistance does not trigger a distance measurement +func (hcsr04 *HCSR04) GetDistance() float32 { + return hcsr04.Measure +} + +// StartDistanceMonitor starts a process which will keep Measure updated +func (hcsr04 *HCSR04) StartDistanceMonitor() error { + hcsr04.distanceMonitorControl = make(chan int) + if hcsr04.distanceMonitorStarted { + return errors.New("monitor already started") + } + go hcsr04.distanceMonitor() + return nil +} + +// StopDistanceMonitor stop the monitor process +func (hcsr04 *HCSR04) StopDistanceMonitor() { + if hcsr04.distanceMonitorStarted { + hcsr04.distanceMonitorControl <- 1 + } +} + +func (h *HCSR04) Name() string { return h.name } + +func (h *HCSR04) SetName(n string) { h.name = n } + +func (h *HCSR04) Start() (err error) { return } + +func (h *HCSR04) Halt() (err error) { return } + +func (h *HCSR04) Connection() gobot.Connection { + return h.connection.(gobot.Connection) +} + +func (hcsr04 *HCSR04) distanceMonitor() { + for { + select { + case <-hcsr04.distanceMonitorControl: + hcsr04.distanceMonitorStarted = false + return + default: + if _, err := hcsr04.MeasureDistance(); err != nil { + fmt.Println("error: impossible to measure distance", err) + } + } + time.Sleep(MonitorUpdate) + } +} + func (hcsr04 *HCSR04) emitTrigger() { hcsr04.triggerPin.On() time.Sleep(10 * time.Microsecond) @@ -114,53 +164,3 @@ func getPinStateChangeTime(pin *gpio.DirectPinDriver, state int, outChan chan in func pulseToDistance(pulseDuration int64) float32 { return float32(pulseDuration) / 1000000000.0 * soundSpeed / 2 } - -// // GetDistance returns the last distance measured -// // Contrary to MeasureDistance, GetDistance does not trigger a distance measurement -func (hcsr04 *HCSR04) GetDistance() float32 { - return hcsr04.Measure -} - -// // StartDistanceMonitor starts a process which will keep Measure updated -func (hcsr04 *HCSR04) StartDistanceMonitor() error { - hcsr04.distanceMonitorControl = make(chan int) - if hcsr04.distanceMonitorStarted { - return errors.New("monitor already started") - } - go hcsr04.distanceMonitor() - return nil -} - -// // StopDistanceMonitor stop the monitor process -func (hcsr04 *HCSR04) StopDistanceMonitor() { - if hcsr04.distanceMonitorStarted { - hcsr04.distanceMonitorControl <- 1 - } -} - -func (hcsr04 *HCSR04) distanceMonitor() { - for { - select { - case <-hcsr04.distanceMonitorControl: - hcsr04.distanceMonitorStarted = false - return - default: - if _, err := hcsr04.MeasureDistance(); err != nil { - fmt.Println("error: impossible to measure distance", err) - } - } - time.Sleep(MonitorUpdate) - } -} - -func (h *HCSR04) Name() string { return h.name } - -func (h *HCSR04) SetName(n string) { h.name = n } - -func (h *HCSR04) Start() (err error) { return } - -func (h *HCSR04) Halt() (err error) { return } - -func (h *HCSR04) Connection() gobot.Connection { - return h.connection.(gobot.Connection) -} From 6c2f2cb108e9d72c896b27446b417c5489c9db5d Mon Sep 17 00:00:00 2001 From: Juan Ignacio Date: Fri, 18 Nov 2022 19:45:34 +0100 Subject: [PATCH 5/5] Start writing example code. Write some example test fot HC-SR04 dirver. --- drivers/gpio/hcsr04_driver.go | 1 - examples/raspi_hcsr04.go | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 examples/raspi_hcsr04.go diff --git a/drivers/gpio/hcsr04_driver.go b/drivers/gpio/hcsr04_driver.go index e2770b416..f8175d3f4 100644 --- a/drivers/gpio/hcsr04_driver.go +++ b/drivers/gpio/hcsr04_driver.go @@ -7,7 +7,6 @@ import ( "time" "gobot.io/x/gobot" - "gobot.io/x/gobot/drivers/gpio" ) const ( diff --git a/examples/raspi_hcsr04.go b/examples/raspi_hcsr04.go new file mode 100644 index 000000000..3f232a39c --- /dev/null +++ b/examples/raspi_hcsr04.go @@ -0,0 +1,38 @@ +//go:build example +// +build example + +// +// Do not build by default. + +package main + +import ( + "fmt" + "time" + + "gobot.io/x/gobot" + "gobot.io/x/gobot/drivers/gpio/hcsr04" + "gobot.io/x/gobot/platforms/raspi" +) + +func main() { + r := raspi.NewAdaptor() + hcsr04 := hcsr04.NewHCSR04(r, "11", "13") + + work := func() { + hsrc04.StartDistanceMonitor() + + gobot.Every(1*time.Second, func() { + fmt.Println(hsrc04.GetDistance()) + time.Sleep(hcsr04.MonitorUpdate) + }) + } + + robot := gobot.NewRobot("distanceBot", + []gobot.Connection{r}, + []gobot.Device{hsrc04}, + work, + ) + + robot.Start() +}