2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2017-03-13 23:01:39 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2017-03-13 23:01:39 +08:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
2024-02-05 01:50:43 +08:00
|
|
|
"gobot.io/x/gobot/v2/drivers/ble/sphero"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/bleclient"
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2/platforms/mqtt"
|
2016-12-08 20:24:03 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
FRENTE = 0
|
|
|
|
DERECHA = 90
|
|
|
|
ATRAS = 180
|
|
|
|
IZQUIERDA = 270
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-02-05 01:50:43 +08:00
|
|
|
bleAdaptor := bleclient.NewAdaptor(os.Args[1])
|
|
|
|
ollie := sphero.NewOllieDriver(bleAdaptor)
|
2016-12-08 20:24:03 +08:00
|
|
|
|
|
|
|
mqttAdaptor := mqtt.NewAdaptor("tcp://iot.eclipse.org:1883", "ollie")
|
|
|
|
|
|
|
|
work := func() {
|
|
|
|
ollie.SetRGB(255, 0, 255)
|
|
|
|
|
2024-02-11 22:34:50 +08:00
|
|
|
_ = mqttAdaptor.On("sensors/dial", func(msg mqtt.Message) {
|
2017-04-19 15:21:34 +08:00
|
|
|
val, _ := strconv.Atoi(string(msg.Payload()))
|
2016-12-08 20:24:03 +08:00
|
|
|
|
|
|
|
if val > 2000 {
|
|
|
|
ollie.SetRGB(0, 255, 0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if val > 1000 {
|
|
|
|
ollie.SetRGB(255, 255, 0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ollie.SetRGB(255, 0, 0)
|
|
|
|
})
|
|
|
|
|
2024-02-11 22:34:50 +08:00
|
|
|
_ = mqttAdaptor.On("rover/frente", func(msg mqtt.Message) {
|
2016-12-08 20:24:03 +08:00
|
|
|
ollie.Roll(40, FRENTE)
|
|
|
|
gobot.After(1*time.Second, func() {
|
|
|
|
ollie.Stop()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-02-11 22:34:50 +08:00
|
|
|
_ = mqttAdaptor.On("rover/derecha", func(msg mqtt.Message) {
|
2016-12-08 20:24:03 +08:00
|
|
|
ollie.Roll(40, DERECHA)
|
|
|
|
gobot.After(1*time.Second, func() {
|
|
|
|
ollie.Stop()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-02-11 22:34:50 +08:00
|
|
|
_ = mqttAdaptor.On("rover/atras", func(msg mqtt.Message) {
|
2016-12-08 20:24:03 +08:00
|
|
|
ollie.Roll(40, ATRAS)
|
|
|
|
gobot.After(1*time.Second, func() {
|
|
|
|
ollie.Stop()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-02-11 22:34:50 +08:00
|
|
|
_ = mqttAdaptor.On("rover/izquierda", func(msg mqtt.Message) {
|
2016-12-08 20:24:03 +08:00
|
|
|
ollie.Roll(40, IZQUIERDA)
|
|
|
|
gobot.After(1*time.Second, func() {
|
|
|
|
ollie.Stop()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("ollieBot",
|
|
|
|
[]gobot.Connection{bleAdaptor, mqttAdaptor},
|
|
|
|
[]gobot.Device{ollie},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := robot.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-12-08 20:24:03 +08:00
|
|
|
}
|