12 #include <hrpModel/Sensor.h> 13 #include <hrpModel/ModelLoaderUtil.h> 15 #include "../ImpedanceController/RatsMatrix.h" 16 #include "hrpsys/util/Hrpsys.h" 17 #include <boost/assign.hpp> 25 "implementation_id",
"ObjectContactTurnaroundDetector",
26 "type_name",
"ObjectContactTurnaroundDetector",
27 "description",
"object contact turnaround detector component",
28 "version", HRPSYS_PACKAGE_VERSION,
30 "category",
"example",
31 "activity_type",
"DataFlowComponent",
34 "lang_type",
"compile",
36 "conf.default.debugLevel",
"0",
44 m_qCurrentIn(
"qCurrent", m_qCurrent),
45 m_rpyIn(
"rpy", m_rpy),
46 m_contactStatesIn(
"contactStates", m_contactStates),
47 m_octdDataOut(
"octdData", m_octdData),
48 m_ObjectContactTurnaroundDetectorServicePort(
"ObjectContactTurnaroundDetectorService"),
64 std::cerr <<
"[" <<
m_profile.instance_name <<
"] onInitialize()" << std::endl;
97 std::string nameServer = rtcManager.
getConfig()[
"corba.nameservers"];
98 int comPos = nameServer.find(
",");
100 comPos = nameServer.length();
102 nameServer = nameServer.substr(0, comPos);
105 CosNaming::NamingContext::_duplicate(
naming.getRootContext())
107 std::cerr <<
"[" <<
m_profile.instance_name <<
"] failed to load model[" << prop[
"model"] <<
"]" << std::endl;
108 return RTC::RTC_ERROR;
113 std::vector<std::string> fsensor_names;
116 for (
unsigned int i=0;
i<npforce;
i++){
120 unsigned int nforce = npforce;
123 std::cerr <<
"[" <<
m_profile.instance_name <<
"] force sensor ports" << std::endl;
124 for (
unsigned int i=0;
i<nforce;
i++){
128 std::cerr <<
"[" <<
m_profile.instance_name <<
"] name = " << fsensor_names[i] << std::endl;
131 unsigned int dof =
m_robot->numJoints();
132 for (
unsigned int i = 0 ;
i < dof;
i++ ){
133 if ( (
int)
i !=
m_robot->joint(
i)->jointId ) {
134 std::cerr <<
"[" <<
m_profile.instance_name <<
"] jointId is not equal to the index number" << std::endl;
135 return RTC::RTC_ERROR;
142 std::map<std::string, std::string> base_name_map;
143 if (end_effectors_str.size() > 0) {
144 size_t prop_num = 10;
145 size_t num = end_effectors_str.size()/prop_num;
146 for (
size_t i = 0;
i < num;
i++) {
147 std::string ee_name, ee_target, ee_base;
152 for (
size_t j = 0;
j < 3;
j++) {
156 for (
int j = 0;
j < 4;
j++ ) {
159 eet.
localR = Eigen::AngleAxis<double>(tmpv[3],
hrp::Vector3(tmpv[0], tmpv[1], tmpv[2])).toRotationMatrix();
162 ee_map.insert(std::pair<std::string, ee_trans>(ee_name , eet));
163 base_name_map.insert(std::pair<std::string, std::string>(ee_name, ee_base));
164 std::cerr <<
"[" <<
m_profile.instance_name <<
"] End Effector [" << ee_name <<
"]" << ee_target <<
" " << ee_base << std::endl;
165 std::cerr <<
"[" <<
m_profile.instance_name <<
"] target = " << ee_target <<
", base = " << ee_base << std::endl;
166 std::cerr <<
"[" <<
m_profile.instance_name <<
"] localPos = " << eet.
localPos.format(Eigen::IOFormat(Eigen::StreamPrecision, 0,
", ",
", ",
"",
"",
" [",
"]")) <<
"[m]" << std::endl;
167 std::cerr <<
"[" <<
m_profile.instance_name <<
"] localR = " << eet.
localR.format(Eigen::IOFormat(Eigen::StreamPrecision, 0,
", ",
"\n",
" [",
"]")) << std::endl;
173 std::cerr <<
"[" <<
m_profile.instance_name <<
"] Add sensor_names" << std::endl;
175 std::string sensor_name =
m_forceIn[
i]->name();
177 std::string sensor_link_name;
180 sensor_link_name = sensor->
link->
name;
182 std::cerr <<
"[" <<
m_profile.instance_name <<
"] unknown force param" << std::endl;
187 bool is_ee_exists =
false;
188 for ( std::map<std::string, ee_trans>::iterator it =
ee_map.begin(); it !=
ee_map.end(); it++ ) {
190 std::string tmp_base_name = base_name_map[it->first];
191 while (alink != NULL && alink->
name != tmp_base_name && !is_ee_exists) {
192 if ( alink->
name == sensor_link_name ) {
200 std::cerr <<
"[" <<
m_profile.instance_name <<
"] No such ee setting for " << sensor_name <<
" and " << sensor_link_name <<
"!!. sensor_name for " << sensor_name <<
" cannot be added!!" << std::endl;
204 ee_map[ee_name].sensor_name = sensor_name;
205 std::cerr <<
"[" <<
m_profile.instance_name <<
"] sensor = " << sensor_name <<
", sensor-link = " << sensor_link_name <<
", ee_name = " << ee_name <<
", ee-link = " <<
ee_map[ee_name].target_name << std::endl;
241 std::cerr <<
"[" <<
m_profile.instance_name<<
"] onActivated(" << ec_id <<
")" << std::endl;
247 std::cerr <<
"[" <<
m_profile.instance_name<<
"] onDeactivated(" << ec_id <<
")" << std::endl;
251 #define DEBUGP ((m_debugLevel==1 && loop%200==0) || m_debugLevel > 1 ) 273 bool is_contact =
false;
324 std::vector<hrp::Vector3> foot_pos;
325 std::vector<hrp::Matrix33> foot_rot;
326 std::vector<std::string> leg_names = boost::assign::list_of(
"rleg")(
"lleg");
327 for (
size_t i = 0;
i < leg_names.size();
i++) {
329 foot_pos.push_back(target_link->
p + target_link->
R *
ee_map[leg_names[
i]].localPos);
330 foot_rot.push_back(target_link->
R *
ee_map[leg_names[i]].localR);
332 new_foot_mid_pos = (foot_pos[0]+foot_pos[1])/2.0;
333 rats::mid_rot(new_foot_mid_rot, 0.5, foot_rot[0], foot_rot[1]);
338 std::vector<rats::coordinates> leg_c_v;
341 std::vector<std::string> leg_names;
342 for ( std::map<std::string, ee_trans>::iterator it =
ee_map.begin(); it !=
ee_map.end(); it++ ) {
344 if (it->first.find(
"leg") != std::string::npos &&
m_contactStates.data[it->second.index]) leg_names.push_back(it->first);
346 for (
size_t i = 0;
i < leg_names.size();
i++) {
353 leg_c.
rot(0,0) = xv1(0); leg_c.
rot(1,0) = xv1(1); leg_c.
rot(2,0) = xv1(2);
354 leg_c.
rot(0,1) = yv1(0); leg_c.
rot(1,1) = yv1(1); leg_c.
rot(2,1) = yv1(2);
355 leg_c.
rot(0,2) = ez(0); leg_c.
rot(1,2) = ez(1); leg_c.
rot(2,2) = ez(2);
356 leg_c_v.push_back(leg_c);
358 if (leg_names.size() == 2) {
361 foot_origin_pos = tmpc.
pos;
362 foot_origin_rot = tmpc.
rot;
364 foot_origin_pos = leg_c_v[0].pos;
365 foot_origin_rot = leg_c_v[0].rot;
374 for (
unsigned int i = 0;
i <
m_robot->numJoints();
i++ ) {
378 m_robot->calcForwardKinematics();
380 std::vector<hrp::Vector3> octd_forces, octd_moments, octd_hposv;
385 fmrotT = fmrot.transpose();
387 std::string sensor_name =
m_forceIn[
i]->name();
390 for ( std::map<std::string, ee_trans>::iterator it =
ee_map.begin(); it !=
ee_map.end(); it++ ) {
391 if ( it->second.sensor_name == sensor_name ) {
394 ee_pos = target_link->
p + target_link->
R * eet.
localPos;
402 hrp::Vector3 ee_moment( (sensor_pos - ee_pos).
cross(sensor_force) + sensor_moment);
404 octd_hposv.push_back(fmrotT*(ee_pos - fmpos));
405 octd_forces.push_back(fmrotT*(sensor_force));
406 octd_moments.push_back(fmrotT*(ee_moment));
409 octd->checkDetection(octd_forces, octd_moments, octd_hposv);
413 for (
size_t i = 0;
i < log_data.size();
i++) {
427 m_robot->rootLink()->p = hrp::Vector3::Zero();
438 octd->startDetection(i_ref_diff_wrench, i_max_time);
440 for (
size_t i = 0;
i < i_ee_names.length();
i++) {
448 octd->startDetectionForGeneralizedWrench();
454 OpenHRP::ObjectContactTurnaroundDetectorService::DetectorMode tmpmode;
455 switch (
octd->getMode(index)) {
457 tmpmode = ObjectContactTurnaroundDetectorService::MODE_DETECTOR_IDLE;
460 tmpmode = ObjectContactTurnaroundDetectorService::MODE_STARTED;
463 tmpmode = ObjectContactTurnaroundDetectorService::MODE_DETECTED;
466 tmpmode = ObjectContactTurnaroundDetectorService::MODE_MAX_TIME;
469 tmpmode = ObjectContactTurnaroundDetectorService::MODE_OTHER_DETECTED;
472 tmpmode = ObjectContactTurnaroundDetectorService::MODE_DETECTOR_IDLE;
487 o_dms->length(
octd->getDetectGeneralizedWrenchDim());
488 for (
size_t i = 0;
i <
octd->getDetectGeneralizedWrenchDim();
i++) {
497 std::cerr <<
"[" <<
m_profile.instance_name <<
"] setObjectContactTurnaroundDetectorParam" << std::endl;
498 octd->setWrenchCutoffFreq(i_param_.wrench_cutoff_freq);
499 octd->setDwrenchCutoffFreq(i_param_.dwrench_cutoff_freq);
500 octd->setDetectRatioThre(i_param_.detect_ratio_thre);
501 octd->setStartRatioThre(i_param_.start_ratio_thre);
502 octd->setDetectTimeThre(i_param_.detect_time_thre);
503 octd->setStartTimeThre(i_param_.start_time_thre);
504 octd->setOtherDetectTimeThre(i_param_.other_detect_time_thre);
505 octd->setForgettingRatioThre(i_param_.forgetting_ratio_thre);
507 for (
size_t i = 0;
i < 3;
i++) tmp(
i) = i_param_.axis[
i];
509 for (
size_t i = 0;
i < 3;
i++) tmp(
i) = i_param_.moment_center[
i];
510 octd->setMomentCenter(tmp);
512 switch (i_param_.detector_total_wrench) {
513 case OpenHRP::ObjectContactTurnaroundDetectorService::TOTAL_FORCE:
516 case OpenHRP::ObjectContactTurnaroundDetectorService::TOTAL_MOMENT:
519 case OpenHRP::ObjectContactTurnaroundDetectorService::TOTAL_MOMENT2:
522 case OpenHRP::ObjectContactTurnaroundDetectorService::GENERALIZED_WRENCH:
528 octd->setDetectorTotalWrench(dtw);
529 octd->setIsHoldValues(i_param_.is_hold_values);
531 if ( (i_param_.constraint_conversion_matrix1.length() % 6 == 0) &&
532 (i_param_.constraint_conversion_matrix1.length() == i_param_.constraint_conversion_matrix2.length()) &&
533 (i_param_.constraint_conversion_matrix1.length() == i_param_.ref_dphi1.length()*6) ) {
534 size_t row_dim = i_param_.constraint_conversion_matrix1.length()/6;
535 std::vector<hrp::dvector6> tmpccm1(row_dim, hrp::dvector6::Zero());
536 for (
size_t i = 0;
i < row_dim;
i++) {
537 for (
size_t j = 0;
j < 6;
j++) {
538 tmpccm1[
i](
j) = i_param_.constraint_conversion_matrix1[
i*6+
j];
541 std::vector<hrp::dvector6> tmpccm2(row_dim, hrp::dvector6::Zero());
542 for (
size_t i = 0;
i < row_dim;
i++) {
543 for (
size_t j = 0;
j < 6;
j++) {
544 tmpccm2[
i](
j) = i_param_.constraint_conversion_matrix2[
i*6+
j];
547 std::vector<double> tmp_ref_dphi1;
548 for (
size_t i = 0;
i < i_param_.ref_dphi1.length();
i++) tmp_ref_dphi1.push_back(i_param_.ref_dphi1[
i]);
549 octd->setConstraintConversionMatricesRefDwrench(tmpccm1, tmpccm2, tmp_ref_dphi1);
551 std::cerr <<
"[" <<
m_profile.instance_name <<
"] Invalid constraint_conversion_matrix size (ccm1 = " 552 << i_param_.constraint_conversion_matrix1.length() <<
", ccm2 = " << i_param_.constraint_conversion_matrix2.length() <<
", ref_dw = " << i_param_.ref_dphi1.length() <<
")" << std::endl;
555 octd->setMaxTime(i_param_.max_time);
557 for (
size_t i = 0;
i < i_param_.limbs.length();
i++) {
562 std::cerr <<
"[" <<
m_profile.instance_name <<
"] sensor_names = [";
564 std::cerr <<
"]" << std::endl;
570 std::cerr <<
"[" <<
m_profile.instance_name <<
"] getObjectContactTurnaroundDetectorParam" << std::endl;
571 i_param_.wrench_cutoff_freq =
octd->getWrenchCutoffFreq();
572 i_param_.dwrench_cutoff_freq =
octd->getDwrenchCutoffFreq();
573 i_param_.detect_ratio_thre =
octd->getDetectRatioThre();
574 i_param_.start_ratio_thre =
octd->getStartRatioThre();
575 i_param_.detect_time_thre =
octd->getDetectTimeThre();
576 i_param_.start_time_thre =
octd->getStartTimeThre();
577 i_param_.other_detect_time_thre =
octd->getOtherDetectTimeThre();
578 i_param_.forgetting_ratio_thre =
octd->getForgettingRatioThre();
580 for (
size_t i = 0;
i < 3;
i++) i_param_.axis[
i] = tmp(
i);
581 tmp =
octd->getMomentCenter();
582 for (
size_t i = 0;
i < 3;
i++) i_param_.moment_center[
i] = tmp(
i);
583 OpenHRP::ObjectContactTurnaroundDetectorService::DetectorTotalWrench dtw;
584 switch (
octd->getDetectorTotalWrench()) {
586 dtw = OpenHRP::ObjectContactTurnaroundDetectorService::TOTAL_FORCE;
589 dtw = OpenHRP::ObjectContactTurnaroundDetectorService::TOTAL_MOMENT;
592 dtw = OpenHRP::ObjectContactTurnaroundDetectorService::TOTAL_MOMENT2;
595 dtw = OpenHRP::ObjectContactTurnaroundDetectorService::GENERALIZED_WRENCH;
600 i_param_.detector_total_wrench = dtw;
601 i_param_.is_hold_values =
octd->getIsHoldValues();
603 std::vector<hrp::dvector6> tmpccm1, tmpccm2;
604 std::vector<double> tmp_ref_dphi1;
605 octd->getConstraintConversionMatricesRefDwrench(tmpccm1, tmpccm2, tmp_ref_dphi1);
606 i_param_.constraint_conversion_matrix1.length(tmpccm1.size()*6);
607 for (
size_t i = 0;
i < tmpccm1.size();
i++) {
608 for (
size_t j = 0;
j < 6;
j++) {
609 i_param_.constraint_conversion_matrix1[
i*6+
j] = tmpccm1[
i](
j);
612 i_param_.constraint_conversion_matrix2.length(tmpccm2.size()*6);
613 for (
size_t i = 0;
i < tmpccm2.size();
i++) {
614 for (
size_t j = 0;
j < 6;
j++) {
615 i_param_.constraint_conversion_matrix2[
i*6+
j] = tmpccm2[
i](
j);
618 i_param_.ref_dphi1.length(tmp_ref_dphi1.size());
619 for (
size_t i = 0;
i < tmp_ref_dphi1.size();
i++) i_param_.ref_dphi1[
i] = tmp_ref_dphi1[
i];
620 i_param_.max_time =
octd->getMaxTime();
628 bool ObjectContactTurnaroundDetector::getObjectForcesMoments(OpenHRP::ObjectContactTurnaroundDetectorService::Dbl3Sequence_out o_forces, OpenHRP::ObjectContactTurnaroundDetectorService::Dbl3Sequence_out o_moments, OpenHRP::ObjectContactTurnaroundDetectorService::DblSequence3_out o_3dofwrench,
double& o_fric_coeff_wrench)
630 std::cerr <<
"[" <<
m_profile.instance_name <<
"] getObjectForcesMoments" << std::endl;
633 o_forces =
new OpenHRP::ObjectContactTurnaroundDetectorService::Dbl3Sequence ();
634 o_moments =
new OpenHRP::ObjectContactTurnaroundDetectorService::Dbl3Sequence ();
637 for (
size_t i = 0;
i < o_forces->length();
i++) o_forces[
i].
length(3);
638 for (
size_t i = 0;
i < o_moments->length();
i++) o_moments[
i].
length(3);
644 o_moments[
i][0] = o_moments[
i][1] = o_moments[
i][2] = 0.0;
646 o_3dofwrench =
new OpenHRP::ObjectContactTurnaroundDetectorService::DblSequence3 ();
647 o_3dofwrench->length(3);
648 for (
size_t i = 0;
i < 3;
i++) (*o_3dofwrench)[
i] = tmpv(
i);
649 o_fric_coeff_wrench =
octd->getFilteredFrictionCoeffWrenchWithHold()[0];
655 std::vector<double> tmp1 =
octd->getFilteredWrenchWithHold();
656 o_param.generalized_constraint_wrench1.length(tmp1.size());
657 for (
size_t i = 0;
i < tmp1.size();
i++) o_param.generalized_constraint_wrench1[
i] = tmp1[
i];
658 std::vector<double> tmp2 =
octd->getFilteredFrictionCoeffWrenchWithHold();
659 o_param.generalized_constraint_wrench2.length(tmp2.size());
660 for (
size_t i = 0;
i < tmp2.size();
i++) o_param.generalized_constraint_wrench2[
i] = tmp2[
i];
662 for (
size_t i = 0;
i < 6;
i++) o_param.resultant_wrench[
i] = tmpr(
i);
673 RTC::Create<ObjectContactTurnaroundDetector>,
674 RTC::Delete<ObjectContactTurnaroundDetector>);
ComponentProfile m_profile
png_infop png_charpp int png_charpp profile
bool stringTo(To &val, const char *str)
vstring split(const std::string &input, const std::string &delimiter, bool ignore_empty)
png_bytep png_bytep png_size_t length
coil::Properties & getProperties()
static Manager & instance()
bool addOutPort(const char *name, OutPortBase &outport)
boost::shared_ptr< Body > BodyPtr
Matrix33 rotFromRpy(const Vector3 &rpy)
std::vector< std::string > vstring
coil::Properties & getConfig()
void rotm3times(hrp::Matrix33 &m12, const hrp::Matrix33 &m1, const hrp::Matrix33 &m2)
ExecutionContextHandle_t UniqueId
bool bindParameter(const char *param_name, VarType &var, const char *def_val, bool(*trans)(VarType &, const char *)=coil::stringTo)
def j(str, encoding="cp932")
CORBA::Long find(const CorbaSequence &seq, Functor f)
int loadBodyFromModelLoader(::World *world, const char *name, const char *url, CosNaming::NamingContext_var cxt)
Vector3 cross(const Vector3 &v1, const Vector3 &v2)
void registerInPort(const char *name, InPortBase &inport)
bool addPort(PortBase &port)
virtual bool write(DataType &value)
Eigen::Matrix< double, 6, 1 > dvector6
bool addInPort(const char *name, InPortBase &inport)
void mid_rot(hrp::Matrix33 &mid_rot, const double p, const hrp::Matrix33 &rot1, const hrp::Matrix33 &rot2, const double eps)
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)
void mid_coords(coordinates &mid_coords, const double p, const coordinates &c1, const coordinates &c2, const double eps)
bool registerProvider(const char *instance_name, const char *type_name, PortableServer::RefCountServantBase &provider)