TorqueController.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00010 #include "TorqueController.h"
00011 #include "hrpsys/util/VectorConvert.h"
00012 
00013 #include <rtm/CorbaNaming.h>
00014 #include <hrpModel/ModelLoaderUtil.h>
00015 #include <hrpUtil/MatrixSolvers.h>
00016 
00017 #include <map>
00018 
00019 // Module specification
00020 // <rtc-template block="module_spec">
00021 static const char* torquecontroller_spec[] =
00022 {
00023   "implementation_id", "TorqueController",
00024   "type_name",         "TorqueController",
00025   "description",       "Component for joint torque control",
00026   "version",           HRPSYS_PACKAGE_VERSION,
00027   "vendor",            "AIST",
00028   "category",          "example",
00029   "activity_type",     "DataFlowComponent",
00030   "max_instance",      "10",
00031   "language",          "C++",
00032   "lang_type",         "compile",
00033   // Configuration variables
00034   "conf.default.debugLevel", "0",
00035   ""
00036 };
00037 // </rtc-template>
00038 
00039 typedef coil::Guard<coil::Mutex> Guard;
00040 
00041 TorqueController::TorqueController(RTC::Manager* manager)
00042   : RTC::DataFlowComponentBase(manager),
00043     // <rtc-template block="initializer">
00044     m_tauCurrentInIn("tauCurrent", m_tauCurrentIn),
00045     m_tauMaxInIn("tauMax", m_tauMaxIn),
00046     m_qCurrentInIn("qCurrent", m_qCurrentIn),
00047     m_qRefInIn("qRef", m_qRefIn),
00048     m_qRefOutOut("q", m_qRefOut),
00049     m_TorqueControllerServicePort("TorqueControllerService"),
00050     m_debugLevel(0)
00051 {
00052   m_service0.torque_controller(this);
00053 }
00054 
00055 TorqueController::~TorqueController()
00056 {
00057 }
00058 
00059 RTC::ReturnCode_t TorqueController::onInitialize()
00060 {
00061   std::cerr << "[" << m_profile.instance_name << "] onInitialize()" << std::endl;
00062   // <rtc-template block="bind_config">
00063   // Bind variables and configuration variable
00064   // <rtc-template block="bind_config">
00065   // Bind variables and configuration variable
00066   bindParameter("debugLevel", m_debugLevel, "0");
00067   
00068   // </rtc-template>
00069 
00070   // Registration: InPort/OutPort/Service
00071   // <rtc-template block="registration">
00072   // Set InPort buffers
00073   addInPort("tauCurrent", m_tauCurrentInIn);
00074   addInPort("tauMax", m_tauMaxInIn);
00075   addInPort("qCurrent", m_qCurrentInIn);
00076   addInPort("qRef", m_qRefInIn); // for naming rule of hrpsys_config.py
00077 
00078   // Set OutPort buffer
00079   addOutPort("q", m_qRefOutOut); // for naming rule of hrpsys_config.py
00080   
00081   // Set service provider to Ports
00082   m_TorqueControllerServicePort.registerProvider("service0", "TorqueControllerService", m_service0);
00083   
00084   // Set service consumers to Ports
00085   
00086   // Set CORBA Service Ports
00087   addPort(m_TorqueControllerServicePort);
00088   
00089   // </rtc-template>
00090 
00091   // read property settings
00092   RTC::Properties& prop = getProperties();
00093   // get dt
00094   coil::stringTo(m_dt, prop["dt"].c_str()); 
00095   // make rtc manager settings
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   // set robot model
00105   m_robot = hrp::BodyPtr(new hrp::Body());
00106   std::cerr << prop["model"].c_str() << std::endl;
00107   if (!loadBodyFromModelLoader(m_robot, prop["model"].c_str(),
00108                                CosNaming::NamingContext::_duplicate(naming.getRootContext())
00109         )){
00110     std::cerr << "[" << m_profile.instance_name << "] failed to load model[" << prop["model"] << "]"
00111               << std::endl;
00112   }
00113   // make torque controller settings
00114   coil::vstring motorTorqueControllerParamsFromConf = coil::split(prop["torque_controller_params"], ",");
00115   // make controlle type map
00116   MotorTorqueController::motor_model_t model_type; 
00117   std::map<int, MotorTorqueController::motor_model_t> param_num_to_motor_model_type;
00118   int tdc_params_num = TwoDofController::TwoDofControllerParam::getControllerParamNum() * m_robot->numJoints();
00119   int tdc_pd_model_params_num = TwoDofControllerPDModel::TwoDofControllerPDModelParam::getControllerParamNum() * m_robot->numJoints();
00120   int tdc_dynamics_model_params_num = TwoDofControllerDynamicsModel::TwoDofControllerDynamicsModelParam::getControllerParamNum() * m_robot->numJoints();
00121   param_num_to_motor_model_type[tdc_params_num] = MotorTorqueController::TWO_DOF_CONTROLLER;
00122   param_num_to_motor_model_type[tdc_pd_model_params_num] = MotorTorqueController::TWO_DOF_CONTROLLER_PD_MODEL;
00123   param_num_to_motor_model_type[tdc_dynamics_model_params_num] = MotorTorqueController::TWO_DOF_CONTROLLER_DYNAMICS_MODEL;
00124   if (param_num_to_motor_model_type.find(motorTorqueControllerParamsFromConf.size()) == param_num_to_motor_model_type.end()) {
00125       std::cerr << "[" <<  m_profile.instance_name << "]" << "torque_controller_params is not correct number, " << motorTorqueControllerParamsFromConf.size() << ". Use default controller." << std::endl;
00126     model_type = MotorTorqueController::TWO_DOF_CONTROLLER; // default
00127   } else {
00128     model_type = param_num_to_motor_model_type[motorTorqueControllerParamsFromConf.size()];
00129   }
00130   // define controller paramters
00131   switch (model_type) {
00132   case MotorTorqueController::TWO_DOF_CONTROLLER_DYNAMICS_MODEL: // use TwoDofControllerDynamicsModel
00133   { // limit scope of tdc_dynamics_model_params
00134     std::cerr << "[" <<  m_profile.instance_name << "]" << "use TwoDofControllerDynamicsModel" << std::endl;
00135     std::vector<TwoDofControllerDynamicsModel::TwoDofControllerDynamicsModelParam> tdc_dynamics_model_params(m_robot->numJoints());
00136     for (unsigned int i = 0; i < m_robot->numJoints(); i++) { 
00137       if (motorTorqueControllerParamsFromConf.size() == (unsigned int)tdc_dynamics_model_params_num) { // use conf params if parameter num is correct
00138         coil::stringTo(tdc_dynamics_model_params[i].alpha, motorTorqueControllerParamsFromConf[4 * i].c_str());
00139         coil::stringTo(tdc_dynamics_model_params[i].beta, motorTorqueControllerParamsFromConf[4 * i + 1].c_str());
00140         coil::stringTo(tdc_dynamics_model_params[i].ki, motorTorqueControllerParamsFromConf[4 * i + 2].c_str());
00141         coil::stringTo(tdc_dynamics_model_params[i].tc, motorTorqueControllerParamsFromConf[4 * i + 3].c_str());
00142       }
00143       tdc_dynamics_model_params[i].dt = m_dt;
00144       m_motorTorqueControllers.push_back(MotorTorqueController(m_robot->joint(i)->name, tdc_dynamics_model_params[i]));
00145     }
00146     if (m_debugLevel > 0) {
00147       std::cerr << "[" <<  m_profile.instance_name << "]" << "torque controller parames:" << std::endl;
00148       for (unsigned int i = 0; i < m_robot->numJoints(); i++) {
00149         std::cerr << "[" <<  m_profile.instance_name << "]" << m_robot->joint(i)->name << ":"
00150                   << tdc_dynamics_model_params[i].alpha << " " << tdc_dynamics_model_params[i].beta << " " << tdc_dynamics_model_params[i].ki
00151                   << " " << tdc_dynamics_model_params[i].tc << " " << tdc_dynamics_model_params[i].dt << std::endl;
00152       }
00153     }
00154     break;
00155   }
00156   case MotorTorqueController::TWO_DOF_CONTROLLER_PD_MODEL: // use TwoDofControllerPDModel
00157   { // limit scope of tdc_pd_model_params
00158     std::cerr << "[" <<  m_profile.instance_name << "]" << "use TwoDofControllerPDModel" << std::endl;
00159     std::vector<TwoDofControllerPDModel::TwoDofControllerPDModelParam> tdc_pd_model_params(m_robot->numJoints());
00160     for (unsigned int i = 0; i < m_robot->numJoints(); i++) {
00161       if (motorTorqueControllerParamsFromConf.size() == (unsigned int)tdc_pd_model_params_num) { // use conf params if parameter num is correct
00162         coil::stringTo(tdc_pd_model_params[i].ke, motorTorqueControllerParamsFromConf[3 * i].c_str());
00163         coil::stringTo(tdc_pd_model_params[i].kd, motorTorqueControllerParamsFromConf[3 * i + 1].c_str());
00164         coil::stringTo(tdc_pd_model_params[i].tc, motorTorqueControllerParamsFromConf[3 * i + 2].c_str());
00165       }
00166       tdc_pd_model_params[i].dt = m_dt;
00167       m_motorTorqueControllers.push_back(MotorTorqueController(m_robot->joint(i)->name, tdc_pd_model_params[i]));
00168     }
00169     if (m_debugLevel > 0) {
00170       std::cerr << "[" <<  m_profile.instance_name << "]" << "torque controller parames:" << std::endl;
00171       for (unsigned int i = 0; i < m_robot->numJoints(); i++) {
00172         std::cerr << "[" <<  m_profile.instance_name << "]" << m_robot->joint(i)->name << ":"
00173                   << tdc_pd_model_params[i].ke << " " << tdc_pd_model_params[i].kd
00174                   << " " << tdc_pd_model_params[i].tc << " " << tdc_pd_model_params[i].dt << std::endl;
00175       }
00176     }
00177     break;
00178   }
00179   case MotorTorqueController::TWO_DOF_CONTROLLER: // use TwoDofController
00180   default:
00181   { // limit scope of tdc_params
00182     std::cerr << "[" <<  m_profile.instance_name << "]" << "use TwoDofController" << std::endl;
00183     std::vector<TwoDofController::TwoDofControllerParam> tdc_params(m_robot->numJoints());
00184     for (unsigned int i = 0; i < m_robot->numJoints(); i++) { 
00185       if (motorTorqueControllerParamsFromConf.size() == (unsigned int)tdc_params_num) { // use conf params if parameter num is correct
00186         coil::stringTo(tdc_params[i].ke, motorTorqueControllerParamsFromConf[2 * i].c_str());
00187         coil::stringTo(tdc_params[i].tc, motorTorqueControllerParamsFromConf[2 * i + 1].c_str());
00188       }
00189       tdc_params[i].dt = m_dt;
00190       m_motorTorqueControllers.push_back(MotorTorqueController(m_robot->joint(i)->name, tdc_params[i]));
00191     }
00192     if (m_debugLevel > 0) {
00193       std::cerr << "[" <<  m_profile.instance_name << "]" << "torque controller parames:" << std::endl;
00194       for (unsigned int i = 0; i < m_robot->numJoints(); i++) {
00195         std::cerr << "[" <<  m_profile.instance_name << "]" << m_robot->joint(i)->name << ":"
00196                   << tdc_params[i].ke << " " << tdc_params[i].tc << " " << tdc_params[i].dt << std::endl;
00197       }
00198     }
00199     break;
00200   }
00201   }
00202 
00203   // parameter setttings for torque controller
00204   bool dq_min_max_from_conf_is_valid = false;
00205   coil::vstring dqMinMaxFromConf = coil::split(prop["torque_controler_min_max_dq"], ",");
00206   if (dqMinMaxFromConf.size() == 2 * m_robot->numJoints()) {
00207     dq_min_max_from_conf_is_valid = true;
00208   } else {
00209     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;
00210     dq_min_max_from_conf_is_valid = false;
00211   }
00212   for (unsigned int i = 0; i < m_robot->numJoints(); i++) {
00213     m_motorTorqueControllers[i].setErrorPrefix(std::string(m_profile.instance_name));
00214     m_motorTorqueControllers[i].setupMotorControllerTransitionMinMaxDq(m_robot->joint(i)->lvlimit * m_dt, m_robot->joint(i)->uvlimit * m_dt);
00215     if(dq_min_max_from_conf_is_valid) {
00216       double tmp_dq_min, tmp_dq_max;
00217       coil::stringTo(tmp_dq_min, dqMinMaxFromConf[2 * i].c_str());
00218       coil::stringTo(tmp_dq_max, dqMinMaxFromConf[2 * i + 1].c_str());
00219       m_motorTorqueControllers[i].setupMotorControllerControlMinMaxDq(tmp_dq_min, tmp_dq_max);
00220     }
00221   }
00222 
00223   // allocate memory for outPorts
00224   m_qRefOut.data.length(m_robot->numJoints());
00225   return RTC::RTC_OK;
00226 }
00227 
00228 
00229 
00230 /*
00231 RTC::ReturnCode_t TorqueController::onFinalize()
00232 {
00233   return RTC::RTC_OK;
00234 }
00235 */
00236 
00237 /*
00238 RTC::ReturnCode_t TorqueController::onStartup(RTC::UniqueId ec_id)
00239 {
00240   return RTC::RTC_OK;
00241 }
00242 */
00243 
00244 /*
00245 RTC::ReturnCode_t TorqueController::onShutdown(RTC::UniqueId ec_id)
00246 {
00247   return RTC::RTC_OK;
00248 }
00249 */
00250 
00251 RTC::ReturnCode_t TorqueController::onActivated(RTC::UniqueId ec_id)
00252 {
00253   std::cerr << "[" << m_profile.instance_name<< "] onActivated(" << ec_id << ")" << std::endl;
00254   return RTC::RTC_OK;
00255 }
00256 
00257 RTC::ReturnCode_t TorqueController::onDeactivated(RTC::UniqueId ec_id)
00258 {
00259   std::cerr << "[" << m_profile.instance_name<< "] onDeactivated(" << ec_id << ")" << std::endl;
00260   return RTC::RTC_OK;
00261 }
00262 
00263 RTC::ReturnCode_t TorqueController::onExecute(RTC::UniqueId ec_id)
00264 { 
00265   m_loop++;
00266 
00267   hrp::dvector dq(m_robot->numJoints());
00268   
00269   // update port
00270   if (m_tauCurrentInIn.isNew()) {
00271     m_tauCurrentInIn.read();
00272   }
00273   if (m_tauMaxInIn.isNew()) {
00274     m_tauMaxInIn.read();
00275   }
00276   if (m_qCurrentInIn.isNew()) {
00277     m_qCurrentInIn.read();
00278   }
00279   if (m_qRefInIn.isNew()) {
00280     m_qRefInIn.read();
00281   }
00282 
00283   if (m_qRefIn.data.length() == m_robot->numJoints()) {
00284     if (m_tauCurrentIn.data.length() == m_robot->numJoints() &&
00285         m_qCurrentIn.data.length() == m_robot->numJoints()) {
00286 
00287       // update model
00288       for ( unsigned int i = 0; i < m_robot->numJoints(); i++ ){
00289         m_robot->joint(i)->q = m_qCurrentIn.data[i];
00290       }
00291       m_robot->calcForwardKinematics();   
00292 
00293       // calculate dq by torque controller
00294       executeTorqueControl(dq);
00295 
00296       // check range of motion and insert to output 
00297       for (unsigned int i = 0; i < m_robot->numJoints(); i++) {
00298         if (m_motorTorqueControllers[i].isEnabled()) {
00299           m_qRefOut.data[i] = std::min(std::max(m_qRefIn.data[i] + dq[i], m_robot->joint(i)->llimit), m_robot->joint(i)->ulimit);
00300         } else {
00301           m_qRefOut.data[i] = m_qRefIn.data[i]; // pass joint angle when controller is disabled
00302         }
00303       }
00304 
00305     } else {
00306       if (isDebug()) {
00307         std::cerr << "[" <<  m_profile.instance_name << "]" << "TorqueController input is not correct" << std::endl;
00308         std::cerr << "[" <<  m_profile.instance_name << "]" << " numJoints: " << m_robot->numJoints() << std::endl;
00309         std::cerr << "[" <<  m_profile.instance_name << "]" << "  qCurrent: " << m_qCurrentIn.data.length() << std::endl;
00310         std::cerr << "[" <<  m_profile.instance_name << "]" << "tauCurrent: " << m_tauCurrentIn.data.length() << std::endl;
00311         std::cerr << std::endl;
00312       }
00313       // pass qRefIn to qRefOut
00314       for (unsigned int i = 0; i < m_robot->numJoints(); i++) {
00315         m_qRefOut.data[i] = m_qRefIn.data[i];
00316       }
00317     }
00318     m_qRefOut.tm = m_qRefIn.tm;
00319     m_qRefOutOut.write();
00320   } else {
00321       if (isDebug()) {
00322         std::cerr << "[" <<  m_profile.instance_name << "]" << "TorqueController has incorrect qRefIn" << std::endl;
00323         std::cerr << "[" <<  m_profile.instance_name << "]" << " numJoints: " << m_robot->numJoints() << std::endl;
00324         std::cerr << "[" <<  m_profile.instance_name << "]" << "    qRefIn: " << m_qRefIn.data.length() << std::endl;
00325         std::cerr << std::endl;
00326       }
00327   }
00328 
00329   return RTC::RTC_OK;
00330 }
00331 
00332 /*
00333 RTC::ReturnCode_t TorqueController::onAborting(RTC::UniqueId ec_id)
00334 {
00335   return RTC::RTC_OK;
00336 }
00337 */
00338 
00339 /*
00340 RTC::ReturnCode_t TorqueController::onError(RTC::UniqueId ec_id)
00341 {
00342   return RTC::RTC_OK;
00343 }
00344 */
00345 
00346 /*
00347 RTC::ReturnCode_t TorqueController::onReset(RTC::UniqueId ec_id)
00348 {
00349   return RTC::RTC_OK;
00350 }
00351 */
00352 
00353 /*
00354 RTC::ReturnCode_t TorqueController::onStateUpdate(RTC::UniqueId ec_id)
00355 {
00356   return RTC::RTC_OK;
00357 }
00358 */
00359 
00360 /*
00361 RTC::ReturnCode_t TorqueController::onRateChanged(RTC::UniqueId ec_id)
00362 {
00363   return RTC::RTC_OK;
00364 }
00365 */
00366 
00367 void TorqueController::executeTorqueControl(hrp::dvector &dq)
00368 {
00369   unsigned int numJoints = m_robot->numJoints();
00370   hrp::dvector tauMax(numJoints);
00371   dq.resize(numJoints);
00372 
00373   // determine tauMax
00374   for(unsigned int i = 0; i < numJoints; i++) {
00375     double tauMaxFromModel = m_robot->joint(i)->climit * m_robot->joint(i)->gearRatio * m_robot->joint(i)->torqueConst;
00376     if ( m_tauMaxIn.data.length() ==  m_robot->numJoints() ) {
00377       tauMax[i] = std::min(tauMaxFromModel, m_tauMaxIn.data[i]);
00378     } else {
00379       tauMax[i] = tauMaxFromModel;
00380     }
00381   }
00382 
00383   // execute torque control
00384   // tauCurrent.length is assumed to be equal to numJoints (check in onExecute)
00385   if (isDebug()) {
00386     std::cerr << "[" <<  m_profile.instance_name << "]" << "tauCurrentIn: ";
00387     for (unsigned int i = 0; i < numJoints; i++) {
00388       std::cerr << " " << m_tauCurrentIn.data[i];
00389     }
00390     std::cerr << std::endl;
00391     std::cerr << "[" <<  m_profile.instance_name << "]" << "tauMax: ";
00392     for (unsigned int i = 0; i < numJoints; i++) {
00393       std::cerr << " " << tauMax[i];
00394     }
00395     std::cerr << std::endl;
00396   }
00397 
00398   Guard guard(m_mutex);
00399   for (unsigned int i = 0; i < numJoints; i++) {
00400     dq[i] = m_motorTorqueControllers[i].execute(m_tauCurrentIn.data[i], tauMax[i]); // twoDofController: tau = -K(q - qRef)
00401     // output debug message
00402     if (isDebug() && m_motorTorqueControllers[i].getMotorControllerState() != MotorTorqueController::INACTIVE) {
00403       m_motorTorqueControllers[i].printMotorControllerVariables();
00404     }
00405 
00406   }
00407     
00408   if (isDebug()) {
00409     std::cerr << "[" <<  m_profile.instance_name << "]" << "dq: ";
00410     for (int i = 0; i < dq.size(); i++) {
00411       std::cerr << dq[i] << " ";
00412     }
00413     std::cerr << std::endl;
00414   }
00415 
00416   return;
00417 }
00418 
00419 bool TorqueController::enableTorqueController(std::string jname)
00420 {
00421   bool succeed = false;
00422   for (std::vector<MotorTorqueController>::iterator it = m_motorTorqueControllers.begin(); it != m_motorTorqueControllers.end(); ++it) {
00423     if ((*it).getJointName() == jname){
00424       if (m_debugLevel > 0) {
00425         std::cerr << "[" <<  m_profile.instance_name << "]" << "Enable torque controller in " << jname << std::endl;
00426       }
00427       succeed = (*it).enable();
00428     }
00429   }
00430   return succeed;
00431 }
00432 
00433 bool TorqueController::enableMultipleTorqueControllers(const OpenHRP::TorqueControllerService::StrSequence& jnames)
00434 {
00435   bool succeed = true;
00436   bool retval;
00437   for (unsigned int i = 0; i < jnames.length(); i++) {
00438     retval = enableTorqueController(std::string(jnames[i]));
00439     if (!retval) { // return false when once failed
00440       succeed = false;
00441     }
00442   }
00443   return succeed;
00444 }
00445 
00446 bool TorqueController::disableTorqueController(std::string jname)
00447 {
00448   bool succeed = false;
00449   for (std::vector<MotorTorqueController>::iterator it = m_motorTorqueControllers.begin(); it != m_motorTorqueControllers.end(); ++it) {
00450     if ((*it).getJointName() == jname){
00451       if (m_debugLevel > 0) {
00452         std::cerr << "[" <<  m_profile.instance_name << "]" << "Disable torque controller in " << jname << std::endl;
00453       }
00454       succeed = (*it).disable();
00455     }
00456   }
00457   return succeed;
00458 }
00459 
00460 bool TorqueController::disableMultipleTorqueControllers(const OpenHRP::TorqueControllerService::StrSequence& jnames)
00461 {
00462   bool succeed = true;
00463   bool retval;
00464   for (unsigned int i = 0; i < jnames.length(); i++) {
00465     retval = disableTorqueController(std::string(jnames[i]));
00466     if (!retval) { // return false when once failed
00467       succeed = false;
00468     }
00469   }
00470   return succeed;
00471 }
00472 
00473 bool TorqueController::startTorqueControl(std::string jname)
00474 {
00475   bool succeed = false;
00476   for (std::vector<MotorTorqueController>::iterator it = m_motorTorqueControllers.begin(); it != m_motorTorqueControllers.end(); ++it) {
00477     if ((*it).getJointName() == jname){
00478       if (m_debugLevel > 0) {
00479         std::cerr << "[" <<  m_profile.instance_name << "]" << "Start torque control in " << jname << std::endl;
00480       }
00481       if (!(*it).isEnabled()) {
00482         succeed = enableTorqueController(jname);
00483         if (!succeed) {
00484           if (m_debugLevel > 0) {
00485             std::cerr << "[" <<  m_profile.instance_name << "]" << "Failed to enable torque control in " << jname << std::endl;
00486           }
00487           return succeed;
00488         }
00489       }
00490       succeed = (*it).activate();
00491     }
00492   }
00493   return succeed;
00494 }
00495 
00496 bool TorqueController::startMultipleTorqueControls(const OpenHRP::TorqueControllerService::StrSequence& jnames)
00497 {
00498   bool succeed = true;
00499   bool retval;
00500   for (unsigned int i = 0; i < jnames.length(); i++) {
00501     retval = startTorqueControl(std::string(jnames[i]));
00502     if (!retval) { // return false when once failed
00503       succeed = false;
00504     }
00505   }
00506   return succeed;
00507 }
00508 
00509 bool TorqueController::stopTorqueControl(std::string jname)
00510 {
00511   bool succeed = false;
00512   for (std::vector<MotorTorqueController>::iterator it = m_motorTorqueControllers.begin(); it != m_motorTorqueControllers.end(); ++it) {
00513     if ((*it).getJointName() == jname){
00514       if (m_debugLevel > 0) {
00515         std::cerr << "[" <<  m_profile.instance_name << "]" << "Stop torque control in " << jname << std::endl;
00516       }
00517       succeed = (*it).deactivate();
00518     }
00519   }
00520   return succeed;
00521 }
00522 
00523 bool TorqueController::stopMultipleTorqueControls(const OpenHRP::TorqueControllerService::StrSequence& jnames)
00524 {
00525   bool succeed = true;
00526   bool retval;
00527   for (unsigned int i = 0; i < jnames.length(); i++) {
00528     retval = stopTorqueControl(std::string(jnames[i]));
00529     if (!retval) { // return false when once failed
00530       succeed = false;
00531     }
00532   }
00533   return succeed;
00534 }
00535 
00536 bool TorqueController::setReferenceTorque(std::string jname, double tauRef)
00537 {
00538   bool succeed = false;
00539   
00540   // lock mutex
00541   Guard guard(m_mutex);
00542 
00543   // search target joint
00544   for (std::vector<MotorTorqueController>::iterator it = m_motorTorqueControllers.begin(); it != m_motorTorqueControllers.end(); ++it) {
00545     if ((*it).getJointName() == jname) {
00546       if (m_debugLevel > 0) {
00547         std::cerr << "[" <<  m_profile.instance_name << "]" << "Set " << jname << " reference torque to " << tauRef << std::endl;
00548       }
00549       succeed = (*it).setReferenceTorque(tauRef);
00550     }
00551   }
00552   return succeed;
00553 }
00554 
00555 bool TorqueController::setMultipleReferenceTorques(const OpenHRP::TorqueControllerService::StrSequence& jnames, const OpenHRP::TorqueControllerService::dSequence& tauRefs)
00556 {
00557   bool succeed = true;
00558   bool retval;
00559   // check accordance of joint name and tauRefs
00560   if (jnames.length() != tauRefs.length()) {
00561     std::cerr << "[" <<  m_profile.instance_name << "]" << "Length of jnames and tauRefs are different." << std::endl;
00562     return false;
00563   }
00564   // set reference torques
00565   for (unsigned int i = 0; i < jnames.length(); i++) {
00566     retval = setReferenceTorque(std::string(jnames[i]), tauRefs[i]);
00567     if (!retval) { // return false when once failed
00568       succeed = false;
00569     }
00570   }
00571   return succeed;
00572 }
00573 
00574 bool TorqueController::setTorqueControllerParam(const std::string jname, const OpenHRP::TorqueControllerService::torqueControllerParam& i_param)
00575 {
00576   Guard guard(m_mutex);
00577 
00578   // find target motor controller
00579   MotorTorqueController *tgt_controller = NULL;
00580   for (std::vector<MotorTorqueController>::iterator it = m_motorTorqueControllers.begin(); it != m_motorTorqueControllers.end(); ++it) {
00581     if ((*it).getJointName() == jname){
00582       std::cerr << "[" <<  m_profile.instance_name << "]" << "target joint:" << jname << std::endl;
00583       tgt_controller = &(*it);
00584     }
00585   }
00586   if (tgt_controller == NULL) {
00587     std::cerr << "[" <<  m_profile.instance_name << "]" << jname << "does not found." << std::endl;
00588     return false;
00589   }
00590 
00591   // update torque controller param
00592   bool retval;
00593   MotorTorqueController::motor_model_t model_type = tgt_controller->getMotorModelType();
00594   switch(model_type) { // dt is defined by controller cycle
00595   case MotorTorqueController::TWO_DOF_CONTROLLER:
00596   { // limit scope for param 
00597     std::cerr << "[" <<  m_profile.instance_name << "]" << "new param:" << i_param.ke << " " << i_param.tc << " " << std::endl;
00598     TwoDofController::TwoDofControllerParam param;
00599     param.ke = i_param.ke; param.tc = i_param.tc; param.dt = m_dt;
00600     retval = tgt_controller->updateControllerParam(param);
00601     break;
00602   }
00603   case MotorTorqueController::TWO_DOF_CONTROLLER_PD_MODEL:
00604   { // limit scope for param 
00605     std::cerr << "[" <<  m_profile.instance_name << "]" << "new param:" << i_param.ke << " " << i_param.kd << " " << i_param.tc << " " << std::endl;
00606     TwoDofControllerPDModel::TwoDofControllerPDModelParam param;
00607     param.ke = i_param.ke; param.kd = i_param.kd; param.tc = i_param.tc; param.dt = m_dt;
00608     retval = tgt_controller->updateControllerParam(param);
00609     break;
00610   }
00611   case MotorTorqueController::TWO_DOF_CONTROLLER_DYNAMICS_MODEL:
00612   { // limit scope for param 
00613     std::cerr << "[" <<  m_profile.instance_name << "]" << "new param:" << i_param.alpha << " " << i_param.beta << " " << i_param.ki << " " << i_param.tc << " " << std::endl;
00614     TwoDofControllerDynamicsModel::TwoDofControllerDynamicsModelParam param;
00615     param.alpha = i_param.alpha; param.beta = i_param.beta; param.ki = i_param.ki; param.tc = i_param.tc; param.dt = m_dt;
00616     retval = tgt_controller->updateControllerParam(param);
00617     break;
00618   }
00619   default:
00620     return false;
00621   }
00622   
00623   return retval;
00624 }
00625 
00626 bool TorqueController::getTorqueControllerParam(const std::string jname, OpenHRP::TorqueControllerService::torqueControllerParam& i_param)
00627 {
00628   Guard guard(m_mutex);
00629   
00630   // find target motor controller
00631   MotorTorqueController *tgt_controller = NULL;
00632   for (std::vector<MotorTorqueController>::iterator it = m_motorTorqueControllers.begin(); it != m_motorTorqueControllers.end(); ++it) {
00633     if ((*it).getJointName() == jname) {
00634       std::cerr << "[" <<  m_profile.instance_name << "]" << "target joint:" << jname << std::endl;
00635       tgt_controller = &(*it);
00636     }
00637   }
00638   if (tgt_controller == NULL) {
00639     std::cerr << "[" <<  m_profile.instance_name << "]" << jname << "does not found." << std::endl;
00640     return false;
00641   }
00642 
00643   // copy torque controller param
00644   bool retval;
00645   MotorTorqueController::motor_model_t model_type = tgt_controller->getMotorModelType();
00646   switch(model_type) { 
00647   case MotorTorqueController::TWO_DOF_CONTROLLER:
00648   { // limit scope for param 
00649     TwoDofController::TwoDofControllerParam param;
00650     retval = tgt_controller->getControllerParam(param);    
00651     i_param.ke = param.ke;
00652     i_param.tc = param.tc;
00653     break;
00654   }
00655   case MotorTorqueController::TWO_DOF_CONTROLLER_PD_MODEL:
00656   { // limit scope for param 
00657     TwoDofControllerPDModel::TwoDofControllerPDModelParam param;
00658     retval = tgt_controller->getControllerParam(param);
00659     i_param.ke = param.ke;
00660     i_param.kd = param.kd;
00661     i_param.tc = param.tc;
00662     break;
00663   }
00664   case MotorTorqueController::TWO_DOF_CONTROLLER_DYNAMICS_MODEL:
00665   { // limit scope for param 
00666     TwoDofControllerDynamicsModel::TwoDofControllerDynamicsModelParam param;
00667     retval = tgt_controller->getControllerParam(param);
00668     i_param.alpha = param.alpha;
00669     i_param.beta = param.beta;
00670     i_param.ki = param.ki;
00671     i_param.tc = param.tc;
00672     break;
00673   }
00674   default:
00675     return false;
00676   }
00677   
00678   return retval;
00679 }
00680 
00681 void TorqueController::updateParam(double &val, double &val_new)
00682 {
00683   // update value unless val_new is not 0
00684   if (val_new != 0) { 
00685     val = val_new;
00686   }
00687   return;
00688 }
00689 
00690 bool TorqueController::isDebug(int cycle)
00691 {
00692   return ((m_debugLevel == 1 && (m_loop % cycle == 0)) || m_debugLevel > 1);
00693 }
00694 
00695 extern "C"
00696 {
00697 
00698   void TorqueControllerInit(RTC::Manager* manager)
00699   {
00700     RTC::Properties profile(torquecontroller_spec);
00701     manager->registerFactory(profile,
00702                              RTC::Create<TorqueController>,
00703                              RTC::Delete<TorqueController>);
00704   }
00705 
00706 };
00707 
00708 


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Wed Sep 6 2017 02:35:56