2017-03-13 23:01:39 +08:00
|
|
|
// +build example
|
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2016-08-26 13:51:40 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-10-03 22:58:43 +08:00
|
|
|
"time"
|
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/platforms/megapi"
|
2016-08-26 13:51:40 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// use "/dev/ttyUSB0" if connecting with USB cable
|
|
|
|
// use "/dev/ttyAMA0" on devices older than Raspberry Pi 3 Model B
|
2016-10-03 22:58:43 +08:00
|
|
|
megaPiAdaptor := megapi.NewAdaptor("/dev/ttyS0")
|
|
|
|
motor := megapi.NewMotorDriver(megaPiAdaptor, 1)
|
2016-08-26 13:51:40 +08:00
|
|
|
|
|
|
|
work := func() {
|
|
|
|
speed := int16(0)
|
|
|
|
fadeAmount := int16(30)
|
|
|
|
|
|
|
|
gobot.Every(100*time.Millisecond, func() {
|
|
|
|
motor.Speed(speed)
|
|
|
|
speed = speed + fadeAmount
|
|
|
|
if speed == 0 || speed == 300 {
|
|
|
|
fadeAmount = -fadeAmount
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("megaPiBot",
|
|
|
|
[]gobot.Connection{megaPiAdaptor},
|
|
|
|
[]gobot.Device{motor},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
2016-10-19 03:08:25 +08:00
|
|
|
robot.Start()
|
2016-08-26 13:51:40 +08:00
|
|
|
}
|