2016-08-25 12:25:41 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hybridgroup/gobot"
|
2016-10-16 01:48:34 +08:00
|
|
|
"github.com/hybridgroup/gobot/drivers/i2c"
|
2016-08-25 12:25:41 +08:00
|
|
|
"github.com/hybridgroup/gobot/platforms/raspi"
|
|
|
|
)
|
|
|
|
|
|
|
|
func adafruitDCMotorRunner(a *i2c.AdafruitMotorHatDriver, dcMotor int) (err error) {
|
|
|
|
log.Printf("DC Motor Run Loop...\n")
|
|
|
|
// set the speed:
|
|
|
|
var speed int32 = 255 // 255 = full speed!
|
|
|
|
if err = a.SetDCMotorSpeed(dcMotor, speed); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// run FORWARD
|
|
|
|
if err = a.RunDCMotor(dcMotor, i2c.AdafruitForward); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Sleep and RELEASE
|
|
|
|
<-time.After(2000 * time.Millisecond)
|
|
|
|
if err = a.RunDCMotor(dcMotor, i2c.AdafruitRelease); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// run BACKWARD
|
|
|
|
if err = a.RunDCMotor(dcMotor, i2c.AdafruitBackward); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Sleep and RELEASE
|
|
|
|
<-time.After(2000 * time.Millisecond)
|
|
|
|
if err = a.RunDCMotor(dcMotor, i2c.AdafruitRelease); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2016-10-19 03:08:25 +08:00
|
|
|
|
2016-08-25 12:25:41 +08:00
|
|
|
func main() {
|
2016-10-16 01:55:43 +08:00
|
|
|
r := raspi.NewAdaptor()
|
|
|
|
adaFruit := i2c.NewAdafruitMotorHatDriver(r)
|
2016-08-25 12:25:41 +08:00
|
|
|
|
|
|
|
work := func() {
|
|
|
|
gobot.Every(5*time.Second, func() {
|
|
|
|
|
2016-08-26 12:04:36 +08:00
|
|
|
dcMotor := 2 // 0-based
|
2016-08-25 12:25:41 +08:00
|
|
|
adafruitDCMotorRunner(adaFruit, dcMotor)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("adaFruitBot",
|
|
|
|
[]gobot.Connection{r},
|
|
|
|
[]gobot.Device{adaFruit},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
2016-10-19 03:08:25 +08:00
|
|
|
robot.Start()
|
2016-08-25 12:25:41 +08:00
|
|
|
}
|