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
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/api"
|
|
|
|
"gobot.io/x/gobot/platforms/sphero"
|
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()
|
|
|
|
|
2016-10-03 22:58:43 +08:00
|
|
|
conn := sphero.NewAdaptor("/dev/rfcomm0")
|
|
|
|
ball := sphero.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
|
|
|
}
|