2023-05-20 14:25:21 +02:00
|
|
|
//go:build example
|
2017-03-13 11:01:39 -04:00
|
|
|
// +build example
|
2023-05-20 14:25:21 +02:00
|
|
|
|
2017-03-13 11:01:39 -04:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2024-11-01 12:54:20 +01:00
|
|
|
//nolint:gosec // ok here
|
2013-11-13 20:49:57 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-04-26 18:07:04 -07:00
|
|
|
"fmt"
|
2014-07-09 09:38:43 -07:00
|
|
|
"time"
|
|
|
|
|
2023-05-20 14:25:21 +02:00
|
|
|
"gobot.io/x/gobot/v2"
|
2024-02-12 16:27:08 +01:00
|
|
|
"gobot.io/x/gobot/v2/drivers/common/spherocommon"
|
|
|
|
"gobot.io/x/gobot/v2/drivers/serial/sphero"
|
2024-02-04 18:50:43 +01:00
|
|
|
"gobot.io/x/gobot/v2/platforms/serialport"
|
2013-11-13 20:49:57 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-02-04 18:50:43 +01:00
|
|
|
adaptor := serialport.NewAdaptor("/dev/rfcomm0")
|
2024-02-12 16:27:08 +01:00
|
|
|
spheroDriver := sphero.NewSpheroDriver(adaptor)
|
2013-11-13 20:49:57 -08:00
|
|
|
|
|
|
|
work := func() {
|
2024-02-12 16:27:08 +01:00
|
|
|
spheroDriver.SetDataStreaming(spherocommon.DefaultDataStreamingConfig())
|
2014-12-25 08:32:24 -08:00
|
|
|
|
2024-02-12 16:27:08 +01:00
|
|
|
_ = spheroDriver.On(spherocommon.CollisionEvent, func(data interface{}) {
|
2014-12-25 08:32:24 -08:00
|
|
|
fmt.Printf("Collision! %+v\n", data)
|
|
|
|
})
|
|
|
|
|
2024-02-12 16:27:08 +01:00
|
|
|
_ = spheroDriver.On(spherocommon.SensorDataEvent, func(data interface{}) {
|
2014-12-25 08:32:24 -08:00
|
|
|
fmt.Printf("Streaming Data! %+v\n", data)
|
2014-04-26 18:07:04 -07:00
|
|
|
})
|
|
|
|
|
2014-05-22 21:20:16 -07:00
|
|
|
gobot.Every(3*time.Second, func() {
|
2014-04-28 11:40:20 -07:00
|
|
|
spheroDriver.Roll(30, uint16(gobot.Rand(360)))
|
2014-04-26 18:07:04 -07:00
|
|
|
})
|
|
|
|
|
2014-05-22 21:20:16 -07:00
|
|
|
gobot.Every(1*time.Second, func() {
|
2014-04-26 18:07:04 -07:00
|
|
|
r := uint8(gobot.Rand(255))
|
|
|
|
g := uint8(gobot.Rand(255))
|
|
|
|
b := uint8(gobot.Rand(255))
|
2014-04-28 11:40:20 -07:00
|
|
|
spheroDriver.SetRGB(r, g, b)
|
2013-11-13 20:49:57 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-08 18:36:14 -07:00
|
|
|
robot := gobot.NewRobot("sphero",
|
|
|
|
[]gobot.Connection{adaptor},
|
|
|
|
[]gobot.Device{spheroDriver},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
2024-02-11 15:34:50 +01:00
|
|
|
if err := robot.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-11-13 20:49:57 -08:00
|
|
|
}
|