-
Notifications
You must be signed in to change notification settings - Fork 110
/
wrapper.go
195 lines (167 loc) · 5.53 KB
/
wrapper.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
// Package wrapper is a package that defines an implementation that wraps a partially implemented arm
package wrapper
import (
"context"
"sync"
"github.com/edaniels/golog"
pb "go.viam.com/api/component/arm/v1"
goutils "go.viam.com/utils"
"go.viam.com/rdk/components/arm"
"go.viam.com/rdk/motionplan"
"go.viam.com/rdk/operation"
"go.viam.com/rdk/referenceframe"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/spatialmath"
)
// Config is used for converting config attributes.
type Config struct {
ModelFilePath string `json:"model-path"`
ArmName string `json:"arm-name"`
}
var model = resource.DefaultModelFamily.WithModel("wrapper_arm")
// Validate ensures all parts of the config are valid.
func (cfg *Config) Validate(path string) ([]string, error) {
var deps []string
if cfg.ArmName == "" {
return nil, goutils.NewConfigValidationFieldRequiredError(path, "arm-name")
}
if _, err := referenceframe.ModelFromPath(cfg.ModelFilePath, ""); err != nil {
return nil, err
}
deps = append(deps, cfg.ArmName)
return deps, nil
}
func init() {
resource.RegisterComponent(arm.API, model, resource.Registration[arm.Arm, *Config]{
Constructor: NewWrapperArm,
})
}
// Arm wraps a partial implementation of another arm.
type Arm struct {
resource.Named
resource.TriviallyCloseable
logger golog.Logger
opMgr operation.SingleOperationManager
mu sync.RWMutex
model referenceframe.Model
actual arm.Arm
}
// NewWrapperArm returns a wrapper component for another arm.
func NewWrapperArm(ctx context.Context, deps resource.Dependencies, conf resource.Config, logger golog.Logger) (arm.Arm, error) {
a := &Arm{
Named: conf.ResourceName().AsNamed(),
logger: logger,
}
if err := a.Reconfigure(ctx, deps, conf); err != nil {
return nil, err
}
return a, nil
}
// Reconfigure atomically reconfigures this arm in place based on the new config.
func (wrapper *Arm) Reconfigure(ctx context.Context, deps resource.Dependencies, conf resource.Config) error {
newConf, err := resource.NativeConfig[*Config](conf)
if err != nil {
return err
}
model, err := referenceframe.ModelFromPath(newConf.ModelFilePath, conf.Name)
if err != nil {
return err
}
newArm, err := arm.FromDependencies(deps, newConf.ArmName)
if err != nil {
return err
}
wrapper.mu.Lock()
wrapper.model = model
wrapper.actual = newArm
wrapper.mu.Unlock()
return nil
}
// ModelFrame returns the dynamic frame of the model.
func (wrapper *Arm) ModelFrame() referenceframe.Model {
wrapper.mu.RLock()
defer wrapper.mu.RUnlock()
return wrapper.model
}
// EndPosition returns the set position.
func (wrapper *Arm) EndPosition(ctx context.Context, extra map[string]interface{}) (spatialmath.Pose, error) {
wrapper.mu.RLock()
defer wrapper.mu.RUnlock()
joints, err := wrapper.JointPositions(ctx, extra)
if err != nil {
return nil, err
}
return motionplan.ComputeOOBPosition(wrapper.model, joints)
}
// MoveToPosition sets the position.
func (wrapper *Arm) MoveToPosition(ctx context.Context, pos spatialmath.Pose, extra map[string]interface{}) error {
ctx, done := wrapper.opMgr.New(ctx)
defer done()
return arm.Move(ctx, wrapper.logger, wrapper, pos)
}
// MoveToJointPositions sets the joints.
func (wrapper *Arm) MoveToJointPositions(ctx context.Context, joints *pb.JointPositions, extra map[string]interface{}) error {
// check that joint positions are not out of bounds
if err := arm.CheckDesiredJointPositions(ctx, wrapper, joints); err != nil {
return err
}
ctx, done := wrapper.opMgr.New(ctx)
defer done()
wrapper.mu.RLock()
defer wrapper.mu.RUnlock()
return wrapper.actual.MoveToJointPositions(ctx, joints, extra)
}
// JointPositions returns the set joints.
func (wrapper *Arm) JointPositions(ctx context.Context, extra map[string]interface{}) (*pb.JointPositions, error) {
wrapper.mu.RLock()
defer wrapper.mu.RUnlock()
joints, err := wrapper.actual.JointPositions(ctx, extra)
if err != nil {
return nil, err
}
return joints, nil
}
// Stop stops the actual arm.
func (wrapper *Arm) Stop(ctx context.Context, extra map[string]interface{}) error {
ctx, done := wrapper.opMgr.New(ctx)
defer done()
wrapper.mu.RLock()
defer wrapper.mu.RUnlock()
return wrapper.actual.Stop(ctx, extra)
}
// IsMoving returns whether the arm is moving.
func (wrapper *Arm) IsMoving(ctx context.Context) (bool, error) {
return wrapper.opMgr.OpRunning(), nil
}
// CurrentInputs returns the current inputs of the arm.
func (wrapper *Arm) CurrentInputs(ctx context.Context) ([]referenceframe.Input, error) {
wrapper.mu.RLock()
defer wrapper.mu.RUnlock()
res, err := wrapper.actual.JointPositions(ctx, nil)
if err != nil {
return nil, err
}
return wrapper.model.InputFromProtobuf(res), nil
}
// GoToInputs moves the arm to the specified goal inputs.
func (wrapper *Arm) GoToInputs(ctx context.Context, goal []referenceframe.Input) error {
// check that joint positions are not out of bounds
positionDegs := wrapper.model.ProtobufFromInput(goal)
if err := arm.CheckDesiredJointPositions(ctx, wrapper, positionDegs); err != nil {
return err
}
return wrapper.MoveToJointPositions(ctx, positionDegs, nil)
}
// Geometries returns the list of geometries associated with the resource, in any order. The poses of the geometries reflect their
// current location relative to the frame of the resource.
func (wrapper *Arm) Geometries(ctx context.Context, extra map[string]interface{}) ([]spatialmath.Geometry, error) {
inputs, err := wrapper.CurrentInputs(ctx)
if err != nil {
return nil, err
}
gif, err := wrapper.model.Geometries(inputs)
if err != nil {
return nil, err
}
return gif.Geometries(), nil
}