gobot.io/x/gobot/v2@v2.1.0/examples/sphero_multiple.go (about) 1 //go:build example 2 // +build example 3 4 // 5 // Do not build by default. 6 7 package main 8 9 import ( 10 "fmt" 11 "time" 12 13 "gobot.io/x/gobot/v2" 14 "gobot.io/x/gobot/v2/api" 15 "gobot.io/x/gobot/v2/platforms/sphero" 16 ) 17 18 func NewSwarmBot(port string) *gobot.Robot { 19 spheroAdaptor := sphero.NewAdaptor(port) 20 spheroDriver := sphero.NewSpheroDriver(spheroAdaptor) 21 spheroDriver.SetName("Sphero" + port) 22 23 work := func() { 24 spheroDriver.Stop() 25 26 spheroDriver.On(sphero.Collision, func(data interface{}) { 27 fmt.Println("Collision Detected!") 28 }) 29 30 gobot.Every(1*time.Second, func() { 31 spheroDriver.Roll(100, uint16(gobot.Rand(360))) 32 }) 33 gobot.Every(3*time.Second, func() { 34 spheroDriver.SetRGB(uint8(gobot.Rand(255)), 35 uint8(gobot.Rand(255)), 36 uint8(gobot.Rand(255)), 37 ) 38 }) 39 } 40 41 robot := gobot.NewRobot("sphero", 42 []gobot.Connection{spheroAdaptor}, 43 []gobot.Device{spheroDriver}, 44 work, 45 ) 46 47 return robot 48 } 49 50 func main() { 51 master := gobot.NewMaster() 52 api.NewAPI(master).Start() 53 54 spheros := []string{ 55 "/dev/rfcomm0", 56 "/dev/rfcomm1", 57 "/dev/rfcomm2", 58 "/dev/rfcomm3", 59 } 60 61 for _, port := range spheros { 62 master.AddRobot(NewSwarmBot(port)) 63 } 64 65 master.Start() 66 }