From f4444fb60de726072c2daf83b56b5ccf3608815c Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 24 Dec 2017 14:29:48 +0100 Subject: incrase stop distance --- fsm.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'fsm.go') diff --git a/fsm.go b/fsm.go index 768e566..a0fe3b9 100644 --- a/fsm.go +++ b/fsm.go @@ -3,6 +3,7 @@ package elegoo import ( "io" "log" + "time" ) type stateFn func() stateFn @@ -10,6 +11,8 @@ type stateFn func() stateFn type FSM struct { events chan *Event commands chan *Command + Look int32 + Speed *Speed } func NewFSM(rw io.ReadWriter) *FSM { @@ -52,25 +55,39 @@ func (f *FSM) Start() { } func (f *FSM) initalState() stateFn { - f.commands <- &Command{Look: 0} + f.commands <- &Command{} return f.readDistance } func (f *FSM) readDistance() stateFn { ev := <-f.events log.Println(ev) - if ev.Head != nil && ev.Head.Distance < 20 { + if ev.Head != nil && ev.Head.Distance < 50 { return f.stop } return f.moveAhead } func (f *FSM) moveAhead() stateFn { - f.commands <- &Command{Speed: &Speed{L: 200, R: 200}} + f.SetSpeed(200, 200, 0) return f.readDistance } func (f *FSM) stop() stateFn { - f.commands <- &Command{} + f.SetSpeed(0, 0, 0) return f.readDistance } + +func (f *FSM) SetSpeed(l, r int32, stop time.Duration) { + f.Speed = &Speed{ + L: l, + R: r, + StopAfter: uint32(stop.Nanoseconds() / 1e6), + } + f.commands <- &Command{Speed: f.Speed, Look: f.Look} +} + +func (f *FSM) SetDirection(dir int32) { + f.Look = dir + f.commands <- &Command{Speed: f.Speed, Look: f.Look} +} -- cgit v1.2.3