forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
motor_driver.go
158 lines (140 loc) · 2.84 KB
/
motor_driver.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
package gpio
import (
"github.com/hybridgroup/gobot"
)
type MotorDriver struct {
gobot.Driver
SpeedPin string
SwitchPin string
DirectionPin string
ForwardPin string
BackwardPin string
CurrentState byte
CurrentSpeed byte
CurrentMode string
CurrentDirection string
}
func NewMotorDriver(a PwmDigitalWriter, name string, pin string) *MotorDriver {
return &MotorDriver{
Driver: *gobot.NewDriver(
name,
"MotorDriver",
a.(gobot.AdaptorInterface),
),
CurrentState: 0,
CurrentSpeed: 0,
CurrentMode: "digital",
CurrentDirection: "forward",
}
}
func (m *MotorDriver) adaptor() PwmDigitalWriter {
return m.Adaptor().(PwmDigitalWriter)
}
func (m *MotorDriver) Start() bool { return true }
func (m *MotorDriver) Halt() bool { return true }
func (m *MotorDriver) Init() bool { return true }
func (m *MotorDriver) Off() {
if m.isDigital() {
m.changeState(0)
} else {
m.Speed(0)
}
}
func (m *MotorDriver) On() {
if m.isDigital() {
m.changeState(1)
} else {
if m.CurrentSpeed == 0 {
m.CurrentSpeed = 255
}
m.Speed(m.CurrentSpeed)
}
}
func (m *MotorDriver) Min() {
m.Off()
}
func (m *MotorDriver) Max() {
m.Speed(255)
}
func (m *MotorDriver) IsOn() bool {
if m.isDigital() {
return m.CurrentState == 1
}
return m.CurrentSpeed > 0
}
func (m *MotorDriver) IsOff() bool {
return !m.IsOn()
}
func (m *MotorDriver) Toggle() {
if m.IsOn() {
m.Off()
} else {
m.On()
}
}
func (m *MotorDriver) Speed(value byte) {
m.CurrentMode = "analog"
m.CurrentSpeed = value
m.adaptor().PwmWrite(m.SpeedPin, value)
}
func (m *MotorDriver) Forward(speed byte) {
m.Direction("forward")
m.Speed(speed)
}
func (m *MotorDriver) Backward(speed byte) {
m.Direction("backward")
m.Speed(speed)
}
func (m *MotorDriver) Direction(direction string) {
m.CurrentDirection = direction
if m.DirectionPin != "" {
var level byte
if direction == "forward" {
level = 1
} else {
level = 0
}
m.adaptor().DigitalWrite(m.DirectionPin, level)
} else {
var forwardLevel, backwardLevel byte
switch direction {
case "forward":
forwardLevel = 1
backwardLevel = 0
case "backward":
forwardLevel = 0
backwardLevel = 1
case "none":
forwardLevel = 0
backwardLevel = 0
}
m.adaptor().DigitalWrite(m.ForwardPin, forwardLevel)
m.adaptor().DigitalWrite(m.BackwardPin, backwardLevel)
}
}
func (m *MotorDriver) isDigital() bool {
if m.CurrentMode == "digital" {
return true
}
return false
}
func (m *MotorDriver) changeState(state byte) {
m.CurrentState = state
if state == 1 {
m.CurrentSpeed = 0
} else {
m.CurrentSpeed = 255
}
if m.ForwardPin != "" {
if state == 0 {
m.Direction(m.CurrentDirection)
if m.SpeedPin != "" {
m.Speed(m.CurrentSpeed)
}
} else {
m.Direction("none")
}
} else {
m.adaptor().DigitalWrite(m.SpeedPin, state)
}
}