-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
dc_motor.go
291 lines (258 loc) · 7.16 KB
/
dc_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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright ©2016 The ev3go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ev3dev
import (
"path/filepath"
"strconv"
"time"
)
var _ idSetter = (*DCMotor)(nil)
// DCMotor represents a handle to a dc-motor.
type DCMotor struct {
id int
// Cached values:
driver string
commands, stopActions []string
err error
}
// Path returns the dc-motor sysfs path.
func (*DCMotor) Path() string { return filepath.Join(prefix, DCMotorPath) }
// Type returns "motor".
func (*DCMotor) Type() string { return motorPrefix }
// String satisfies the fmt.Stringer interface.
func (m *DCMotor) String() string {
if m == nil {
return motorPrefix + "*"
}
return motorPrefix + strconv.Itoa(m.id)
}
// Err returns the error state of the DCMotor and clears it.
func (m *DCMotor) Err() error {
err := m.err
m.err = nil
return err
}
// idInt and setID satisfy the idSetter interface.
func (m *DCMotor) setID(id int) error {
t := DCMotor{id: id}
var err error
t.commands, err = stringSliceFrom(attributeOf(&t, commands))
if err != nil {
goto fail
}
t.stopActions, err = stringSliceFrom(attributeOf(&t, stopActions))
if err != nil {
goto fail
}
t.driver, err = DriverFor(&t)
if err != nil {
goto fail
}
*m = t
return nil
fail:
*m = DCMotor{id: -1}
return err
}
func (m *DCMotor) idInt() int {
if m == nil {
return -1
}
return m.id
}
// DCMotorFor returns a DCMotor for the given ev3 port name and driver. If the
// motor driver does not match the driver string, a DCMotor for the port is
// returned with a DriverMismatch error.
// If port is empty, the first dc-motor satisfying the driver name is returned.
func DCMotorFor(port, driver string) (*DCMotor, error) {
id, err := deviceIDFor(port, driver, (*DCMotor)(nil), -1)
if id == -1 {
return nil, err
}
var m DCMotor
_err := m.setID(id)
if _err != nil {
err = _err
}
return &m, err
}
// Next returns a DCMotor for the next motor with the same device driver as
// the receiver.
func (m *DCMotor) Next() (*DCMotor, error) {
driver, err := DriverFor(m)
if err != nil {
return nil, err
}
id, err := deviceIDFor("", driver, (*DCMotor)(nil), m.id)
if id == -1 {
return nil, err
}
return &DCMotor{id: id}, err
}
// Driver returns the driver used by the DCMotor.
func (m *DCMotor) Driver() string {
return m.driver
}
// Commands returns the available commands for the DCMotor.
func (m *DCMotor) Commands() []string {
if m.commands == nil {
return nil
}
// Return a copy to prevent users
// changing the values under our feet.
avail := make([]string, len(m.commands))
copy(avail, m.commands)
return avail
}
// Command issues a command to the DCMotor.
func (m *DCMotor) Command(comm string) *DCMotor {
if m.err != nil {
return m
}
ok := false
for _, c := range m.commands {
if c == comm {
ok = true
break
}
}
if !ok {
m.err = newInvalidValueError(m, command, "", comm, m.Commands())
return m
}
m.err = setAttributeOf(m, command, comm)
return m
}
// DutyCycle returns the current duty cycle value for the DCMotor.
func (m *DCMotor) DutyCycle() (int, error) {
return intFrom(attributeOf(m, dutyCycle))
}
// DutyCycleSetpoint returns the current duty cycle setpoint value for the DCMotor.
func (m *DCMotor) DutyCycleSetpoint() (int, error) {
return intFrom(attributeOf(m, dutyCycleSetpoint))
}
// SetDutyCycleSetpoint sets the duty cycle setpoint value for the DCMotor
func (m *DCMotor) SetDutyCycleSetpoint(sp int) *DCMotor {
if m.err != nil {
return m
}
if sp < -100 || 100 < sp {
m.err = newValueOutOfRangeError(m, dutyCycleSetpoint, sp, -100, 100)
return m
}
m.err = setAttributeOf(m, dutyCycleSetpoint, strconv.Itoa(sp))
return m
}
// Polarity returns the current polarity of the DCMotor.
func (m *DCMotor) Polarity() (Polarity, error) {
p, err := stringFrom(attributeOf(m, polarity))
return Polarity(p), err
}
// SetPolarity sets the polarity of the DCMotor
func (m *DCMotor) SetPolarity(p Polarity) *DCMotor {
if m.err != nil {
return m
}
if p != Normal && p != Inversed {
m.err = newInvalidValueError(m, polarity, "", string(p), []string{string(Normal), string(Inversed)})
return m
}
m.err = setAttributeOf(m, polarity, string(p))
return m
}
// RampUpSetpoint returns the current ramp up setpoint value for the DCMotor.
func (m *DCMotor) RampUpSetpoint() (time.Duration, error) {
return durationFrom(attributeOf(m, rampUpSetpoint))
}
// SetRampUpSetpoint sets the ramp up setpoint value for the DCMotor.
func (m *DCMotor) SetRampUpSetpoint(sp time.Duration) *DCMotor {
if m.err != nil {
return m
}
if sp < 0 || 10*time.Second < sp {
m.err = newDurationOutOfRangeError(m, rampUpSetpoint, sp, 0, 10*time.Second)
return m
}
m.err = setAttributeOf(m, rampUpSetpoint, strconv.Itoa(int(sp/time.Millisecond)))
return m
}
// RampDownSetpoint returns the current ramp down setpoint value for the DCMotor.
func (m *DCMotor) RampDownSetpoint() (time.Duration, error) {
return durationFrom(attributeOf(m, rampDownSetpoint))
}
// SetRampDownSetpoint sets the ramp down setpoint value for the DCMotor.
func (m *DCMotor) SetRampDownSetpoint(sp time.Duration) *DCMotor {
if m.err != nil {
return m
}
if sp < 0 || 10*time.Second < sp {
m.err = newDurationOutOfRangeError(m, rampDownSetpoint, sp, 0, 10*time.Second)
return m
}
m.err = setAttributeOf(m, rampDownSetpoint, strconv.Itoa(int(sp/time.Millisecond)))
return m
}
// State returns the current state of the DCMotor.
func (m *DCMotor) State() (MotorState, error) {
if m.err != nil {
return 0, m.Err()
}
return stateFrom(attributeOf(m, state))
}
// StopAction returns the stop action used when a stop command is issued
// to the DCMotor.
func (m *DCMotor) StopAction() (string, error) {
return stringFrom(attributeOf(m, stopAction))
}
// SetStopAction sets the stop action to be used when a stop command is
// issued to the DCMotor.
func (m *DCMotor) SetStopAction(action string) *DCMotor {
if m.err != nil {
return m
}
ok := false
for _, a := range m.stopActions {
if a == action {
ok = true
break
}
}
if !ok {
m.err = newInvalidValueError(m, stopAction, "", action, m.StopActions())
return m
}
m.err = setAttributeOf(m, stopAction, action)
return m
}
// StopActions returns the available stop actions for the DCMotor.
func (m *DCMotor) StopActions() []string {
if m.stopActions == nil {
return nil
}
// Return a copy to prevent users
// changing the values under our feet.
avail := make([]string, len(m.stopActions))
copy(avail, m.stopActions)
return avail
}
// TimeSetpoint returns the current time setpoint value for the DCMotor.
func (m *DCMotor) TimeSetpoint() (time.Duration, error) {
return durationFrom(attributeOf(m, timeSetpoint))
}
// SetTimeSetpoint sets the time setpoint value for the DCMotor.
func (m *DCMotor) SetTimeSetpoint(sp time.Duration) *DCMotor {
if m.err != nil {
return m
}
if sp < 0 {
m.err = newNegativeDurationError(m, timeSetpoint, sp)
return m
}
m.err = setAttributeOf(m, timeSetpoint, strconv.Itoa(int(sp/time.Millisecond)))
return m
}
// Uevent returns the current uevent state for the DCMotor.
func (m *DCMotor) Uevent() (map[string]string, error) {
return ueventFrom(attributeOf(m, uevent))
}