hybridgroup.gobot/examples/sphero_multiple.go

52 lines
1005 B
Go
Raw Normal View History

package main
import (
"fmt"
"github.com/hybridgroup/gobot"
2014-05-23 12:20:16 +08:00
"github.com/hybridgroup/gobot/platforms/sphero"
"time"
)
func main() {
2014-07-09 09:36:14 +08:00
gbot := gobot.NewGobot()
spheros := []string{
"/dev/rfcomm0",
"/dev/rfcomm1",
"/dev/rfcomm2",
"/dev/rfcomm3",
}
2014-07-09 09:36:14 +08:00
for _, port := range spheros {
spheroAdaptor := sphero.NewSpheroAdaptor("Sphero", port)
spheroDriver := sphero.NewSpheroDriver(spheroAdaptor, "Sphero"+port)
work := func() {
2014-04-29 02:40:20 +08:00
spheroDriver.Stop()
2014-07-09 09:36:14 +08:00
gobot.On(spheroDriver.Event("collision"), func(data interface{}) {
fmt.Println("Collision Detected!")
})
2014-05-23 12:20:16 +08:00
gobot.Every(1*time.Second, func() {
2014-04-29 02:40:20 +08:00
spheroDriver.Roll(100, uint16(gobot.Rand(360)))
})
2014-05-23 12:20:16 +08:00
gobot.Every(3*time.Second, func() {
2014-07-09 09:36:14 +08:00
spheroDriver.SetRGB(uint8(gobot.Rand(255)),
uint8(gobot.Rand(255)),
uint8(gobot.Rand(255)),
)
})
}
2014-07-09 09:36:14 +08:00
robot := gobot.NewRobot("sphero",
[]gobot.Connection{spheroAdaptor},
[]gobot.Device{spheroDriver},
work,
)
gbot.AddRobot(robot)
}
2014-07-09 09:36:14 +08:00
gbot.Start()
}