-
Notifications
You must be signed in to change notification settings - Fork 110
/
gripper.go
237 lines (200 loc) · 5.41 KB
/
gripper.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
// Package softrobotics implements the vacuum gripper from Soft Robotics.
package softrobotics
import (
"context"
"time"
"github.com/pkg/errors"
"go.uber.org/multierr"
"go.viam.com/utils"
"go.viam.com/rdk/components/board"
"go.viam.com/rdk/components/gripper"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/operation"
"go.viam.com/rdk/referenceframe"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/spatialmath"
)
var model = resource.DefaultModelFamily.WithModel("softrobotics")
// Config is the config for a trossen gripper.
type Config struct {
Board string `json:"board"`
Open string `json:"open"`
Close string `json:"close"`
Power string `json:"power"`
AnalogReader string `json:"analog_reader"`
}
// Validate ensures all parts of the config are valid.
func (cfg *Config) Validate(path string) ([]string, error) {
var deps []string
if cfg.Board == "" {
return nil, resource.NewConfigValidationFieldRequiredError(path, "board")
}
if cfg.Open == "" {
return nil, resource.NewConfigValidationFieldRequiredError(path, "open")
}
if cfg.Close == "" {
return nil, resource.NewConfigValidationFieldRequiredError(path, "close")
}
if cfg.Power == "" {
return nil, resource.NewConfigValidationFieldRequiredError(path, "power")
}
if cfg.AnalogReader != "psi" {
return nil, resource.NewConfigValidationError(path,
errors.Errorf("analog_reader %s on board must be created and called 'psi'", cfg.AnalogReader))
}
deps = append(deps, cfg.Board)
return deps, nil
}
func init() {
resource.RegisterComponent(gripper.API, model, resource.Registration[gripper.Gripper, *Config]{
Constructor: func(
ctx context.Context,
deps resource.Dependencies,
conf resource.Config,
logger logging.Logger,
) (gripper.Gripper, error) {
b, err := board.FromDependencies(deps, "local")
if err != nil {
return nil, err
}
return newGripper(b, conf, logger)
},
})
}
// softGripper TODO
//
// open is 5
// close is 6.
type softGripper struct {
resource.Named
resource.AlwaysRebuild
resource.TriviallyCloseable
theBoard board.Board
psi board.Analog
pinOpen, pinClose, pinPower board.GPIOPin
logger logging.Logger
opMgr *operation.SingleOperationManager
geometries []spatialmath.Geometry
}
// newGripper instantiates a new Gripper of softGripper type.
func newGripper(b board.Board, conf resource.Config, logger logging.Logger) (gripper.Gripper, error) {
newConf, err := resource.NativeConfig[*Config](conf)
if err != nil {
return nil, err
}
psi, err := b.AnalogByName("psi")
if err != nil {
return nil, err
}
pinOpen, err := b.GPIOPinByName(newConf.Open)
if err != nil {
return nil, err
}
pinClose, err := b.GPIOPinByName(newConf.Close)
if err != nil {
return nil, err
}
pinPower, err := b.GPIOPinByName(newConf.Power)
if err != nil {
return nil, err
}
theGripper := &softGripper{
Named: conf.ResourceName().AsNamed(),
theBoard: b,
psi: psi,
pinOpen: pinOpen,
pinClose: pinClose,
pinPower: pinPower,
logger: logger,
opMgr: operation.NewSingleOperationManager(),
}
if theGripper.psi == nil {
return nil, errors.New("no psi analog reader")
}
if conf.Frame != nil && conf.Frame.Geometry != nil {
geometry, err := conf.Frame.Geometry.ParseConfig()
if err != nil {
return nil, err
}
theGripper.geometries = []spatialmath.Geometry{geometry}
}
return theGripper, nil
}
// Stop TODO.
func (g *softGripper) Stop(ctx context.Context, extra map[string]interface{}) error {
ctx, done := g.opMgr.New(ctx)
defer done()
return multierr.Combine(
g.pinOpen.Set(ctx, false, nil),
g.pinClose.Set(ctx, false, nil),
g.pinPower.Set(ctx, false, nil),
)
}
// Open TODO.
func (g *softGripper) Open(ctx context.Context, extra map[string]interface{}) error {
ctx, done := g.opMgr.New(ctx)
defer done()
err := multierr.Combine(
g.pinOpen.Set(ctx, true, nil),
g.pinPower.Set(ctx, true, nil),
)
if err != nil {
return err
}
for {
if !utils.SelectContextOrWait(ctx, 10*time.Millisecond) {
return ctx.Err()
} // REMOVE
val, err := g.psi.Read(ctx, nil)
if err != nil {
return multierr.Combine(err, g.Stop(ctx, extra))
}
if val.Value > 500 {
break
}
if !utils.SelectContextOrWait(ctx, 10*time.Millisecond) {
return ctx.Err()
}
}
return g.Stop(ctx, extra)
}
// Grab TODO.
func (g *softGripper) Grab(ctx context.Context, extra map[string]interface{}) (bool, error) {
ctx, done := g.opMgr.New(ctx)
defer done()
err := multierr.Combine(
g.pinClose.Set(ctx, true, nil),
g.pinPower.Set(ctx, true, nil),
)
if err != nil {
return false, err
}
for {
if !utils.SelectContextOrWait(ctx, 100*time.Millisecond) {
return false, ctx.Err()
} // REMOVE
val, err := g.psi.Read(ctx, nil)
if err != nil {
return false, multierr.Combine(err, g.Stop(ctx, extra))
}
if val.Value <= 200 {
break
}
if !utils.SelectContextOrWait(ctx, 10*time.Millisecond) {
return false, ctx.Err()
}
}
return false, g.Stop(ctx, extra)
}
// IsMoving returns whether the gripper is moving.
func (g *softGripper) IsMoving(ctx context.Context) (bool, error) {
return g.opMgr.OpRunning(), nil
}
// ModelFrame is unimplemented for softGripper.
func (g *softGripper) ModelFrame() referenceframe.Model {
return nil
}
// Geometries returns the geometries associated with the softGripper.
func (g *softGripper) Geometries(ctx context.Context, extra map[string]interface{}) ([]spatialmath.Geometry, error) {
return g.geometries, nil
}