2014-04-26 18:11:51 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-07-11 08:21:21 +08:00
|
|
|
"time"
|
|
|
|
|
2014-04-26 18:11:51 +08:00
|
|
|
"github.com/hybridgroup/gobot"
|
2014-05-23 12:20:16 +08:00
|
|
|
"github.com/hybridgroup/gobot/platforms/sphero"
|
2014-04-26 18:11:51 +08:00
|
|
|
)
|
|
|
|
|
2016-10-19 01:32:45 +08:00
|
|
|
func NewSwarmBot(port string) *gobot.Robot {
|
|
|
|
spheroAdaptor := sphero.NewAdaptor(port)
|
|
|
|
spheroDriver := sphero.NewSpheroDriver(spheroAdaptor)
|
|
|
|
spheroDriver.SetName("Sphero" + port)
|
|
|
|
|
|
|
|
work := func() {
|
|
|
|
spheroDriver.Stop()
|
|
|
|
|
|
|
|
spheroDriver.On(sphero.Collision, func(data interface{}) {
|
|
|
|
fmt.Println("Collision Detected!")
|
|
|
|
})
|
|
|
|
|
|
|
|
gobot.Every(1*time.Second, func() {
|
|
|
|
spheroDriver.Roll(100, uint16(gobot.Rand(360)))
|
|
|
|
})
|
|
|
|
gobot.Every(3*time.Second, func() {
|
|
|
|
spheroDriver.SetRGB(uint8(gobot.Rand(255)),
|
|
|
|
uint8(gobot.Rand(255)),
|
|
|
|
uint8(gobot.Rand(255)),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("sphero",
|
|
|
|
[]gobot.Connection{spheroAdaptor},
|
|
|
|
[]gobot.Device{spheroDriver},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
|
|
|
return robot
|
|
|
|
}
|
|
|
|
|
2014-04-26 18:11:51 +08:00
|
|
|
func main() {
|
2016-10-19 01:32:45 +08:00
|
|
|
master := gobot.NewMaster()
|
2014-04-26 18:11:51 +08:00
|
|
|
|
|
|
|
spheros := []string{
|
|
|
|
"/dev/rfcomm0",
|
|
|
|
"/dev/rfcomm1",
|
|
|
|
"/dev/rfcomm2",
|
|
|
|
"/dev/rfcomm3",
|
|
|
|
}
|
|
|
|
|
2014-07-09 09:36:14 +08:00
|
|
|
for _, port := range spheros {
|
2016-10-19 01:32:45 +08:00
|
|
|
master.AddRobot(NewSwarmBot(port))
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2016-10-19 01:32:45 +08:00
|
|
|
master.Start()
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|