-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
servo_motor.go
251 lines (221 loc) · 6.63 KB
/
servo_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
// 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 = (*ServoMotor)(nil)
// ServoMotor represents a handle to a servo-motor.
type ServoMotor struct {
id int
// Cached value:
driver string
err error
}
// Path returns the servo-motor sysfs path.
func (*ServoMotor) Path() string { return filepath.Join(prefix, ServoMotorPath) }
// Type returns "motor".
func (*ServoMotor) Type() string { return motorPrefix }
// String satisfies the fmt.Stringer interface.
func (m *ServoMotor) String() string {
if m == nil {
return motorPrefix + "*"
}
return motorPrefix + strconv.Itoa(m.id)
}
// Err returns the error state of the ServoMotor and clears it.
func (m *ServoMotor) Err() error {
err := m.err
m.err = nil
return err
}
// idInt and setID satisfy the idSetter interface.
func (m *ServoMotor) setID(id int) error {
t := ServoMotor{id: id}
var err error
t.driver, err = DriverFor(&t)
if err != nil {
*m = ServoMotor{id: -1}
return err
}
*m = t
return nil
}
func (m *ServoMotor) idInt() int {
if m == nil {
return -1
}
return m.id
}
// ServoMotorFor returns a ServoMotor for the given ev3 port name and driver.
// If the motor driver does not match the driver string, a ServoMotor for the port
// is returned with a DriverMismatch error.
// If port is empty, the first servo-motor satisfying the driver name is returned.
func ServoMotorFor(port, driver string) (*ServoMotor, error) {
id, err := deviceIDFor(port, driver, (*ServoMotor)(nil), -1)
if id == -1 {
return nil, err
}
var m ServoMotor
_err := m.setID(id)
if _err != nil {
err = _err
}
return &m, err
}
// Next returns a ServoMotor for the next motor with the same device driver as
// the receiver.
func (m *ServoMotor) Next() (*ServoMotor, error) {
driver, err := DriverFor(m)
if err != nil {
return nil, err
}
id, err := deviceIDFor("", driver, (*ServoMotor)(nil), m.id)
if id == -1 {
return nil, err
}
return &ServoMotor{id: id}, err
}
// Driver returns the driver used by the ServoMotor.
func (p *ServoMotor) Driver() string {
return p.driver
}
// Commands returns the available commands for the ServoMotor.
func (m *ServoMotor) Commands() []string {
return []string{
"run",
"float",
}
}
// Command issues a command to the ServoMotor.
func (m *ServoMotor) Command(comm string) *ServoMotor {
if m.err != nil {
return m
}
avail := m.Commands()
ok := false
for _, c := range avail {
if c == comm {
ok = true
break
}
}
if !ok {
m.err = newInvalidValueError(m, command, "", comm, avail)
return m
}
m.err = setAttributeOf(m, command, comm)
return m
}
// MaxPulseSetpoint returns the current max pulse setpoint value for the ServoMotor.
func (m *ServoMotor) MaxPulseSetpoint() (time.Duration, error) {
return durationFrom(attributeOf(m, maxPulseSetpoint))
}
// SetMaxPulseSetpoint sets the max pulse setpoint value for the ServoMotor
func (m *ServoMotor) SetMaxPulseSetpoint(sp time.Duration) *ServoMotor {
if m.err != nil {
return m
}
if sp < 2300*time.Millisecond || 2700*time.Millisecond < sp {
m.err = newDurationOutOfRangeError(m, maxPulseSetpoint, sp, 2300*time.Millisecond, 2700*time.Millisecond)
return m
}
m.err = setAttributeOf(m, maxPulseSetpoint, strconv.Itoa(int(sp/time.Millisecond)))
return m
}
// MidPulseSetpoint returns the current mid pulse setpoint value for the ServoMotor.
func (m *ServoMotor) MidPulseSetpoint() (time.Duration, error) {
return durationFrom(attributeOf(m, midPulseSetpoint))
}
// SetMidPulseSetpoint sets the mid pulse setpoint value for the ServoMotor
func (m *ServoMotor) SetMidPulseSetpoint(sp time.Duration) *ServoMotor {
if m.err != nil {
return m
}
if sp < 1300*time.Millisecond || 1700*time.Millisecond < sp {
m.err = newDurationOutOfRangeError(m, midPulseSetpoint, sp, 1300*time.Millisecond, 1700*time.Millisecond)
return m
}
m.err = setAttributeOf(m, midPulseSetpoint, strconv.Itoa(int(sp/time.Millisecond)))
return m
}
// MinPulseSetpoint returns the current min pulse setpoint value for the ServoMotor.
func (m *ServoMotor) MinPulseSetpoint() (time.Duration, error) {
return durationFrom(attributeOf(m, minPulseSetpoint))
}
// SetMinPulseSetpoint sets the min pulse setpoint value for the ServoMotor
func (m *ServoMotor) SetMinPulseSetpoint(sp time.Duration) *ServoMotor {
if m.err != nil {
return m
}
if sp < 300*time.Millisecond || 700*time.Millisecond < sp {
m.err = newDurationOutOfRangeError(m, minPulseSetpoint, sp, 300*time.Millisecond, 700*time.Millisecond)
return m
}
m.err = setAttributeOf(m, minPulseSetpoint, strconv.Itoa(int(sp/time.Millisecond)))
return m
}
// Polarity returns the current polarity of the ServoMotor.
func (m *ServoMotor) Polarity() (Polarity, error) {
p, err := stringFrom(attributeOf(m, polarity))
return Polarity(p), err
}
// SetPolarity sets the polarity of the ServoMotor
func (m *ServoMotor) SetPolarity(p Polarity) *ServoMotor {
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
}
// PositionSetpoint returns the current position setpoint value for
// the ServoMotor.
func (m *ServoMotor) PositionSetpoint() (int, error) {
return intFrom(attributeOf(m, positionSetpoint))
}
// SetPositionSetpoint sets the position value for the ServoMotor.
func (m *ServoMotor) SetPositionSetpoint(sp int) *ServoMotor {
if m.err != nil {
return m
}
if sp < -100 || 100 < sp {
m.err = newValueOutOfRangeError(m, positionSetpoint, sp, -100, 100)
return m
}
m.err = setAttributeOf(m, positionSetpoint, strconv.Itoa(sp))
return m
}
// RateSetpoint returns the current rate setpoint value for the ServoMotor.
func (m *ServoMotor) RateSetpoint() (time.Duration, error) {
return durationFrom(attributeOf(m, rateSetpoint))
}
// SetRateSetpoint sets the rate setpoint value for the ServoMotor.
func (m *ServoMotor) SetRateSetpoint(sp time.Duration) *ServoMotor {
if m.err != nil {
return m
}
if sp < 0 {
m.err = newNegativeDurationError(m, rateSetpoint, sp)
return m
}
m.err = setAttributeOf(m, rateSetpoint, strconv.Itoa(int(sp/time.Millisecond)))
return m
}
// State returns the current state of the ServoMotor.
func (m *ServoMotor) State() (MotorState, error) {
if m.err != nil {
return 0, m.Err()
}
return stateFrom(attributeOf(m, state))
}
// Uevent returns the current uevent state for the ServoMotor.
func (m *ServoMotor) Uevent() (map[string]string, error) {
return ueventFrom(attributeOf(m, uevent))
}