11 #include "hrpsys/util/VectorConvert.h" 14 #include <hrpModel/ModelLoaderUtil.h> 23 "implementation_id",
"TorqueController",
24 "type_name",
"TorqueController",
25 "description",
"Component for joint torque control",
26 "version", HRPSYS_PACKAGE_VERSION,
28 "category",
"example",
29 "activity_type",
"DataFlowComponent",
32 "lang_type",
"compile",
34 "conf.default.debugLevel",
"0",
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"),
61 std::cerr <<
"[" <<
m_profile.instance_name <<
"] onInitialize()" << std::endl;
97 std::string nameServer = rtcManager.
getConfig()[
"corba.nameservers"];
98 int comPos = nameServer.find(
",");
100 comPos = nameServer.length();
102 nameServer = nameServer.substr(0, comPos);
106 std::cerr << prop[
"model"].c_str() << std::endl;
108 CosNaming::NamingContext::_duplicate(
naming.getRootContext())
110 std::cerr <<
"[" <<
m_profile.instance_name <<
"] failed to load model[" << prop[
"model"] <<
"]" 117 std::map<int, MotorTorqueController::motor_model_t> param_num_to_motor_model_type;
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;
128 model_type = param_num_to_motor_model_type[motorTorqueControllerParamsFromConf.size()];
131 switch (model_type) {
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) {
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());
143 tdc_dynamics_model_params[
i].dt =
m_dt;
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;
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) {
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());
166 tdc_pd_model_params[
i].dt =
m_dt;
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;
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) {
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());
189 tdc_params[
i].dt =
m_dt;
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;
204 bool dq_min_max_from_conf_is_valid =
false;
206 if (dqMinMaxFromConf.size() == 2 * m_robot->numJoints()) {
207 dq_min_max_from_conf_is_valid =
true;
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;
212 for (
unsigned int i = 0;
i < m_robot->numJoints();
i++) {
215 if(dq_min_max_from_conf_is_valid) {
216 double tmp_dq_min, tmp_dq_max;
224 m_qRefOut.data.length(m_robot->numJoints());
253 std::cerr <<
"[" <<
m_profile.instance_name<<
"] onActivated(" << ec_id <<
")" << std::endl;
259 std::cerr <<
"[" <<
m_profile.instance_name<<
"] onDeactivated(" << ec_id <<
")" << std::endl;
288 for (
unsigned int i = 0;
i <
m_robot->numJoints();
i++ ){
291 m_robot->calcForwardKinematics();
297 for (
unsigned int i = 0;
i <
m_robot->numJoints();
i++) {
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;
314 for (
unsigned int i = 0;
i <
m_robot->numJoints();
i++) {
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;
369 unsigned int numJoints =
m_robot->numJoints();
371 dq.resize(numJoints);
374 for(
unsigned int i = 0;
i < numJoints;
i++) {
379 tauMax[
i] = tauMaxFromModel;
386 std::cerr <<
"[" <<
m_profile.instance_name <<
"]" <<
"tauCurrentIn: ";
387 for (
unsigned int i = 0;
i < numJoints;
i++) {
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];
395 std::cerr << std::endl;
399 for (
unsigned int i = 0;
i < numJoints;
i++) {
409 std::cerr <<
"[" <<
m_profile.instance_name <<
"]" <<
"dq: ";
410 for (
int i = 0;
i < dq.size();
i++) {
411 std::cerr << dq[
i] <<
" ";
413 std::cerr << std::endl;
421 bool succeed =
false;
423 if ((*it).getJointName() == jname){
425 std::cerr <<
"[" <<
m_profile.instance_name <<
"]" <<
"Enable torque controller in " << jname << std::endl;
427 succeed = (*it).enable();
437 for (
unsigned int i = 0;
i < jnames.length();
i++) {
448 bool succeed =
false;
450 if ((*it).getJointName() == jname){
452 std::cerr <<
"[" <<
m_profile.instance_name <<
"]" <<
"Disable torque controller in " << jname << std::endl;
454 succeed = (*it).disable();
464 for (
unsigned int i = 0;
i < jnames.length();
i++) {
475 bool succeed =
false;
477 if ((*it).getJointName() == jname){
479 std::cerr <<
"[" <<
m_profile.instance_name <<
"]" <<
"Start torque control in " << jname << std::endl;
481 if (!(*it).isEnabled()) {
485 std::cerr <<
"[" <<
m_profile.instance_name <<
"]" <<
"Failed to enable torque control in " << jname << std::endl;
490 succeed = (*it).activate();
500 for (
unsigned int i = 0;
i < jnames.length();
i++) {
511 bool succeed =
false;
513 if ((*it).getJointName() == jname){
515 std::cerr <<
"[" <<
m_profile.instance_name <<
"]" <<
"Stop torque control in " << jname << std::endl;
517 succeed = (*it).deactivate();
527 for (
unsigned int i = 0;
i < jnames.length();
i++) {
538 bool succeed =
false;
545 if ((*it).getJointName() == jname) {
547 std::cerr <<
"[" <<
m_profile.instance_name <<
"]" <<
"Set " << jname <<
" reference torque to " << tauRef << std::endl;
549 succeed = (*it).setReferenceTorque(tauRef);
560 if (jnames.length() != tauRefs.length()) {
561 std::cerr <<
"[" <<
m_profile.instance_name <<
"]" <<
"Length of jnames and tauRefs are different." << std::endl;
565 for (
unsigned int i = 0;
i < jnames.length();
i++) {
581 if ((*it).getJointName() == jname){
582 std::cerr <<
"[" <<
m_profile.instance_name <<
"]" <<
"target joint:" << jname << std::endl;
583 tgt_controller = &(*it);
586 if (tgt_controller == NULL) {
587 std::cerr <<
"[" <<
m_profile.instance_name <<
"]" << jname <<
"does not found." << std::endl;
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;
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;
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;
633 if ((*it).getJointName() == jname) {
634 std::cerr <<
"[" <<
m_profile.instance_name <<
"]" <<
"target joint:" << jname << std::endl;
635 tgt_controller = &(*it);
638 if (tgt_controller == NULL) {
639 std::cerr <<
"[" <<
m_profile.instance_name <<
"]" << jname <<
"does not found." << std::endl;
651 i_param.ke = param.
ke;
652 i_param.tc = param.
tc;
659 i_param.ke = param.
ke;
660 i_param.kd = param.
kd;
661 i_param.tc = param.
tc;
668 i_param.alpha = param.
alpha;
669 i_param.beta = param.
beta;
670 i_param.ki = param.
ki;
671 i_param.tc = param.
tc;
702 RTC::Create<TorqueController>,
703 RTC::Delete<TorqueController>);
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)
InPort< TimedDoubleSeq > m_tauCurrentInIn
bool setReferenceTorque(std::string jname, double tauRef)
static int getControllerParamNum()
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
static int getControllerParamNum()
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)
InPort< TimedDoubleSeq > m_qRefInIn
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
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)
RTC::CorbaPort m_TorqueControllerServicePort
bool isDebug(int cycle=20)
coil::Guard< coil::Mutex > Guard
bool enableMultipleTorqueControllers(const OpenHRP::TorqueControllerService::StrSequence &jnames)
bool addPort(PortBase &port)
virtual bool write(DataType &value)
virtual RTC::ReturnCode_t onInitialize()
virtual ~TorqueController()
Destructor.
static int getControllerParamNum()
bool addInPort(const char *name, InPortBase &inport)
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
bool registerProvider(const char *instance_name, const char *type_name, PortableServer::RefCountServantBase &provider)