41 typedef map<int, const char*> IdToLabelMap;
43 IdToLabelMap commandLabelMaps[2];
45 IdToLabelMap commandLabelMap;
47 IdLabel commandLabels[] = {
49 { DynamicsSimulator::POSITION_GIVEN,
"Position Given (High Gain Mode)" },
60 void initializeCommandLabelMaps()
62 if(commandLabelMap.empty()){
65 IdLabel& idLabel = commandLabels[i++];
69 commandLabelMap[idLabel.id] = idLabel.label;
74 const char* getLabelOfLinkDataType(DynamicsSimulator::LinkDataType
type)
76 IdToLabelMap::iterator p = commandLabelMap.find(type);
77 return (p != commandLabelMap.end()) ? p->second :
"Requesting Unknown Data Type";
82 template<
typename X,
typename X_ptr>
85 CosNaming::Name ncName;
87 ncName[0].id = CORBA::string_dup(n.c_str());
88 ncName[0].kind = CORBA::string_dup(
"");
91 srv = X::_narrow(cxt->resolve(ncName));
93 std::cerr << n <<
" not found: ";
95 case CosNaming::NamingContext::missing_node:
96 std::cerr <<
"Missing Node" << std::endl;
97 case CosNaming::NamingContext::not_context:
98 std::cerr <<
"Not Context" << std::endl;
100 case CosNaming::NamingContext::not_object:
101 std::cerr <<
"Not Object" << std::endl;
105 }
catch(CosNaming::NamingContext::CannotProceed &exc) {
106 std::cerr <<
"Resolve " << n <<
" CannotProceed" << std::endl;
107 }
catch(CosNaming::NamingContext::AlreadyBound &exc) {
108 std::cerr <<
"Resolve " << n <<
" InvalidName" << std::endl;
115 : orb_(CORBA::ORB::_duplicate(orb))
117 world.setCurrentTime(0.0);
118 world.setRungeKuttaMethod();
121 CollisionDetectorFactory_var collisionDetectorFactory;
123 CORBA::Object_var nS = orb->resolve_initial_references(
"NameService");
124 CosNaming::NamingContext_var cxT;
125 cxT = CosNaming::NamingContext::_narrow(nS);
126 collisionDetectorFactory =
128 CollisionDetectorFactory_var>(
"CollisionDetectorFactory", cxT);
129 if (CORBA::is_nil(collisionDetectorFactory)) {
130 std::cerr <<
"CollisionDetectorFactory not found" << std::endl;
133 collisionDetector = collisionDetectorFactory->create();
135 collisions =
new CollisionSequence;
136 collidingLinkPairs =
new LinkPairSequence;
137 allCharacterPositions =
new CharacterPositionSequence;
138 allCharacterSensorStates =
new SensorStateSequence;
140 needToUpdatePositions =
true;
141 needToUpdateSensorStates =
true;
152 PortableServer::POA_var poa_ = _default_POA();
153 PortableServer::ObjectId_var
id = poa_->servant_to_id(
this);
154 poa_->deactivate_object(
id);
164 collisionDetector->registerCharacter(
name, body);
170 CORBA::Double _timestep,
171 OpenHRP::DynamicsSimulator::IntegrateMethod integrateOpt,
172 OpenHRP::DynamicsSimulator::SensorOption sensorOpt)
176 world.setTimeStep(_timestep);
177 world.setCurrentTime(0.0);
179 if(integrateOpt == OpenHRP::DynamicsSimulator::EULER){
180 world.setEulerMethod();
182 world.setRungeKuttaMethod();
185 world.enableSensors((sensorOpt == OpenHRP::DynamicsSimulator::ENABLE_SENSOR));
187 _setupCharacterData();
189 isFirstSimulationLoop =
true;
201 jlist.push_back(cur);
214 const char *charName1,
215 const char *linkName1,
216 const char *charName2,
217 const char *linkName2,
218 const CORBA::Double staticFriction,
219 const CORBA::Double slipFriction,
220 const DblSequence6 & K,
221 const DblSequence6 &
C,
222 const double culling_thresh,
223 const double restitution)
225 const double epsilon = 0.0;
228 std::string emptyString =
"";
229 std::vector<Joint*> joints1;
230 std::vector<Joint*> joints2;
233 if(emptyString == linkName1)
241 if(jnt1) joints1.push_back(jnt1);
243 if(emptyString == linkName2)
251 if(jnt2) joints2.push_back(jnt2);
254 for(
size_t i=0;
i < joints1.size(); ++
i)
257 for(
size_t j=0;
j < joints2.size(); ++
j)
260 if(j1 && j2 && j1 != j2)
263 world.addCollisionCheckLinkPair(j1, j2, staticFriction, slipFriction, epsilon);
264 LinkPair_var linkPair =
new LinkPair();
265 linkPair->charName1 = CORBA::string_dup(charName1);
266 linkPair->linkName1 = CORBA::string_dup(j1->
basename);
267 linkPair->charName2 = CORBA::string_dup(charName2);
268 linkPair->linkName2 = CORBA::string_dup(j2->
basename);
269 linkPair->tolerance = 0;
270 collisionDetector->addCollisionPair(linkPair);
277 const char *charName1,
278 const char *linkName1,
279 const char *charName2,
280 const char *linkName2,
281 const double tolerance)
285 std::string emptyString =
"";
286 std::vector<Joint*> joints1;
287 std::vector<Joint*> joints2;
290 if(emptyString == linkName1)
298 if(jnt1) joints1.push_back(jnt1);
300 if(emptyString == linkName2)
308 if(jnt2) joints2.push_back(jnt2);
311 for(
size_t i=0;
i < joints1.size(); ++
i)
314 for(
size_t j=0;
j < joints2.size(); ++
j)
317 if(j1 && j2 && j1 != j2)
319 LinkPair_var linkPair =
new LinkPair();
320 linkPair->charName1 = CORBA::string_dup(charName1);
321 linkPair->linkName1 = CORBA::string_dup(j1->
basename);
322 linkPair->charName2 = CORBA::string_dup(charName2);
323 linkPair->linkName2 = CORBA::string_dup(j2->
basename);
324 linkPair->tolerance = tolerance;
325 collisionDetector->addCollisionPair(linkPair);
333 const char* charName1,
334 const char* linkName1,
335 const char* charName2,
336 const char* linkName2,
337 const DblSequence3& link1LocalPos,
338 const DblSequence3& link2LocalPos,
339 const ExtraJointType jointType,
340 const DblSequence3& jointAxis,
341 const char* extraJointName)
347 const char * characterName,
348 const char * extraJointName,
349 DblSequence6_out contactForce)
356 seq[CORBA::ULong(offset++)] = vec(0);
357 seq[CORBA::ULong(offset++)] = vec(1);
358 seq[CORBA::ULong(offset)] = vec(2);
369 const char *characterName,
370 const char *sensorName,
371 DblSequence_out sensorOutput)
373 sensorOutput =
new DblSequence;
374 Sensor* sensor = world.findSensor(sensorName, characterName);
383 sensorOutput->length(6);
388 sensorOutput[CORBA::ULong(0)] = forceSensor->
f(0);
389 sensorOutput[CORBA::ULong(1)] = forceSensor->
f(1);
390 sensorOutput[CORBA::ULong(2)] = forceSensor->
f(2);
391 sensorOutput[CORBA::ULong(3)] = forceSensor->
tau(0);
392 sensorOutput[CORBA::ULong(4)] = forceSensor->
tau(1);
393 sensorOutput[CORBA::ULong(5)] = forceSensor->
tau(2);
398 case Sensor::RATE_GYRO:
401 sensorOutput->length(3);
405 sensorOutput[CORBA::ULong(0)] = gyro->
w(0);
406 sensorOutput[CORBA::ULong(1)] = gyro->
w(1);
407 sensorOutput[CORBA::ULong(2)] = gyro->
w(2);
412 case Sensor::ACCELERATION:
415 sensorOutput->length(3);
419 sensorOutput[CORBA::ULong(0)] = accelSensor->
dv(0);
420 sensorOutput[CORBA::ULong(1)] = accelSensor->
dv(1);
421 sensorOutput[CORBA::ULong(2)] = accelSensor->
dv(2);
436 _updateCharacterPositions();
437 collisionDetector->queryContactDeterminationForDefinedPairs(allCharacterPositions.in(), collisions.out());
440 timeMeasureFinished =
false;
441 timeMeasure1.begin();
448 world.calcNextState(collisions);
452 cout <<
" " << timeMeasure2.time() <<
"\n";
455 needToUpdateSensorStates =
true;
457 _updateCharacterPositions();
459 collisionDetector->queryContactDeterminationForDefinedPairs(allCharacterPositions.in(), collisions.out());
463 if(world.currentTime() > 10.0 && !timeMeasureFinished)
465 timeMeasureFinished =
true;
467 cout <<
"Total elapsed time = " << timeMeasure1.totalTime() <<
"\n";
468 cout <<
"Internal elapsed time = " << timeMeasure2.totalTime();
469 cout <<
", the avarage = " << timeMeasure2.avarageTime() << endl;;
477 const char* characterName,
478 const char* linkName,
479 OpenHRP::DynamicsSimulator::LinkDataType
type,
480 const DblSequence& wdata)
483 Joint* joint = world.
Chain()->FindJoint(linkName, characterName);
488 case OpenHRP::DynamicsSimulator::POSITION_GIVEN:
489 joint->
t_given = !(wdata[0] > 0.0);
510 fVec3 abs_pos(wdata[0], wdata[1], wdata[2]);
511 fMat33 abs_att(wdata[3], wdata[4], wdata[5], wdata[6], wdata[7], wdata[8], wdata[9], wdata[10], wdata[11]);
518 fVec3 abs_lin_vel(wdata[0], wdata[1], wdata[2]);
519 fVec3 abs_ang_vel(wdata[3], wdata[4], wdata[5]);
532 fVec3 n0(wdata[3], wdata[4], wdata[5]);
543 needToUpdatePositions =
true;
544 needToUpdateSensorStates =
true;
549 const char * characterName,
550 const char * linkName,
551 OpenHRP::DynamicsSimulator::LinkDataType
type,
552 DblSequence_out out_rdata)
555 Joint* joint = world.
Chain()->FindJoint(linkName, characterName);
558 DblSequence_var rdata =
new DblSequence;
569 rdata[0] = joint->
qd;
574 rdata[0] = joint->
qdd;
579 rdata[0] = joint->
tau;
591 rdata[4] = ep.
Axis()(0);
592 rdata[5] = ep.
Axis()(1);
593 rdata[6] = ep.
Axis()(2);
633 out_rdata = rdata._retn();
638 const char * characterName,
639 OpenHRP::DynamicsSimulator::LinkDataType
type,
640 DblSequence_out rdata)
643 rdata =
new DblSequence();
645 world.getAllCharacterData(characterName,
type, rdata);
650 const char * characterName,
651 OpenHRP::DynamicsSimulator::LinkDataType
type,
652 const DblSequence & wdata)
655 world.setAllCharacterData(characterName,
type, wdata);
660 const DblSequence3& wdata)
662 assert(wdata.length() == 3);
667 world.setGravityAcceleration(g);
673 DblSequence3_out wdata)
676 fVec3 g = world.getGravityAcceleration();
684 const char * characterName,
685 OpenHRP::DynamicsSimulator::JointDriveMode jointMode)
688 bool isTorqueMode = (jointMode != OpenHRP::DynamicsSimulator::HIGH_GAIN_MODE);
690 world.Chain()->SetCharacterTorqueGiven(characterName, isTorqueMode);
696 const char * characterName,
697 const char * fromLink,
const char * toLink,
698 const LinkPosition& target)
709 const char * characterName)
712 world.Chain()->CalcPosition();
714 needToUpdatePositions =
true;
715 needToUpdateSensorStates =
true;
722 world.Chain()->CalcPosition();
724 needToUpdatePositions =
true;
725 needToUpdateSensorStates =
true;
732 if (needToUpdatePositions) _updateCharacterPositions();
734 wstate =
new WorldState;
736 wstate->time = world.currentTime();
737 wstate->characterPositions = allCharacterPositions;
738 wstate->collisions = collisions;
746 int n_char = world.numCharacter();
747 for(
int i=0;
i<n_char;
i++)
750 if(!strcmp(j->
CharName(), characterName))
758 if(needToUpdateSensorStates)
760 _updateSensorStates();
762 sstate =
new SensorState(allCharacterSensorStates[index]);
766 sstate =
new SensorState;
773 int nchar = world.numCharacter();
775 allCharacterPositions->length(nchar);
776 allCharacterSensorStates->length(nchar);
780 for(
int i=0;
i<nchar;
i++)
784 CharacterPosition& characterPosition = allCharacterPositions[
i];
785 characterPosition.characterName = CORBA::string_dup(j->
CharName());
786 int n_links = world.numLinks(
i);
788 LinkPositionSequence& linkPositions = characterPosition.linkPositions;
789 linkPositions.length(n_links);
790 SensorState& sensorState = allCharacterSensorStates[
i];
791 int n_joints = world.numJoints(
i);
793 sensorState.q.length(n_joints);
794 sensorState.dq.length(n_joints);
795 sensorState.u.length(n_joints);
796 sensorState.force.length(world.numSensors(Sensor::FORCE, j->
CharName()));
797 sensorState.rateGyro.length(world.numSensors(Sensor::RATE_GYRO, j->
CharName()));
798 sensorState.accel.length(world.numSensors(Sensor::ACCELERATION, j->
CharName()));
805 world.getAllCharacterPositions(allCharacterPositions.inout());
806 needToUpdatePositions =
false;
809 int n_char = allCharacterPositions->length();
810 for(
int i=0;
i<n_char;
i++)
812 CharacterPosition& cpos = allCharacterPositions[
i];
813 int n_link = cpos.linkPositions.length();
814 for(
int j=0;
j<n_link;
j++)
816 LinkPosition& lpos = cpos.linkPositions[
j];
824 world.getAllSensorStates(allCharacterSensorStates);
825 needToUpdateSensorStates =
false;
834 const char *characterName,
835 LinkPairSequence_out pairs)
837 std::vector<unsigned int> locations;
839 for(
unsigned int i=0;
i < collisions->length(); ++
i) {
841 if( (strcmp(characterName, collisions[
i].pair.charName1) == 0) ||
842 (strcmp(characterName, collisions[i].pair.charName2) == 0))
843 locations.push_back(i);
846 pairs->length(locations.size());
849 for(std::vector<unsigned int>::iterator iter = locations.begin();
850 iter != locations.end(); ++iter) {
852 strcpy(pairs[n].charName1, collisions[*iter].pair.charName1);
853 strcpy(pairs[n].charName2, collisions[*iter].pair.charName2);
854 strcpy(pairs[n].linkName1, collisions[*iter].pair.linkName1);
855 strcpy(pairs[n].linkName2, collisions[*iter].pair.linkName2);
863 const char *characterName,
864 const char *baseLink,
865 const char *targetLink,
866 DblSequence_out jacobian)
869 world.calcCharacterJacobian(characterName, baseLink, targetLink, J);
874 jacobian->length(height * width);
876 for(
int r=0; r <
height; ++r){
878 (*jacobian)[i++] = J(r,
c);
885 calcWorldForwardKinematics();
886 _updateCharacterPositions();
888 return collisionDetector->queryContactDeterminationForDefinedPairs(allCharacterPositions.in(), collisions.out());
890 return collisionDetector->queryIntersectionForDefinedPairs(checkAll, allCharacterPositions.in(), collidingLinkPairs.out());
896 calcWorldForwardKinematics();
897 _updateCharacterPositions();
898 DistanceSequence_var distances =
new DistanceSequence;
899 collisionDetector->queryDistanceForDefinedPairs(allCharacterPositions.in(), distances);
900 return distances._retn();
905 calcWorldForwardKinematics();
906 _updateCharacterPositions();
907 LinkPairSequence_var pairs =
new LinkPairSequence;
908 collisionDetector->queryIntersectionForDefinedPairs(checkAll, allCharacterPositions.in(), pairs);
909 return pairs._retn();
917 orb_(CORBA::ORB::_duplicate(orb))
919 initializeCommandLabelMaps();
925 PortableServer::POA_var poa = _default_POA();
926 PortableServer::ObjectId_var
id = poa -> servant_to_id(
this);
927 poa -> deactivate_object(
id);
935 PortableServer::ServantBase_var integratorrServant = integratorImpl;
936 PortableServer::POA_var poa_ = _default_POA();
937 PortableServer::ObjectId_var
id =
938 poa_ -> activate_object(integratorImpl);
940 return integratorImpl -> _this();
946 orb_->shutdown(
false);
virtual void 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, const double culling_thresh, const double restitution)
fVec3 loc_lin_vel
linear velocity in local frame
void add(const fVec3 &vec1, const fVec3 &vec2)
DynamicsSimulator_impl(CORBA::ORB_ptr orb)
X_ptr checkCorbaServer(std::string n, CosNaming::NamingContext_var &cxt)
Joint * child
pointer to the child joint
png_infop png_charp png_int_32 png_int_32 int * type
virtual void getCharacterSensorState(const char *characterName, SensorState_out sstate)
~DynamicsSimulatorFactory_impl()
int col() const
Returns the number of columns.
~DynamicsSimulator_impl()
int t_given
torque or motion controlled
int row() const
Returns the number of rows.
void cross(const fVec3 &vec1, const fVec3 &vec2)
Cross product.
void _updateCharacterPositions()
void _updateSensorStates()
Forward dynamics computation based on Assembly-Disassembly Algorithm.
png_infop png_charpp name
fVec3 loc_ang_vel
angular velocity in local frame
virtual void getGVector(DblSequence3_out wdata)
virtual void registerExtraJoint(const char *charName1, const char *linkName1, const char *charName2, const char *linkName2, const DblSequence3 &link1LocalPos, const DblSequence3 &link2LocalPos, const ExtraJointType jointType, const DblSequence3 &jointAxis, const char *extraJointName)
HRPMODEL_API bool loadBodyFromBodyInfo(BodyPtr body, OpenHRP::BodyInfo_ptr bodyInfo, bool loadGeometryForCollisionDetection=false, Link *(*f)()=NULL)
virtual DistanceSequence * checkDistance()
virtual bool checkCollision(bool checkAll)
friend fVec3 & Axis(fEulerPara &ep)
Access the vector part (a*sin(theta/2))
static const bool enableTimeMeasure
char * basename
joint base name (without the character name)
virtual void getCharacterLinkData(const char *characterName, const char *link, OpenHRP::DynamicsSimulator::LinkDataType type, DblSequence_out rdata)
void set(const fVec3 &v, double s)
Set the elements.
int SetJointAcc(double _qdd)
double q
joint value (for 1DOF joints)
double tau
joint force/torque (for 1DOF joints)
png_infop png_uint_32 * width
void _setupCharacterData()
virtual void calcWorldForwardKinematics()
virtual void getExtraJointConstraintForce(const char *characterName, const char *extraJointName, DblSequence6_out contactForce)
virtual void init(CORBA::Double timeStep, OpenHRP::DynamicsSimulator::IntegrateMethod integrateOpt, OpenHRP::DynamicsSimulator::SensorOption sensorOpt)
virtual void calcCharacterForwardKinematics(const char *characterName)
int SetJointForce(double _tau)
static void joint_traverse_sub(Joint *cur, std::vector< Joint * > &jlist)
virtual CORBA::Boolean calcCharacterInverseKinematics(const char *characterName, const char *baseLink, const char *targetLink, const LinkPosition &target)
virtual void setGVector(const DblSequence3 &wdata)
DynamicsSimulatorFactory_impl(CORBA::ORB_ptr orb)
png_infop png_uint_32 png_uint_32 * height
double qd
joint velocity (for 1DOF joints)
def j(str, encoding="cp932")
fVec3 ext_force
external force
fMat33 abs_att
absolute orientation
char * CharName() const
Returns the character name.
friend double & Ang(fEulerPara &ep)
Access the scalar part (cos(theta/2))
virtual LinkPairSequence * checkIntersection(CORBA::Boolean checkAll)
virtual void initSimulation()
Joint * brother
pointer to the brother joint
virtual void getWorldState(WorldState_out wstate)
virtual void setCharacterLinkData(const char *characterName, const char *link, OpenHRP::DynamicsSimulator::LinkDataType type, const DblSequence &data)
virtual void stepSimulation()
DynamicsSimulator_ptr create()
void sub(const fVec3 &vec1, const fVec3 &vec2)
int SetJointValue(double _q)
virtual void getCharacterAllLinkData(const char *characterName, OpenHRP::DynamicsSimulator::LinkDataType type, DblSequence_out wdata)
double qdd
joint acceleration (for 1DOF joints)
Joint * FindJoint(const char *jname, const char *charname=0)
Find a joint from name.
virtual void registerIntersectionCheckPair(const char *char1, const char *name1, const char *char2, const char *name2, const double tolerance)
virtual void setCharacterAllJointModes(const char *characterName, OpenHRP::DynamicsSimulator::JointDriveMode jointMode)
fVec3 ext_moment
external moment around the local frame
static void vec3_to_seq(const fVec3 &vec, DblSequence_out &seq, size_t offset=0)
static void joint_traverse(Joint *r, std::vector< Joint * > &jlist)
fVec3 abs_pos
absolute position
void mul(const fVec3 &vec, double d)
Main class for forward dynamics computation.
virtual void setCharacterAllLinkData(const char *characterName, OpenHRP::DynamicsSimulator::LinkDataType type, const DblSequence &wdata)
The class for representing a joint.
static void seq_to_vec3(const DblSequence3 &seq, fVec3 &vec)
virtual CORBA::Boolean getCharacterCollidingPairs(const char *characterName, LinkPairSequence_out pairs)
int SetJointVel(double _qd)
virtual void registerCharacter(const char *name, BodyInfo_ptr binfo)
virtual void calcCharacterJacobian(const char *characterName, const char *baseLink, const char *targetLink, DblSequence_out jacobian)
Matrix of generic size. The elements are stored in a one-dimensional array in row-major order...
virtual void getCharacterSensorValues(const char *characterName, const char *sensorName, DblSequence_out values)
Joint * FindCharacterRoot(const char *charname)
Find the root joint of the character with name charname.