Skip to content

Commit

Permalink
add onstart callback, throw error for set functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tinogoehlert committed Jan 27, 2019
1 parent 48807b4 commit 299e1a6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
9 changes: 7 additions & 2 deletions cmd/gobot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ func main() {
a := gk.NewAdaptorTCP("127.0.0.1:3333")
kb := gk.NewDriver(a)

kb.OnStart(func() {
kb.SetGyroTolerance(0)
})

work := func() {
kb.OnWheelsCurrent(func(w *ks.CurrentWheels) {
log.Printf("%d:%d", w.LeftMotor, w.RightMotor)

kb.OnGyro(func(g *ks.GyroData) {
log.Printf("%f : %f : %f", g.X, g.Y, g.Z)
})

gobot.Every(1*time.Minute, func() {
Expand Down
27 changes: 23 additions & 4 deletions gobot/driver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kobuki

import (
"errors"
"fmt"

"github.com/tinogoehlert/gobuki/commands"
Expand Down Expand Up @@ -52,6 +53,7 @@ const (
type Driver struct {
name string
adaptor *Adaptor
started func()
gobot.Eventer
}

Expand Down Expand Up @@ -93,7 +95,7 @@ func (d *Driver) SetName(s string) {
// Start initiates the Driver
func (d *Driver) Start() error {
d.adaptor.bot.Start()

d.started()
d.adaptor.bot.OnAll(func(name string, data interface{}) {
d.Publish(name, data)
})
Expand All @@ -108,18 +110,30 @@ func (d *Driver) Halt() error {
}

// SetCliffADCTolerance set tolerance for Cliff ADC
func (d *Driver) SetCliffADCTolerance(t int) {
func (d *Driver) SetCliffADCTolerance(t int) error {
if d.adaptor.bot == nil {
return errors.New("driver not started")
}
d.adaptor.bot.SetCliffADCTolerance(t)
return nil
}

// SetGyroTolerance set tolerance for gyroscope
func (d *Driver) SetGyroTolerance(t float64) {
func (d *Driver) SetGyroTolerance(t float64) error {
if d.adaptor.bot == nil {
return errors.New("driver not started")
}
d.adaptor.bot.SetGyroTolerance(t)
return nil
}

// SetCurrentWheelsTolerance set tolerance for wheels current
func (d *Driver) SetCurrentWheelsTolerance(t int) {
func (d *Driver) SetCurrentWheelsTolerance(t int) error {
if d.adaptor.bot == nil {
return errors.New("driver not started")
}
d.adaptor.bot.SetCurrentWheelsTolerance(t)
return nil
}

// Move moves the robot
Expand All @@ -137,6 +151,11 @@ func (d *Driver) Connection() gobot.Connection {
return d.adaptor
}

// OnStart triggered when driver was started, useful for setup
func (d *Driver) OnStart(f func()) {
d.started = f
}

// OnGyro new GyroData available
func (d *Driver) OnGyro(f func(*sensors.GyroData)) {
d.adaptor.bot.On(GyroEvent, func(data interface{}) {
Expand Down

0 comments on commit 299e1a6

Please sign in to comment.