2023-05-20 14:25:21 +02:00
|
|
|
//go:build example
|
2017-03-13 11:01:39 -04:00
|
|
|
// +build example
|
2023-05-20 14:25:21 +02:00
|
|
|
|
2017-03-13 11:01:39 -04:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2016-08-25 22:51:40 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-02-11 15:34:50 +01:00
|
|
|
"fmt"
|
2016-10-03 16:58:43 +02:00
|
|
|
"time"
|
|
|
|
|
2023-05-20 14:25:21 +02:00
|
|
|
"gobot.io/x/gobot/v2"
|
2024-02-12 17:55:26 +01:00
|
|
|
"gobot.io/x/gobot/v2/drivers/serial/megapi"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/serialport"
|
2016-08-25 22:51:40 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// use "/dev/ttyUSB0" if connecting with USB cable
|
|
|
|
// use "/dev/ttyAMA0" on devices older than Raspberry Pi 3 Model B
|
2024-02-12 17:55:26 +01:00
|
|
|
adaptor := serialport.NewAdaptor("/dev/ttyS0", serialport.WithName("MegaPi"))
|
|
|
|
motor := megapi.NewMotorDriver(adaptor, 1)
|
2016-08-25 22:51:40 -07:00
|
|
|
|
|
|
|
work := func() {
|
|
|
|
speed := int16(0)
|
|
|
|
fadeAmount := int16(30)
|
|
|
|
|
|
|
|
gobot.Every(100*time.Millisecond, func() {
|
2024-02-11 15:34:50 +01:00
|
|
|
if err := motor.Speed(speed); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2016-08-25 22:51:40 -07:00
|
|
|
speed = speed + fadeAmount
|
|
|
|
if speed == 0 || speed == 300 {
|
|
|
|
fadeAmount = -fadeAmount
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("megaPiBot",
|
2024-02-12 17:55:26 +01:00
|
|
|
[]gobot.Connection{adaptor},
|
2016-08-25 22:51:40 -07:00
|
|
|
[]gobot.Device{motor},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
2024-02-11 15:34:50 +01:00
|
|
|
if err := robot.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-08-25 22:51:40 -07:00
|
|
|
}
|