gobot.io/x/gobot@v1.16.0/examples/sphero_multiple.go (about)

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