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.
|
|
|
|
|
2015-10-23 07:56:03 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-09-01 19:32:40 +08:00
|
|
|
"time"
|
2015-10-23 07:56:03 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/api"
|
2024-02-05 01:50:43 +08:00
|
|
|
"gobot.io/x/gobot/v2/drivers/serial"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/serialport"
|
2015-10-23 07:56:03 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2016-10-19 03:08:25 +08:00
|
|
|
master := gobot.NewMaster()
|
|
|
|
a := api.NewAPI(master)
|
2016-09-01 19:32:40 +08:00
|
|
|
a.Start()
|
|
|
|
|
2024-02-05 01:50:43 +08:00
|
|
|
conn := serialport.NewAdaptor("/dev/rfcomm0")
|
|
|
|
ball := serial.NewSpheroDriver(conn)
|
2016-09-01 19:32:40 +08:00
|
|
|
|
|
|
|
robot := gobot.NewRobot("sphero-dpad",
|
|
|
|
[]gobot.Connection{conn},
|
|
|
|
[]gobot.Device{ball},
|
|
|
|
)
|
|
|
|
|
|
|
|
robot.AddCommand("move", func(params map[string]interface{}) interface{} {
|
|
|
|
direction := params["direction"].(string)
|
|
|
|
|
|
|
|
switch direction {
|
|
|
|
case "up":
|
|
|
|
ball.Roll(100, 0)
|
|
|
|
case "down":
|
|
|
|
ball.Roll(100, 180)
|
|
|
|
case "left":
|
|
|
|
ball.Roll(100, 270)
|
|
|
|
case "right":
|
|
|
|
ball.Roll(100, 90)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
ball.Stop()
|
|
|
|
return "ok"
|
|
|
|
})
|
|
|
|
|
2016-10-19 03:08:25 +08:00
|
|
|
master.AddRobot(robot)
|
2016-09-01 19:32:40 +08:00
|
|
|
|
2016-10-19 03:08:25 +08:00
|
|
|
master.Start()
|
2015-10-23 07:56:03 +08:00
|
|
|
}
|