forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raspi_adafruit_stepper.go
50 lines (41 loc) · 967 Bytes
/
raspi_adafruit_stepper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"log"
"time"
"gobot.io/x/gobot"
"gobot.io/x/gobot/drivers/i2c"
"gobot.io/x/gobot/platforms/raspi"
)
func adafruitStepperMotorRunner(a *i2c.AdafruitMotorHatDriver, motor int) (err error) {
log.Printf("Stepper Motor Run Loop...\n")
// set the speed state:
speed := 30 // rpm
style := i2c.AdafruitDouble
steps := 20
a.SetStepperMotorSpeed(motor, speed)
if err = a.Step(motor, steps, i2c.AdafruitForward, style); err != nil {
log.Printf(err.Error())
return
}
if err = a.Step(motor, steps, i2c.AdafruitBackward, style); err != nil {
log.Printf(err.Error())
return
}
return
}
func main() {
r := raspi.NewAdaptor()
adaFruit := i2c.NewAdafruitMotorHatDriver(r)
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,
)
robot.Start()
}