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
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2016-10-16 01:55:43 +08:00
|
|
|
r := raspi.NewAdaptor()
|
2023-10-30 02:35:26 +08:00
|
|
|
|
|
|
|
// we use the default address 0x60 for DC/Stepper Motor HAT on top of the Pi
|
|
|
|
adaFruit := i2c.NewAdafruit2348Driver(r)
|
2016-08-25 12:25:41 +08:00
|
|
|
|
|
|
|
work := func() {
|
|
|
|
gobot.Every(5*time.Second, func() {
|
|
|
|
motor := 0 // 0-based
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := adafruitStepperMotorRunner(adaFruit, motor); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2016-08-25 12:25:41 +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)
|
|
|
|
}
|
2016-08-25 12:25:41 +08:00
|
|
|
}
|
2023-10-30 02:35:26 +08:00
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
func adafruitStepperMotorRunner(a *i2c.Adafruit2348Driver, motor int) error {
|
2023-10-30 02:35:26 +08:00
|
|
|
log.Printf("Stepper Motor Run Loop...\n")
|
|
|
|
// set the speed state:
|
|
|
|
speed := 30 // rpm
|
|
|
|
style := i2c.Adafruit2348Double
|
|
|
|
steps := 20
|
|
|
|
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := a.SetStepperMotorSpeed(motor, speed); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-30 02:35:26 +08:00
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
if err := a.Step(motor, steps, i2c.Adafruit2348Forward, style); err != nil {
|
|
|
|
return err
|
2023-10-30 02:35:26 +08:00
|
|
|
}
|
2024-02-11 22:34:50 +08:00
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
if err := a.Step(motor, steps, i2c.Adafruit2348Backward, style); err != nil {
|
|
|
|
return err
|
2023-10-30 02:35:26 +08:00
|
|
|
}
|
2024-02-11 22:34:50 +08:00
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
return nil
|
2023-10-30 02:35:26 +08:00
|
|
|
}
|