ObjectContactTurnaroundDetector.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
10 #include <rtm/CorbaNaming.h>
11 #include <hrpModel/Link.h>
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>
18 
20 
21 // Module specification
22 // <rtc-template block="module_spec">
24  {
25  "implementation_id", "ObjectContactTurnaroundDetector",
26  "type_name", "ObjectContactTurnaroundDetector",
27  "description", "object contact turnaround detector component",
28  "version", HRPSYS_PACKAGE_VERSION,
29  "vendor", "AIST",
30  "category", "example",
31  "activity_type", "DataFlowComponent",
32  "max_instance", "10",
33  "language", "C++",
34  "lang_type", "compile",
35  // Configuration variables
36  "conf.default.debugLevel", "0",
37  ""
38  };
39 // </rtc-template>
40 
42  : RTC::DataFlowComponentBase(manager),
43  // <rtc-template block="initializer">
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"),
49  // </rtc-template>
50  m_robot(hrp::BodyPtr()),
51  m_debugLevel(0),
52  dummy(0)
53 {
54  m_service0.octd(this);
55 }
56 
58 {
59 }
60 
61 
63 {
64  std::cerr << "[" << m_profile.instance_name << "] onInitialize()" << std::endl;
65  bindParameter("debugLevel", m_debugLevel, "0");
66 
67  // Registration: InPort/OutPort/Service
68  // <rtc-template block="registration">
69  // Set InPort buffers
70  addInPort("qCurrent", m_qCurrentIn);
71  addInPort("rpy", m_rpyIn);
72  addInPort("contactStates", m_contactStatesIn);
73 
74  // Set OutPort buffer
75  addOutPort("octdData", m_octdDataOut);
76 
77  // Set service provider to Ports
78  m_ObjectContactTurnaroundDetectorServicePort.registerProvider("service0", "ObjectContactTurnaroundDetectorService", m_service0);
79 
80  // Set service consumers to Ports
81 
82  // Set CORBA Service Ports
84 
85  // </rtc-template>
86  // <rtc-template block="bind_config">
87  // Bind variables and configuration variable
88 
89  // </rtc-template>
90 
92  coil::stringTo(m_dt, prop["dt"].c_str());
93 
95 
96  RTC::Manager& rtcManager = RTC::Manager::instance();
97  std::string nameServer = rtcManager.getConfig()["corba.nameservers"];
98  int comPos = nameServer.find(",");
99  if (comPos < 0){
100  comPos = nameServer.length();
101  }
102  nameServer = nameServer.substr(0, comPos);
103  RTC::CorbaNaming naming(rtcManager.getORB(), nameServer.c_str());
104  if (!loadBodyFromModelLoader(m_robot, prop["model"].c_str(),
105  CosNaming::NamingContext::_duplicate(naming.getRootContext())
106  )){
107  std::cerr << "[" << m_profile.instance_name << "] failed to load model[" << prop["model"] << "]" << std::endl;
108  return RTC::RTC_ERROR;
109  }
110 
111 
112  // Setting for wrench data ports (real)
113  std::vector<std::string> fsensor_names;
114  // find names for real force sensors
115  unsigned int npforce = m_robot->numSensors(hrp::Sensor::FORCE);
116  for (unsigned int i=0; i<npforce; i++){
117  fsensor_names.push_back(m_robot->sensor(hrp::Sensor::FORCE, i)->name);
118  }
119  // add ports for all force sensors
120  unsigned int nforce = npforce;
121  m_force.resize(nforce);
122  m_forceIn.resize(nforce);
123  std::cerr << "[" << m_profile.instance_name << "] force sensor ports" << std::endl;
124  for (unsigned int i=0; i<nforce; i++){
125  m_forceIn[i] = new InPort<TimedDoubleSeq>(fsensor_names[i].c_str(), m_force[i]);
126  m_force[i].data.length(6);
127  registerInPort(fsensor_names[i].c_str(), *m_forceIn[i]);
128  std::cerr << "[" << m_profile.instance_name << "] name = " << fsensor_names[i] << std::endl;
129  }
130 
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;
136  }
137  }
138 
139  // setting from conf file
140  // rleg,TARGET_LINK,BASE_LINK,x,y,z,rx,ry,rz,rth #<=pos + rot (axis+angle)
141  coil::vstring end_effectors_str = coil::split(prop["end_effectors"], ",");
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;
148  coil::stringTo(ee_name, end_effectors_str[i*prop_num].c_str());
149  coil::stringTo(ee_target, end_effectors_str[i*prop_num+1].c_str());
150  coil::stringTo(ee_base, end_effectors_str[i*prop_num+2].c_str());
151  ee_trans eet;
152  for (size_t j = 0; j < 3; j++) {
153  coil::stringTo(eet.localPos(j), end_effectors_str[i*prop_num+3+j].c_str());
154  }
155  double tmpv[4];
156  for (int j = 0; j < 4; j++ ) {
157  coil::stringTo(tmpv[j], end_effectors_str[i*prop_num+6+j].c_str());
158  }
159  eet.localR = Eigen::AngleAxis<double>(tmpv[3], hrp::Vector3(tmpv[0], tmpv[1], tmpv[2])).toRotationMatrix(); // rotation in VRML is represented by axis + angle
160  eet.target_name = ee_target;
161  eet.index = i;
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;
168  }
169  m_contactStates.data.length(num);
170  }
171 
172  // initialize sensor_names
173  std::cerr << "[" << m_profile.instance_name << "] Add sensor_names" << std::endl;
174  for (unsigned int i=0; i<m_forceIn.size(); i++){
175  std::string sensor_name = m_forceIn[i]->name();
176  hrp::ForceSensor* sensor = m_robot->sensor<hrp::ForceSensor>(sensor_name);
177  std::string sensor_link_name;
178  if ( sensor ) {
179  // real force sensor
180  sensor_link_name = sensor->link->name;
181  } else {
182  std::cerr << "[" << m_profile.instance_name << "] unknown force param" << std::endl;
183  continue;
184  }
185  // 1. Check whether adequate ee_map exists for the sensor.
186  std::string ee_name;
187  bool is_ee_exists = false;
188  for ( std::map<std::string, ee_trans>::iterator it = ee_map.begin(); it != ee_map.end(); it++ ) {
189  hrp::Link* alink = m_robot->link(it->second.target_name);
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 ) {
193  is_ee_exists = true;
194  ee_name = it->first;
195  }
196  alink = alink->parent;
197  }
198  }
199  if (!is_ee_exists) {
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;
201  continue;
202  }
203  // 4. Set impedance param
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;
206  }
207 
209  octd->setPrintStr(std::string(m_profile.instance_name));
210 
211  // allocate memory for outPorts
212  loop = 0;
213  m_octdData.data.length(4); // mode, raw, filtered, dfiltered
214 
215  return RTC::RTC_OK;
216 }
217 
218 
219 
221 {
222  return RTC::RTC_OK;
223 }
224 
225 /*
226  RTC::ReturnCode_t ObjectContactTurnaroundDetector::onStartup(RTC::UniqueId ec_id)
227  {
228  return RTC::RTC_OK;
229  }
230 */
231 
232 /*
233  RTC::ReturnCode_t ObjectContactTurnaroundDetector::onShutdown(RTC::UniqueId ec_id)
234  {
235  return RTC::RTC_OK;
236  }
237 */
238 
240 {
241  std::cerr << "[" << m_profile.instance_name<< "] onActivated(" << ec_id << ")" << std::endl;
242  return RTC::RTC_OK;
243 }
244 
246 {
247  std::cerr << "[" << m_profile.instance_name<< "] onDeactivated(" << ec_id << ")" << std::endl;
248  return RTC::RTC_OK;
249 }
250 
251 #define DEBUGP ((m_debugLevel==1 && loop%200==0) || m_debugLevel > 1 )
253 {
254  //std::cout << "ObjectContactTurnaroundDetector::onExecute(" << ec_id << ")" << std::endl;
255  loop ++;
256 
257  // check dataport input
258  for (unsigned int i=0; i<m_forceIn.size(); i++){
259  if ( m_forceIn[i]->isNew() ) {
260  m_forceIn[i]->read();
261  }
262  }
263  if (m_rpyIn.isNew()) {
264  m_rpyIn.read();
265  }
266  if (m_qCurrentIn.isNew()) {
267  m_qCurrentIn.read();
268  m_octdData.tm = m_qCurrent.tm;
269  }
270  if (m_contactStatesIn.isNew()) {
272  }
273  bool is_contact = false;
274  for (size_t i = 0; i < m_contactStates.data.length(); i++) {
275  if (m_contactStates.data[i]) is_contact = true;
276  }
277  if ( m_qCurrent.data.length() == m_robot->numJoints() &&
278  is_contact && // one or more limbs are in contact
279  ee_map.find("rleg") != ee_map.end() && ee_map.find("lleg") != ee_map.end() ) { // if legged robot
280  Guard guard(m_mutex);
283  }
284  return RTC::RTC_OK;
285 }
286 
287 /*
288  RTC::ReturnCode_t ObjectContactTurnaroundDetector::onAborting(RTC::UniqueId ec_id)
289  {
290  return RTC::RTC_OK;
291  }
292 */
293 
294 /*
295  RTC::ReturnCode_t ObjectContactTurnaroundDetector::onError(RTC::UniqueId ec_id)
296  {
297  return RTC::RTC_OK;
298  }
299 */
300 
301 /*
302  RTC::ReturnCode_t ObjectContactTurnaroundDetector::onReset(RTC::UniqueId ec_id)
303  {
304  return RTC::RTC_OK;
305  }
306 */
307 
308 /*
309  RTC::ReturnCode_t ObjectContactTurnaroundDetector::onStateUpdate(RTC::UniqueId ec_id)
310  {
311  return RTC::RTC_OK;
312  }
313 */
314 
315 /*
316  RTC::ReturnCode_t ObjectContactTurnaroundDetector::onRateChanged(RTC::UniqueId ec_id)
317  {
318  return RTC::RTC_OK;
319  }
320 */
321 
323 {
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++) {
328  hrp::Link* target_link = m_robot->link(ee_map[leg_names[i]].target_name);
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);
331  }
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]);
334 }
335 
337 {
338  std::vector<rats::coordinates> leg_c_v;
339  hrp::Vector3 ez = hrp::Vector3::UnitZ();
340  hrp::Vector3 ex = hrp::Vector3::UnitX();
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++ ) {
343  // If rleg or lleg, and if reference contact states is true
344  if (it->first.find("leg") != std::string::npos && m_contactStates.data[it->second.index]) leg_names.push_back(it->first);
345  }
346  for (size_t i = 0; i < leg_names.size(); i++) {
347  hrp::Link* target_link = m_robot->link(ee_map[leg_names[i]].target_name);
348  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));
349  hrp::Vector3 xv1(leg_c.rot * ex);
350  xv1(2)=0.0;
351  xv1.normalize();
352  hrp::Vector3 yv1(ez.cross(xv1));
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);
357  }
358  if (leg_names.size() == 2) {
359  rats::coordinates tmpc;
360  rats::mid_coords(tmpc, 0.5, leg_c_v[0], leg_c_v[1]);
361  foot_origin_pos = tmpc.pos;
362  foot_origin_rot = tmpc.rot;
363  } else { // size = 1
364  foot_origin_pos = leg_c_v[0].pos;
365  foot_origin_rot = leg_c_v[0].rot;
366  }
367 }
368 
370 {
371  // TODO
372  // Currently only for legged robots
373  // Set actual state
374  for ( unsigned int i = 0; i < m_robot->numJoints(); i++ ) {
375  m_robot->joint(i)->q = m_qCurrent.data[i];
376  }
378  m_robot->calcForwardKinematics();
379  // Calc
380  std::vector<hrp::Vector3> octd_forces, octd_moments, octd_hposv;
381  hrp::Vector3 fmpos;
382  hrp::Matrix33 fmrot, fmrotT;
383  //calcFootMidCoords(fmpos, fmrot);
384  calcFootOriginCoords(fmpos, fmrot);
385  fmrotT = fmrot.transpose();
386  for (unsigned int i=0; i<m_forceIn.size(); i++) {
387  std::string sensor_name = m_forceIn[i]->name();
388  if ( find(octd_sensor_names.begin(), octd_sensor_names.end(), sensor_name) != octd_sensor_names.end() ) {
389  hrp::Vector3 ee_pos; // End Effector Position
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 ) {
392  ee_trans& eet = it->second;
393  hrp::Link* target_link = m_robot->link(eet.target_name);
394  ee_pos = target_link->p + target_link->R * eet.localPos;
395  }
396  }
397  hrp::ForceSensor* sensor = m_robot->sensor<hrp::ForceSensor>(sensor_name);
398  hrp::Matrix33 sensor_rot = sensor->link->R * sensor->localR;
399  hrp::Vector3 sensor_pos(sensor->link->R * sensor->localPos + sensor->link->p);
400  hrp::Vector3 sensor_force(sensor_rot*hrp::Vector3(m_force[i].data[0], m_force[i].data[1], m_force[i].data[2]));
401  hrp::Vector3 sensor_moment(sensor_rot*hrp::Vector3(m_force[i].data[3], m_force[i].data[4], m_force[i].data[5]));
402  hrp::Vector3 ee_moment( (sensor_pos - ee_pos).cross(sensor_force) + sensor_moment);
403  // Change to FootOriginCoords relative values
404  octd_hposv.push_back(fmrotT*(ee_pos - fmpos)); // Change to FootOriginCoords relative hand pos
405  octd_forces.push_back(fmrotT*(sensor_force)); // Change to FootOriginCoords relative ee force, and sensor force = ee force
406  octd_moments.push_back(fmrotT*(ee_moment)); // Change to FootOriginCoords relative ee force
407  }
408  }
409  octd->checkDetection(octd_forces, octd_moments, octd_hposv);
410  // octdData
411  hrp::dvector log_data = octd->getDataForLogger();
412  if (m_octdData.data.length() != log_data.size()) m_octdData.data.length(log_data.size());
413  for (size_t i = 0; i < log_data.size(); i++) {
414  m_octdData.data[i] = log_data(i);
415  }
416 };
417 
418 //
419 
421 {
422  if ( m_robot->numSensors(hrp::Sensor::ACCELERATION) > 0) {
423  hrp::Sensor *sensor = m_robot->sensor(hrp::Sensor::ACCELERATION, 0);
424  hrp::Matrix33 tmpr;
425  rats::rotm3times(tmpr, hrp::Matrix33(sensor->link->R*sensor->localR).transpose(), m_robot->rootLink()->R);
426  rats::rotm3times(m_robot->rootLink()->R, hrp::rotFromRpy(tmprpy.data.r, tmprpy.data.p, tmprpy.data.y), tmpr);
427  m_robot->rootLink()->p = hrp::Vector3::Zero();
428  }
429 }
430 
431 //
432 // ObjectContactTurnaroundDetector
433 //
434 
435 void ObjectContactTurnaroundDetector::startObjectContactTurnaroundDetection(const double i_ref_diff_wrench, const double i_max_time, const OpenHRP::ObjectContactTurnaroundDetectorService::StrSequence& i_ee_names)
436 {
437  Guard guard(m_mutex);
438  octd->startDetection(i_ref_diff_wrench, i_max_time);
439  octd_sensor_names.clear();
440  for (size_t i = 0; i < i_ee_names.length(); i++) {
441  octd_sensor_names.push_back(ee_map[std::string(i_ee_names[i])].sensor_name);
442  }
443 }
444 
446 {
447  Guard guard(m_mutex);
448  octd->startDetectionForGeneralizedWrench();
449  return true;
450 }
451 
452 OpenHRP::ObjectContactTurnaroundDetectorService::DetectorMode ObjectContactTurnaroundDetector::checkObjectContactTurnaroundDetectionCommon(const size_t index)
453 {
454  OpenHRP::ObjectContactTurnaroundDetectorService::DetectorMode tmpmode;
455  switch (octd->getMode(index)) {
457  tmpmode = ObjectContactTurnaroundDetectorService::MODE_DETECTOR_IDLE;
458  break;
460  tmpmode = ObjectContactTurnaroundDetectorService::MODE_STARTED;
461  break;
463  tmpmode = ObjectContactTurnaroundDetectorService::MODE_DETECTED;
464  break;
466  tmpmode = ObjectContactTurnaroundDetectorService::MODE_MAX_TIME;
467  break;
469  tmpmode = ObjectContactTurnaroundDetectorService::MODE_OTHER_DETECTED;
470  break;
471  default:
472  tmpmode = ObjectContactTurnaroundDetectorService::MODE_DETECTOR_IDLE;
473  break;
474  }
475  return tmpmode;
476 }
477 
478 OpenHRP::ObjectContactTurnaroundDetectorService::DetectorMode ObjectContactTurnaroundDetector::checkObjectContactTurnaroundDetection()
479 {
480  Guard guard(m_mutex);
482 }
483 
484 bool ObjectContactTurnaroundDetector::checkObjectContactTurnaroundDetectionForGeneralizedWrench(OpenHRP::ObjectContactTurnaroundDetectorService::DetectorModeSequence_out o_dms)
485 {
486  Guard guard(m_mutex);
487  o_dms->length(octd->getDetectGeneralizedWrenchDim());
488  for (size_t i = 0; i < octd->getDetectGeneralizedWrenchDim(); i++) {
490  }
491  return true;
492 }
493 
494 bool ObjectContactTurnaroundDetector::setObjectContactTurnaroundDetectorParam(const OpenHRP::ObjectContactTurnaroundDetectorService::objectContactTurnaroundDetectorParam &i_param_)
495 {
496  Guard guard(m_mutex);
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);
506  hrp::Vector3 tmp;
507  for (size_t i = 0; i < 3; i++) tmp(i) = i_param_.axis[i];
508  octd->setAxis(tmp);
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:
515  break;
516  case OpenHRP::ObjectContactTurnaroundDetectorService::TOTAL_MOMENT:
518  break;
519  case OpenHRP::ObjectContactTurnaroundDetectorService::TOTAL_MOMENT2:
521  break;
522  case OpenHRP::ObjectContactTurnaroundDetectorService::GENERALIZED_WRENCH:
524  break;
525  default:
526  break;
527  }
528  octd->setDetectorTotalWrench(dtw);
529  octd->setIsHoldValues(i_param_.is_hold_values);
530  // For GENERALIZED_WRENCH mode
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];
539  }
540  }
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];
545  }
546  }
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);
550  } else {
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;
553  return false;
554  }
555  octd->setMaxTime(i_param_.max_time);
556  octd_sensor_names.clear();
557  for (size_t i = 0; i < i_param_.limbs.length(); i++) {
558  octd_sensor_names.push_back(ee_map[std::string(i_param_.limbs[i])].sensor_name);
559  }
560  // Print
561  octd->printParams();
562  std::cerr << "[" << m_profile.instance_name << "] sensor_names = [";
563  for (size_t i = 0; i < octd_sensor_names.size(); i++) std::cerr << getEENameFromSensorName(octd_sensor_names[i]) << " ";
564  std::cerr << "]" << std::endl;
565  return true;
566 };
567 
568 bool ObjectContactTurnaroundDetector::getObjectContactTurnaroundDetectorParam(OpenHRP::ObjectContactTurnaroundDetectorService::objectContactTurnaroundDetectorParam& i_param_)
569 {
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();
579  hrp::Vector3 tmp = octd->getAxis();
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;
587  break;
589  dtw = OpenHRP::ObjectContactTurnaroundDetectorService::TOTAL_MOMENT;
590  break;
592  dtw = OpenHRP::ObjectContactTurnaroundDetectorService::TOTAL_MOMENT2;
593  break;
595  dtw = OpenHRP::ObjectContactTurnaroundDetectorService::GENERALIZED_WRENCH;
596  break;
597  default:
598  break;
599  }
600  i_param_.detector_total_wrench = dtw;
601  i_param_.is_hold_values = octd->getIsHoldValues();
602  // For GENERALIZED_WRENCH mode
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);
610  }
611  }
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);
616  }
617  }
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();
621  i_param_.limbs.length(octd_sensor_names.size());
622  for (size_t i = 0; i < octd_sensor_names.size(); i++) {
623  i_param_.limbs[i] = getEENameFromSensorName(octd_sensor_names[i]).c_str();
624  }
625  return true;
626 }
627 
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)
629 {
630  std::cerr << "[" << m_profile.instance_name << "] getObjectForcesMoments" << std::endl;
631  if (octd_sensor_names.size() == 0) return false;
632  hrp::Vector3 tmpv = octd->getAxis() * octd->getFilteredWrenchWithHold()[0];
633  o_forces = new OpenHRP::ObjectContactTurnaroundDetectorService::Dbl3Sequence ();
634  o_moments = new OpenHRP::ObjectContactTurnaroundDetectorService::Dbl3Sequence ();
635  o_forces->length(octd_sensor_names.size());
636  o_moments->length(octd_sensor_names.size());
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);
639  // Temp
640  for (size_t i = 0; i < octd_sensor_names.size(); i++) {
641  o_forces[i][0] = tmpv(0)/octd_sensor_names.size();
642  o_forces[i][1] = tmpv(1)/octd_sensor_names.size();
643  o_forces[i][2] = tmpv(2)/octd_sensor_names.size();
644  o_moments[i][0] = o_moments[i][1] = o_moments[i][2] = 0.0;
645  }
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];
650  return true;
651 }
652 
653 bool ObjectContactTurnaroundDetector::getObjectGeneralizedConstraintWrenches(OpenHRP::ObjectContactTurnaroundDetectorService::objectGeneralizedConstraintWrenchesParam& o_param)
654 {
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];
661  hrp::dvector6 tmpr = octd->getFilteredResultantWrenchWithHold();
662  for (size_t i = 0; i < 6; i++) o_param.resultant_wrench[i] = tmpr(i);
663  return true;
664 }
665 
666 extern "C"
667 {
668 
670  {
672  manager->registerFactory(profile,
673  RTC::Create<ObjectContactTurnaroundDetector>,
674  RTC::Delete<ObjectContactTurnaroundDetector>);
675  }
676 
677 };
ComponentProfile m_profile
png_infop png_charpp int png_charpp profile
void updateRootLinkPosRot(TimedOrientation3D tmprpy)
boost::shared_ptr< ObjectContactTurnaroundDetectorBase > octd
OpenHRP::ObjectContactTurnaroundDetectorService::DetectorMode checkObjectContactTurnaroundDetectionCommon(const size_t index)
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id)
bool stringTo(To &val, const char *str)
hrp::Matrix33 rot
Definition: RatsMatrix.h:20
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
vstring split(const std::string &input, const std::string &delimiter, bool ignore_empty)
CORBA::ORB_ptr getORB()
png_bytep png_bytep png_size_t length
png_uint_32 i
Eigen::VectorXd dvector
coil::Properties & getProperties()
static Manager & instance()
Matrix33 localR
void calcFootMidCoords(hrp::Vector3 &new_foot_mid_pos, hrp::Matrix33 &new_foot_mid_rot)
bool addOutPort(const char *name, OutPortBase &outport)
coil::Guard< coil::Mutex > Guard
boost::shared_ptr< Body > BodyPtr
void calcFootOriginCoords(hrp::Vector3 &foot_origin_pos, hrp::Matrix33 &foot_origin_rot)
object contact turnaround detector component
Eigen::Vector3d Vector3
Matrix33 rotFromRpy(const Vector3 &rpy)
Eigen::Matrix3d Matrix33
std::vector< std::string > vstring
coil::Properties & getConfig()
void ObjectContactTurnaroundDetectorInit(RTC::Manager *manager)
void rotm3times(hrp::Matrix33 &m12, const hrp::Matrix33 &m1, const hrp::Matrix33 &m2)
Definition: RatsMatrix.cpp:31
ExecutionContextHandle_t UniqueId
bool bindParameter(const char *param_name, VarType &var, const char *def_val, bool(*trans)(VarType &, const char *)=coil::stringTo)
std::string getEENameFromSensorName(const std::string &sensor_name)
def j(str, encoding="cp932")
Vector3 localPos
CORBA::Long find(const CorbaSequence &seq, Functor f)
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)
ObjectContactTurnaroundDetectorService_impl m_service0
static const char * objectcontactturnarounddetector_spec[]
std::vector< InPort< TimedDoubleSeq > * > m_forceIn
int loadBodyFromModelLoader(::World *world, const char *name, const char *url, CosNaming::NamingContext_var cxt)
bool getObjectContactTurnaroundDetectorParam(OpenHRP::ObjectContactTurnaroundDetectorService::objectContactTurnaroundDetectorParam &i_param_)
bool getObjectGeneralizedConstraintWrenches(OpenHRP::ObjectContactTurnaroundDetectorService::objectGeneralizedConstraintWrenchesParam &o_param)
void startObjectContactTurnaroundDetection(const double i_ref_diff_wrench, const double i_max_time, const OpenHRP::ObjectContactTurnaroundDetectorService::StrSequence &i_ee_names)
bool setObjectContactTurnaroundDetectorParam(const OpenHRP::ObjectContactTurnaroundDetectorService::objectContactTurnaroundDetectorParam &i_param_)
prop
naming
Vector3 cross(const Vector3 &v1, const Vector3 &v2)
void registerInPort(const char *name, InPortBase &inport)
virtual bool isNew()
std::map< std::string, ee_trans > ee_map
bool checkObjectContactTurnaroundDetectionForGeneralizedWrench(OpenHRP::ObjectContactTurnaroundDetectorService::DetectorModeSequence_out o_dms)
bool addPort(PortBase &port)
virtual bool write(DataType &value)
hrp::BodyPtr m_robot
hrp::Vector3 pos
Definition: RatsMatrix.h:19
Eigen::Matrix< double, 6, 1 > dvector6
JSAMPIMAGE data
int num
bool getObjectForcesMoments(OpenHRP::ObjectContactTurnaroundDetectorService::Dbl3Sequence_out o_forces, OpenHRP::ObjectContactTurnaroundDetectorService::Dbl3Sequence_out o_moments, OpenHRP::ObjectContactTurnaroundDetectorService::DblSequence3_out o_3dofwrench, double &o_fric_coeff_wrench)
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)
Definition: RatsMatrix.cpp:46
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)
Definition: RatsMatrix.cpp:58
bool registerProvider(const char *instance_name, const char *type_name, PortableServer::RefCountServantBase &provider)
OpenHRP::ObjectContactTurnaroundDetectorService::DetectorMode checkObjectContactTurnaroundDetection()


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Thu May 6 2021 02:41:50