hybridgroup.gobot/examples/raspi_stepper_move.go

83 lines
1.6 KiB
Go
Raw Normal View History

//go:build example
// +build example
//
// Do not build by default.
2017-12-02 21:22:41 +08:00
package main
import (
"log"
"os"
"time"
2017-12-02 21:22:41 +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() {
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()
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() {
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
// 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
}
// 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
}
return
2017-12-02 21:22:41 +08:00
}
robot := gobot.NewRobot("stepperBot",
[]gobot.Connection{r},
[]gobot.Device{stepper},
work,
)
robot.Start()
}