hcsr04

package module
v0.0.0-...-5458d00 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 18, 2020 License: MIT Imports: 6 Imported by: 0

README

HC-SR04

Go Reference golangci-lint Go Report Card

hcsr04 is a driver allowing to usea HC-SR04 ultrasound distance sensor

Documentation

For full documentation, please visit Go Reference

Quick start

package main

import (
	"fmt"
	"os"
	"time"

	"github.com/raspberrypi-go-drivers/hcsr04"
	"github.com/stianeikeland/go-rpio/v4"
)

func main() {
	err := rpio.Open()
	if err != nil {
		os.Exit(1)
	}
	defer rpio.Close()
	h := hcsr04.NewHCSR04(17, 27)
	h.StartDistanceMonitor()
	for {
		fmt.Println(h.GetDistance())
		time.Sleep(hcsr04.MonitorUpdate)
	}
}

Raspberry Pi compatibility

This driver has has only been tested on an Raspberry Pi Zero WH using integrated bluetooth but may work well on other Raspberry Pi having integrated Bluetooth

License

MIT License


Special thanks to @stianeikeland

This driver is based on his work in stianeikeland/go-rpio

Documentation

Overview

Package hcsr04 is a driver allowing to use a HC-SR04 ultrasound distance sensor

Example
package main

import (
	"fmt"
	"os"

	"github.com/raspberrypi-go-drivers/hcsr04"
	"github.com/stianeikeland/go-rpio/v4"
)

func main() {
	err := rpio.Open()
	if err != nil {
		os.Exit(1)
	}
	defer rpio.Close()
	h := hcsr04.NewHCSR04(17, 27)
	fmt.Println(h.MeasureDistance())
}
Example (Monitor)
package main

import (
	"fmt"
	"os"
	"time"

	"github.com/raspberrypi-go-drivers/hcsr04"
	log "github.com/sirupsen/logrus"
	"github.com/stianeikeland/go-rpio/v4"
)

func main() {
	err := rpio.Open()
	if err != nil {
		os.Exit(1)
	}
	defer rpio.Close()
	h := hcsr04.NewHCSR04(17, 27)
	if err := h.StartDistanceMonitor(); err != nil {
		log.WithField("error", err).Error("impossible to start distance monitor")
		os.Exit(1)
	} else {
		defer h.StopDistanceMonitor()
	}
	for {
		fmt.Println(h.GetDistance())
		time.Sleep(hcsr04.MonitorUpdate)
	}
}

Index

Examples

Constants

View Source
const (

	// MonitorUpdate is the time between each monitor update
	MonitorUpdate time.Duration = 100 * time.Millisecond
)

Variables

This section is empty.

Functions

This section is empty.

Types

type HCSR04

type HCSR04 struct {
	Measure float32 // The last measure
	// contains filtered or unexported fields
}

HCSR04 instance

func NewHCSR04

func NewHCSR04(triggerPinID uint8, echoPinID uint8) *HCSR04

NewHCSR04 creates a new HCSR04 instance

Example
package main

import (
	"fmt"
	"os"

	"github.com/raspberrypi-go-drivers/hcsr04"
	"github.com/stianeikeland/go-rpio/v4"
)

func main() {
	err := rpio.Open()
	if err != nil {
		os.Exit(1)
	}
	defer rpio.Close()
	h := hcsr04.NewHCSR04(17, 27)
	fmt.Println(h.MeasureDistance())
}

func (*HCSR04) GetDistance

func (hcsr04 *HCSR04) GetDistance() float32

GetDistance returns the last distance measured Contrary to MeasureDistance, GetDistance does not trigger a distance measurement

Example
package main

import (
	"fmt"
	"os"
	"time"

	"github.com/raspberrypi-go-drivers/hcsr04"
	log "github.com/sirupsen/logrus"
	"github.com/stianeikeland/go-rpio/v4"
)

func main() {
	err := rpio.Open()
	if err != nil {
		os.Exit(1)
	}
	defer rpio.Close()
	h := hcsr04.NewHCSR04(17, 27)
	if err := h.StartDistanceMonitor(); err != nil {
		log.WithField("error", err).Error("impossible to start distance monitor")
		os.Exit(1)
	} else {
		defer h.StopDistanceMonitor()
	}
	for {
		fmt.Println(h.GetDistance())
		time.Sleep(hcsr04.MonitorUpdate)
	}
}

func (*HCSR04) MeasureDistance

func (hcsr04 *HCSR04) MeasureDistance() (float32, error)

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

Example
package main

import (
	"fmt"
	"os"

	"github.com/raspberrypi-go-drivers/hcsr04"
	"github.com/stianeikeland/go-rpio/v4"
)

func main() {
	err := rpio.Open()
	if err != nil {
		os.Exit(1)
	}
	defer rpio.Close()
	h := hcsr04.NewHCSR04(17, 27)
	fmt.Println(h.MeasureDistance())
}

func (*HCSR04) StartDistanceMonitor

func (hcsr04 *HCSR04) StartDistanceMonitor() error

StartDistanceMonitor starts a process which will keep Measure updated

func (*HCSR04) StopDistanceMonitor

func (hcsr04 *HCSR04) StopDistanceMonitor()

StopDistanceMonitor stop the monitor process

Example
package main

import (
	"fmt"
	"os"
	"time"

	"github.com/raspberrypi-go-drivers/hcsr04"
	log "github.com/sirupsen/logrus"
	"github.com/stianeikeland/go-rpio/v4"
)

func main() {
	err := rpio.Open()
	if err != nil {
		os.Exit(1)
	}
	defer rpio.Close()
	h := hcsr04.NewHCSR04(17, 27)
	if err := h.StartDistanceMonitor(); err != nil {
		log.WithField("error", err).Error("impossible to start distance monitor")
		os.Exit(1)
	} else {
		defer h.StopDistanceMonitor()
	}
	for {
		fmt.Println(h.GetDistance())
		time.Sleep(hcsr04.MonitorUpdate)
	}
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL