-
Notifications
You must be signed in to change notification settings - Fork 110
/
ams_as5048.go
346 lines (309 loc) · 9.43 KB
/
ams_as5048.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//go:build linux
// Package ams implements the AMS_AS5048 encoder
package ams
import (
"context"
"fmt"
"math"
"sync"
"time"
"github.com/pkg/errors"
"go.viam.com/utils"
"go.viam.com/rdk/components/board/genericlinux/buses"
"go.viam.com/rdk/components/encoder"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/resource"
)
const (
i2cConn = "i2c"
transitionEpsilon = 90
)
var (
model = resource.DefaultModelFamily.WithModel("AMS-AS5048")
scalingFactor = 360.0 / math.Pow(2, 14)
supportedConnections = utils.NewStringSet(i2cConn)
)
// the wait time necessary to operate the position updating
// loop at 50 Hz.
var waitTimeNano = (1.0 / 50.0) * 1000000000.0
func init() {
resource.RegisterComponent(
encoder.API,
model,
resource.Registration[encoder.Encoder, *Config]{
Constructor: newAS5048Encoder,
},
)
}
// Config contains the connection information for
// configuring an AS5048 encoder.
type Config struct {
// We include connection type here in anticipation for
// future SPI support
ConnectionType string `json:"connection_type"`
*I2CConfig `json:"i2c_attributes,omitempty"`
}
// Validate checks the attributes of an initialized config
// for proper values.
func (conf *Config) Validate(path string) ([]string, error) {
var deps []string
connType := conf.ConnectionType
if len(connType) == 0 {
// TODO: stop defaulting to I2C when SPI support is implemented
conf.ConnectionType = i2cConn
// return nil, errors.New("must specify connection type")
}
_, isSupported := supportedConnections[connType]
if !isSupported {
return nil, errors.Errorf("%s is not a supported connection type", connType)
}
if connType == i2cConn {
if conf.I2CConfig == nil {
return nil, errors.New("i2c selected as connection type, but no attributes supplied")
}
err := conf.I2CConfig.ValidateI2C(path)
if err != nil {
return nil, err
}
}
return deps, nil
}
// I2CConfig stores the configuration information for I2C connection.
type I2CConfig struct {
I2CBus string `json:"i2c_bus"`
I2CAddr int `json:"i2c_addr"`
}
// ValidateI2C ensures all parts of the config are valid.
func (cfg *I2CConfig) ValidateI2C(path string) error {
if cfg.I2CBus == "" {
return resource.NewConfigValidationFieldRequiredError(path, "i2c_bus")
}
if cfg.I2CAddr == 0 {
return resource.NewConfigValidationFieldRequiredError(path, "i2c_addr")
}
return nil
}
// Encoder is a struct representing an instance of a hardware unit
// in AMS's AS5048 series of Hall-effect encoders.
type Encoder struct {
resource.Named
mu sync.RWMutex
logger logging.Logger
position float64
positionOffset float64
rotations int
positionType encoder.PositionType
i2cBus buses.I2C
i2cAddr byte
i2cBusName string // This is nessesary to check whether we need to create a new i2cBus during reconfigure.
cancelCtx context.Context
cancel context.CancelFunc
activeBackgroundWorkers sync.WaitGroup
}
func newAS5048Encoder(
ctx context.Context,
deps resource.Dependencies,
conf resource.Config,
logger logging.Logger,
) (encoder.Encoder, error) {
return makeAS5048Encoder(ctx, deps, conf, logger, nil)
}
// This function is separated to inject a mock i2c bus during tests.
func makeAS5048Encoder(
ctx context.Context,
deps resource.Dependencies,
conf resource.Config,
logger logging.Logger,
bus buses.I2C,
) (encoder.Encoder, error) {
cfg, err := resource.NativeConfig[*Config](conf)
if err != nil {
return nil, err
}
cancelCtx, cancel := context.WithCancel(context.Background())
res := &Encoder{
Named: conf.ResourceName().AsNamed(),
cancelCtx: cancelCtx,
cancel: cancel,
logger: logger,
positionType: encoder.PositionTypeTicks,
i2cBus: bus,
i2cBusName: cfg.I2CBus,
}
if err := res.Reconfigure(ctx, deps, conf); err != nil {
return nil, err
}
if err := res.startPositionLoop(ctx); err != nil {
return nil, err
}
return res, nil
}
// Reconfigure reconfigures the encoder atomically.
func (enc *Encoder) Reconfigure(
ctx context.Context,
deps resource.Dependencies,
conf resource.Config,
) error {
newConf, err := resource.NativeConfig[*Config](conf)
if err != nil {
return err
}
enc.mu.Lock()
defer enc.mu.Unlock()
if enc.i2cBusName != newConf.I2CBus || enc.i2cBus == nil {
bus, err := buses.NewI2cBus(newConf.I2CBus)
if err != nil {
msg := fmt.Sprintf("can't find I2C bus '%q' for AMS encoder", newConf.I2CBus)
return errors.Wrap(err, msg)
}
enc.i2cBusName = newConf.I2CBus
enc.i2cBus = bus
}
if enc.i2cAddr != byte(newConf.I2CAddr) {
enc.i2cAddr = byte(newConf.I2CAddr)
}
return nil
}
func (enc *Encoder) startPositionLoop(ctx context.Context) error {
if err := enc.ResetPosition(ctx, map[string]interface{}{}); err != nil {
return err
}
enc.activeBackgroundWorkers.Add(1)
utils.ManagedGo(func() {
for {
if enc.cancelCtx.Err() != nil {
return
}
if err := enc.updatePosition(enc.cancelCtx); err != nil {
enc.logger.CErrorf(ctx,
"error in position loop (skipping update): %s", err.Error(),
)
}
time.Sleep(time.Duration(waitTimeNano))
}
}, enc.activeBackgroundWorkers.Done)
return nil
}
func (enc *Encoder) readPosition(ctx context.Context) (float64, error) {
i2cHandle, err := enc.i2cBus.OpenHandle(enc.i2cAddr)
if err != nil {
return 0, err
}
defer utils.UncheckedErrorFunc(i2cHandle.Close)
// retrieve the 8 most significant bits of the 14-bit resolution
// position
msB, err := i2cHandle.ReadByteData(ctx, byte(0xFE))
if err != nil {
return 0, err
}
// retrieve the 6 least significant bits of as a byte (where
// the front two bits are irrelevant)
lsB, err := i2cHandle.ReadByteData(ctx, byte(0xFF))
if err != nil {
return 0, err
}
return convertBytesToAngle(msB, lsB), nil
}
func convertBytesToAngle(msB, lsB byte) float64 {
// obtain the 14-bit resolution position, which represents a
// portion of a full rotation. We then scale appropriately
// by (360 / 2^14) to get degrees
byteData := (int(msB) << 6) | int(lsB)
return (float64(byteData) * scalingFactor)
}
func (enc *Encoder) updatePosition(ctx context.Context) error {
enc.mu.Lock()
defer enc.mu.Unlock()
angleDeg, err := enc.readPosition(ctx)
if err != nil {
return err
}
angleDeg += enc.positionOffset
// in order to keep track of multiple rotations, we increment / decrement
// a rotations counter whenever two subsequent positions are on either side
// of 0 (or 360) within a window of 2 * transitionEpsilon
forwardsTransition := (angleDeg <= transitionEpsilon) && ((360.0 - enc.position) <= transitionEpsilon)
backwardsTransition := (enc.position <= transitionEpsilon) && ((360.0 - angleDeg) <= transitionEpsilon)
if forwardsTransition {
enc.rotations++
} else if backwardsTransition {
enc.rotations--
}
enc.position = angleDeg
return nil
}
// Position returns the total number of rotations detected
// by the encoder (rather than a number of pulse state transitions)
// because this encoder is absolute and not incremental. As a result
// a user MUST set ticks_per_rotation on the config of the corresponding
// motor to 1. Any other value will result in completely incorrect
// position measurements by the motor.
func (enc *Encoder) Position(
ctx context.Context, positionType encoder.PositionType, extra map[string]interface{},
) (float64, encoder.PositionType, error) {
enc.mu.RLock()
defer enc.mu.RUnlock()
if positionType == encoder.PositionTypeDegrees {
enc.positionType = encoder.PositionTypeDegrees
return enc.position, enc.positionType, nil
}
ticks := float64(enc.rotations) + enc.position/360.0
enc.positionType = encoder.PositionTypeTicks
return ticks, enc.positionType, nil
}
// ResetPosition sets the current position measured by the encoder to be
// considered its new zero position.
func (enc *Encoder) ResetPosition(
ctx context.Context, extra map[string]interface{},
) error {
enc.mu.Lock()
defer enc.mu.Unlock()
// NOTE (GV): potential improvement could be writing the offset position
// to the zero register of the encoder rather than keeping track
// on the struct
enc.position = 0
enc.rotations = 0
i2cHandle, err := enc.i2cBus.OpenHandle(enc.i2cAddr)
if err != nil {
return err
}
defer utils.UncheckedErrorFunc(i2cHandle.Close)
// clear current zero position
if err := i2cHandle.WriteByteData(ctx, byte(0x16), byte(0)); err != nil {
return err
}
if err := i2cHandle.WriteByteData(ctx, byte(0x17), byte(0)); err != nil {
return err
}
// read current position
currentMSB, err := i2cHandle.ReadByteData(ctx, byte(0xFE))
if err != nil {
return err
}
currentLSB, err := i2cHandle.ReadByteData(ctx, byte(0xFF))
if err != nil {
return err
}
// write current position to zero register
if err := i2cHandle.WriteByteData(ctx, byte(0x16), currentMSB); err != nil {
return err
}
if err := i2cHandle.WriteByteData(ctx, byte(0x17), currentLSB); err != nil {
return err
}
return nil
}
// Properties returns a list of all the position types that are supported by a given encoder.
func (enc *Encoder) Properties(ctx context.Context, extra map[string]interface{}) (encoder.Properties, error) {
return encoder.Properties{
TicksCountSupported: true,
AngleDegreesSupported: true,
}, nil
}
// Close stops the position loop of the encoder when the component
// is closed.
func (enc *Encoder) Close(ctx context.Context) error {
enc.cancel()
enc.activeBackgroundWorkers.Wait()
return nil
}