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 adafruitStepperMotorRunner(a *i2c.AdafruitMotorHatDriver, motor int) (err error) {
|
|
|
|
log.Printf("Stepper Motor Run Loop...\n")
|
|
|
|
// set the speed state:
|
|
|
|
speed := 30 // rpm
|
2016-09-02 20:44:50 +08:00
|
|
|
style := i2c.AdafruitDouble
|
|
|
|
steps := 20
|
2016-08-26 10:51:00 +08:00
|
|
|
|
2016-08-25 12:25:41 +08:00
|
|
|
a.SetStepperMotorSpeed(motor, speed)
|
|
|
|
|
2016-08-26 10:51:00 +08:00
|
|
|
if err = a.Step(motor, steps, i2c.AdafruitForward, style); err != nil {
|
2016-08-25 12:25:41 +08:00
|
|
|
log.Printf(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2016-08-26 10:51:00 +08:00
|
|
|
if err = a.Step(motor, steps, i2c.AdafruitBackward, style); err != nil {
|
2016-08-25 12:25:41 +08:00
|
|
|
log.Printf(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2016-10-16 01:55:43 +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() {
|
|
|
|
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
|
|
|
}
|