Master Gobot

Use Master Gobot when you want to provide an API to your robot, drone, or Internet connected thing.

Also, use Master Gobot when you want to create a swarm of devices, and control them as a single unit.

Let's take the "Blink" code example from the "Getting Started" page, and add the Gobot API:

package main

import (
        "time"

        "gobot.io/x/gobot"
        "gobot.io/x/gobot/api"
        "gobot.io/x/gobot/drivers/gpio"
        "gobot.io/x/gobot/platforms/firmata"
)

func main() {
        master := gobot.NewMaster()
        a := api.NewAPI(master)
        a.Start()

        firmataAdaptor := firmata.NewAdaptor("/dev/ttyACM0")
        led := gpio.NewLedDriver(firmataAdaptor, "13")

        work := func() {
                gobot.Every(1*time.Second, func() {
                        led.Toggle()
                })
        }

        robot := gobot.NewRobot("bot",
                []gobot.Connection{firmataAdaptor},
                []gobot.Device{led},
                work,
        )

        master.AddRobot(robot)

        master.Start()
}

Just by adding a few lines of code, we get the entire RESTful API on top of your Gobot Master.

Another way to use Master Gobot is to control a swarm or group of Robots. Here is an example that controls a sawrn of Sphero robots:

Swarm of Spheros

package main

import (
        "fmt"
        "time"

        "gobot.io/x/gobot"
        "gobot.io/x/gobot/api"
        "gobot.io/x/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() {
        master := gobot.NewMaster()
        api.NewAPI(master).Start()

        spheros := []string{
                "/dev/rfcomm0",
                "/dev/rfcomm1",
                "/dev/rfcomm2",
                "/dev/rfcomm3",
        }

        for _, port := range spheros {
                master.AddRobot(NewSwarmBot(port))
        }

        master.Start()
}

How to use Master Gobot

The "Master" is what you use to attach the Gobot API, and also to control the various Robots under the Master's "control program".

Create a new master by calling the gobot.NewMaster() function.

Define your Robot or Robots the same way as you do when working with Classic Gobot. You can create a new Robot using the gobot.NewRobot() function.

Pass in the slice of one or more Connection structs, a slice of one or more Device structs, and a function with the Work that the Robot is expected to do.

Once your Robot is created, add it to the Master using the AddRobot() function. You then call the Master's Start() function which will Start() each of the individual robots under its control.