-
Notifications
You must be signed in to change notification settings - Fork 109
/
roboclaw.go
242 lines (203 loc) · 6.21 KB
/
roboclaw.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
// Package roboclaw is the driver for the roboclaw motor drivers
package roboclaw
import (
"context"
"errors"
"fmt"
"time"
"github.com/CPRT/roboclaw"
"github.com/edaniels/golog"
utils "go.viam.com/utils"
"go.viam.com/rdk/components/generic"
"go.viam.com/rdk/components/motor"
"go.viam.com/rdk/config"
"go.viam.com/rdk/operation"
"go.viam.com/rdk/registry"
rdkutils "go.viam.com/rdk/utils"
)
const modelname = "roboclaw"
// AttrConfig is used for converting motor config attributes.
type AttrConfig struct {
SerialPort string `json:"serial_port"`
Baud int `json:"baud_rate"`
Number int `json:"number_of_motors"` // this is 1 or 2
Address int `json:"address,omitempty"`
TicksPerRotation int `json:"ticks_per_rotation,omitempty"`
}
// Validate ensures all parts of the config are valid.
func (config *AttrConfig) Validate(path string) error {
if config.Number < 1 || config.Number > 2 {
return config.wrongNumberError()
}
if config.SerialPort == "" {
return utils.NewConfigValidationFieldRequiredError(path, "serial_port")
}
if config.Baud <= 0 {
return utils.NewConfigValidationFieldRequiredError(path, "baud")
}
return nil
}
func (config *AttrConfig) wrongNumberError() error {
return fmt.Errorf("roboclawConfig Number has to be 1 or 2, but is %d", config.Number)
}
func init() {
registry.RegisterComponent(
motor.Subtype,
modelname,
registry.Component{
Constructor: func(ctx context.Context, deps registry.Dependencies, config config.Component, logger golog.Logger) (interface{}, error) {
return newRoboClaw(deps, config, logger)
},
},
)
config.RegisterComponentAttributeMapConverter(
motor.SubtypeName,
modelname,
func(attributes config.AttributeMap) (interface{}, error) {
var conf AttrConfig
return config.TransformAttributeMapToStruct(&conf, attributes)
},
&AttrConfig{},
)
}
func getOrCreateConnection(deps registry.Dependencies, config *AttrConfig) (*roboclaw.Roboclaw, error) {
for _, res := range deps {
m, ok := rdkutils.UnwrapProxy(res).(*roboclawMotor)
if !ok {
continue
}
if m.conf.SerialPort != config.SerialPort {
continue
}
if m.conf.Baud != config.Baud {
return nil, errors.New("cannot have multiple roboclaw motors with different baud")
}
return m.conn, nil
}
c := &roboclaw.Config{Name: config.SerialPort, Retries: 3}
if config.Baud > 0 {
c.Baud = config.Baud
}
return roboclaw.Init(c)
}
func newRoboClaw(deps registry.Dependencies, config config.Component, logger golog.Logger) (motor.Motor, error) {
motorConfig, ok := config.ConvertedAttributes.(*AttrConfig)
if !ok {
return nil, rdkutils.NewUnexpectedTypeError(motorConfig, config.ConvertedAttributes)
}
if motorConfig.Number < 1 || motorConfig.Number > 2 {
return nil, motorConfig.wrongNumberError()
}
if motorConfig.Address == 0 {
motorConfig.Address = 128
}
if motorConfig.TicksPerRotation <= 0 {
motorConfig.TicksPerRotation = 1
}
c, err := getOrCreateConnection(deps, motorConfig)
if err != nil {
return nil, err
}
return &roboclawMotor{conn: c, conf: motorConfig, addr: uint8(motorConfig.Address), logger: logger}, nil
}
var _ = motor.LocalMotor(&roboclawMotor{})
type roboclawMotor struct {
conn *roboclaw.Roboclaw
conf *AttrConfig
addr uint8
logger golog.Logger
opMgr operation.SingleOperationManager
generic.Unimplemented
}
func (m *roboclawMotor) SetPower(ctx context.Context, powerPct float64, extra map[string]interface{}) error {
m.opMgr.CancelRunning(ctx)
switch m.conf.Number {
case 1:
return m.conn.DutyM1(m.addr, int16(powerPct*32767))
case 2:
return m.conn.DutyM2(m.addr, int16(powerPct*32767))
default:
return m.conf.wrongNumberError()
}
}
func (m *roboclawMotor) GoFor(ctx context.Context, rpm, revolutions float64, extra map[string]interface{}) error {
ctx, done := m.opMgr.New(ctx)
defer done()
ticks := uint32(revolutions * float64(m.conf.TicksPerRotation))
ticksPerSecond := int32((rpm * float64(m.conf.TicksPerRotation)) / 60)
var err error
switch m.conf.Number {
case 1:
err = m.conn.SpeedDistanceM1(m.addr, ticksPerSecond, ticks, true)
case 2:
err = m.conn.SpeedDistanceM2(m.addr, ticksPerSecond, ticks, true)
default:
return m.conf.wrongNumberError()
}
if err != nil {
return err
}
return m.opMgr.WaitTillNotPowered(ctx, time.Millisecond, m)
}
func (m *roboclawMotor) GoTo(ctx context.Context, rpm, positionRevolutions float64, extra map[string]interface{}) error {
pos, err := m.Position(ctx, extra)
if err != nil {
return err
}
return m.GoFor(ctx, rpm, positionRevolutions-pos, extra)
}
func (m *roboclawMotor) ResetZeroPosition(ctx context.Context, offset float64, extra map[string]interface{}) error {
newTicks := int32(offset * float64(m.conf.TicksPerRotation))
switch m.conf.Number {
case 1:
return m.conn.SetEncM1(m.addr, newTicks)
case 2:
return m.conn.SetEncM2(m.addr, newTicks)
default:
return m.conf.wrongNumberError()
}
}
func (m *roboclawMotor) Position(ctx context.Context, extra map[string]interface{}) (float64, error) {
var ticks uint32
var err error
switch m.conf.Number {
case 1:
ticks, _, err = m.conn.ReadEncM1(m.addr)
case 2:
ticks, _, err = m.conn.ReadEncM2(m.addr)
default:
return 0, m.conf.wrongNumberError()
}
if err != nil {
return 0, err
}
return float64(ticks) / float64(m.conf.TicksPerRotation), nil
}
func (m *roboclawMotor) Properties(ctx context.Context, extra map[string]interface{}) (map[motor.Feature]bool, error) {
return map[motor.Feature]bool{
motor.PositionReporting: true,
}, nil
}
func (m *roboclawMotor) Stop(ctx context.Context, extra map[string]interface{}) error {
return m.SetPower(ctx, 0, extra)
}
func (m *roboclawMotor) IsMoving(ctx context.Context) (bool, error) {
return m.IsPowered(ctx, nil)
}
func (m *roboclawMotor) IsPowered(ctx context.Context, extra map[string]interface{}) (bool, error) {
pow1, pow2, err := m.conn.ReadPWMs(m.addr)
if err != nil {
return false, err
}
switch m.conf.Number {
case 1:
return pow1 == 0, nil
case 2:
return pow2 == 0, nil
default:
return false, m.conf.wrongNumberError()
}
}
func (m *roboclawMotor) GoTillStop(ctx context.Context, rpm float64, stopFunc func(ctx context.Context) bool) error {
return motor.NewGoTillStopUnsupportedError("(name unavailable)")
}