2015-03-29 16:02:10 +02:00
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
// Control a stepper motor (28BJY-48)
|
|
|
|
//
|
|
|
|
// Datasheet:
|
|
|
|
// http://www.raspberrypi-spy.co.uk/wp-content/uploads/2012/07/Stepper-Motor-28BJY-48-Datasheet.pdf
|
|
|
|
//
|
|
|
|
// this is a port of Matt Hawkins' example impl from
|
|
|
|
// http://www.raspberrypi-spy.co.uk/2012/07/stepper-motor-control-in-python/
|
|
|
|
// (link privides additional instructions for wiring your pi)
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"time"
|
2015-03-31 02:10:19 +02:00
|
|
|
|
|
|
|
"github.com/kidoman/embd"
|
|
|
|
_ "github.com/kidoman/embd/host/rpi"
|
2015-03-29 16:02:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2015-03-31 02:10:19 +02:00
|
|
|
stepDelay := flag.Int("step-delay", 10, "milliseconds between steps")
|
2015-03-29 16:02:10 +02:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if err := embd.InitGPIO(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer embd.CloseGPIO()
|
|
|
|
|
2015-03-31 02:10:19 +02:00
|
|
|
// Physical pins 11,15,16,18 on rasp pi
|
2015-03-29 16:02:10 +02:00
|
|
|
// GPIO17,GPIO22,GPIO23,GPIO24
|
|
|
|
stepPinNums := []int{17, 22, 23, 24}
|
|
|
|
|
|
|
|
stepPins := make([]embd.DigitalPin, 4)
|
|
|
|
|
|
|
|
for i, pinNum := range stepPinNums {
|
|
|
|
pin, err := embd.NewDigitalPin(pinNum)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer pin.Close()
|
|
|
|
if err := pin.SetDirection(embd.Out); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := pin.Write(embd.Low); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-04-01 01:16:02 +02:00
|
|
|
defer pin.SetDirection(embd.In)
|
2015-03-29 16:02:10 +02:00
|
|
|
|
|
|
|
stepPins[i] = pin
|
|
|
|
}
|
|
|
|
|
2015-03-31 02:10:19 +02:00
|
|
|
// Define sequence described in manufacturer's datasheet
|
2015-03-29 16:02:10 +02:00
|
|
|
seq := [][]int{
|
|
|
|
[]int{1, 0, 0, 0},
|
|
|
|
[]int{1, 1, 0, 0},
|
|
|
|
[]int{0, 1, 0, 0},
|
|
|
|
[]int{0, 1, 1, 0},
|
|
|
|
[]int{0, 0, 1, 0},
|
|
|
|
[]int{0, 0, 1, 1},
|
|
|
|
[]int{0, 0, 0, 1},
|
|
|
|
[]int{1, 0, 0, 1},
|
|
|
|
}
|
|
|
|
stepCount := len(seq) - 1
|
|
|
|
stepDir := 2 // Set to 1 or 2 for clockwise, -1 or -2 for counter-clockwise
|
|
|
|
|
|
|
|
quit := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(quit, os.Interrupt, os.Kill)
|
|
|
|
defer signal.Stop(quit)
|
|
|
|
|
2015-03-31 02:10:19 +02:00
|
|
|
// Start main loop
|
|
|
|
ticker := time.NewTicker(time.Duration(*stepDelay) * time.Millisecond)
|
2015-04-01 03:22:47 +02:00
|
|
|
defer ticker.Stop()
|
2015-03-31 02:10:19 +02:00
|
|
|
|
|
|
|
var stepCounter int
|
2015-03-29 16:02:10 +02:00
|
|
|
for {
|
|
|
|
select {
|
2015-03-31 02:10:19 +02:00
|
|
|
case <-ticker.C:
|
|
|
|
// set pins to appropriate values for given position in the sequence
|
2015-03-29 16:02:10 +02:00
|
|
|
for i, pin := range stepPins {
|
|
|
|
if seq[stepCounter][i] != 0 {
|
|
|
|
fmt.Printf("Enable pin %d, step %d\n", i, stepCounter)
|
|
|
|
if err := pin.Write(embd.High); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := pin.Write(embd.Low); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stepCounter += stepDir
|
|
|
|
|
|
|
|
// If we reach the end of the sequence start again
|
|
|
|
if stepCounter >= stepCount {
|
|
|
|
stepCounter = 0
|
|
|
|
} else if stepCounter < 0 {
|
|
|
|
stepCounter = stepCount
|
|
|
|
}
|
2015-03-31 02:10:19 +02:00
|
|
|
|
2015-03-29 16:02:10 +02:00
|
|
|
case <-quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|