-
Notifications
You must be signed in to change notification settings - Fork 0
/
motor.go
174 lines (151 loc) · 3.7 KB
/
motor.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"fmt"
"time"
rpio "github.com/stianeikeland/go-rpio"
)
const (
maxSpeed int64 = 1000 // steps per sec
maxInitSpeed int64 = 500
//switchInputReadFrequency time.Duration = 100 * time.Millisecond
)
const (
limitSwitchPinA rpio.Pin = rpio.Pin(5)
limitSwitchPinB rpio.Pin = rpio.Pin(13)
)
// Define GPIO signals to use
// Physical pins 11,15,16,18
// GPIO17,GPIO22,GPIO23,GPIO24
var StepPins = []uint8{17, 22, 23, 24}
var stepSequence = [][]int{
{1, 0, 0, 0},
{1, 1, 0, 0},
{0, 1, 0, 0},
{0, 1, 1, 0},
{0, 0, 1, 0},
{0, 0, 1, 1},
{0, 0, 0, 1},
{1, 0, 0, 1},
}
type Motor struct {
maxSteps int64
isInitialized bool
stepPins []rpio.Pin
stepDirection int
currentStep int
limitSwitchA *LimitSwitch
limitSwitchB *LimitSwitch
}
func NewMotor() *Motor {
m := &Motor{
stepDirection: 1,
limitSwitchA: NewLimitSwitch(limitSwitchPinA),
limitSwitchB: NewLimitSwitch(limitSwitchPinB),
}
for _, pinNum := range StepPins {
pin := rpio.Pin(pinNum)
pin.Output()
m.stepPins = append(m.stepPins, pin)
}
m.init()
m.ToggleDirection()
fmt.Println("Max number of steps: ", m.maxSteps)
return m
}
func (m *Motor) Step() {
//fmt.Printf("Current step [%d]: [%#v]\n", m.currentStep, stepSequence[m.currentStep])
nextStep := m.currentStep + m.stepDirection
if nextStep < 0 {
nextStep = len(stepSequence) - 1
}
nextStep = nextStep % len(stepSequence)
//fmt.Printf("Next step [%d]: [%#v]\n", nextStep, stepSequence[nextStep])
for i, val := range stepSequence[nextStep] {
if val == 1 {
m.stepPins[i].High()
} else {
m.stepPins[i].Low()
}
}
// Save the current step position.
m.currentStep = nextStep
}
func (m *Motor) ToggleDirection() {
m.stepDirection = -1 * m.stepDirection
}
func (m *Motor) Run(stopChan <-chan bool, stepDuration time.Duration) <-chan bool {
ticker := time.NewTicker(stepDuration)
doneChan := make(chan bool, 1)
switchNotificationA := m.limitSwitchA.Notify()
switchNotificationB := m.limitSwitchB.Notify()
// Run the motor asynchronously.
go func() {
defer func() {
ticker.Stop()
doneChan <- true
}()
for i := int64(0); i < m.maxSteps; i++ {
select {
case <-ticker.C:
m.Step()
case <-stopChan:
fmt.Println("Stopping the motor")
return
case <-switchNotificationA:
fmt.Println("Received stop notification from the switch-A:")
return
case <-switchNotificationB:
fmt.Println("Received stop notification from the switch-B:")
return
}
}
fmt.Printf("Max steps [%d] reached. Stop switch didn't fire. Stopping as a precaution\n", m.maxSteps)
}()
return doneChan
}
func (m *Motor) init() {
fmt.Println("Resetting the motor")
for _, pin := range m.stepPins {
pin.Low()
}
m.Reset()
m.ToggleDirection()
switchNotificationA := m.limitSwitchA.Notify()
switchNotificationB := m.limitSwitchB.Notify()
stepTicker := time.NewTicker(time.Second / time.Duration(maxInitSpeed))
m.maxSteps = 0
for {
select {
case <-switchNotificationA:
stepTicker.Stop()
fmt.Println("Received notification from the switch-A:")
return
case <-switchNotificationB:
stepTicker.Stop()
fmt.Println("Received notification from the switch-B:")
return
case <-stepTicker.C:
m.maxSteps++
m.Step()
}
}
}
func (m *Motor) Reset() {
stepTicker := time.NewTicker(time.Second / time.Duration(maxInitSpeed))
switchNotificationA := m.limitSwitchA.Notify()
switchNotificationB := m.limitSwitchB.Notify()
for {
select {
case <-switchNotificationA:
stepTicker.Stop()
fmt.Println("Received notification from the switch-A:")
return
case <-switchNotificationB:
stepTicker.Stop()
fmt.Println("Received notification from the switch-B:")
return
case <-stepTicker.C:
m.Step()
}
}
}