2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2017-03-13 23:01:39 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2017-03-13 23:01:39 +08:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2016-08-25 12:25:41 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/drivers/i2c"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/raspi"
|
2016-08-25 12:25:41 +08:00
|
|
|
)
|
|
|
|
|
2023-10-30 02:35:26 +08:00
|
|
|
func main() {
|
|
|
|
r := raspi.NewAdaptor()
|
|
|
|
|
|
|
|
// we use the default address 0x60 for DC/Stepper Motor HAT on top of the Pi
|
|
|
|
adaFruit := i2c.NewAdafruit2348Driver(r)
|
|
|
|
|
|
|
|
work := func() {
|
|
|
|
gobot.Every(5*time.Second, func() {
|
|
|
|
dcMotor := 2 // 0-based
|
|
|
|
adafruitDCMotorRunner(adaFruit, dcMotor)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("adaFruitBot",
|
|
|
|
[]gobot.Connection{r},
|
|
|
|
[]gobot.Device{adaFruit},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
|
|
|
robot.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
func adafruitDCMotorRunner(a *i2c.Adafruit2348Driver, dcMotor int) (err error) {
|
2016-08-25 12:25:41 +08:00
|
|
|
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
|
2023-10-30 02:35:26 +08:00
|
|
|
if err = a.RunDCMotor(dcMotor, i2c.Adafruit2348Forward); err != nil {
|
2016-08-25 12:25:41 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Sleep and RELEASE
|
2016-11-05 20:05:49 +08:00
|
|
|
time.Sleep(2000 * time.Millisecond)
|
2023-10-30 02:35:26 +08:00
|
|
|
if err = a.RunDCMotor(dcMotor, i2c.Adafruit2348Release); err != nil {
|
2016-08-25 12:25:41 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// run BACKWARD
|
2023-10-30 02:35:26 +08:00
|
|
|
if err = a.RunDCMotor(dcMotor, i2c.Adafruit2348Backward); err != nil {
|
2016-08-25 12:25:41 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Sleep and RELEASE
|
2016-11-05 20:05:49 +08:00
|
|
|
time.Sleep(2000 * time.Millisecond)
|
2023-10-30 02:35:26 +08:00
|
|
|
if err = a.RunDCMotor(dcMotor, i2c.Adafruit2348Release); err != nil {
|
2016-08-25 12:25:41 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|