-
Notifications
You must be signed in to change notification settings - Fork 110
/
incremental_encoder.go
270 lines (231 loc) · 6.9 KB
/
incremental_encoder.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
package encoder
import (
"context"
"sync"
"sync/atomic"
"github.com/edaniels/golog"
"github.com/pkg/errors"
"go.viam.com/utils"
"go.viam.com/rdk/components/board"
"go.viam.com/rdk/components/generic"
"go.viam.com/rdk/config"
"go.viam.com/rdk/registry"
"go.viam.com/rdk/resource"
)
var incrModel = resource.NewDefaultModel("incremental")
func init() {
registry.RegisterComponent(
Subtype,
incrModel,
registry.Component{Constructor: func(
ctx context.Context,
deps registry.Dependencies,
config config.Component,
logger golog.Logger,
) (interface{}, error) {
return NewIncrementalEncoder(ctx, deps, config, logger)
}})
config.RegisterComponentAttributeMapConverter(
Subtype,
incrModel,
func(attributes config.AttributeMap) (interface{}, error) {
var conf IncrementalConfig
return config.TransformAttributeMapToStruct(&conf, attributes)
},
&IncrementalConfig{})
}
// IncrementalEncoder keeps track of a motor position using a rotary incremental encoder.
type IncrementalEncoder struct {
A, B board.DigitalInterrupt
position int64
pRaw int64
pState int64
logger golog.Logger
CancelCtx context.Context
cancelFunc func()
activeBackgroundWorkers sync.WaitGroup
generic.Unimplemented
}
// IncrementalPins describes the configuration of Pins for a quadrature encoder.
type IncrementalPins struct {
A string `json:"a"`
B string `json:"b"`
}
// IncrementalConfig describes the configuration of a quadrature encoder.
type IncrementalConfig struct {
Pins IncrementalPins `json:"pins"`
BoardName string `json:"board"`
}
// Validate ensures all parts of the config are valid.
func (config *IncrementalConfig) Validate(path string) ([]string, error) {
var deps []string
if config.Pins.A == "" {
return nil, errors.New("expected nonempty string for a")
}
if config.Pins.B == "" {
return nil, errors.New("expected nonempty string for b")
}
if len(config.BoardName) == 0 {
return nil, errors.New("expected nonempty board")
}
deps = append(deps, config.BoardName)
return deps, nil
}
// NewIncrementalEncoder creates a new IncrementalEncoder.
func NewIncrementalEncoder(
ctx context.Context,
deps registry.Dependencies,
cfg config.Component,
logger golog.Logger,
) (*IncrementalEncoder, error) {
cancelCtx, cancelFunc := context.WithCancel(ctx)
e := &IncrementalEncoder{logger: logger, CancelCtx: cancelCtx, cancelFunc: cancelFunc, position: 0, pRaw: 0, pState: 0}
if cfg, ok := cfg.ConvertedAttributes.(*IncrementalConfig); ok {
board, err := board.FromDependencies(deps, cfg.BoardName)
if err != nil {
return nil, err
}
e.A, ok = board.DigitalInterruptByName(cfg.Pins.A)
if !ok {
return nil, errors.Errorf("cannot find pin (%s) for incremental Encoder", cfg.Pins.A)
}
e.B, ok = board.DigitalInterruptByName(cfg.Pins.B)
if !ok {
return nil, errors.Errorf("cannot find pin (%s) for incremental Encoder", cfg.Pins.B)
}
e.Start(ctx)
return e, nil
}
return nil, errors.New("encoder config for incremental Encoder is not valid")
}
// Start starts the IncrementalEncoder background thread.
func (e *IncrementalEncoder) Start(ctx context.Context) {
/**
a rotary encoder looks like
picture from https://github.com/joan2937/pigpio/blob/master/EXAMPLES/C/ROTARY_ENCODER/rotary_encoder.c
1 2 3 4 1 2 3 4 1
+---------+ +---------+ 0
| | | |
A | | | |
| | | |
+---------+ +---------+ +----- 1
+---------+ +---------+ 0
| | | |
B | | | |
| | | |
----+ +---------+ +---------+ 1
*/
// State Transition Table
// +---------------+----+----+----+----+
// | pState/nState | 00 | 01 | 10 | 11 |
// +---------------+----+----+----+----+
// | 00 | 0 | -1 | +1 | x |
// +---------------+----+----+----+----+
// | 01 | +1 | 0 | x | -1 |
// +---------------+----+----+----+----+
// | 10 | -1 | x | 0 | +1 |
// +---------------+----+----+----+----+
// | 11 | x | +1 | -1 | 0 |
// +---------------+----+----+----+----+
// 0 -> same state
// x -> impossible state
chanA := make(chan board.Tick)
chanB := make(chan board.Tick)
e.A.AddCallback(chanA)
e.B.AddCallback(chanB)
aLevel, err := e.A.Value(ctx, nil)
if err != nil {
utils.Logger.Errorw("error reading a level", "error", err)
}
bLevel, err := e.B.Value(ctx, nil)
if err != nil {
utils.Logger.Errorw("error reading b level", "error", err)
}
e.pState = aLevel | (bLevel << 1)
e.activeBackgroundWorkers.Add(1)
utils.ManagedGo(func() {
defer e.A.RemoveCallback(chanA)
defer e.B.RemoveCallback(chanB)
for {
select {
case <-e.CancelCtx.Done():
return
default:
}
var tick board.Tick
select {
case <-e.CancelCtx.Done():
return
case tick = <-chanA:
aLevel = 0
if tick.High {
aLevel = 1
}
case tick = <-chanB:
bLevel = 0
if tick.High {
bLevel = 1
}
}
nState := aLevel | (bLevel << 1)
if e.pState == nState {
continue
}
switch (e.pState << 2) | nState {
case 0b0001:
fallthrough
case 0b0111:
fallthrough
case 0b1000:
fallthrough
case 0b1110:
e.dec()
atomic.StoreInt64(&e.position, atomic.LoadInt64(&e.pRaw)>>1)
e.pState = nState
case 0b0010:
fallthrough
case 0b0100:
fallthrough
case 0b1011:
fallthrough
case 0b1101:
e.inc()
atomic.StoreInt64(&e.position, atomic.LoadInt64(&e.pRaw)>>1)
e.pState = nState
}
}
}, e.activeBackgroundWorkers.Done)
}
// TicksCount returns number of ticks since last zeroing.
func (e *IncrementalEncoder) TicksCount(ctx context.Context, extra map[string]interface{}) (float64, error) {
res := atomic.LoadInt64(&e.position)
return float64(res), nil
}
// Reset sets the current position of the motor (adjusted by a given offset)
// to be its new zero position..
func (e *IncrementalEncoder) Reset(ctx context.Context, offset float64, extra map[string]interface{}) error {
if err := ValidateIntegerOffset(offset); err != nil {
return err
}
offsetInt := int64(offset)
atomic.StoreInt64(&e.position, offsetInt)
atomic.StoreInt64(&e.pRaw, (offsetInt<<1)|atomic.LoadInt64(&e.pRaw)&0x1)
return nil
}
// RawPosition returns the raw position of the encoder.
func (e *IncrementalEncoder) RawPosition() int64 {
return atomic.LoadInt64(&e.pRaw)
}
func (e *IncrementalEncoder) inc() {
atomic.AddInt64(&e.pRaw, 1)
}
func (e *IncrementalEncoder) dec() {
atomic.AddInt64(&e.pRaw, -1)
}
// Close shuts down the IncrementalEncoder.
func (e *IncrementalEncoder) Close() error {
e.logger.Debug("Closing incremental Encoder")
e.cancelFunc()
e.activeBackgroundWorkers.Wait()
return nil
}