-
Notifications
You must be signed in to change notification settings - Fork 5
/
DynamicsSimulator_impl.cpp
464 lines (352 loc) · 11.9 KB
/
DynamicsSimulator_impl.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/**
\file
\author Shin'ichiro Nakaoka
*/
#include "DynamicsSimulator_impl.h"
#include <cnoid/Link>
using namespace std;
using namespace cnoid;
using namespace OpenHRP;
DynamicsSimulator_impl::DynamicsSimulator_impl(const BodyPtr& body)
{
this->body = body;
for(auto joint : body->joints()){
joint->setActuationMode(Link::JOINT_EFFORT);
}
forceSensorIdMap = body->devices<ForceSensor>().getSortedById();
gyroIdMap = body->devices<RateGyroSensor>().getSortedById();
accelSensorIdMap = body->devices<AccelerationSensor>().getSortedById();
}
DynamicsSimulator_impl::~DynamicsSimulator_impl()
{
PortableServer::POA_var poa = _default_POA();
PortableServer::ObjectId_var id = poa->servant_to_id(this);
poa->deactivate_object(id);
}
void DynamicsSimulator_impl::destroy()
{
}
#ifdef OPENHRP_3_0
void DynamicsSimulator_impl::registerCharacter(const char* name, CharacterInfo_ptr cinfo)
{
}
#elif OPENHRP_3_1
void DynamicsSimulator_impl::registerCharacter(const char* name, BodyInfo_ptr cinfo)
{
}
#endif
#ifdef OPENHRP_3_0
void DynamicsSimulator_impl::registerCollisionCheckPair(
const char* char1, const char* name1, const char* char2, const char* name2,
::CORBA::Double staticFriction, ::CORBA::Double slipFriction, const DblSequence6& K, const DblSequence6& C)
{
}
#elif OPENHRP_3_1
void DynamicsSimulator_impl::registerCollisionCheckPair(
const char* char1, const char* name1, const char* char2, const char* name2,
::CORBA::Double staticFriction, ::CORBA::Double slipFriction, const DblSequence6& K, const DblSequence6& C, ::CORBA::Double culling_thresh)
{
}
void DynamicsSimulator_impl::registerIntersectionCheckPair(const char* char1, const char* name1, const char* char2, const char* name2, ::CORBA::Double tolerance)
{
}
#endif
void DynamicsSimulator_impl::init(
::CORBA::Double timeStep, OpenHRP::DynamicsSimulator::IntegrateMethod integrateOpt, OpenHRP::DynamicsSimulator::SensorOption sensorOpt)
{
}
void DynamicsSimulator_impl::registerVirtualLink(
const char* char1, const char* link1, const char* char2, const char* link2,
const LinkPosition& relTransform, ::CORBA::Short transformDefined, const DblSequence9& constraint, const char* connectionName)
{
}
void DynamicsSimulator_impl::getConnectionConstraintForce(
const char* characterName, const char* connectionName, DblSequence6_out contactForce)
{
}
void DynamicsSimulator_impl::getCharacterSensorValues
(const char* character, const char* sensorName, DblSequence_out out_values)
{
DblSequence_var values = new DblSequence;
if(Device* sensor = body->findDevice(sensorName)){
values->length(sensor->stateSize());
sensor->writeState(&values[0]);
}
out_values = values._retn();
}
void DynamicsSimulator_impl::initSimulation()
{
}
void DynamicsSimulator_impl::stepSimulation()
{
}
void DynamicsSimulator_impl::setCharacterLinkData(
const char* character, const char* linkName, OpenHRP::DynamicsSimulator::LinkDataType type, const DblSequence& data)
{
Link* link = body->link(linkName);
if(link){
switch(type) {
case OpenHRP::DynamicsSimulator::POSITION_GIVEN:
if(link->isRoot()){
link->setActuationMode(Link::LINK_POSITION);
} else {
link->setActuationMode(Link::JOINT_DISPLACEMENT);
}
break;
case OpenHRP::DynamicsSimulator::JOINT_VALUE:
if(!link->isFixedJoint()){
link->q_target() = data[0];
}
break;
case OpenHRP::DynamicsSimulator::JOINT_VELOCITY:
if(!link->isFixedJoint() ||
link->jointType() == Link::PSEUDO_CONTINUOUS_TRACK ||
link->actuationMode() == Link::JOINT_SURFACE_VELOCITY){
link->dq_target() = data[0];
}
break;
case OpenHRP::DynamicsSimulator::JOINT_ACCELERATION:
if(!link->isFixedJoint()){
link->ddq() = data[0];
}
break;
case OpenHRP::DynamicsSimulator::JOINT_TORQUE:
if(!link->isFixedJoint()){
link->u() = data[0];
}
break;
case OpenHRP::DynamicsSimulator::ABS_TRANSFORM:
{
link->p() = Eigen::Map<const Vector3>(&data[0]);
Matrix3 R;
R << data[3], data[4], data[5],
data[6], data[7], data[8],
data[9], data[10], data[11];
link->setAttitude(R);
break;
}
case OpenHRP::DynamicsSimulator::ABS_VELOCITY:
{
link->v() = Eigen::Map<const Vector3>(&data[0]);
link->w() = Eigen::Map<const Vector3>(&data[3]);
break;
}
case OpenHRP::DynamicsSimulator::EXTERNAL_FORCE:
{
link->f_ext() = Eigen::Map<const Vector3>(&data[0]);
link->tau_ext() = Eigen::Map<const Vector3>(&data[3]);
break;
}
default:
break;
}
}
}
void DynamicsSimulator_impl::getCharacterLinkData(
const char* characterName, const char* linkName, OpenHRP::DynamicsSimulator::LinkDataType type, DblSequence_out out_rdata)
{
const Link* link = body->link(linkName);
DblSequence_var rdata = new DblSequence;
if(link){
switch(type) {
case OpenHRP::DynamicsSimulator::JOINT_VALUE:
rdata->length(1);
rdata[0] = link->q();
break;
case OpenHRP::DynamicsSimulator::JOINT_VELOCITY:
rdata->length(1);
rdata[0] = link->dq();
break;
case OpenHRP::DynamicsSimulator::JOINT_ACCELERATION:
rdata->length(1);
rdata[0] = link->ddq();
break;
case OpenHRP::DynamicsSimulator::JOINT_TORQUE:
rdata->length(1);
rdata[0] = link->u();
break;
case OpenHRP::DynamicsSimulator::ABS_TRANSFORM:
{
rdata->length(12);
rdata[0] = link->p().x();
rdata[1] = link->p().y();
rdata[2] = link->p().z();
const Matrix3 R = link->attitude();
for(int i=0; i < 3; ++i){
for(int j=0; j < 3; ++j){
rdata[3 + i * 3 + j] = R(i, j);
}
}
}
break;
case OpenHRP::DynamicsSimulator::ABS_VELOCITY:
rdata->length(6);
rdata[0] = link->v().x();
rdata[1] = link->v().y();
rdata[2] = link->v().z();
rdata[3] = link->w().x();
rdata[4] = link->w().y();
rdata[5] = link->w().z();
break;
case OpenHRP::DynamicsSimulator::EXTERNAL_FORCE:
rdata->length(6);
rdata[0] = link->f_ext().x();
rdata[1] = link->f_ext().y();
rdata[2] = link->f_ext().z();
rdata[3] = link->tau_ext().x();
rdata[4] = link->tau_ext().y();
rdata[5] = link->tau_ext().z();
break;
default:
break;
}
}
out_rdata = rdata._retn();
}
void DynamicsSimulator_impl::getCharacterAllLinkData(
const char* characterName, OpenHRP::DynamicsSimulator::LinkDataType type, DblSequence_out rdata)
{
const int n = body->numJoints();
rdata = new DblSequence();
rdata->length(n);
switch(type) {
case OpenHRP::DynamicsSimulator::JOINT_VALUE:
for(int i=0; i < n; ++i){
(*rdata)[i] = body->joint(i)->q();
}
break;
case OpenHRP::DynamicsSimulator::JOINT_VELOCITY:
for(int i=0; i < n; ++i){
(*rdata)[i] = body->joint(i)->dq();
}
break;
case OpenHRP::DynamicsSimulator::JOINT_ACCELERATION:
for(int i=0; i < n; ++i){
(*rdata)[i] = body->joint(i)->ddq();
}
break;
case OpenHRP::DynamicsSimulator::JOINT_TORQUE:
for(int i=0; i < n; ++i){
(*rdata)[i] = body->joint(i)->u();
}
break;
default:
// put error here
break;
}
}
void DynamicsSimulator_impl::setCharacterAllLinkData(
const char* characterName, OpenHRP::DynamicsSimulator::LinkDataType type, const DblSequence& wdata)
{
int n = wdata.length();
if(n > body->numJoints()){
n = body->numJoints();
}
switch(type) {
case OpenHRP::DynamicsSimulator::JOINT_VALUE:
for(int i=0; i < n; ++i){
body->joint(i)->q_target() = wdata[i];
}
break;
case OpenHRP::DynamicsSimulator::JOINT_VELOCITY:
for(int i=0; i < n; ++i){
body->joint(i)->dq_target() = wdata[i];
}
break;
case OpenHRP::DynamicsSimulator::JOINT_ACCELERATION:
for(int i=0; i < n; ++i){
body->joint(i)->ddq() = wdata[i];
}
break;
case OpenHRP::DynamicsSimulator::JOINT_TORQUE:
for(int i=0; i < n; ++i){
body->joint(i)->u() = wdata[i];
}
break;
default:
// put error here
break;
}
}
void DynamicsSimulator_impl::setGVector(const DblSequence3& wdata)
{
}
void DynamicsSimulator_impl::getGVector(DblSequence3_out wdata)
{
}
void DynamicsSimulator_impl::setCharacterAllJointModes(const char* characterName, OpenHRP::DynamicsSimulator::JointDriveMode jointMode)
{
Link::ActuationMode actuationMode = Link::JOINT_EFFORT;
if(jointMode == OpenHRP::DynamicsSimulator::HIGH_GAIN_MODE){
actuationMode = Link::JOINT_DISPLACEMENT;
}
for(auto joint : body->joints()){
joint->setActuationMode(actuationMode);
}
}
::CORBA::Boolean DynamicsSimulator_impl::calcCharacterInverseKinematics(
const char* characterName, const char* baseLink, const char* targetLink, const LinkPosition& target)
{
return true;
}
void DynamicsSimulator_impl::calcCharacterForwardKinematics(const char* characterName)
{
}
void DynamicsSimulator_impl::calcWorldForwardKinematics()
{
}
#ifdef OPENHRP_3_0
void DynamicsSimulator_impl::checkCollision()
{
}
#elif OPENHRP_3_1
::CORBA::Boolean DynamicsSimulator_impl::checkCollision(::CORBA::Boolean checkAll)
{
return false;
}
LinkPairSequence* DynamicsSimulator_impl::checkIntersection(::CORBA::Boolean checkAll)
{
return 0;
}
DistanceSequence* DynamicsSimulator_impl::checkDistance()
{
return 0;
}
#endif
void DynamicsSimulator_impl::getWorldState(WorldState_out wstate)
{
}
void DynamicsSimulator_impl::getCharacterSensorState(const char* characterName, SensorState_out sstate)
{
sstate = new SensorState;
int numJoints = body->numJoints();
sstate->q.length(numJoints);
sstate->dq.length(numJoints);
sstate->u.length(numJoints);
sstate->force.length(forceSensorIdMap.size());
sstate->rateGyro.length(gyroIdMap.size());
sstate->accel.length(accelSensorIdMap.size());
for(int j=0; j < numJoints; j++){
Link* joint = body->joint(j);
sstate->q [j] = joint->q();
sstate->dq[j] = joint->dq();
sstate->u [j] = joint->u();
}
for(size_t id = 0; id < forceSensorIdMap.size(); ++id){
Eigen::Map<Vector6>(&sstate->force[id][0]) = forceSensorIdMap[id]->F();
}
for(size_t id = 0; id < gyroIdMap.size(); ++id){
Eigen::Map<Vector3>(sstate->rateGyro[id]) = gyroIdMap[id]->w();
}
for(size_t id = 0; id < accelSensorIdMap.size(); ++id){
Eigen::Map<Vector3>(sstate->accel[id]) = accelSensorIdMap[id]->dv();
}
}
::CORBA::Boolean DynamicsSimulator_impl::getCharacterCollidingPairs(const char* characterName, LinkPairSequence_out pairs)
{
return true;
}
void DynamicsSimulator_impl::calcCharacterJacobian(
const char* characterName, const char* baseLink, const char* targetLink, DblSequence_out jacobian)
{
}