Initial commit
This commit is contained in:
5
go.mod
Normal file
5
go.mod
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
module github.com/firestuff/hh
|
||||||
|
|
||||||
|
go 1.18
|
||||||
|
|
||||||
|
require github.com/stianeikeland/go-rpio/v4 v4.6.0
|
||||||
2
go.sum
Normal file
2
go.sum
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
github.com/stianeikeland/go-rpio/v4 v4.6.0 h1:eAJgtw3jTtvn/CqwbC82ntcS+dtzUTgo5qlZKe677EY=
|
||||||
|
github.com/stianeikeland/go-rpio/v4 v4.6.0/go.mod h1:A3GvHxC1Om5zaId+HqB3HKqx4K/AqeckxB7qRjxMK7o=
|
||||||
21
hh.go
Normal file
21
hh.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
import "github.com/stianeikeland/go-rpio/v4"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
err := rpio.Open()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer rpio.Close()
|
||||||
|
|
||||||
|
us := NewUltrasonic(context.TODO(), 6, 5)
|
||||||
|
mf := NewMedianFilter(us.C, 9)
|
||||||
|
|
||||||
|
for dist := range mf {
|
||||||
|
fmt.Printf("%f\n", dist)
|
||||||
|
}
|
||||||
|
}
|
||||||
36
medianfilter.go
Normal file
36
medianfilter.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "sort"
|
||||||
|
|
||||||
|
func NewMedianFilter(in chan float64, num int) chan float64 {
|
||||||
|
out := make(chan float64)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer close(out)
|
||||||
|
|
||||||
|
buf := make([]float64, num, num)
|
||||||
|
srt := make([]float64, num, num)
|
||||||
|
next := 0
|
||||||
|
|
||||||
|
for {
|
||||||
|
v, ok := <-in
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
buf[next%num] = v
|
||||||
|
next++
|
||||||
|
|
||||||
|
if next < num {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
copy(srt, buf)
|
||||||
|
sort.Float64s(srt)
|
||||||
|
|
||||||
|
out <- srt[num/2+1]
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return out
|
||||||
|
}
|
||||||
25
relay.go
Normal file
25
relay.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "github.com/stianeikeland/go-rpio/v4"
|
||||||
|
|
||||||
|
type Relay struct {
|
||||||
|
pin rpio.Pin
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRelay(pin int) *Relay {
|
||||||
|
r := &Relay{
|
||||||
|
pin: rpio.Pin(pin),
|
||||||
|
}
|
||||||
|
|
||||||
|
r.pin.Output()
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Relay) On() {
|
||||||
|
r.pin.High()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Relay) Off() {
|
||||||
|
r.pin.Low()
|
||||||
|
}
|
||||||
87
ultrasonic.go
Normal file
87
ultrasonic.go
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
import "errors"
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
import "github.com/stianeikeland/go-rpio/v4"
|
||||||
|
|
||||||
|
type Ultrasonic struct {
|
||||||
|
C chan float64
|
||||||
|
|
||||||
|
trig rpio.Pin
|
||||||
|
echo rpio.Pin
|
||||||
|
}
|
||||||
|
|
||||||
|
var timeout = errors.New("timed out waiting for device")
|
||||||
|
|
||||||
|
func NewUltrasonic(ctx context.Context, trig, echo int) *Ultrasonic {
|
||||||
|
us := &Ultrasonic{
|
||||||
|
C: make(chan float64),
|
||||||
|
trig: rpio.Pin(trig),
|
||||||
|
echo: rpio.Pin(echo),
|
||||||
|
}
|
||||||
|
|
||||||
|
us.trig.Output()
|
||||||
|
us.trig.Low()
|
||||||
|
|
||||||
|
us.echo.Input()
|
||||||
|
|
||||||
|
go us.loop(ctx)
|
||||||
|
|
||||||
|
return us
|
||||||
|
}
|
||||||
|
|
||||||
|
func (us *Ultrasonic) loop(ctx context.Context) {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
|
||||||
|
default:
|
||||||
|
us.measure()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (us *Ultrasonic) measure() {
|
||||||
|
deadline := time.NewTimer(200 * time.Millisecond)
|
||||||
|
|
||||||
|
us.trigger()
|
||||||
|
|
||||||
|
start := us.wait(rpio.High, deadline)
|
||||||
|
end := us.wait(rpio.Low, deadline)
|
||||||
|
|
||||||
|
if start.IsZero() || end.IsZero() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
duration := end.Sub(start)
|
||||||
|
cm := duration.Seconds() * 17150
|
||||||
|
|
||||||
|
if cm > 400 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
us.C <- cm
|
||||||
|
}
|
||||||
|
|
||||||
|
func (us *Ultrasonic) trigger() {
|
||||||
|
us.trig.High()
|
||||||
|
time.Sleep(10 * time.Microsecond)
|
||||||
|
us.trig.Low()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (us *Ultrasonic) wait(goal rpio.State, deadline *time.Timer) time.Time {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-deadline.C:
|
||||||
|
return time.Time{}
|
||||||
|
|
||||||
|
default:
|
||||||
|
if us.echo.Read() == goal {
|
||||||
|
return time.Now()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user