TorqueController.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
10 #include "TorqueController.h"
11 #include "hrpsys/util/VectorConvert.h"
12 
13 #include <rtm/CorbaNaming.h>
14 #include <hrpModel/ModelLoaderUtil.h>
15 #include <hrpUtil/MatrixSolvers.h>
16 
17 #include <map>
18 
19 // Module specification
20 // <rtc-template block="module_spec">
21 static const char* torquecontroller_spec[] =
22 {
23  "implementation_id", "TorqueController",
24  "type_name", "TorqueController",
25  "description", "Component for joint torque control",
26  "version", HRPSYS_PACKAGE_VERSION,
27  "vendor", "AIST",
28  "category", "example",
29  "activity_type", "DataFlowComponent",
30  "max_instance", "10",
31  "language", "C++",
32  "lang_type", "compile",
33  // Configuration variables
34  "conf.default.debugLevel", "0",
35  ""
36 };
37 // </rtc-template>
38 
40 
42  : RTC::DataFlowComponentBase(manager),
43  // <rtc-template block="initializer">
44  m_tauCurrentInIn("tauCurrent", m_tauCurrentIn),
45  m_tauMaxInIn("tauMax", m_tauMaxIn),
46  m_qCurrentInIn("qCurrent", m_qCurrentIn),
47  m_qRefInIn("qRef", m_qRefIn),
48  m_qRefOutOut("q", m_qRefOut),
49  m_TorqueControllerServicePort("TorqueControllerService"),
50  m_debugLevel(0)
51 {
53 }
54 
56 {
57 }
58 
59 RTC::ReturnCode_t TorqueController::onInitialize()
60 {
61  std::cerr << "[" << m_profile.instance_name << "] onInitialize()" << std::endl;
62  // <rtc-template block="bind_config">
63  // Bind variables and configuration variable
64  // <rtc-template block="bind_config">
65  // Bind variables and configuration variable
66  bindParameter("debugLevel", m_debugLevel, "0");
67 
68  // </rtc-template>
69 
70  // Registration: InPort/OutPort/Service
71  // <rtc-template block="registration">
72  // Set InPort buffers
73  addInPort("tauCurrent", m_tauCurrentInIn);
74  addInPort("tauMax", m_tauMaxInIn);
75  addInPort("qCurrent", m_qCurrentInIn);
76  addInPort("qRef", m_qRefInIn); // for naming rule of hrpsys_config.py
77 
78  // Set OutPort buffer
79  addOutPort("q", m_qRefOutOut); // for naming rule of hrpsys_config.py
80 
81  // Set service provider to Ports
82  m_TorqueControllerServicePort.registerProvider("service0", "TorqueControllerService", m_service0);
83 
84  // Set service consumers to Ports
85 
86  // Set CORBA Service Ports
88 
89  // </rtc-template>
90 
91  // read property settings
93  // get dt
94  coil::stringTo(m_dt, prop["dt"].c_str());
95  // make rtc manager settings
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  // set robot model
105  m_robot = hrp::BodyPtr(new hrp::Body());
106  std::cerr << prop["model"].c_str() << std::endl;
107  if (!loadBodyFromModelLoader(m_robot, prop["model"].c_str(),
108  CosNaming::NamingContext::_duplicate(naming.getRootContext())
109  )){
110  std::cerr << "[" << m_profile.instance_name << "] failed to load model[" << prop["model"] << "]"
111  << std::endl;
112  }
113  // make torque controller settings
114  coil::vstring motorTorqueControllerParamsFromConf = coil::split(prop["torque_controller_params"], ",");
115  // make controlle type map
117  std::map<int, MotorTorqueController::motor_model_t> param_num_to_motor_model_type;
118  int tdc_params_num = TwoDofController::TwoDofControllerParam::getControllerParamNum() * m_robot->numJoints();
119  int tdc_pd_model_params_num = TwoDofControllerPDModel::TwoDofControllerPDModelParam::getControllerParamNum() * m_robot->numJoints();
120  int tdc_dynamics_model_params_num = TwoDofControllerDynamicsModel::TwoDofControllerDynamicsModelParam::getControllerParamNum() * m_robot->numJoints();
121  param_num_to_motor_model_type[tdc_params_num] = MotorTorqueController::TWO_DOF_CONTROLLER;
122  param_num_to_motor_model_type[tdc_pd_model_params_num] = MotorTorqueController::TWO_DOF_CONTROLLER_PD_MODEL;
123  param_num_to_motor_model_type[tdc_dynamics_model_params_num] = MotorTorqueController::TWO_DOF_CONTROLLER_DYNAMICS_MODEL;
124  if (param_num_to_motor_model_type.find(motorTorqueControllerParamsFromConf.size()) == param_num_to_motor_model_type.end()) {
125  std::cerr << "[" << m_profile.instance_name << "]" << "torque_controller_params is not correct number, " << motorTorqueControllerParamsFromConf.size() << ". Use default controller." << std::endl;
126  model_type = MotorTorqueController::TWO_DOF_CONTROLLER; // default
127  } else {
128  model_type = param_num_to_motor_model_type[motorTorqueControllerParamsFromConf.size()];
129  }
130  // define controller paramters
131  switch (model_type) {
132  case MotorTorqueController::TWO_DOF_CONTROLLER_DYNAMICS_MODEL: // use TwoDofControllerDynamicsModel
133  { // limit scope of tdc_dynamics_model_params
134  std::cerr << "[" << m_profile.instance_name << "]" << "use TwoDofControllerDynamicsModel" << std::endl;
135  std::vector<TwoDofControllerDynamicsModel::TwoDofControllerDynamicsModelParam> tdc_dynamics_model_params(m_robot->numJoints());
136  for (unsigned int i = 0; i < m_robot->numJoints(); i++) {
137  if (motorTorqueControllerParamsFromConf.size() == (unsigned int)tdc_dynamics_model_params_num) { // use conf params if parameter num is correct
138  coil::stringTo(tdc_dynamics_model_params[i].alpha, motorTorqueControllerParamsFromConf[4 * i].c_str());
139  coil::stringTo(tdc_dynamics_model_params[i].beta, motorTorqueControllerParamsFromConf[4 * i + 1].c_str());
140  coil::stringTo(tdc_dynamics_model_params[i].ki, motorTorqueControllerParamsFromConf[4 * i + 2].c_str());
141  coil::stringTo(tdc_dynamics_model_params[i].tc, motorTorqueControllerParamsFromConf[4 * i + 3].c_str());
142  }
143  tdc_dynamics_model_params[i].dt = m_dt;
144  m_motorTorqueControllers.push_back(MotorTorqueController(m_robot->joint(i)->name, tdc_dynamics_model_params[i]));
145  }
146  if (m_debugLevel > 0) {
147  std::cerr << "[" << m_profile.instance_name << "]" << "torque controller parames:" << std::endl;
148  for (unsigned int i = 0; i < m_robot->numJoints(); i++) {
149  std::cerr << "[" << m_profile.instance_name << "]" << m_robot->joint(i)->name << ":"
150  << tdc_dynamics_model_params[i].alpha << " " << tdc_dynamics_model_params[i].beta << " " << tdc_dynamics_model_params[i].ki
151  << " " << tdc_dynamics_model_params[i].tc << " " << tdc_dynamics_model_params[i].dt << std::endl;
152  }
153  }
154  break;
155  }
156  case MotorTorqueController::TWO_DOF_CONTROLLER_PD_MODEL: // use TwoDofControllerPDModel
157  { // limit scope of tdc_pd_model_params
158  std::cerr << "[" << m_profile.instance_name << "]" << "use TwoDofControllerPDModel" << std::endl;
159  std::vector<TwoDofControllerPDModel::TwoDofControllerPDModelParam> tdc_pd_model_params(m_robot->numJoints());
160  for (unsigned int i = 0; i < m_robot->numJoints(); i++) {
161  if (motorTorqueControllerParamsFromConf.size() == (unsigned int)tdc_pd_model_params_num) { // use conf params if parameter num is correct
162  coil::stringTo(tdc_pd_model_params[i].ke, motorTorqueControllerParamsFromConf[3 * i].c_str());
163  coil::stringTo(tdc_pd_model_params[i].kd, motorTorqueControllerParamsFromConf[3 * i + 1].c_str());
164  coil::stringTo(tdc_pd_model_params[i].tc, motorTorqueControllerParamsFromConf[3 * i + 2].c_str());
165  }
166  tdc_pd_model_params[i].dt = m_dt;
167  m_motorTorqueControllers.push_back(MotorTorqueController(m_robot->joint(i)->name, tdc_pd_model_params[i]));
168  }
169  if (m_debugLevel > 0) {
170  std::cerr << "[" << m_profile.instance_name << "]" << "torque controller parames:" << std::endl;
171  for (unsigned int i = 0; i < m_robot->numJoints(); i++) {
172  std::cerr << "[" << m_profile.instance_name << "]" << m_robot->joint(i)->name << ":"
173  << tdc_pd_model_params[i].ke << " " << tdc_pd_model_params[i].kd
174  << " " << tdc_pd_model_params[i].tc << " " << tdc_pd_model_params[i].dt << std::endl;
175  }
176  }
177  break;
178  }
179  case MotorTorqueController::TWO_DOF_CONTROLLER: // use TwoDofController
180  default:
181  { // limit scope of tdc_params
182  std::cerr << "[" << m_profile.instance_name << "]" << "use TwoDofController" << std::endl;
183  std::vector<TwoDofController::TwoDofControllerParam> tdc_params(m_robot->numJoints());
184  for (unsigned int i = 0; i < m_robot->numJoints(); i++) {
185  if (motorTorqueControllerParamsFromConf.size() == (unsigned int)tdc_params_num) { // use conf params if parameter num is correct
186  coil::stringTo(tdc_params[i].ke, motorTorqueControllerParamsFromConf[2 * i].c_str());
187  coil::stringTo(tdc_params[i].tc, motorTorqueControllerParamsFromConf[2 * i + 1].c_str());
188  }
189  tdc_params[i].dt = m_dt;
190  m_motorTorqueControllers.push_back(MotorTorqueController(m_robot->joint(i)->name, tdc_params[i]));
191  }
192  if (m_debugLevel > 0) {
193  std::cerr << "[" << m_profile.instance_name << "]" << "torque controller parames:" << std::endl;
194  for (unsigned int i = 0; i < m_robot->numJoints(); i++) {
195  std::cerr << "[" << m_profile.instance_name << "]" << m_robot->joint(i)->name << ":"
196  << tdc_params[i].ke << " " << tdc_params[i].tc << " " << tdc_params[i].dt << std::endl;
197  }
198  }
199  break;
200  }
201  }
202 
203  // parameter setttings for torque controller
204  bool dq_min_max_from_conf_is_valid = false;
205  coil::vstring dqMinMaxFromConf = coil::split(prop["torque_controler_min_max_dq"], ",");
206  if (dqMinMaxFromConf.size() == 2 * m_robot->numJoints()) {
207  dq_min_max_from_conf_is_valid = true;
208  } else {
209  std::cerr << "[" << m_profile.instance_name << "]" << "size of torque_controller_min_max_dq " << dqMinMaxFromConf.size() << " is not correct number " << 2 * m_robot->numJoints() << ". Use default values." << std::endl;
210  dq_min_max_from_conf_is_valid = false;
211  }
212  for (unsigned int i = 0; i < m_robot->numJoints(); i++) {
213  m_motorTorqueControllers[i].setErrorPrefix(std::string(m_profile.instance_name));
214  m_motorTorqueControllers[i].setupMotorControllerTransitionMinMaxDq(m_robot->joint(i)->lvlimit * m_dt, m_robot->joint(i)->uvlimit * m_dt);
215  if(dq_min_max_from_conf_is_valid) {
216  double tmp_dq_min, tmp_dq_max;
217  coil::stringTo(tmp_dq_min, dqMinMaxFromConf[2 * i].c_str());
218  coil::stringTo(tmp_dq_max, dqMinMaxFromConf[2 * i + 1].c_str());
219  m_motorTorqueControllers[i].setupMotorControllerControlMinMaxDq(tmp_dq_min, tmp_dq_max);
220  }
221  }
222 
223  // allocate memory for outPorts
224  m_qRefOut.data.length(m_robot->numJoints());
225  return RTC::RTC_OK;
226 }
227 
228 
229 
230 /*
231 RTC::ReturnCode_t TorqueController::onFinalize()
232 {
233  return RTC::RTC_OK;
234 }
235 */
236 
237 /*
238 RTC::ReturnCode_t TorqueController::onStartup(RTC::UniqueId ec_id)
239 {
240  return RTC::RTC_OK;
241 }
242 */
243 
244 /*
245 RTC::ReturnCode_t TorqueController::onShutdown(RTC::UniqueId ec_id)
246 {
247  return RTC::RTC_OK;
248 }
249 */
250 
252 {
253  std::cerr << "[" << m_profile.instance_name<< "] onActivated(" << ec_id << ")" << std::endl;
254  return RTC::RTC_OK;
255 }
256 
258 {
259  std::cerr << "[" << m_profile.instance_name<< "] onDeactivated(" << ec_id << ")" << std::endl;
260  return RTC::RTC_OK;
261 }
262 
264 {
265  m_loop++;
266 
267  hrp::dvector dq(m_robot->numJoints());
268 
269  // update port
270  if (m_tauCurrentInIn.isNew()) {
272  }
273  if (m_tauMaxInIn.isNew()) {
274  m_tauMaxInIn.read();
275  }
276  if (m_qCurrentInIn.isNew()) {
278  }
279  if (m_qRefInIn.isNew()) {
280  m_qRefInIn.read();
281  }
282 
283  if (m_qRefIn.data.length() == m_robot->numJoints()) {
284  if (m_tauCurrentIn.data.length() == m_robot->numJoints() &&
285  m_qCurrentIn.data.length() == m_robot->numJoints()) {
286 
287  // update model
288  for ( unsigned int i = 0; i < m_robot->numJoints(); i++ ){
289  m_robot->joint(i)->q = m_qCurrentIn.data[i];
290  }
291  m_robot->calcForwardKinematics();
292 
293  // calculate dq by torque controller
295 
296  // check range of motion and insert to output
297  for (unsigned int i = 0; i < m_robot->numJoints(); i++) {
298  if (m_motorTorqueControllers[i].isEnabled()) {
299  m_qRefOut.data[i] = std::min(std::max(m_qRefIn.data[i] + dq[i], m_robot->joint(i)->llimit), m_robot->joint(i)->ulimit);
300  } else {
301  m_qRefOut.data[i] = m_qRefIn.data[i]; // pass joint angle when controller is disabled
302  }
303  }
304 
305  } else {
306  if (isDebug()) {
307  std::cerr << "[" << m_profile.instance_name << "]" << "TorqueController input is not correct" << std::endl;
308  std::cerr << "[" << m_profile.instance_name << "]" << " numJoints: " << m_robot->numJoints() << std::endl;
309  std::cerr << "[" << m_profile.instance_name << "]" << " qCurrent: " << m_qCurrentIn.data.length() << std::endl;
310  std::cerr << "[" << m_profile.instance_name << "]" << "tauCurrent: " << m_tauCurrentIn.data.length() << std::endl;
311  std::cerr << std::endl;
312  }
313  // pass qRefIn to qRefOut
314  for (unsigned int i = 0; i < m_robot->numJoints(); i++) {
315  m_qRefOut.data[i] = m_qRefIn.data[i];
316  }
317  }
318  m_qRefOut.tm = m_qRefIn.tm;
320  } else {
321  if (isDebug()) {
322  std::cerr << "[" << m_profile.instance_name << "]" << "TorqueController has incorrect qRefIn" << std::endl;
323  std::cerr << "[" << m_profile.instance_name << "]" << " numJoints: " << m_robot->numJoints() << std::endl;
324  std::cerr << "[" << m_profile.instance_name << "]" << " qRefIn: " << m_qRefIn.data.length() << std::endl;
325  std::cerr << std::endl;
326  }
327  }
328 
329  return RTC::RTC_OK;
330 }
331 
332 /*
333 RTC::ReturnCode_t TorqueController::onAborting(RTC::UniqueId ec_id)
334 {
335  return RTC::RTC_OK;
336 }
337 */
338 
339 /*
340 RTC::ReturnCode_t TorqueController::onError(RTC::UniqueId ec_id)
341 {
342  return RTC::RTC_OK;
343 }
344 */
345 
346 /*
347 RTC::ReturnCode_t TorqueController::onReset(RTC::UniqueId ec_id)
348 {
349  return RTC::RTC_OK;
350 }
351 */
352 
353 /*
354 RTC::ReturnCode_t TorqueController::onStateUpdate(RTC::UniqueId ec_id)
355 {
356  return RTC::RTC_OK;
357 }
358 */
359 
360 /*
361 RTC::ReturnCode_t TorqueController::onRateChanged(RTC::UniqueId ec_id)
362 {
363  return RTC::RTC_OK;
364 }
365 */
366 
368 {
369  unsigned int numJoints = m_robot->numJoints();
370  hrp::dvector tauMax(numJoints);
371  dq.resize(numJoints);
372 
373  // determine tauMax
374  for(unsigned int i = 0; i < numJoints; i++) {
375  double tauMaxFromModel = m_robot->joint(i)->climit * m_robot->joint(i)->gearRatio * m_robot->joint(i)->torqueConst;
376  if ( m_tauMaxIn.data.length() == m_robot->numJoints() ) {
377  tauMax[i] = std::min(tauMaxFromModel, m_tauMaxIn.data[i]);
378  } else {
379  tauMax[i] = tauMaxFromModel;
380  }
381  }
382 
383  // execute torque control
384  // tauCurrent.length is assumed to be equal to numJoints (check in onExecute)
385  if (isDebug()) {
386  std::cerr << "[" << m_profile.instance_name << "]" << "tauCurrentIn: ";
387  for (unsigned int i = 0; i < numJoints; i++) {
388  std::cerr << " " << m_tauCurrentIn.data[i];
389  }
390  std::cerr << std::endl;
391  std::cerr << "[" << m_profile.instance_name << "]" << "tauMax: ";
392  for (unsigned int i = 0; i < numJoints; i++) {
393  std::cerr << " " << tauMax[i];
394  }
395  std::cerr << std::endl;
396  }
397 
398  Guard guard(m_mutex);
399  for (unsigned int i = 0; i < numJoints; i++) {
400  dq[i] = m_motorTorqueControllers[i].execute(m_tauCurrentIn.data[i], tauMax[i]); // twoDofController: tau = -K(q - qRef)
401  // output debug message
402  if (isDebug() && m_motorTorqueControllers[i].getMotorControllerState() != MotorTorqueController::INACTIVE) {
403  m_motorTorqueControllers[i].printMotorControllerVariables();
404  }
405 
406  }
407 
408  if (isDebug()) {
409  std::cerr << "[" << m_profile.instance_name << "]" << "dq: ";
410  for (int i = 0; i < dq.size(); i++) {
411  std::cerr << dq[i] << " ";
412  }
413  std::cerr << std::endl;
414  }
415 
416  return;
417 }
418 
420 {
421  bool succeed = false;
422  for (std::vector<MotorTorqueController>::iterator it = m_motorTorqueControllers.begin(); it != m_motorTorqueControllers.end(); ++it) {
423  if ((*it).getJointName() == jname){
424  if (m_debugLevel > 0) {
425  std::cerr << "[" << m_profile.instance_name << "]" << "Enable torque controller in " << jname << std::endl;
426  }
427  succeed = (*it).enable();
428  }
429  }
430  return succeed;
431 }
432 
433 bool TorqueController::enableMultipleTorqueControllers(const OpenHRP::TorqueControllerService::StrSequence& jnames)
434 {
435  bool succeed = true;
436  bool retval;
437  for (unsigned int i = 0; i < jnames.length(); i++) {
438  retval = enableTorqueController(std::string(jnames[i]));
439  if (!retval) { // return false when once failed
440  succeed = false;
441  }
442  }
443  return succeed;
444 }
445 
447 {
448  bool succeed = false;
449  for (std::vector<MotorTorqueController>::iterator it = m_motorTorqueControllers.begin(); it != m_motorTorqueControllers.end(); ++it) {
450  if ((*it).getJointName() == jname){
451  if (m_debugLevel > 0) {
452  std::cerr << "[" << m_profile.instance_name << "]" << "Disable torque controller in " << jname << std::endl;
453  }
454  succeed = (*it).disable();
455  }
456  }
457  return succeed;
458 }
459 
460 bool TorqueController::disableMultipleTorqueControllers(const OpenHRP::TorqueControllerService::StrSequence& jnames)
461 {
462  bool succeed = true;
463  bool retval;
464  for (unsigned int i = 0; i < jnames.length(); i++) {
465  retval = disableTorqueController(std::string(jnames[i]));
466  if (!retval) { // return false when once failed
467  succeed = false;
468  }
469  }
470  return succeed;
471 }
472 
474 {
475  bool succeed = false;
476  for (std::vector<MotorTorqueController>::iterator it = m_motorTorqueControllers.begin(); it != m_motorTorqueControllers.end(); ++it) {
477  if ((*it).getJointName() == jname){
478  if (m_debugLevel > 0) {
479  std::cerr << "[" << m_profile.instance_name << "]" << "Start torque control in " << jname << std::endl;
480  }
481  if (!(*it).isEnabled()) {
482  succeed = enableTorqueController(jname);
483  if (!succeed) {
484  if (m_debugLevel > 0) {
485  std::cerr << "[" << m_profile.instance_name << "]" << "Failed to enable torque control in " << jname << std::endl;
486  }
487  return succeed;
488  }
489  }
490  succeed = (*it).activate();
491  }
492  }
493  return succeed;
494 }
495 
496 bool TorqueController::startMultipleTorqueControls(const OpenHRP::TorqueControllerService::StrSequence& jnames)
497 {
498  bool succeed = true;
499  bool retval;
500  for (unsigned int i = 0; i < jnames.length(); i++) {
501  retval = startTorqueControl(std::string(jnames[i]));
502  if (!retval) { // return false when once failed
503  succeed = false;
504  }
505  }
506  return succeed;
507 }
508 
509 bool TorqueController::stopTorqueControl(std::string jname)
510 {
511  bool succeed = false;
512  for (std::vector<MotorTorqueController>::iterator it = m_motorTorqueControllers.begin(); it != m_motorTorqueControllers.end(); ++it) {
513  if ((*it).getJointName() == jname){
514  if (m_debugLevel > 0) {
515  std::cerr << "[" << m_profile.instance_name << "]" << "Stop torque control in " << jname << std::endl;
516  }
517  succeed = (*it).deactivate();
518  }
519  }
520  return succeed;
521 }
522 
523 bool TorqueController::stopMultipleTorqueControls(const OpenHRP::TorqueControllerService::StrSequence& jnames)
524 {
525  bool succeed = true;
526  bool retval;
527  for (unsigned int i = 0; i < jnames.length(); i++) {
528  retval = stopTorqueControl(std::string(jnames[i]));
529  if (!retval) { // return false when once failed
530  succeed = false;
531  }
532  }
533  return succeed;
534 }
535 
536 bool TorqueController::setReferenceTorque(std::string jname, double tauRef)
537 {
538  bool succeed = false;
539 
540  // lock mutex
541  Guard guard(m_mutex);
542 
543  // search target joint
544  for (std::vector<MotorTorqueController>::iterator it = m_motorTorqueControllers.begin(); it != m_motorTorqueControllers.end(); ++it) {
545  if ((*it).getJointName() == jname) {
546  if (m_debugLevel > 0) {
547  std::cerr << "[" << m_profile.instance_name << "]" << "Set " << jname << " reference torque to " << tauRef << std::endl;
548  }
549  succeed = (*it).setReferenceTorque(tauRef);
550  }
551  }
552  return succeed;
553 }
554 
555 bool TorqueController::setMultipleReferenceTorques(const OpenHRP::TorqueControllerService::StrSequence& jnames, const OpenHRP::TorqueControllerService::dSequence& tauRefs)
556 {
557  bool succeed = true;
558  bool retval;
559  // check accordance of joint name and tauRefs
560  if (jnames.length() != tauRefs.length()) {
561  std::cerr << "[" << m_profile.instance_name << "]" << "Length of jnames and tauRefs are different." << std::endl;
562  return false;
563  }
564  // set reference torques
565  for (unsigned int i = 0; i < jnames.length(); i++) {
566  retval = setReferenceTorque(std::string(jnames[i]), tauRefs[i]);
567  if (!retval) { // return false when once failed
568  succeed = false;
569  }
570  }
571  return succeed;
572 }
573 
574 bool TorqueController::setTorqueControllerParam(const std::string jname, const OpenHRP::TorqueControllerService::torqueControllerParam& i_param)
575 {
576  Guard guard(m_mutex);
577 
578  // find target motor controller
579  MotorTorqueController *tgt_controller = NULL;
580  for (std::vector<MotorTorqueController>::iterator it = m_motorTorqueControllers.begin(); it != m_motorTorqueControllers.end(); ++it) {
581  if ((*it).getJointName() == jname){
582  std::cerr << "[" << m_profile.instance_name << "]" << "target joint:" << jname << std::endl;
583  tgt_controller = &(*it);
584  }
585  }
586  if (tgt_controller == NULL) {
587  std::cerr << "[" << m_profile.instance_name << "]" << jname << "does not found." << std::endl;
588  return false;
589  }
590 
591  // update torque controller param
592  bool retval;
593  MotorTorqueController::motor_model_t model_type = tgt_controller->getMotorModelType();
594  switch(model_type) { // dt is defined by controller cycle
596  { // limit scope for param
597  std::cerr << "[" << m_profile.instance_name << "]" << "new param:" << i_param.ke << " " << i_param.tc << " " << std::endl;
599  param.ke = i_param.ke; param.tc = i_param.tc; param.dt = m_dt;
600  retval = tgt_controller->updateControllerParam(param);
601  break;
602  }
604  { // limit scope for param
605  std::cerr << "[" << m_profile.instance_name << "]" << "new param:" << i_param.ke << " " << i_param.kd << " " << i_param.tc << " " << std::endl;
607  param.ke = i_param.ke; param.kd = i_param.kd; param.tc = i_param.tc; param.dt = m_dt;
608  retval = tgt_controller->updateControllerParam(param);
609  break;
610  }
612  { // limit scope for param
613  std::cerr << "[" << m_profile.instance_name << "]" << "new param:" << i_param.alpha << " " << i_param.beta << " " << i_param.ki << " " << i_param.tc << " " << std::endl;
615  param.alpha = i_param.alpha; param.beta = i_param.beta; param.ki = i_param.ki; param.tc = i_param.tc; param.dt = m_dt;
616  retval = tgt_controller->updateControllerParam(param);
617  break;
618  }
619  default:
620  return false;
621  }
622 
623  return retval;
624 }
625 
626 bool TorqueController::getTorqueControllerParam(const std::string jname, OpenHRP::TorqueControllerService::torqueControllerParam& i_param)
627 {
628  Guard guard(m_mutex);
629 
630  // find target motor controller
631  MotorTorqueController *tgt_controller = NULL;
632  for (std::vector<MotorTorqueController>::iterator it = m_motorTorqueControllers.begin(); it != m_motorTorqueControllers.end(); ++it) {
633  if ((*it).getJointName() == jname) {
634  std::cerr << "[" << m_profile.instance_name << "]" << "target joint:" << jname << std::endl;
635  tgt_controller = &(*it);
636  }
637  }
638  if (tgt_controller == NULL) {
639  std::cerr << "[" << m_profile.instance_name << "]" << jname << "does not found." << std::endl;
640  return false;
641  }
642 
643  // copy torque controller param
644  bool retval;
645  MotorTorqueController::motor_model_t model_type = tgt_controller->getMotorModelType();
646  switch(model_type) {
648  { // limit scope for param
650  retval = tgt_controller->getControllerParam(param);
651  i_param.ke = param.ke;
652  i_param.tc = param.tc;
653  break;
654  }
656  { // limit scope for param
658  retval = tgt_controller->getControllerParam(param);
659  i_param.ke = param.ke;
660  i_param.kd = param.kd;
661  i_param.tc = param.tc;
662  break;
663  }
665  { // limit scope for param
667  retval = tgt_controller->getControllerParam(param);
668  i_param.alpha = param.alpha;
669  i_param.beta = param.beta;
670  i_param.ki = param.ki;
671  i_param.tc = param.tc;
672  break;
673  }
674  default:
675  return false;
676  }
677 
678  return retval;
679 }
680 
681 void TorqueController::updateParam(double &val, double &val_new)
682 {
683  // update value unless val_new is not 0
684  if (val_new != 0) {
685  val = val_new;
686  }
687  return;
688 }
689 
691 {
692  return ((m_debugLevel == 1 && (m_loop % cycle == 0)) || m_debugLevel > 1);
693 }
694 
695 extern "C"
696 {
697 
699  {
701  manager->registerFactory(profile,
702  RTC::Create<TorqueController>,
703  RTC::Delete<TorqueController>);
704  }
705 
706 };
707 
708 
ComponentProfile m_profile
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)
png_infop png_charpp int png_charpp profile
bool disableTorqueController(std::string jname)
#define max(a, b)
InPort< TimedDoubleSeq > m_tauCurrentInIn
bool setReferenceTorque(std::string jname, double tauRef)
bool startTorqueControl(std::string jname)
void updateParam(double &val, double &val_new)
bool setMultipleReferenceTorques(const OpenHRP::TorqueControllerService::StrSequence &jnames, const OpenHRP::TorqueControllerService::dSequence &tauRefs)
bool stopMultipleTorqueControls(const OpenHRP::TorqueControllerService::StrSequence &jnames)
bool updateControllerParam(TwoDofController::TwoDofControllerParam &_param)
TimedDoubleSeq m_tauCurrentIn
bool stringTo(To &val, const char *str)
InPort< TimedDoubleSeq > m_tauMaxInIn
InPort< TimedDoubleSeq > m_qCurrentInIn
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
TorqueController(RTC::Manager *manager)
Constructor.
void executeTorqueControl(hrp::dvector &dq)
vstring split(const std::string &input, const std::string &delimiter, bool ignore_empty)
CORBA::ORB_ptr getORB()
InPort< TimedDoubleSeq > m_qRefInIn
png_uint_32 i
Eigen::VectorXd dvector
bool disableMultipleTorqueControllers(const OpenHRP::TorqueControllerService::StrSequence &jnames)
coil::Properties & getProperties()
static Manager & instance()
static const char * torquecontroller_spec[]
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id)
bool addOutPort(const char *name, OutPortBase &outport)
bool stopTorqueControl(std::string jname)
std::vector< MotorTorqueController > m_motorTorqueControllers
boost::shared_ptr< Body > BodyPtr
bool getTorqueControllerParam(const std::string jname, OpenHRP::TorqueControllerService::torqueControllerParam &i_param)
unsigned int m_debugLevel
#define min(a, b)
OutPort< TimedDoubleSeq > m_qRefOutOut
std::vector< std::string > vstring
bool setTorqueControllerParam(const std::string jname, const OpenHRP::TorqueControllerService::torqueControllerParam &i_param)
coil::Properties & getConfig()
bool startMultipleTorqueControls(const OpenHRP::TorqueControllerService::StrSequence &jnames)
ExecutionContextHandle_t UniqueId
bool bindParameter(const char *param_name, VarType &var, const char *def_val, bool(*trans)(VarType &, const char *)=coil::stringTo)
motor_model_t getMotorModelType(void)
int loadBodyFromModelLoader(::World *world, const char *name, const char *url, CosNaming::NamingContext_var cxt)
bool getControllerParam(TwoDofController::TwoDofControllerParam &_param)
bool enableTorqueController(std::string jname)
prop
RTC::CorbaPort m_TorqueControllerServicePort
bool isDebug(int cycle=20)
naming
typedef int
coil::Guard< coil::Mutex > Guard
bool enableMultipleTorqueControllers(const OpenHRP::TorqueControllerService::StrSequence &jnames)
virtual bool isNew()
bool addPort(PortBase &port)
virtual bool write(DataType &value)
virtual RTC::ReturnCode_t onInitialize()
virtual ~TorqueController()
Destructor.
TimedDoubleSeq m_qRefOut
bool addInPort(const char *name, InPortBase &inport)
TimedDoubleSeq m_qRefIn
TorqueControllerService_impl m_service0
void TorqueControllerInit(RTC::Manager *manager)
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)
TimedDoubleSeq m_qCurrentIn
void torque_controller(TorqueController *i_torque_controller)
TimedDoubleSeq m_tauMaxIn
null component
hrp::BodyPtr m_robot
bool registerProvider(const char *instance_name, const char *type_name, PortableServer::RefCountServantBase &provider)


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