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 (
|
2024-02-11 22:34:50 +08:00
|
|
|
"fmt"
|
2016-08-25 12:25:41 +08:00
|
|
|
"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
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := adafruitDCMotorRunner(adaFruit, dcMotor); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2023-10-30 02:35:26 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("adaFruitBot",
|
|
|
|
[]gobot.Connection{r},
|
|
|
|
[]gobot.Device{adaFruit},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := robot.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-10-30 02:35:26 +08:00
|
|
|
}
|
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
func adafruitDCMotorRunner(a *i2c.Adafruit2348Driver, dcMotor int) 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!
|
2023-11-16 03:51:52 +08:00
|
|
|
if err := a.SetDCMotorSpeed(dcMotor, speed); err != nil {
|
|
|
|
return err
|
2016-08-25 12:25:41 +08:00
|
|
|
}
|
|
|
|
// run FORWARD
|
2023-11-16 03:51:52 +08:00
|
|
|
if err := a.RunDCMotor(dcMotor, i2c.Adafruit2348Forward); err != nil {
|
|
|
|
return err
|
2016-08-25 12:25:41 +08:00
|
|
|
}
|
|
|
|
// Sleep and RELEASE
|
2016-11-05 20:05:49 +08:00
|
|
|
time.Sleep(2000 * time.Millisecond)
|
2023-11-16 03:51:52 +08:00
|
|
|
if err := a.RunDCMotor(dcMotor, i2c.Adafruit2348Release); err != nil {
|
|
|
|
return err
|
2016-08-25 12:25:41 +08:00
|
|
|
}
|
|
|
|
// run BACKWARD
|
2023-11-16 03:51:52 +08:00
|
|
|
if err := a.RunDCMotor(dcMotor, i2c.Adafruit2348Backward); err != nil {
|
|
|
|
return err
|
2016-08-25 12:25:41 +08:00
|
|
|
}
|
|
|
|
// Sleep and RELEASE
|
2016-11-05 20:05:49 +08:00
|
|
|
time.Sleep(2000 * time.Millisecond)
|
2023-11-16 03:51:52 +08:00
|
|
|
if err := a.RunDCMotor(dcMotor, i2c.Adafruit2348Release); err != nil {
|
|
|
|
return err
|
2016-08-25 12:25:41 +08:00
|
|
|
}
|
2023-11-16 03:51:52 +08:00
|
|
|
return nil
|
2016-08-25 12:25:41 +08:00
|
|
|
}
|