examples: better example of using Master Gobot
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
a0b3a7934c
commit
2811295e3f
|
@ -8,8 +8,40 @@ import (
|
|||
"github.com/hybridgroup/gobot/platforms/sphero"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func main() {
|
||||
gbot := gobot.NewMaster()
|
||||
master := gobot.NewMaster()
|
||||
|
||||
spheros := []string{
|
||||
"/dev/rfcomm0",
|
||||
|
@ -19,35 +51,8 @@ func main() {
|
|||
}
|
||||
|
||||
for _, port := range spheros {
|
||||
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,
|
||||
)
|
||||
gbot.AddRobot(robot)
|
||||
master.AddRobot(NewSwarmBot(port))
|
||||
}
|
||||
|
||||
gbot.Start()
|
||||
master.Start()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue