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-12 23:27:08 +08:00
|
|
|
"gobot.io/x/gobot/v2/drivers/serial/sphero"
|
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() {
|
2024-02-13 23:30:25 +08:00
|
|
|
manager := gobot.NewManager()
|
|
|
|
a := api.NewAPI(manager)
|
2023-09-27 02:26:07 +08:00
|
|
|
a.Start()
|
|
|
|
|
2024-02-05 01:50:43 +08:00
|
|
|
ballConn := serialport.NewAdaptor("/dev/rfcomm0")
|
2024-02-12 23:27:08 +08:00
|
|
|
ball := sphero.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,
|
|
|
|
)
|
|
|
|
|
2024-02-13 23:30:25 +08:00
|
|
|
manager.AddRobot(robot)
|
2023-09-27 02:26:07 +08:00
|
|
|
|
2024-02-13 23:30:25 +08:00
|
|
|
if err := manager.Start(); err != nil {
|
2024-02-11 22:34:50 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
2023-09-27 02:26:07 +08:00
|
|
|
}
|