2016-08-25 22:51:40 -07:00
|
|
|
# MegaPi
|
|
|
|
|
2023-06-04 18:36:55 +02:00
|
|
|
The [MegaPi](http://learn.makeblock.com/en/megapi/) is a motor controller by MakeBlock that is compatible with the
|
|
|
|
Raspberry Pi.
|
2016-08-25 22:51:40 -07:00
|
|
|
|
|
|
|
The code is based on a python implementation that can be found [here](https://github.com/Makeblock-official/PythonForMegaPi).
|
|
|
|
|
|
|
|
## How to Install
|
|
|
|
|
2023-06-04 18:36:55 +02:00
|
|
|
Please refer to the main [README.md](https://github.com/hybridgroup/gobot/blob/release/README.md)
|
2016-08-25 22:51:40 -07:00
|
|
|
|
|
|
|
## How to Use
|
|
|
|
|
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-06-04 18:36:55 +02:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/megapi"
|
|
|
|
"time"
|
2016-08-25 22:51:40 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2023-06-04 18:36:55 +02:00
|
|
|
// use "/dev/ttyUSB0" if connecting with USB cable
|
|
|
|
// use "/dev/ttyAMA0" on devices older than Raspberry Pi 3 Model B
|
|
|
|
megaPiAdaptor := megapi.NewAdaptor("/dev/ttyS0")
|
|
|
|
motor := megapi.NewMotorDriver(megaPiAdaptor, 1)
|
|
|
|
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
|
|
|
robot.Start()
|
2016-08-25 22:51:40 -07:00
|
|
|
}
|
|
|
|
```
|