-
Notifications
You must be signed in to change notification settings - Fork 217
/
OperationalModel.cpp
340 lines (302 loc) · 9.55 KB
/
OperationalModel.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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//
// Copyright (c) 2009, Markus Rickert
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#include <QStatusBar>
#include <rl/math/Constants.h>
#include <rl/math/Rotation.h>
#include <rl/mdl/Exception.h>
#include <rl/mdl/Kinematic.h>
#include <rl/mdl/JacobianInverseKinematics.h>
#include <rl/sg/Body.h>
#ifdef RL_MDL_NLOPT
#include <rl/mdl/NloptInverseKinematics.h>
#endif
#include "ConfigurationModel.h"
#include "OperationalModel.h"
#include "MainWindow.h"
OperationalModel::OperationalModel(QObject* parent) :
QAbstractTableModel(parent),
id(0)
{
}
OperationalModel::~OperationalModel()
{
}
int
OperationalModel::columnCount(const QModelIndex& parent) const
{
return 6;
}
void
OperationalModel::configurationChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
{
this->beginResetModel();
this->endResetModel();
}
QVariant
OperationalModel::data(const QModelIndex& index, int role) const
{
if (nullptr == MainWindow::instance()->kinematicModels[this->id])
{
return QVariant();
}
if (!index.isValid())
{
return QVariant();
}
const rl::math::Transform& transform = MainWindow::instance()->kinematicModels[this->id]->getOperationalPosition(index.row());
rl::math::Transform::ConstTranslationPart position = transform.translation();
rl::math::Vector3 orientation = transform.rotation().eulerAngles(2, 1, 0).reverse();
switch (role)
{
case Qt::CheckStateRole:
switch (index.column())
{
case 0:
return MainWindow::instance()->operationalGoals[this->id][index.row()] ? Qt::Checked : Qt::Unchecked;
break;
default:
break;
}
break;
case Qt::DisplayRole:
switch (index.column())
{
case 0:
case 1:
case 2:
return QString::number(position(index.column()), 'f', 4) + QString(" m");
break;
case 3:
case 4:
case 5:
return QString::number(orientation(index.column() - 3) * rl::math::constants::rad2deg, 'f', 2) + QChar(176);
break;
default:
break;
}
break;
case Qt::EditRole:
switch (index.column())
{
case 0:
case 1:
case 2:
return position(index.column());
break;
case 3:
case 4:
case 5:
return orientation(index.column() - 3) * rl::math::constants::rad2deg;
break;
default:
break;
}
break;
case Qt::TextAlignmentRole:
return QVariant(Qt::AlignRight | Qt::AlignVCenter);
break;
default:
break;
}
return QVariant();
}
Qt::ItemFlags
OperationalModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
{
return Qt::NoItemFlags;
}
if (0 == index.column())
{
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsUserCheckable;
}
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}
QVariant
OperationalModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (nullptr == MainWindow::instance()->kinematicModels[this->id])
{
return QVariant();
}
if (Qt::DisplayRole == role && Qt::Horizontal == orientation)
{
switch (section)
{
case 0:
return "x";
break;
case 1:
return "y";
break;
case 2:
return "z";
break;
case 3:
return "a";
break;
case 4:
return "b";
break;
case 5:
return "c";
break;
default:
break;
}
}
if (Qt::DisplayRole == role && Qt::Vertical == orientation)
{
return QString::fromStdString(MainWindow::instance()->kinematicModels[this->id]->getOperationalFrame(section)->getName());
}
return QVariant();
}
int
OperationalModel::rowCount(const QModelIndex& parent) const
{
if (nullptr == MainWindow::instance()->kinematicModels[this->id])
{
return 0;
}
return MainWindow::instance()->kinematicModels[this->id]->getOperationalDof();
}
bool
OperationalModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
if (nullptr == MainWindow::instance()->kinematicModels[this->id])
{
return false;
}
if (!index.isValid())
{
return false;
}
if (Qt::CheckStateRole == role)
{
if (0 == index.column())
{
MainWindow::instance()->operationalGoals[this->id][index.row()] = value.value<bool>();
}
}
else if (Qt::EditRole == role)
{
if (rl::mdl::Kinematic* kinematic = dynamic_cast<rl::mdl::Kinematic*>(MainWindow::instance()->kinematicModels[this->id].get()))
{
rl::math::Transform transform = kinematic->getOperationalPosition(index.row());
rl::math::Vector3 orientation = transform.linear().eulerAngles(2, 1, 0).reverse();
switch (index.column())
{
case 0:
case 1:
case 2:
transform.translation()(index.column()) = value.value<rl::math::Real>();
break;
case 3:
transform.linear() = (
rl::math::AngleAxis(orientation.z(), rl::math::Vector3::UnitZ()) *
rl::math::AngleAxis(orientation.y(), rl::math::Vector3::UnitY()) *
rl::math::AngleAxis(value.value<rl::math::Real>() * rl::math::constants::deg2rad, rl::math::Vector3::UnitX())
).toRotationMatrix();
break;
case 4:
transform.linear() = (
rl::math::AngleAxis(orientation.z(), rl::math::Vector3::UnitZ()) *
rl::math::AngleAxis(value.value<rl::math::Real>() * rl::math::constants::deg2rad, rl::math::Vector3::UnitY()) *
rl::math::AngleAxis(orientation.x(), rl::math::Vector3::UnitX())
).toRotationMatrix();
break;
case 5:
transform.linear() = (
rl::math::AngleAxis(value.value<rl::math::Real>() * rl::math::constants::deg2rad, rl::math::Vector3::UnitZ()) *
rl::math::AngleAxis(orientation.y(), rl::math::Vector3::UnitY()) *
rl::math::AngleAxis(orientation.x(), rl::math::Vector3::UnitX())
).toRotationMatrix();
break;
default:
break;
}
rl::math::Vector q = kinematic->getPosition();
std::shared_ptr<rl::mdl::InverseKinematics> ik;
if ("JacobianInverseKinematics" == MainWindow::instance()->ikAlgorithmComboBox->currentText())
{
ik = std::make_shared<rl::mdl::JacobianInverseKinematics>(kinematic);
rl::mdl::JacobianInverseKinematics* jacobianIk = static_cast<rl::mdl::JacobianInverseKinematics*>(ik.get());
jacobianIk->setDuration(std::chrono::milliseconds(MainWindow::instance()->ikDurationSpinBox->cleanText().toUInt()));
jacobianIk->setIterations(MainWindow::instance()->ikIterationsSpinBox->cleanText().toUInt());
if ("DLS" == MainWindow::instance()->ikJacobianComboBox->currentText())
{
jacobianIk->setMethod(rl::mdl::JacobianInverseKinematics::Method::dls);
}
else if ("SVD" == MainWindow::instance()->ikJacobianComboBox->currentText())
{
jacobianIk->setMethod(rl::mdl::JacobianInverseKinematics::Method::svd);
}
else if ("Transpose" == MainWindow::instance()->ikJacobianComboBox->currentText())
{
jacobianIk->setMethod(rl::mdl::JacobianInverseKinematics::Method::transpose);
}
}
#ifdef RL_MDL_NLOPT
else if ("NloptInverseKinematics" == MainWindow::instance()->ikAlgorithmComboBox->currentText())
{
ik = std::make_shared<rl::mdl::NloptInverseKinematics>(kinematic);
rl::mdl::NloptInverseKinematics* nloptIk = static_cast<rl::mdl::NloptInverseKinematics*>(ik.get());
nloptIk->setDuration(std::chrono::milliseconds(MainWindow::instance()->ikDurationSpinBox->cleanText().toUInt()));
nloptIk->setIterations(MainWindow::instance()->ikIterationsSpinBox->cleanText().toUInt());
}
#endif
for (std::size_t i = 0; i < kinematic->getOperationalDof(); ++i)
{
if (MainWindow::instance()->operationalGoals[this->id][i])
{
ik->addGoal(i == index.row() ? transform : kinematic->getOperationalPosition(i), i);
}
}
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
bool solved = ik->solve();
std::chrono::steady_clock::time_point stop = std::chrono::steady_clock::now();
if (solved)
{
MainWindow::instance()->statusBar()->showMessage("IK solved in " + QString::number(std::chrono::duration<double>(stop - start).count() * rl::math::constants::unit2milli) + " ms", 2000);
kinematic->forwardPosition();
for (std::size_t i = 0; i < MainWindow::instance()->geometryModels[this->id]->getNumBodies(); ++i)
{
MainWindow::instance()->geometryModels[this->id]->getBody(i)->setFrame(kinematic->getBodyFrame(i));
}
emit dataChanged(this->createIndex(0, 0), this->createIndex(this->rowCount(), this->columnCount()));
return true;
}
else
{
MainWindow::instance()->statusBar()->showMessage("IK failed", 2000);
kinematic->setPosition(q);
kinematic->forwardPosition();
}
}
}
return false;
}