ObjectContactTurnaroundDetector.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00010 #include <rtm/CorbaNaming.h>
00011 #include <hrpModel/Link.h>
00012 #include <hrpModel/Sensor.h>
00013 #include <hrpModel/ModelLoaderUtil.h>
00014 #include "ObjectContactTurnaroundDetector.h"
00015 #include "../ImpedanceController/RatsMatrix.h"
00016 #include "hrpsys/util/Hrpsys.h"
00017 #include <boost/assign.hpp>
00018 
00019 typedef coil::Guard<coil::Mutex> Guard;
00020 
00021 // Module specification
00022 // <rtc-template block="module_spec">
00023 static const char* objectcontactturnarounddetector_spec[] =
00024     {
00025         "implementation_id", "ObjectContactTurnaroundDetector",
00026         "type_name",         "ObjectContactTurnaroundDetector",
00027         "description",       "object contact turnaround detector component",
00028         "version",           HRPSYS_PACKAGE_VERSION,
00029         "vendor",            "AIST",
00030         "category",          "example",
00031         "activity_type",     "DataFlowComponent",
00032         "max_instance",      "10",
00033         "language",          "C++",
00034         "lang_type",         "compile",
00035         // Configuration variables
00036         "conf.default.debugLevel", "0",
00037         ""
00038     };
00039 // </rtc-template>
00040 
00041 ObjectContactTurnaroundDetector::ObjectContactTurnaroundDetector(RTC::Manager* manager)
00042     : RTC::DataFlowComponentBase(manager),
00043       // <rtc-template block="initializer">
00044       m_qCurrentIn("qCurrent", m_qCurrent),
00045       m_rpyIn("rpy", m_rpy),
00046       m_contactStatesIn("contactStates", m_contactStates),
00047       m_octdDataOut("octdData", m_octdData),
00048       m_ObjectContactTurnaroundDetectorServicePort("ObjectContactTurnaroundDetectorService"),
00049       // </rtc-template>
00050       m_robot(hrp::BodyPtr()),
00051       m_debugLevel(0),
00052       dummy(0)
00053 {
00054     m_service0.octd(this);
00055 }
00056 
00057 ObjectContactTurnaroundDetector::~ObjectContactTurnaroundDetector()
00058 {
00059 }
00060 
00061 
00062 RTC::ReturnCode_t ObjectContactTurnaroundDetector::onInitialize()
00063 {
00064     std::cerr << "[" << m_profile.instance_name << "] onInitialize()" << std::endl;
00065     bindParameter("debugLevel", m_debugLevel, "0");
00066 
00067     // Registration: InPort/OutPort/Service
00068     // <rtc-template block="registration">
00069     // Set InPort buffers
00070     addInPort("qCurrent", m_qCurrentIn);
00071     addInPort("rpy", m_rpyIn);
00072     addInPort("contactStates", m_contactStatesIn);
00073 
00074     // Set OutPort buffer
00075     addOutPort("octdData", m_octdDataOut);
00076   
00077     // Set service provider to Ports
00078     m_ObjectContactTurnaroundDetectorServicePort.registerProvider("service0", "ObjectContactTurnaroundDetectorService", m_service0);
00079   
00080     // Set service consumers to Ports
00081   
00082     // Set CORBA Service Ports
00083     addPort(m_ObjectContactTurnaroundDetectorServicePort);
00084   
00085     // </rtc-template>
00086     // <rtc-template block="bind_config">
00087     // Bind variables and configuration variable
00088   
00089     // </rtc-template>
00090 
00091     RTC::Properties& prop = getProperties();
00092     coil::stringTo(m_dt, prop["dt"].c_str());
00093 
00094     m_robot = hrp::BodyPtr(new hrp::Body());
00095 
00096     RTC::Manager& rtcManager = RTC::Manager::instance();
00097     std::string nameServer = rtcManager.getConfig()["corba.nameservers"];
00098     int comPos = nameServer.find(",");
00099     if (comPos < 0){
00100         comPos = nameServer.length();
00101     }
00102     nameServer = nameServer.substr(0, comPos);
00103     RTC::CorbaNaming naming(rtcManager.getORB(), nameServer.c_str());
00104     if (!loadBodyFromModelLoader(m_robot, prop["model"].c_str(), 
00105                                  CosNaming::NamingContext::_duplicate(naming.getRootContext())
00106                                  )){
00107       std::cerr << "[" << m_profile.instance_name << "] failed to load model[" << prop["model"] << "]" << std::endl;
00108       return RTC::RTC_ERROR;
00109     }
00110 
00111 
00112     // Setting for wrench data ports (real)
00113     std::vector<std::string> fsensor_names;
00114     //   find names for real force sensors
00115     unsigned int npforce = m_robot->numSensors(hrp::Sensor::FORCE);
00116     for (unsigned int i=0; i<npforce; i++){
00117         fsensor_names.push_back(m_robot->sensor(hrp::Sensor::FORCE, i)->name);
00118     }
00119     //   add ports for all force sensors
00120     unsigned int nforce  = npforce;
00121     m_force.resize(nforce);
00122     m_forceIn.resize(nforce);
00123     std::cerr << "[" << m_profile.instance_name << "] force sensor ports" << std::endl;
00124     for (unsigned int i=0; i<nforce; i++){
00125         m_forceIn[i] = new InPort<TimedDoubleSeq>(fsensor_names[i].c_str(), m_force[i]);
00126         m_force[i].data.length(6);
00127         registerInPort(fsensor_names[i].c_str(), *m_forceIn[i]);
00128         std::cerr << "[" << m_profile.instance_name << "]   name = " << fsensor_names[i] << std::endl;
00129     }
00130 
00131     unsigned int dof = m_robot->numJoints();
00132     for ( unsigned int i = 0 ; i < dof; i++ ){
00133       if ( (int)i != m_robot->joint(i)->jointId ) {
00134         std::cerr << "[" << m_profile.instance_name << "] jointId is not equal to the index number" << std::endl;
00135         return RTC::RTC_ERROR;
00136       }
00137     }
00138 
00139     // setting from conf file
00140     // rleg,TARGET_LINK,BASE_LINK,x,y,z,rx,ry,rz,rth #<=pos + rot (axis+angle)
00141     coil::vstring end_effectors_str = coil::split(prop["end_effectors"], ",");
00142     std::map<std::string, std::string> base_name_map;
00143     if (end_effectors_str.size() > 0) {
00144         size_t prop_num = 10;
00145         size_t num = end_effectors_str.size()/prop_num;
00146         for (size_t i = 0; i < num; i++) {
00147             std::string ee_name, ee_target, ee_base;
00148             coil::stringTo(ee_name, end_effectors_str[i*prop_num].c_str());
00149             coil::stringTo(ee_target, end_effectors_str[i*prop_num+1].c_str());
00150             coil::stringTo(ee_base, end_effectors_str[i*prop_num+2].c_str());
00151             ee_trans eet;
00152             for (size_t j = 0; j < 3; j++) {
00153                 coil::stringTo(eet.localPos(j), end_effectors_str[i*prop_num+3+j].c_str());
00154             }
00155             double tmpv[4];
00156             for (int j = 0; j < 4; j++ ) {
00157                 coil::stringTo(tmpv[j], end_effectors_str[i*prop_num+6+j].c_str());
00158             }
00159             eet.localR = Eigen::AngleAxis<double>(tmpv[3], hrp::Vector3(tmpv[0], tmpv[1], tmpv[2])).toRotationMatrix(); // rotation in VRML is represented by axis + angle
00160             eet.target_name = ee_target;
00161             eet.index = i;
00162             ee_map.insert(std::pair<std::string, ee_trans>(ee_name , eet));
00163             base_name_map.insert(std::pair<std::string, std::string>(ee_name, ee_base));
00164             std::cerr << "[" << m_profile.instance_name << "] End Effector [" << ee_name << "]" << ee_target << " " << ee_base << std::endl;
00165             std::cerr << "[" << m_profile.instance_name << "]   target = " << ee_target << ", base = " << ee_base << std::endl;
00166             std::cerr << "[" << m_profile.instance_name << "]   localPos = " << eet.localPos.format(Eigen::IOFormat(Eigen::StreamPrecision, 0, ", ", ", ", "", "", "    [", "]")) << "[m]" << std::endl;
00167             std::cerr << "[" << m_profile.instance_name << "]   localR = " << eet.localR.format(Eigen::IOFormat(Eigen::StreamPrecision, 0, ", ", "\n", "    [", "]")) << std::endl;
00168         }
00169         m_contactStates.data.length(num);
00170     }
00171 
00172     // initialize sensor_names
00173     std::cerr << "[" << m_profile.instance_name << "] Add sensor_names" << std::endl;
00174     for (unsigned int i=0; i<m_forceIn.size(); i++){
00175         std::string sensor_name = m_forceIn[i]->name();
00176         hrp::ForceSensor* sensor = m_robot->sensor<hrp::ForceSensor>(sensor_name);
00177         std::string sensor_link_name;
00178         if ( sensor ) {
00179             // real force sensor
00180             sensor_link_name = sensor->link->name;
00181         } else {
00182             std::cerr << "[" << m_profile.instance_name << "]   unknown force param" << std::endl;
00183             continue;
00184         }
00185         // 1. Check whether adequate ee_map exists for the sensor.
00186         std::string ee_name;
00187         bool is_ee_exists = false;
00188         for ( std::map<std::string, ee_trans>::iterator it = ee_map.begin(); it != ee_map.end(); it++ ) {
00189             hrp::Link* alink = m_robot->link(it->second.target_name);
00190             std::string tmp_base_name = base_name_map[it->first];
00191             while (alink != NULL && alink->name != tmp_base_name && !is_ee_exists) {
00192                 if ( alink->name == sensor_link_name ) {
00193                     is_ee_exists = true;
00194                     ee_name = it->first;
00195                 }
00196                 alink = alink->parent;
00197             }
00198         }
00199         if (!is_ee_exists) {
00200             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;
00201             continue;
00202         }
00203         // 4. Set impedance param
00204         ee_map[ee_name].sensor_name = sensor_name;
00205         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;
00206     }
00207 
00208     octd = boost::shared_ptr<ObjectContactTurnaroundDetectorBase>(new ObjectContactTurnaroundDetectorBase(m_dt));
00209     octd->setPrintStr(std::string(m_profile.instance_name));
00210 
00211     // allocate memory for outPorts
00212     loop = 0;
00213     m_octdData.data.length(4); // mode, raw, filtered, dfiltered
00214 
00215     return RTC::RTC_OK;
00216 }
00217 
00218 
00219 
00220 RTC::ReturnCode_t ObjectContactTurnaroundDetector::onFinalize()
00221 {
00222     return RTC::RTC_OK;
00223 }
00224 
00225 /*
00226   RTC::ReturnCode_t ObjectContactTurnaroundDetector::onStartup(RTC::UniqueId ec_id)
00227   {
00228   return RTC::RTC_OK;
00229   }
00230 */
00231 
00232 /*
00233   RTC::ReturnCode_t ObjectContactTurnaroundDetector::onShutdown(RTC::UniqueId ec_id)
00234   {
00235   return RTC::RTC_OK;
00236   }
00237 */
00238 
00239 RTC::ReturnCode_t ObjectContactTurnaroundDetector::onActivated(RTC::UniqueId ec_id)
00240 {
00241     std::cerr << "[" << m_profile.instance_name<< "] onActivated(" << ec_id << ")" << std::endl;
00242     return RTC::RTC_OK;
00243 }
00244 
00245 RTC::ReturnCode_t ObjectContactTurnaroundDetector::onDeactivated(RTC::UniqueId ec_id)
00246 {
00247   std::cerr << "[" << m_profile.instance_name<< "] onDeactivated(" << ec_id << ")" << std::endl;
00248   return RTC::RTC_OK;
00249 }
00250 
00251 #define DEBUGP ((m_debugLevel==1 && loop%200==0) || m_debugLevel > 1 )
00252 RTC::ReturnCode_t ObjectContactTurnaroundDetector::onExecute(RTC::UniqueId ec_id)
00253 {
00254     //std::cout << "ObjectContactTurnaroundDetector::onExecute(" << ec_id << ")" << std::endl;
00255     loop ++;
00256 
00257     // check dataport input
00258     for (unsigned int i=0; i<m_forceIn.size(); i++){
00259         if ( m_forceIn[i]->isNew() ) {
00260             m_forceIn[i]->read();
00261         }
00262     }
00263     if (m_rpyIn.isNew()) {
00264       m_rpyIn.read();
00265     }
00266     if (m_qCurrentIn.isNew()) {
00267       m_qCurrentIn.read();
00268       m_octdData.tm = m_qCurrent.tm;
00269     }
00270     if (m_contactStatesIn.isNew()) {
00271       m_contactStatesIn.read();
00272     }
00273     bool is_contact = false;
00274     for (size_t i = 0; i < m_contactStates.data.length(); i++) {
00275       if (m_contactStates.data[i]) is_contact = true;
00276     }
00277     if ( m_qCurrent.data.length() ==  m_robot->numJoints() &&
00278          is_contact && // one or more limbs are in contact
00279          ee_map.find("rleg") != ee_map.end() && ee_map.find("lleg") != ee_map.end() ) { // if legged robot
00280         Guard guard(m_mutex);
00281         calcObjectContactTurnaroundDetectorState();
00282         m_octdDataOut.write();
00283     }
00284     return RTC::RTC_OK;
00285 }
00286 
00287 /*
00288   RTC::ReturnCode_t ObjectContactTurnaroundDetector::onAborting(RTC::UniqueId ec_id)
00289   {
00290   return RTC::RTC_OK;
00291   }
00292 */
00293 
00294 /*
00295   RTC::ReturnCode_t ObjectContactTurnaroundDetector::onError(RTC::UniqueId ec_id)
00296   {
00297   return RTC::RTC_OK;
00298   }
00299 */
00300 
00301 /*
00302   RTC::ReturnCode_t ObjectContactTurnaroundDetector::onReset(RTC::UniqueId ec_id)
00303   {
00304   return RTC::RTC_OK;
00305   }
00306 */
00307 
00308 /*
00309   RTC::ReturnCode_t ObjectContactTurnaroundDetector::onStateUpdate(RTC::UniqueId ec_id)
00310   {
00311   return RTC::RTC_OK;
00312   }
00313 */
00314 
00315 /*
00316   RTC::ReturnCode_t ObjectContactTurnaroundDetector::onRateChanged(RTC::UniqueId ec_id)
00317   {
00318   return RTC::RTC_OK;
00319   }
00320 */
00321 
00322 void ObjectContactTurnaroundDetector::calcFootMidCoords (hrp::Vector3& new_foot_mid_pos, hrp::Matrix33& new_foot_mid_rot)
00323 {
00324   std::vector<hrp::Vector3> foot_pos;
00325   std::vector<hrp::Matrix33> foot_rot;
00326   std::vector<std::string> leg_names = boost::assign::list_of("rleg")("lleg");
00327   for (size_t i = 0; i < leg_names.size(); i++) {
00328     hrp::Link* target_link = m_robot->link(ee_map[leg_names[i]].target_name);
00329     foot_pos.push_back(target_link->p + target_link->R * ee_map[leg_names[i]].localPos);
00330     foot_rot.push_back(target_link->R * ee_map[leg_names[i]].localR);
00331   }
00332   new_foot_mid_pos = (foot_pos[0]+foot_pos[1])/2.0;
00333   rats::mid_rot(new_foot_mid_rot, 0.5, foot_rot[0], foot_rot[1]);
00334 }
00335 
00336 void ObjectContactTurnaroundDetector::calcFootOriginCoords (hrp::Vector3& foot_origin_pos, hrp::Matrix33& foot_origin_rot)
00337 {
00338   std::vector<rats::coordinates> leg_c_v;
00339   hrp::Vector3 ez = hrp::Vector3::UnitZ();
00340   hrp::Vector3 ex = hrp::Vector3::UnitX();
00341   std::vector<std::string> leg_names;
00342   for ( std::map<std::string, ee_trans>::iterator it = ee_map.begin(); it != ee_map.end(); it++ ) {
00343       // If rleg or lleg, and if reference contact states is true
00344       if (it->first.find("leg") != std::string::npos && m_contactStates.data[it->second.index]) leg_names.push_back(it->first);
00345   }
00346   for (size_t i = 0; i < leg_names.size(); i++) {
00347     hrp::Link* target_link = m_robot->link(ee_map[leg_names[i]].target_name);
00348     rats::coordinates leg_c(hrp::Vector3(target_link->p + target_link->R * ee_map[leg_names[i]].localPos), hrp::Matrix33(target_link->R * ee_map[leg_names[i]].localR));
00349     hrp::Vector3 xv1(leg_c.rot * ex);
00350     xv1(2)=0.0;
00351     xv1.normalize();
00352     hrp::Vector3 yv1(ez.cross(xv1));
00353     leg_c.rot(0,0) = xv1(0); leg_c.rot(1,0) = xv1(1); leg_c.rot(2,0) = xv1(2);
00354     leg_c.rot(0,1) = yv1(0); leg_c.rot(1,1) = yv1(1); leg_c.rot(2,1) = yv1(2);
00355     leg_c.rot(0,2) = ez(0); leg_c.rot(1,2) = ez(1); leg_c.rot(2,2) = ez(2);
00356     leg_c_v.push_back(leg_c);
00357   }
00358   if (leg_names.size() == 2) {
00359     rats::coordinates tmpc;
00360     rats::mid_coords(tmpc, 0.5, leg_c_v[0], leg_c_v[1]);
00361     foot_origin_pos = tmpc.pos;
00362     foot_origin_rot = tmpc.rot;
00363   } else { // size = 1
00364     foot_origin_pos = leg_c_v[0].pos;
00365     foot_origin_rot = leg_c_v[0].rot;
00366   }
00367 }
00368 
00369 void ObjectContactTurnaroundDetector::calcObjectContactTurnaroundDetectorState()
00370 {
00371     // TODO
00372     // Currently only for legged robots
00373     // Set actual state
00374     for ( unsigned int i = 0; i < m_robot->numJoints(); i++ ) {
00375         m_robot->joint(i)->q = m_qCurrent.data[i];
00376     }
00377     updateRootLinkPosRot(m_rpy);
00378     m_robot->calcForwardKinematics();
00379     // Calc
00380     std::vector<hrp::Vector3> octd_forces, octd_moments, octd_hposv;
00381     hrp::Vector3 fmpos;
00382     hrp::Matrix33 fmrot, fmrotT;
00383     //calcFootMidCoords(fmpos, fmrot);
00384     calcFootOriginCoords(fmpos, fmrot);
00385     fmrotT = fmrot.transpose();
00386     for (unsigned int i=0; i<m_forceIn.size(); i++) {
00387         std::string sensor_name = m_forceIn[i]->name();
00388         if ( find(octd_sensor_names.begin(), octd_sensor_names.end(), sensor_name) != octd_sensor_names.end() ) {
00389             hrp::Vector3 ee_pos; // End Effector Position
00390             for ( std::map<std::string, ee_trans>::iterator it = ee_map.begin(); it != ee_map.end(); it++ ) {
00391                 if ( it->second.sensor_name == sensor_name ) {
00392                     ee_trans& eet = it->second;
00393                     hrp::Link* target_link = m_robot->link(eet.target_name);
00394                     ee_pos = target_link->p + target_link->R * eet.localPos;
00395                 }
00396             }
00397             hrp::ForceSensor* sensor = m_robot->sensor<hrp::ForceSensor>(sensor_name);
00398             hrp::Matrix33 sensor_rot = sensor->link->R * sensor->localR;
00399             hrp::Vector3 sensor_pos(sensor->link->R * sensor->localPos + sensor->link->p);
00400             hrp::Vector3 sensor_force(sensor_rot*hrp::Vector3(m_force[i].data[0], m_force[i].data[1], m_force[i].data[2]));
00401             hrp::Vector3 sensor_moment(sensor_rot*hrp::Vector3(m_force[i].data[3], m_force[i].data[4], m_force[i].data[5]));
00402             hrp::Vector3 ee_moment( (sensor_pos - ee_pos).cross(sensor_force) + sensor_moment);
00403             // Change to FootOriginCoords relative values
00404             octd_hposv.push_back(fmrotT*(ee_pos - fmpos)); // Change to FootOriginCoords relative hand pos
00405             octd_forces.push_back(fmrotT*(sensor_force)); // Change to FootOriginCoords relative ee force, and sensor force = ee force
00406             octd_moments.push_back(fmrotT*(ee_moment)); // Change to FootOriginCoords relative ee force
00407         }
00408     }
00409     octd->checkDetection(octd_forces, octd_moments, octd_hposv);
00410     // octdData
00411     hrp::dvector log_data = octd->getDataForLogger();
00412     if (m_octdData.data.length() != log_data.size()) m_octdData.data.length(log_data.size());
00413     for (size_t i = 0; i < log_data.size(); i++) {
00414         m_octdData.data[i] = log_data(i);
00415     }
00416 };
00417 
00418 //
00419 
00420 void ObjectContactTurnaroundDetector::updateRootLinkPosRot (TimedOrientation3D tmprpy)
00421 {
00422   if ( m_robot->numSensors(hrp::Sensor::ACCELERATION) > 0) {
00423       hrp::Sensor *sensor = m_robot->sensor(hrp::Sensor::ACCELERATION, 0);
00424       hrp::Matrix33 tmpr;
00425       rats::rotm3times(tmpr, hrp::Matrix33(sensor->link->R*sensor->localR).transpose(), m_robot->rootLink()->R);
00426       rats::rotm3times(m_robot->rootLink()->R, hrp::rotFromRpy(tmprpy.data.r, tmprpy.data.p, tmprpy.data.y), tmpr);
00427       m_robot->rootLink()->p = hrp::Vector3::Zero();
00428   }
00429 }
00430 
00431 //
00432 // ObjectContactTurnaroundDetector
00433 //
00434 
00435 void ObjectContactTurnaroundDetector::startObjectContactTurnaroundDetection(const double i_ref_diff_wrench, const double i_max_time, const OpenHRP::ObjectContactTurnaroundDetectorService::StrSequence& i_ee_names)
00436 {
00437     Guard guard(m_mutex);
00438     octd->startDetection(i_ref_diff_wrench, i_max_time);
00439     octd_sensor_names.clear();
00440     for (size_t i = 0; i < i_ee_names.length(); i++) {
00441         octd_sensor_names.push_back(ee_map[std::string(i_ee_names[i])].sensor_name);
00442     }
00443 }
00444 
00445 bool ObjectContactTurnaroundDetector::startObjectContactTurnaroundDetectionForGeneralizedWrench ()
00446 {
00447     Guard guard(m_mutex);
00448     octd->startDetectionForGeneralizedWrench();
00449     return true;
00450 }
00451 
00452 OpenHRP::ObjectContactTurnaroundDetectorService::DetectorMode ObjectContactTurnaroundDetector::checkObjectContactTurnaroundDetectionCommon(const size_t index)
00453 {
00454     OpenHRP::ObjectContactTurnaroundDetectorService::DetectorMode tmpmode;
00455     switch (octd->getMode(index)) {
00456     case ObjectContactTurnaroundDetectorBase::MODE_IDLE:
00457         tmpmode = ObjectContactTurnaroundDetectorService::MODE_DETECTOR_IDLE;
00458         break;
00459     case ObjectContactTurnaroundDetectorBase::MODE_STARTED:
00460         tmpmode = ObjectContactTurnaroundDetectorService::MODE_STARTED;
00461         break;
00462     case ObjectContactTurnaroundDetectorBase::MODE_DETECTED:
00463         tmpmode = ObjectContactTurnaroundDetectorService::MODE_DETECTED;
00464         break;
00465     case ObjectContactTurnaroundDetectorBase::MODE_MAX_TIME:
00466         tmpmode = ObjectContactTurnaroundDetectorService::MODE_MAX_TIME;
00467         break;
00468     case ObjectContactTurnaroundDetectorBase::MODE_OTHER_DETECTED:
00469         tmpmode = ObjectContactTurnaroundDetectorService::MODE_OTHER_DETECTED;
00470         break;
00471     default:
00472         tmpmode = ObjectContactTurnaroundDetectorService::MODE_DETECTOR_IDLE;
00473         break;
00474     }
00475     return tmpmode;
00476 }
00477 
00478 OpenHRP::ObjectContactTurnaroundDetectorService::DetectorMode ObjectContactTurnaroundDetector::checkObjectContactTurnaroundDetection()
00479 {
00480     Guard guard(m_mutex);
00481     return checkObjectContactTurnaroundDetectionCommon(0);
00482 }
00483 
00484 bool ObjectContactTurnaroundDetector::checkObjectContactTurnaroundDetectionForGeneralizedWrench(OpenHRP::ObjectContactTurnaroundDetectorService::DetectorModeSequence_out o_dms)
00485 {
00486     Guard guard(m_mutex);
00487     o_dms->length(octd->getDetectGeneralizedWrenchDim());
00488     for (size_t i = 0; i < octd->getDetectGeneralizedWrenchDim(); i++) {
00489         o_dms[i] = checkObjectContactTurnaroundDetectionCommon(i);
00490     }
00491     return true;
00492 }
00493 
00494 bool ObjectContactTurnaroundDetector::setObjectContactTurnaroundDetectorParam(const OpenHRP::ObjectContactTurnaroundDetectorService::objectContactTurnaroundDetectorParam &i_param_)
00495 {
00496     Guard guard(m_mutex);
00497     std::cerr << "[" << m_profile.instance_name << "] setObjectContactTurnaroundDetectorParam" << std::endl;
00498     octd->setWrenchCutoffFreq(i_param_.wrench_cutoff_freq);
00499     octd->setDwrenchCutoffFreq(i_param_.dwrench_cutoff_freq);
00500     octd->setDetectRatioThre(i_param_.detect_ratio_thre);
00501     octd->setStartRatioThre(i_param_.start_ratio_thre);
00502     octd->setDetectTimeThre(i_param_.detect_time_thre);
00503     octd->setStartTimeThre(i_param_.start_time_thre);
00504     octd->setOtherDetectTimeThre(i_param_.other_detect_time_thre);
00505     octd->setForgettingRatioThre(i_param_.forgetting_ratio_thre);
00506     hrp::Vector3 tmp;
00507     for (size_t i = 0; i < 3; i++) tmp(i) = i_param_.axis[i];
00508     octd->setAxis(tmp);
00509     for (size_t i = 0; i < 3; i++) tmp(i) = i_param_.moment_center[i];
00510     octd->setMomentCenter(tmp);
00511     ObjectContactTurnaroundDetectorBase::detector_total_wrench dtw;
00512     switch (i_param_.detector_total_wrench) {
00513     case OpenHRP::ObjectContactTurnaroundDetectorService::TOTAL_FORCE:
00514         dtw = ObjectContactTurnaroundDetectorBase::TOTAL_FORCE;
00515         break;
00516     case OpenHRP::ObjectContactTurnaroundDetectorService::TOTAL_MOMENT:
00517         dtw = ObjectContactTurnaroundDetectorBase::TOTAL_MOMENT;
00518         break;
00519     case OpenHRP::ObjectContactTurnaroundDetectorService::TOTAL_MOMENT2:
00520         dtw = ObjectContactTurnaroundDetectorBase::TOTAL_MOMENT2;
00521         break;
00522     case OpenHRP::ObjectContactTurnaroundDetectorService::GENERALIZED_WRENCH:
00523         dtw = ObjectContactTurnaroundDetectorBase::GENERALIZED_WRENCH;
00524         break;
00525     default:
00526         break;
00527     }
00528     octd->setDetectorTotalWrench(dtw);
00529     octd->setIsHoldValues(i_param_.is_hold_values);
00530     // For GENERALIZED_WRENCH mode
00531     if ( (i_param_.constraint_conversion_matrix1.length() % 6 == 0) &&
00532          (i_param_.constraint_conversion_matrix1.length() == i_param_.constraint_conversion_matrix2.length()) &&
00533          (i_param_.constraint_conversion_matrix1.length() == i_param_.ref_dphi1.length()*6) ) {
00534         size_t row_dim = i_param_.constraint_conversion_matrix1.length()/6;
00535         std::vector<hrp::dvector6> tmpccm1(row_dim, hrp::dvector6::Zero());
00536         for (size_t i = 0; i < row_dim; i++) {
00537             for (size_t j = 0; j < 6; j++) {
00538                 tmpccm1[i](j) = i_param_.constraint_conversion_matrix1[i*6+j];
00539             }
00540         }
00541         std::vector<hrp::dvector6> tmpccm2(row_dim, hrp::dvector6::Zero());
00542         for (size_t i = 0; i < row_dim; i++) {
00543             for (size_t j = 0; j < 6; j++) {
00544                 tmpccm2[i](j) = i_param_.constraint_conversion_matrix2[i*6+j];
00545             }
00546         }
00547         std::vector<double> tmp_ref_dphi1;
00548         for (size_t i = 0; i < i_param_.ref_dphi1.length(); i++) tmp_ref_dphi1.push_back(i_param_.ref_dphi1[i]);
00549         octd->setConstraintConversionMatricesRefDwrench(tmpccm1, tmpccm2, tmp_ref_dphi1);
00550     } else {
00551         std::cerr << "[" << m_profile.instance_name << "]   Invalid constraint_conversion_matrix size (ccm1 = "
00552                   << i_param_.constraint_conversion_matrix1.length() << ", ccm2 = " << i_param_.constraint_conversion_matrix2.length() << ", ref_dw = " << i_param_.ref_dphi1.length() << ")" << std::endl;
00553         return false;
00554     }
00555     octd->setMaxTime(i_param_.max_time);
00556     octd_sensor_names.clear();
00557     for (size_t i = 0; i < i_param_.limbs.length(); i++) {
00558         octd_sensor_names.push_back(ee_map[std::string(i_param_.limbs[i])].sensor_name);
00559     }
00560     // Print
00561     octd->printParams();
00562     std::cerr << "[" << m_profile.instance_name << "]    sensor_names = [";
00563     for (size_t i = 0; i < octd_sensor_names.size(); i++) std::cerr << getEENameFromSensorName(octd_sensor_names[i]) << " ";
00564     std::cerr << "]" << std::endl;
00565     return true;
00566 };
00567 
00568 bool ObjectContactTurnaroundDetector::getObjectContactTurnaroundDetectorParam(OpenHRP::ObjectContactTurnaroundDetectorService::objectContactTurnaroundDetectorParam& i_param_)
00569 {
00570     std::cerr << "[" << m_profile.instance_name << "] getObjectContactTurnaroundDetectorParam" << std::endl;
00571     i_param_.wrench_cutoff_freq = octd->getWrenchCutoffFreq();
00572     i_param_.dwrench_cutoff_freq = octd->getDwrenchCutoffFreq();
00573     i_param_.detect_ratio_thre = octd->getDetectRatioThre();
00574     i_param_.start_ratio_thre = octd->getStartRatioThre();
00575     i_param_.detect_time_thre = octd->getDetectTimeThre();
00576     i_param_.start_time_thre = octd->getStartTimeThre();
00577     i_param_.other_detect_time_thre = octd->getOtherDetectTimeThre();
00578     i_param_.forgetting_ratio_thre = octd->getForgettingRatioThre();
00579     hrp::Vector3 tmp = octd->getAxis();
00580     for (size_t i = 0; i < 3; i++) i_param_.axis[i] = tmp(i);
00581     tmp = octd->getMomentCenter();
00582     for (size_t i = 0; i < 3; i++) i_param_.moment_center[i] = tmp(i);
00583     OpenHRP::ObjectContactTurnaroundDetectorService::DetectorTotalWrench dtw;
00584     switch (octd->getDetectorTotalWrench()) {
00585     case ObjectContactTurnaroundDetectorBase::TOTAL_FORCE:
00586         dtw = OpenHRP::ObjectContactTurnaroundDetectorService::TOTAL_FORCE;
00587         break;
00588     case ObjectContactTurnaroundDetectorBase::TOTAL_MOMENT:
00589         dtw = OpenHRP::ObjectContactTurnaroundDetectorService::TOTAL_MOMENT;
00590         break;
00591     case ObjectContactTurnaroundDetectorBase::TOTAL_MOMENT2:
00592         dtw = OpenHRP::ObjectContactTurnaroundDetectorService::TOTAL_MOMENT2;
00593         break;
00594     case ObjectContactTurnaroundDetectorBase::GENERALIZED_WRENCH:
00595         dtw = OpenHRP::ObjectContactTurnaroundDetectorService::GENERALIZED_WRENCH;
00596         break;
00597     default:
00598         break;
00599     }
00600     i_param_.detector_total_wrench = dtw;
00601     i_param_.is_hold_values = octd->getIsHoldValues();
00602     // For GENERALIZED_WRENCH mode
00603     std::vector<hrp::dvector6> tmpccm1, tmpccm2;
00604     std::vector<double> tmp_ref_dphi1;
00605     octd->getConstraintConversionMatricesRefDwrench(tmpccm1, tmpccm2, tmp_ref_dphi1);
00606     i_param_.constraint_conversion_matrix1.length(tmpccm1.size()*6);
00607     for (size_t i = 0; i < tmpccm1.size(); i++) {
00608         for (size_t j = 0; j < 6; j++) {
00609             i_param_.constraint_conversion_matrix1[i*6+j] = tmpccm1[i](j);
00610         }
00611     }
00612     i_param_.constraint_conversion_matrix2.length(tmpccm2.size()*6);
00613     for (size_t i = 0; i < tmpccm2.size(); i++) {
00614         for (size_t j = 0; j < 6; j++) {
00615             i_param_.constraint_conversion_matrix2[i*6+j] = tmpccm2[i](j);
00616         }
00617     }
00618     i_param_.ref_dphi1.length(tmp_ref_dphi1.size());
00619     for (size_t i = 0; i < tmp_ref_dphi1.size(); i++) i_param_.ref_dphi1[i] = tmp_ref_dphi1[i];
00620     i_param_.max_time = octd->getMaxTime();
00621     i_param_.limbs.length(octd_sensor_names.size());
00622     for (size_t i = 0; i < octd_sensor_names.size(); i++) {
00623         i_param_.limbs[i] = getEENameFromSensorName(octd_sensor_names[i]).c_str();
00624     }
00625     return true;
00626 }
00627 
00628 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)
00629 {
00630     std::cerr << "[" << m_profile.instance_name << "] getObjectForcesMoments" << std::endl;
00631     if (octd_sensor_names.size() == 0) return false;
00632     hrp::Vector3 tmpv = octd->getAxis() * octd->getFilteredWrenchWithHold()[0];
00633     o_forces = new OpenHRP::ObjectContactTurnaroundDetectorService::Dbl3Sequence ();
00634     o_moments = new OpenHRP::ObjectContactTurnaroundDetectorService::Dbl3Sequence ();
00635     o_forces->length(octd_sensor_names.size());
00636     o_moments->length(octd_sensor_names.size());
00637     for (size_t i = 0; i < o_forces->length(); i++) o_forces[i].length(3);
00638     for (size_t i = 0; i < o_moments->length(); i++) o_moments[i].length(3);
00639     // Temp
00640     for (size_t i = 0; i < octd_sensor_names.size(); i++) {
00641         o_forces[i][0] = tmpv(0)/octd_sensor_names.size();
00642         o_forces[i][1] = tmpv(1)/octd_sensor_names.size();
00643         o_forces[i][2] = tmpv(2)/octd_sensor_names.size();
00644         o_moments[i][0] = o_moments[i][1] = o_moments[i][2] = 0.0;
00645     }
00646     o_3dofwrench = new OpenHRP::ObjectContactTurnaroundDetectorService::DblSequence3 ();
00647     o_3dofwrench->length(3);
00648     for (size_t i = 0; i < 3; i++) (*o_3dofwrench)[i] = tmpv(i);
00649     o_fric_coeff_wrench = octd->getFilteredFrictionCoeffWrenchWithHold()[0];
00650     return true;
00651 }
00652 
00653 bool ObjectContactTurnaroundDetector::getObjectGeneralizedConstraintWrenches(OpenHRP::ObjectContactTurnaroundDetectorService::objectGeneralizedConstraintWrenchesParam& o_param)
00654 {
00655     std::vector<double> tmp1 = octd->getFilteredWrenchWithHold();
00656     o_param.generalized_constraint_wrench1.length(tmp1.size());
00657     for (size_t i = 0; i < tmp1.size(); i++) o_param.generalized_constraint_wrench1[i] = tmp1[i];
00658     std::vector<double> tmp2 = octd->getFilteredFrictionCoeffWrenchWithHold();
00659     o_param.generalized_constraint_wrench2.length(tmp2.size());
00660     for (size_t i = 0; i < tmp2.size(); i++) o_param.generalized_constraint_wrench2[i] = tmp2[i];
00661     hrp::dvector6 tmpr = octd->getFilteredResultantWrenchWithHold();
00662     for (size_t i = 0; i < 6; i++) o_param.resultant_wrench[i] = tmpr(i);
00663     return true;
00664 }
00665 
00666 extern "C"
00667 {
00668 
00669     void ObjectContactTurnaroundDetectorInit(RTC::Manager* manager)
00670     {
00671         RTC::Properties profile(objectcontactturnarounddetector_spec);
00672         manager->registerFactory(profile,
00673                                  RTC::Create<ObjectContactTurnaroundDetector>,
00674                                  RTC::Delete<ObjectContactTurnaroundDetector>);
00675     }
00676 
00677 };


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Wed May 15 2019 05:02:18