-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOpenSotTask.cpp
More file actions
319 lines (258 loc) · 8.98 KB
/
OpenSotTask.cpp
File metadata and controls
319 lines (258 loc) · 8.98 KB
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
#include "opensot/OpenSotTask.h"
#include <OpenSoT/utils/AutoStack.h>
#include "../../utils/DynamicLoading.h"
#include "opensot/OpenSotCartesian.h"
#include "opensot/OpenSotJointLimits.h"
#include "opensot/OpenSotVelocityLimits.h"
#include "opensot/OpenSotConstraintFromTask.h"
#include "opensot/OpenSotPostural.h"
#include "opensot/OpenSotCom.h"
#include "opensot/OpenSotSubtask.h"
#include "opensot/OpenSotOmniWheels4X.h"
#include "opensot/OpenSotGaze.h"
#include "fmt/format.h"
using namespace XBot::Cartesian;
OpenSotTaskAdapter::OpenSotTaskAdapter(TaskDescription::Ptr task,
Context::ConstPtr context
):
_vars({}),
_model(context->model()),
_ci_task(std::dynamic_pointer_cast<TaskDescriptionImpl>(task)),
_ctx(context)
{
if(!_ci_task)
{
throw std::runtime_error("invalid task provided: "
"not a TaskDescriptionImpl");
}
}
bool OpenSotTaskAdapter::initialize(const OpenSoT::OptvarHelper& vars)
{
/* Save variables */
_vars = vars;
/* Let derived class construct the task */
_opensot_task = constructTask();
_sub_task = _opensot_task;
if(!_opensot_task)
{
throw std::runtime_error(fmt::format("OpenSotTaskAdapter: constructTask() for task '{}' returned null pointer",
_ci_task->getName()));
}
/* Set generic task parameters */
// active joint mask
if(_ci_task->getDisabledJoints().size() > 0)
{
std::vector<bool> active_joints_mask(_model->getJointNum(), true);
for(auto jstr : _ci_task->getDisabledJoints())
{
active_joints_mask.at(_model->getDofIndex(jstr)) = false;
}
_opensot_task->setActiveJointsMask(active_joints_mask);
}
// lambda
_opensot_task->setLambda(_ci_task->getLambda());
// weight
_opensot_task->setWeight(getOpenSotWeight());
// indices
if(_ci_task->getIndices().size() != _ci_task->getSize())
{
std::list<uint> indices_list(_ci_task->getIndices().begin(),
_ci_task->getIndices().end());
_sub_task = _opensot_task % indices_list;
}
// activation state
auto activ = _ci_task->getActivationState();
if(activ == ActivationState::Disabled)
{
_sub_task->setActive(false);
}
/* Register observer */
_ci_task->registerObserver(shared_from_this());
return true;
}
OpenSoT::OptvarHelper::VariableVector OpenSotTaskAdapter::getRequiredVariables() const
{
return {};
}
void OpenSotTaskAdapter::update(double time, double period)
{
_opensot_task->setLambda(_ci_task->getLambda());
}
void OpenSotTaskAdapter::processSolution(const Eigen::VectorXd& solution)
{
_task_err.noalias() = _opensot_task->getWA()*solution -
_opensot_task->getWb();
_ci_task->setTaskError(_task_err);
}
TaskPtr OpenSotTaskAdapter::getOpenSotTask()
{
return _sub_task;
}
TaskDescriptionImpl::Ptr OpenSotTaskAdapter::getTaskDescription() const
{
return _ci_task;
}
bool OpenSotTaskAdapter::onWeightChanged()
{
_opensot_task->setWeight(getOpenSotWeight());
return true;
}
bool OpenSotTaskAdapter::onActivationStateChanged()
{
auto activ = _ci_task->getActivationState();
if(activ == ActivationState::Disabled)
{
_sub_task->setActive(false);
}
if(activ == ActivationState::Enabled)
{
_sub_task->setActive(true);
}
return true;
}
OpenSotTaskAdapter::Ptr OpenSotTaskAdapter::MakeInstance(TaskDescription::Ptr task,
Context::ConstPtr context)
{
OpenSotTaskAdapter * task_adapter = nullptr;
/* If lib name specified, load factory from plugin */
if(task->getLibName() != "")
{
task_adapter = CallFunction<OpenSotTaskAdapter*>(task->getLibName(),
"create_opensot_" + task->getType() + "_adapter",
task,
context,
detail::Version CARTESIO_ABI_VERSION);
}
else if(task->getType() == "Cartesian") /* Otherwise, construct supported tasks */
{
task_adapter = new OpenSotCartesianAdapter(task, context);
}
else if(task->getType() == "Postural")
{
task_adapter = new OpenSotPosturalAdapter(task, context);
}
else if(task->getType() == "Com")
{
task_adapter = new OpenSotComAdapter(task, context);
}
else if(task->getType() == "Gaze")
{
task_adapter = new OpenSotGazeAdapter(task, context);
}
else if(task->getType() == "Subtask") /* Otherwise, construct supported tasks */
{
task_adapter = new OpenSotSubtaskAdapter(task, context);
}
else
{
auto str = fmt::format("Unable to construct OpenSotTaskAdapter instance for task '{}': "
"empty lib name for unrecoginized task type '{}'",
task->getName(), task->getType());
throw std::runtime_error(str);
}
Ptr task_shared_ptr(task_adapter);
return task_shared_ptr;
}
const Eigen::MatrixXd& OpenSotTaskAdapter::getOpenSotWeight() const
{
return _ci_task->getWeight();
}
OpenSoT::OptvarHelper OpenSotTaskAdapter::DefaultVars()
{
OpenSoT::OptvarHelper vars({});
return vars;
}
OpenSotConstraintAdapter::OpenSotConstraintAdapter(ConstraintDescription::Ptr constr,
Context::ConstPtr context
):
_ci_constr(constr),
_model(context->model()),
_vars({}),
_ctx(context)
{
}
bool OpenSotConstraintAdapter::initialize(const OpenSoT::OptvarHelper& vars)
{
/* Save variables */
_vars = vars;
_opensot_constr = constructConstraint();
_sub_constr = _opensot_constr;
if(!_opensot_constr)
{
throw std::runtime_error(fmt::format("OpenSotConstraintAdapter: "
"constructConstraint() for constraint '{}' returned null pointer",
_ci_constr->getName()));
}
// indices
if(_ci_constr->getIndices().size() != _ci_constr->getSize())
{
std::list<uint> indices_list(_ci_constr->getIndices().begin(),
_ci_constr->getIndices().end());
_sub_constr = _opensot_constr % indices_list;
}
/* Register observer */
_ci_constr->registerObserver(shared_from_this());
return true;
}
OpenSoT::OptvarHelper::VariableVector OpenSotConstraintAdapter::getRequiredVariables() const
{
return {};
}
void OpenSotConstraintAdapter::update(double time, double period)
{
}
void OpenSotConstraintAdapter::processSolution(const Eigen::VectorXd& solution)
{
}
ConstraintPtr OpenSotConstraintAdapter::getOpenSotConstraint()
{
return _sub_constr;
}
ConstraintDescription::Ptr OpenSotConstraintAdapter::getConstraintDescription() const
{
return _ci_constr;
}
OpenSotConstraintAdapter::Ptr OpenSotConstraintAdapter::MakeInstance(ConstraintDescription::Ptr constr,
Context::ConstPtr context)
{
OpenSotConstraintAdapter * constr_adapter = nullptr;
/* If lib name specified, load factory from plugin */
if(constr->getLibName() != "")
{
constr_adapter = CallFunction<OpenSotConstraintAdapter*>(constr->getLibName(),
"create_opensot_" + constr->getType() + "_adapter",
constr,
context,
detail::Version CARTESIO_ABI_VERSION);
}
else if(constr->getType() == "ConstraintFromTask")
{
constr_adapter = new OpenSotConstraintFromTaskAdapter(constr, context);
}
else if(constr->getType() == "JointLimits") /* Otherwise, construct supported tasks */
{
constr_adapter = new OpenSotJointLimitsAdapter(constr, context);
}
else if(constr->getType() == "VelocityLimits")
{
constr_adapter = new OpenSotVelocityLimitsAdapter(constr, context);
}
else if(constr->getType() == "OmniWheels4X")
{
constr_adapter = new OpenSotOmniWheels4XAdapter(constr, context);
}
else
{
auto str = fmt::format("Unable to construct OpenSotConstraintAdapter instance for task '{}': "
"empty lib name for unrecoginized constraint type '{}'",
constr->getName(), constr->getType());
throw std::runtime_error(str);
}
Ptr constr_shared_ptr(constr_adapter);
return constr_shared_ptr;
}
OpenSoT::OptvarHelper OpenSotConstraintAdapter::DefaultVars()
{
OpenSoT::OptvarHelper vars({});
return vars;
}