gobot.io/x/gobot@v1.16.0/platforms/megapi/README.md (about)

     1  # MegaPi
     2  
     3  The [MegaPi](http://learn.makeblock.com/en/megapi/) is a motor controller by MakeBlock that is compatible with the Raspberry Pi.
     4  
     5  The code is based on a python implementation that can be found [here](https://github.com/Makeblock-official/PythonForMegaPi).
     6  
     7  ## How to Install
     8  
     9  ```
    10  go get -d -u gobot.io/x/gobot/...
    11  ```
    12  
    13  ## How to Use
    14  
    15  ```go
    16  package main
    17  
    18  import (
    19  	"gobot.io/x/gobot"
    20  	"gobot.io/x/gobot/platforms/megapi"
    21  	"time"
    22  )
    23  
    24  func main() {
    25  	// use "/dev/ttyUSB0" if connecting with USB cable
    26  	// use "/dev/ttyAMA0" on devices older than Raspberry Pi 3 Model B
    27  	megaPiAdaptor := megapi.NewAdaptor("/dev/ttyS0")
    28  	motor := megapi.NewMotorDriver(megaPiAdaptor, 1)
    29  
    30  	work := func() {
    31  		speed := int16(0)
    32  		fadeAmount := int16(30)
    33  
    34  		gobot.Every(100*time.Millisecond, func() {
    35  			motor.Speed(speed)
    36  			speed = speed + fadeAmount
    37  			if speed == 0 || speed == 300 {
    38  				fadeAmount = -fadeAmount
    39  			}
    40  		})
    41  	}
    42  
    43  	robot := gobot.NewRobot("megaPiBot",
    44  		[]gobot.Connection{megaPiAdaptor},
    45  		[]gobot.Device{motor},
    46  		work,
    47  	)
    48  
    49  	robot.Start()
    50  }
    51  ```