forked from viamrobotics/rdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptgGridSim.go
211 lines (165 loc) · 5.53 KB
/
ptgGridSim.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
package tpspace
import (
"math"
"go.viam.com/rdk/spatialmath"
)
const (
defaultMaxTime = 15.
defaultDiffT = 0.005
defaultAlphaCnt uint = 121
defaultSearchRadius = 10.
defaultMaxHeadingChange = 1.95 * math.Pi
)
// ptgGridSim will take a PrecomputePTG, and simulate out a number of trajectories through some requested time/distance for speed of lookup
// later. It will store the trajectories in a grid data structure allowing relatively fast lookups.
type ptgGridSim struct {
refDist float64
alphaCnt uint
maxTime float64 // secs of robot execution to simulate
diffT float64 // discretize trajectory simulation to this time granularity
simPTG PrecomputePTG
precomputeTraj [][]*TrajNode
// Discretized x[y][]node maps for rapid NN lookups
trajNodeGrid map[int]map[int][]*TrajNode
searchRad float64 // Distance around a query point to search for precompute in the cached grid
}
// NewPTGGridSim creates a new PTG by simulating a PrecomputePTG for some distance, then cacheing the results in a grid for fast lookup.
func NewPTGGridSim(simPTG PrecomputePTG, arcs uint, simDist float64) (PTG, error) {
if arcs == 0 {
arcs = defaultAlphaCnt
}
ptg := &ptgGridSim{
refDist: simDist,
alphaCnt: arcs,
maxTime: defaultMaxTime,
diffT: defaultDiffT,
searchRad: defaultSearchRadius,
trajNodeGrid: map[int]map[int][]*TrajNode{},
}
ptg.simPTG = simPTG
precomp, err := ptg.simulateTrajectories(ptg.simPTG)
if err != nil {
return nil, err
}
ptg.precomputeTraj = precomp
return ptg, nil
}
func (ptg *ptgGridSim) CToTP(x, y float64) []*TrajNode {
nearbyNodes := []*TrajNode{}
// First, try to do a quick grid-based lookup
// TODO: an octree should be faster
for tx := int(math.Round(x - ptg.searchRad)); tx < int(math.Round(x+ptg.searchRad)); tx++ {
if ptg.trajNodeGrid[tx] != nil {
for ty := int(math.Round(y - ptg.searchRad)); ty < int(math.Round(y+ptg.searchRad)); ty++ {
nearbyNodes = append(nearbyNodes, ptg.trajNodeGrid[tx][ty]...)
}
}
}
if len(nearbyNodes) > 0 {
return nearbyNodes
}
// Try to find a closest point to the paths:
bestDist := math.Inf(1)
var bestNode *TrajNode
for k := 0; k < int(ptg.alphaCnt); k++ {
nMax := len(ptg.precomputeTraj[k]) - 1
for n := 0; n <= nMax; n++ {
distToPoint := math.Pow(ptg.precomputeTraj[k][n].ptX-x, 2) + math.Pow(ptg.precomputeTraj[k][n].ptY-y, 2)
if distToPoint < bestDist {
bestDist = distToPoint
bestNode = ptg.precomputeTraj[k][n]
}
}
}
if bestNode != nil {
return []*TrajNode{bestNode}
}
// Given a point (x,y), compute the "k_closest" whose extrapolation
// is closest to the point, and the associated "d_closest" distance,
// which can be normalized by "1/refDistance" to get TP-Space distances.
for k := 0; k < int(ptg.alphaCnt); k++ {
n := len(ptg.precomputeTraj[k]) - 1
distToPoint := math.Pow(ptg.precomputeTraj[k][n].Dist, 2) +
math.Pow(ptg.precomputeTraj[k][n].ptX-x, 2) + math.Pow(ptg.precomputeTraj[k][n].ptY-y, 2)
if distToPoint < bestDist {
bestDist = distToPoint
bestNode = ptg.precomputeTraj[k][n]
}
}
return []*TrajNode{bestNode}
}
func (ptg *ptgGridSim) RefDistance() float64 {
return ptg.refDist
}
func (ptg *ptgGridSim) Trajectory(k uint) []*TrajNode {
if int(k) >= len(ptg.precomputeTraj) {
return nil
}
return ptg.precomputeTraj[k]
}
func (ptg *ptgGridSim) simulateTrajectories(simPtg PrecomputePTG) ([][]*TrajNode, error) {
xMin := 500.0
xMax := -500.0
yMin := 500.0
yMax := -500.0
// C-space path structure
allTraj := make([][]*TrajNode, 0, ptg.alphaCnt)
for k := uint(0); k < ptg.alphaCnt; k++ {
alpha := index2alpha(k, ptg.alphaCnt)
// Initialize trajectory with an all-zero node
alphaTraj := []*TrajNode{{Pose: spatialmath.NewZeroPose()}}
var err error
var w float64
var v float64
var x float64
var y float64
var phi float64
var t float64
var dist float64
// Last saved waypoints
accumulatedHeadingChange := 0.
lastVs := [2]float64{0, 0}
lastWs := [2]float64{0, 0}
// Step through each time point for this alpha
for t < ptg.maxTime && dist < ptg.refDist && accumulatedHeadingChange < defaultMaxHeadingChange {
v, w, err = simPtg.PTGVelocities(alpha, t, x, y, phi)
if err != nil {
return nil, err
}
lastVs[1] = lastVs[0]
lastWs[1] = lastWs[0]
lastVs[0] = v
lastWs[0] = w
// finite difference eq
x += math.Cos(phi) * v * ptg.diffT
y += math.Sin(phi) * v * ptg.diffT
phi += w * ptg.diffT
accumulatedHeadingChange += w * ptg.diffT
dist += v * ptg.diffT
t += ptg.diffT
// Update velocities of last node because reasons
alphaTraj[len(alphaTraj)-1].LinVelMMPS = v
alphaTraj[len(alphaTraj)-1].AngVelRPS = w
pose := xythetaToPose(x, y, phi)
alphaTraj = append(alphaTraj, &TrajNode{pose, t, dist, k, v, w, pose.Point().X, pose.Point().Y})
// For the grid!
xMin = math.Min(xMin, x)
xMax = math.Max(xMax, x)
yMin = math.Min(yMin, y)
yMax = math.Max(yMax, y)
}
// Add final node
alphaTraj[len(alphaTraj)-1].LinVelMMPS = v
alphaTraj[len(alphaTraj)-1].AngVelRPS = w
pose := xythetaToPose(x, y, phi)
tNode := &TrajNode{pose, t, dist, k, v, w, pose.Point().X, pose.Point().Y}
// Discretize into a grid for faster lookups later
if _, ok := ptg.trajNodeGrid[int(math.Round(x))]; !ok {
ptg.trajNodeGrid[int(math.Round(x))] = map[int][]*TrajNode{}
}
ptg.trajNodeGrid[int(math.Round(x))][int(math.Round(y))] = append(ptg.trajNodeGrid[int(math.Round(x))][int(math.Round(y))], tNode)
alphaTraj = append(alphaTraj, tNode)
allTraj = append(allTraj, alphaTraj)
}
return allTraj, nil
}