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
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
adafruitStepperMotorRunner(adaFruit, motor)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
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
|
|
|
|
|
|
|
|
a.SetStepperMotorSpeed(motor, speed)
|
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
if err := a.Step(motor, steps, i2c.Adafruit2348Forward, style); err != nil {
|
2023-10-30 02:35:26 +08:00
|
|
|
log.Printf(err.Error())
|
2023-11-16 03:51:52 +08:00
|
|
|
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.Adafruit2348Backward, style); err != nil {
|
2023-10-30 02:35:26 +08:00
|
|
|
log.Printf(err.Error())
|
2023-11-16 03:51:52 +08:00
|
|
|
return err
|
2023-10-30 02:35:26 +08:00
|
|
|
}
|
2023-11-16 03:51:52 +08:00
|
|
|
return nil
|
2023-10-30 02:35:26 +08:00
|
|
|
}
|