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