2023-09-27 02:26:07 +08:00
|
|
|
//go:build example
|
|
|
|
// +build example
|
|
|
|
|
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
2023-09-27 02:26:07 +08:00
|
|
|
"gobot.io/x/gobot/v2/platforms/keyboard"
|
2024-02-05 01:50:43 +08:00
|
|
|
"gobot.io/x/gobot/v2/platforms/serialport"
|
2023-09-27 02:26:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
master := gobot.NewMaster()
|
|
|
|
a := api.NewAPI(master)
|
|
|
|
a.Start()
|
|
|
|
|
2024-02-05 01:50:43 +08:00
|
|
|
ballConn := serialport.NewAdaptor("/dev/rfcomm0")
|
|
|
|
ball := serial.NewSpheroDriver(ballConn)
|
2023-09-27 02:26:07 +08:00
|
|
|
|
|
|
|
keys := keyboard.NewDriver()
|
|
|
|
|
|
|
|
calibrating := false
|
|
|
|
|
|
|
|
work := func() {
|
2024-02-11 22:34:50 +08:00
|
|
|
_ = keys.On(keyboard.Key, func(data interface{}) {
|
2023-09-27 02:26:07 +08:00
|
|
|
key := data.(keyboard.KeyEvent)
|
|
|
|
|
|
|
|
switch key.Key {
|
|
|
|
case keyboard.ArrowUp:
|
|
|
|
if calibrating {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
ball.Roll(100, 0)
|
|
|
|
case keyboard.ArrowDown:
|
|
|
|
if calibrating {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
ball.Roll(100, 100)
|
|
|
|
case keyboard.ArrowLeft:
|
|
|
|
ball.Roll(100, 270)
|
|
|
|
case keyboard.ArrowRight:
|
|
|
|
ball.Roll(100, 90)
|
|
|
|
case keyboard.Spacebar:
|
|
|
|
if calibrating {
|
|
|
|
ball.FinishCalibration()
|
|
|
|
} else {
|
|
|
|
ball.StartCalibration()
|
|
|
|
}
|
|
|
|
calibrating = !calibrating
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("sphero-calibration",
|
|
|
|
[]gobot.Connection{ballConn},
|
|
|
|
[]gobot.Device{ball, keys},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
|
|
|
master.AddRobot(robot)
|
|
|
|
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := master.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-09-27 02:26:07 +08:00
|
|
|
}
|