/
sphero.go
38 lines (31 loc) · 884 Bytes
/
sphero.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/i2c"
"github.com/hybridgroup/gobot/platforms/intel-iot/edison"
"github.com/hybridgroup/gobot/platforms/sphero"
"math"
"time"
)
func main() {
gbot := gobot.NewGobot()
adaptor := sphero.NewSpheroAdaptor("Sphero", "/dev/rfcomm0")
board := edison.NewEdisonAdaptor("edison")
spheroDriver := sphero.NewSpheroDriver(adaptor, "sphero")
sensorAccel := i2c.NewGroveAccelerometerDriver(board, "accel")
work := func() {
gobot.Every(time.Millisecond*20, func() {
if x, y, z, err := sensorAccel.XYZ(); err == nil {
degree := math.Sin(y/x) * 360
spheroDriver.Roll(uint8(100+z), uint16(degree))
}
})
}
robot := gobot.NewRobot("sphero",
[]gobot.Connection{adaptor, board},
[]gobot.Device{sensorAccel, spheroDriver},
work,
)
gbot.AddRobot(robot)
gbot.Start()
}