2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2018-09-06 22:30:41 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2018-09-06 22:30:41 +08:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2017-12-02 21:22:41 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-11-10 03:31:18 +08:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
2017-12-02 21:22:41 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/drivers/gpio"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/raspi"
|
2017-12-02 21:22:41 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2023-11-10 03:31:18 +08:00
|
|
|
const (
|
|
|
|
coilA1 = "7"
|
|
|
|
coilA2 = "13"
|
|
|
|
coilB1 = "11"
|
|
|
|
coilB2 = "15"
|
|
|
|
|
|
|
|
degPerStep = 1.875
|
|
|
|
countRot = 10
|
|
|
|
)
|
|
|
|
stepPerRevision := int(360.0 / degPerStep)
|
|
|
|
|
2017-12-02 21:22:41 +08:00
|
|
|
r := raspi.NewAdaptor()
|
2023-11-10 03:31:18 +08:00
|
|
|
stepper := gpio.NewStepperDriver(r, [4]string{coilA1, coilB1, coilA2, coilB2}, gpio.StepperModes.DualPhaseStepping,
|
|
|
|
uint(stepPerRevision))
|
2017-12-02 21:22:41 +08:00
|
|
|
|
|
|
|
work := func() {
|
2023-11-10 03:31:18 +08:00
|
|
|
defer func() {
|
|
|
|
ec := 0
|
|
|
|
// set current to zero to prevent overheating
|
|
|
|
if err := stepper.Sleep(); err != nil {
|
|
|
|
ec = 1
|
|
|
|
log.Println("work done", err)
|
|
|
|
} else {
|
|
|
|
log.Println("work done")
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(ec)
|
|
|
|
}()
|
|
|
|
|
|
|
|
gobot.After(5*time.Second, func() {
|
|
|
|
// this stops only the current movement and the next will start immediately (if any)
|
|
|
|
// this means for the example, that the first rotation stops after ~5 rotations
|
|
|
|
log.Println("asynchron stop after 5 sec.")
|
|
|
|
if err := stepper.Stop(); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// one rotation per second
|
|
|
|
if err := stepper.SetSpeed(60); err != nil {
|
|
|
|
log.Println("set speed", err)
|
|
|
|
}
|
2017-12-02 21:22:41 +08:00
|
|
|
|
2023-11-10 03:31:18 +08:00
|
|
|
// Move forward N revolution
|
|
|
|
if err := stepper.Move(stepPerRevision * countRot); err != nil {
|
|
|
|
log.Println("move forward", err)
|
2017-12-02 21:22:41 +08:00
|
|
|
}
|
|
|
|
|
2023-11-10 03:31:18 +08:00
|
|
|
// Move backward N revolution
|
|
|
|
if err := stepper.MoveDeg(-360 * countRot); err != nil {
|
|
|
|
log.Println("move backward", err)
|
2017-12-02 21:22:41 +08:00
|
|
|
}
|
2023-11-10 03:31:18 +08:00
|
|
|
return
|
2017-12-02 21:22:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("stepperBot",
|
|
|
|
[]gobot.Connection{r},
|
|
|
|
[]gobot.Device{stepper},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
|
|
|
robot.Start()
|
|
|
|
}
|