2018-02-14 15:24:39 +08:00
|
|
|
// Copyright 2014-2018 The Hybrid Group. All rights reserved.
|
2014-04-27 00:13:33 +08:00
|
|
|
|
|
|
|
/*
|
2023-11-16 03:51:52 +08:00
|
|
|
Package gobot is the primary entrypoint for Gobot (http://gobot.io), a framework for robotics, physical computing, and
|
|
|
|
the Internet of Things written using the Go programming language .
|
2014-07-19 04:18:01 +08:00
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
It provides a simple, yet powerful way to create solutions that incorporate multiple, different hardware devices at the
|
|
|
|
same time.
|
2014-07-19 04:18:01 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
# Classic Gobot
|
2014-07-19 04:18:01 +08:00
|
|
|
|
2016-12-20 01:10:34 +08:00
|
|
|
Here is a "Classic Gobot" program that blinks an LED using an Arduino:
|
2014-07-19 04:18:01 +08:00
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
package main
|
2014-07-19 04:18:01 +08:00
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
import (
|
|
|
|
"time"
|
2014-07-19 04:18:01 +08:00
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/drivers/gpio"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/firmata"
|
|
|
|
)
|
2014-04-27 00:13:33 +08:00
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
func main() {
|
|
|
|
firmataAdaptor := firmata.NewAdaptor("/dev/ttyACM0")
|
|
|
|
led := gpio.NewLedDriver(firmataAdaptor, "13")
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
work := func() {
|
|
|
|
gobot.Every(1*time.Second, func() {
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := led.Toggle(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2023-10-21 02:50:42 +08:00
|
|
|
})
|
|
|
|
}
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
robot := gobot.NewRobot("bot",
|
|
|
|
[]gobot.Connection{firmataAdaptor},
|
|
|
|
[]gobot.Device{led},
|
|
|
|
work,
|
|
|
|
)
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := robot.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-10-21 02:50:42 +08:00
|
|
|
}
|
2014-07-19 04:18:01 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
# Metal Gobot
|
2014-11-14 03:06:57 +08:00
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
You can also use Metal Gobot and pick and choose from the various Gobot packages to control hardware with nothing but
|
|
|
|
pure idiomatic Golang code. For example:
|
2016-12-20 01:10:34 +08:00
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
package main
|
2016-12-20 01:10:34 +08:00
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
import (
|
|
|
|
"gobot.io/x/gobot/v2/drivers/gpio"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/intel-iot/edison"
|
|
|
|
"time"
|
|
|
|
)
|
2016-12-20 01:10:34 +08:00
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
func main() {
|
|
|
|
e := edison.NewAdaptor()
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := e.Connect(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2016-12-20 01:10:34 +08:00
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
led := gpio.NewLedDriver(e, "13")
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := led.Start(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2016-12-20 01:10:34 +08:00
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
for {
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := led.Toggle(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2023-10-21 02:50:42 +08:00
|
|
|
time.Sleep(1000 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
2016-12-20 01:10:34 +08:00
|
|
|
|
2024-02-13 23:30:25 +08:00
|
|
|
# Manager Gobot
|
2016-12-20 01:10:34 +08:00
|
|
|
|
2024-02-13 23:30:25 +08:00
|
|
|
Finally, you can use Manager Gobot to add the complete Gobot API or control swarms of Robots:
|
2014-07-19 04:18:01 +08:00
|
|
|
|
2024-02-05 01:50:43 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"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"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/serialport"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewSwarmBot(port string) *gobot.Robot {
|
|
|
|
spheroAdaptor := serialport.NewAdaptor(port)
|
2024-02-12 23:27:08 +08:00
|
|
|
spheroDriver := sphero.NewSpheroDriver(spheroAdaptor, serial.WithName("Sphero" + port))
|
2024-02-05 01:50:43 +08:00
|
|
|
|
|
|
|
work := func() {
|
|
|
|
spheroDriver.Stop()
|
|
|
|
|
2024-02-11 22:34:50 +08:00
|
|
|
_ = spheroDriver.On(sphero.CollisionEvent, func(data interface{}) {
|
2024-02-05 01:50:43 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2024-02-13 23:30:25 +08:00
|
|
|
manager := gobot.NewManager()
|
|
|
|
api.NewAPI(manager).Start()
|
2024-02-05 01:50:43 +08:00
|
|
|
|
|
|
|
spheros := []string{
|
|
|
|
"/dev/rfcomm0",
|
|
|
|
"/dev/rfcomm1",
|
|
|
|
"/dev/rfcomm2",
|
|
|
|
"/dev/rfcomm3",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, port := range spheros {
|
2024-02-13 23:30:25 +08:00
|
|
|
manager.AddRobot(NewSwarmBot(port))
|
2024-02-05 01:50:43 +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)
|
|
|
|
}
|
2024-02-05 01:50:43 +08:00
|
|
|
}
|
2014-11-14 03:06:57 +08:00
|
|
|
|
2018-02-14 15:24:39 +08:00
|
|
|
Copyright (c) 2013-2018 The Hybrid Group. Licensed under the Apache 2.0 license.
|
2014-04-27 00:13:33 +08:00
|
|
|
*/
|
2023-05-20 20:25:21 +08:00
|
|
|
package gobot // import "gobot.io/x/gobot/v2"
|