From ae26e1d7f02b5702585ae8669a193f902ecd1a6f Mon Sep 17 00:00:00 2001 From: Ben Schwartz Date: Sun, 29 Mar 2015 09:02:10 -0500 Subject: [PATCH] adding stepper motor example --- samples/28bjy-48.go | 111 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 samples/28bjy-48.go diff --git a/samples/28bjy-48.go b/samples/28bjy-48.go new file mode 100644 index 0000000..1e61d2a --- /dev/null +++ b/samples/28bjy-48.go @@ -0,0 +1,111 @@ +// +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" + "github.com/kidoman/embd" + _ "github.com/kidoman/embd/host/rpi" + "os" + "os/signal" + "time" +) + +func main() { + stepWait := flag.Int("step-delay", 10, "milliseconds between steps") + flag.Parse() + + if err := embd.InitGPIO(); err != nil { + panic(err) + } + defer embd.CloseGPIO() + + // Physical pins 11,15,16,18 + // 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) + } + defer func() { + if err := pin.SetDirection(embd.In); err != nil { + panic(err) + } + }() + + stepPins[i] = pin + } + + // Define advanced sequence + // as shown in manufacturers datasheet + 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 + + // Start main loop + quit := make(chan os.Signal, 1) + signal.Notify(quit, os.Interrupt, os.Kill) + defer signal.Stop(quit) + + stepCounter := 0 + for { + select { + case <-time.After(time.Duration(*stepWait) * time.Millisecond): + 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 + } + case <-quit: + return + } + } + +}