-
Notifications
You must be signed in to change notification settings - Fork 110
/
plan.go
309 lines (277 loc) · 10.1 KB
/
plan.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
package motionplan
import (
"errors"
"fmt"
"math"
"github.com/golang/geo/r3"
geo "github.com/kellydunn/golang-geo"
pb "go.viam.com/api/service/motion/v1"
"go.viam.com/rdk/motionplan/ik"
"go.viam.com/rdk/referenceframe"
"go.viam.com/rdk/spatialmath"
)
// Plan is an interface that describes plans returned by this package. There are two key components to a Plan:
// Its Trajectory contains information pertaining to the commands required to actuate the robot to realize the Plan.
// Its Path contains information describing the Pose of the robot as it travels the Plan.
type Plan interface {
Trajectory() Trajectory
Path() Path
}
// RemainingPlan returns a new Plan equal to the given plan from the waypointIndex onwards.
func RemainingPlan(plan Plan, waypointIndex int) (Plan, error) {
if waypointIndex < 0 {
return nil, errors.New("could not access plan with negative waypoint index")
}
traj := plan.Trajectory()
if traj != nil && waypointIndex > len(traj) {
return nil, fmt.Errorf("could not access trajectory index %d, must be less than %d", waypointIndex, len(plan.Trajectory()))
}
path := plan.Path()
if path != nil && waypointIndex > len(path) {
return nil, fmt.Errorf("could not access path index %d, must be less than %d", waypointIndex, len(plan.Path()))
}
simplePlan := NewSimplePlan(path[waypointIndex:], traj[waypointIndex:])
if rrt, ok := plan.(*rrtPlan); ok {
return &rrtPlan{SimplePlan: *simplePlan, nodes: rrt.nodes[waypointIndex:]}, nil
}
return simplePlan, nil
}
// OffsetPlan returns a new Plan that is equivalent to the given Plan if its Path was offset by the given Pose.
// Does not modify Trajectory.
func OffsetPlan(plan Plan, offset spatialmath.Pose) Plan {
path := plan.Path()
if path == nil {
return NewSimplePlan(nil, plan.Trajectory())
}
newPath := make([]PathStep, 0, len(path))
for _, step := range path {
newStep := make(PathStep, len(step))
for frame, pose := range step {
newStep[frame] = referenceframe.NewPoseInFrame(pose.Parent(), spatialmath.Compose(offset, pose.Pose()))
}
newPath = append(newPath, newStep)
}
simplePlan := NewSimplePlan(newPath, plan.Trajectory())
if rrt, ok := plan.(*rrtPlan); ok {
return &rrtPlan{SimplePlan: *simplePlan, nodes: rrt.nodes}
}
return simplePlan
}
// Trajectory is a slice of maps describing a series of Inputs for a robot to travel to in the course of following a Plan.
// Each item in this slice maps a Frame's name (found by calling frame.Name()) to the Inputs that Frame should be modified by.
type Trajectory []map[string][]referenceframe.Input
// GetFrameInputs is a helper function which will extract the waypoints of a single frame from the map output of a trajectory.
func (traj Trajectory) GetFrameInputs(frameName string) ([][]referenceframe.Input, error) {
solution := make([][]referenceframe.Input, 0, len(traj))
for _, step := range traj {
frameStep, ok := step[frameName]
if !ok {
return nil, fmt.Errorf("frame named %s not found in trajectory", frameName)
}
solution = append(solution, frameStep)
}
return solution, nil
}
// String returns a human-readable version of the trajectory, suitable for debugging.
func (traj Trajectory) String() string {
var str string
for _, step := range traj {
str += "\n"
for frame, input := range step {
if len(input) > 0 {
str += fmt.Sprintf("%s: %v\t", frame, input)
}
}
}
return str
}
// EvaluateCost calculates a cost to a trajectory as measured by the given distFunc Metric.
func (traj Trajectory) EvaluateCost(distFunc ik.SegmentMetric) float64 {
var totalCost float64
last := map[string][]referenceframe.Input{}
for _, step := range traj {
for frame, inputs := range step {
if len(inputs) > 0 {
if lastInputs, ok := last[frame]; ok {
cost := distFunc(&ik.Segment{StartConfiguration: lastInputs, EndConfiguration: inputs})
totalCost += cost
}
last[frame] = inputs
}
}
}
return totalCost
}
// Path is a slice of PathSteps describing a series of Poses for a robot to travel to in the course of following a Plan.
// The pose of the PathStep is the pose at the end of the corresponding set of inputs in the Trajectory.
type Path []PathStep
func newPath(solution []node, sf *solverFrame) (Path, error) {
path := make(Path, 0, len(solution))
for _, step := range solution {
inputMap := sf.sliceToMap(step.Q())
poseMap := make(map[string]*referenceframe.PoseInFrame)
for frame := range inputMap {
tf, err := sf.fss.Transform(inputMap, referenceframe.NewPoseInFrame(frame, spatialmath.NewZeroPose()), referenceframe.World)
if err != nil {
return nil, err
}
pose, ok := tf.(*referenceframe.PoseInFrame)
if !ok {
return nil, errors.New("pose not transformable")
}
poseMap[frame] = pose
}
path = append(path, poseMap)
}
return path, nil
}
func newPathFromRelativePath(path Path) (Path, error) {
if len(path) < 2 {
return nil, errors.New("need to have at least 2 elements in path")
}
newPath := make([]PathStep, 0, len(path))
newPath = append(newPath, path[0])
for i, step := range path[1:] {
newStep := make(PathStep, len(step))
for frame, pose := range step {
lastPose := newPath[i][frame].Pose()
newStep[frame] = referenceframe.NewPoseInFrame(referenceframe.World, spatialmath.Compose(lastPose, pose.Pose()))
}
newPath = append(newPath, newStep)
}
return newPath, nil
}
// GetFramePoses returns a slice of poses a given frame should visit in the course of the Path.
func (path Path) GetFramePoses(frameName string) ([]spatialmath.Pose, error) {
poses := []spatialmath.Pose{}
for _, step := range path {
poseInFrame, ok := step[frameName]
if !ok {
return nil, fmt.Errorf("frame named %s not found in path", frameName)
}
poses = append(poses, poseInFrame.Pose())
}
return poses, nil
}
func (path Path) String() string {
var str string
for _, step := range path {
str += "\n"
for frame, pose := range step {
str += fmt.Sprintf("%s: %v %v\t", frame, pose.Pose().Point(), pose.Pose().Orientation().OrientationVectorDegrees())
}
}
return str
}
// PathStep is a mapping of Frame names to PoseInFrames.
type PathStep map[string]*referenceframe.PoseInFrame
// ToProto converts a PathStep to its representation in protobuf.
func (ps PathStep) ToProto() *pb.PlanStep {
step := make(map[string]*pb.ComponentState)
for name, pose := range ps {
pbPose := spatialmath.PoseToProtobuf(pose.Pose())
step[name] = &pb.ComponentState{Pose: pbPose}
}
return &pb.PlanStep{Step: step}
}
// PathStepFromProto converts a *pb.PlanStep to a PlanStep.
func PathStepFromProto(ps *pb.PlanStep) (PathStep, error) {
if ps == nil {
return PathStep{}, errors.New("received nil *pb.PlanStep")
}
step := make(PathStep, len(ps.Step))
for k, v := range ps.Step {
step[k] = referenceframe.NewPoseInFrame(referenceframe.World, spatialmath.NewPoseFromProtobuf(v.Pose))
}
return step, nil
}
// NewGeoPlan returns a Plan containing a Path with GPS coordinates smuggled into the Pose struct. Each GPS point is created using:
// A Point with X as the longitude and Y as the latitude
// An orientation using the heading as the theta in an OrientationVector with Z=1.
func NewGeoPlan(plan Plan, pt *geo.Point) Plan {
newPath := make([]PathStep, 0, len(plan.Path()))
for _, step := range plan.Path() {
newStep := make(PathStep)
for frame, pif := range step {
pose := pif.Pose()
geoPose := spatialmath.PoseToGeoPose(spatialmath.NewGeoPose(pt, 0), pose)
heading := math.Mod(math.Abs(geoPose.Heading()-360), 360)
o := &spatialmath.OrientationVectorDegrees{OZ: 1, Theta: heading}
smuggledGeoPose := spatialmath.NewPose(r3.Vector{X: geoPose.Location().Lng(), Y: geoPose.Location().Lat()}, o)
newStep[frame] = referenceframe.NewPoseInFrame(pif.Parent(), smuggledGeoPose)
}
newPath = append(newPath, newStep)
}
return NewSimplePlan(newPath, plan.Trajectory())
}
// SimplePlan is a struct containing a Path and a Trajectory, together these comprise a Plan.
type SimplePlan struct {
path Path
traj Trajectory
}
// NewSimplePlan instantiates a new Plan from a Path and Trajectory.
func NewSimplePlan(path Path, traj Trajectory) *SimplePlan {
if path == nil {
path = Path{}
}
if traj == nil {
traj = Trajectory{}
}
return &SimplePlan{path: path, traj: traj}
}
// Path returns the Path associated with the Plan.
func (plan *SimplePlan) Path() Path {
return plan.path
}
// Trajectory returns the Trajectory associated with the Plan.
func (plan *SimplePlan) Trajectory() Trajectory {
return plan.traj
}
// ExecutionState describes a plan and a particular state along it.
type ExecutionState struct {
plan Plan
index int
// The current inputs of input-enabled elements described by the plan
currentInputs map[string][]referenceframe.Input
// The current PoseInFrames of input-enabled elements described by this plan.
currentPose map[string]*referenceframe.PoseInFrame
}
// NewExecutionState will construct an ExecutionState struct.
func NewExecutionState(
plan Plan,
index int,
currentInputs map[string][]referenceframe.Input,
currentPose map[string]*referenceframe.PoseInFrame,
) (ExecutionState, error) {
if plan == nil {
return ExecutionState{}, errors.New("cannot create new ExecutionState with nil plan")
}
if currentInputs == nil {
return ExecutionState{}, errors.New("cannot create new ExecutionState with nil currentInputs")
}
if currentPose == nil {
return ExecutionState{}, errors.New("cannot create new ExecutionState with nil currentPose")
}
return ExecutionState{
plan: plan,
index: index,
currentInputs: currentInputs,
currentPose: currentPose,
}, nil
}
// Plan returns the plan associated with the execution state.
func (e *ExecutionState) Plan() Plan {
return e.plan
}
// Index returns the currently-executing index of the execution state's Plan.
func (e *ExecutionState) Index() int {
return e.index
}
// CurrentInputs returns the current inputs of the components associated with the ExecutionState.
func (e *ExecutionState) CurrentInputs() map[string][]referenceframe.Input {
return e.currentInputs
}
// CurrentPoses returns the current poses in frame of the components associated with the ExecutionState.
func (e *ExecutionState) CurrentPoses() map[string]*referenceframe.PoseInFrame {
return e.currentPose
}