2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2017-03-13 23:01:39 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2017-03-13 23:01:39 +08:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2014-04-26 18:11:51 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-07-11 08:21:21 +08:00
|
|
|
"time"
|
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/api"
|
2024-02-12 23:27:08 +08:00
|
|
|
"gobot.io/x/gobot/v2/drivers/common/spherocommon"
|
2024-02-05 01:50:43 +08:00
|
|
|
"gobot.io/x/gobot/v2/drivers/serial"
|
2024-02-12 23:27:08 +08:00
|
|
|
"gobot.io/x/gobot/v2/drivers/serial/sphero"
|
2024-02-05 01:50:43 +08:00
|
|
|
"gobot.io/x/gobot/v2/platforms/serialport"
|
2014-04-26 18:11:51 +08:00
|
|
|
)
|
|
|
|
|
2016-10-19 01:32:45 +08:00
|
|
|
func NewSwarmBot(port string) *gobot.Robot {
|
2024-02-05 01:50:43 +08:00
|
|
|
spheroAdaptor := serialport.NewAdaptor(port)
|
2024-02-12 23:27:08 +08:00
|
|
|
spheroDriver := sphero.NewSpheroDriver(spheroAdaptor, serial.WithName("Sphero"+port))
|
2016-10-19 01:32:45 +08:00
|
|
|
|
|
|
|
work := func() {
|
|
|
|
spheroDriver.Stop()
|
|
|
|
|
2024-02-12 23:27:08 +08:00
|
|
|
_ = spheroDriver.On(spherocommon.CollisionEvent, func(data interface{}) {
|
2016-10-19 01:32:45 +08:00
|
|
|
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() {
|
2024-02-13 23:30:25 +08:00
|
|
|
manager := gobot.NewManager()
|
|
|
|
api.NewAPI(manager).Start()
|
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 {
|
2024-02-13 23:30:25 +08:00
|
|
|
manager.AddRobot(NewSwarmBot(port))
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2024-02-13 23:30:25 +08:00
|
|
|
if err := manager.Start(); err != nil {
|
2024-02-11 22:34:50 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|