-
Notifications
You must be signed in to change notification settings - Fork 14
/
ScrewTheoryIkProblem.cpp
271 lines (218 loc) · 8.25 KB
/
ScrewTheoryIkProblem.cpp
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
// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
#include "ScrewTheoryIkProblem.hpp"
#include <numeric> // std::accumulate
#include <vector>
using namespace roboticslab;
// -----------------------------------------------------------------------------
namespace
{
inline double getTheta(const KDL::JntArray & q, int i, bool reversed)
{
return reversed ? -q(q.rows() - 1 - i) : q(i);
}
struct solution_accumulator
{
int operator()(int count, const ScrewTheoryIkSubproblem * subproblem)
{
return count * subproblem->solutions();
}
}
solutionAccumulator;
inline int computeSolutions(const ScrewTheoryIkProblem::Steps & steps)
{
if (!steps.empty())
{
return std::accumulate(steps.begin(), steps.end(), 1, solutionAccumulator);
}
else
{
return 0;
}
}
}
// -----------------------------------------------------------------------------
ScrewTheoryIkProblem::ScrewTheoryIkProblem(const PoeExpression & _poe, const Steps & _steps, bool _reversed)
: poe(_poe),
steps(_steps),
reversed(_reversed),
soln(computeSolutions(steps))
{}
// -----------------------------------------------------------------------------
ScrewTheoryIkProblem::~ScrewTheoryIkProblem()
{
for (int i = 0; i < steps.size(); i++)
{
delete steps[i];
}
}
// -----------------------------------------------------------------------------
bool ScrewTheoryIkProblem::solve(const KDL::Frame & H_S_T, Solutions & solutions)
{
if (!solutions.empty())
{
solutions.clear();
}
// Reserve space in memory to avoid additional allocations on runtime.
solutions.reserve(soln);
solutions.emplace_back(poe.size());
PoeTerms poeTerms(poe.size(), EXP_UNKNOWN);
Frames rhsFrames;
rhsFrames.reserve(soln);
rhsFrames.push_back((reversed ? H_S_T.Inverse() : H_S_T) * poe.getTransform().Inverse());
bool firstIteration = true;
bool reachable = true;
for (int i = 0; i < steps.size(); i++)
{
if (!firstIteration)
{
// Re-compute right-hand side of PoE equation, i.e. prod(e_i) = H_S_T_q * H_S_T_0^(-1)
recalculateFrames(solutions, rhsFrames, poeTerms);
}
// Save this, the vector of solutions might be resized in the following loop, increase size
// on demand for improved efficiency (instead of allocating everything from the start).
int previousSize = solutions.size();
for (int j = 0; j < previousSize; j++)
{
// Apply known frames to the first characteristic point for each subproblem.
const KDL::Frame & H = transformPoint(solutions[j], poeTerms);
ScrewTheoryIkSubproblem::Solutions partialSolutions;
// Actually solve each subproblem, use current right-hand side of PoE to obtain
// the right-hand side of said subproblem.
reachable = reachable & steps[i]->solve(rhsFrames[j], H, partialSolutions);
// The global number of solutions is increased by this step.
if (partialSolutions.size() > 1)
{
// Noop if current size is not less than requested.
solutions.resize(previousSize * partialSolutions.size());
rhsFrames.resize(previousSize * partialSolutions.size());
for (int k = 1; k < partialSolutions.size(); k++)
{
// Replicate known solutions, these won't change further on.
solutions[j + previousSize * k] = solutions[j];
// Replicate right-hand side frames for the next iteration, these might change.
rhsFrames[j + previousSize * k] = rhsFrames[j];
}
}
// For each local solution of this subproblem...
for (int k = 0; k < partialSolutions.size(); k++)
{
const ScrewTheoryIkSubproblem::JointIdsToSolutions & jointIdsToSolutions = partialSolutions[k];
// For each joint-id-to-value pair of this local solution...
for (int l = 0; l < jointIdsToSolutions.size(); l++)
{
const ScrewTheoryIkSubproblem::JointIdToSolution & jointIdToSolution = jointIdsToSolutions[l];
auto [id, theta] = jointIdToSolution;
// Preserve mapping of ids (associated to `poe`).
poeTerms[id] = EXP_KNOWN;
if (reversed)
{
id = poe.size() - 1 - id;
theta = -theta;
}
// Store the final value in the desired index, don't shuffle it after this point.
solutions[j + previousSize * k](id) = theta;
}
}
}
firstIteration = false;
}
return reachable;
}
// -----------------------------------------------------------------------------
void ScrewTheoryIkProblem::recalculateFrames(const Solutions & solutions, Frames & frames, PoeTerms & poeTerms)
{
std::vector<KDL::Frame> pre, post;
// Leftmost known terms of the PoE.
if (recalculateFrames(solutions, pre, poeTerms, false))
{
for (int i = 0; i < solutions.size(); i++)
{
frames[i] = pre[i].Inverse() * frames[i];
}
}
// Rightmost known terms of the PoE.
if (recalculateFrames(solutions, post, poeTerms, true))
{
for (int i = 0; i < solutions.size(); i++)
{
frames[i] = frames[i] * post[i].Inverse();
}
}
}
// -----------------------------------------------------------------------------
bool ScrewTheoryIkProblem::recalculateFrames(const Solutions & solutions, Frames & frames, PoeTerms & poeTerms, bool backwards)
{
frames.resize(solutions.size());
bool hasMultipliedTerms = false;
for (int i = 0; i < poeTerms.size(); i++)
{
if (backwards)
{
i = poeTerms.size() - 1 - i;
}
if (poeTerms[i] == EXP_KNOWN)
{
for (int j = 0; j < solutions.size(); j++)
{
const MatrixExponential & exp = poe.exponentialAtJoint(i);
frames[j] = frames[j] * exp.asFrame(getTheta(solutions[j], i, reversed));
}
// Mark as 'computed' and include in right-hand side of PoE so that this
// term will be ignored in future iterations.
poeTerms[i] = EXP_COMPUTED;
hasMultipliedTerms = true;
}
else if (poeTerms[i] == EXP_COMPUTED)
{
continue;
}
else
{
// We hit an unknown term, quit this loop.
break;
}
}
return hasMultipliedTerms;
}
// -----------------------------------------------------------------------------
KDL::Frame ScrewTheoryIkProblem::transformPoint(const KDL::JntArray & jointValues, const PoeTerms & poeTerms)
{
KDL::Frame H;
bool foundKnown = false;
bool foundUnknown = false;
for (int i = poeTerms.size() - 1; i >= 0; i--)
{
if (poeTerms[i] == EXP_KNOWN)
{
const MatrixExponential & exp = poe.exponentialAtJoint(i);
H = exp.asFrame(getTheta(jointValues, i, reversed)) * H;
foundKnown = true;
}
else if (poeTerms[i] == EXP_UNKNOWN)
{
foundUnknown = true;
if (foundKnown)
{
// Already applied at least one transformation, can't proceed further.
break;
}
}
else if (poeTerms[i] == EXP_COMPUTED)
{
if (foundKnown || foundUnknown)
{
// Only skip this if we have a sequence of aldeady-computed terms in the
// rightmost end of the PoE.
break;
}
}
}
return H;
}
// -----------------------------------------------------------------------------
ScrewTheoryIkProblem * ScrewTheoryIkProblem::create(const PoeExpression & poe, const Steps & steps, bool reversed)
{
// TODO: validate
return new ScrewTheoryIkProblem(poe, steps, reversed);
}
// -----------------------------------------------------------------------------