SamplePD_HG.cpp
Go to the documentation of this file.
00001 // -*- mode: c++; indent-tabs-mode: t; tab-width: 4; c-basic-offset: 4; -*-
00002 /*
00003  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
00004  * All rights reserved. This program is made available under the terms of the
00005  * Eclipse Public License v1.0 which accompanies this distribution, and is
00006  * available at http://www.eclipse.org/legal/epl-v10.html
00007  * Contributors:
00008  * National Institute of Advanced Industrial Science and Technology (AIST)
00009  * General Robotix Inc. 
00010  */
00019 #include "SamplePD_HG.h"
00020 
00021 #include <iostream>
00022 
00023 #define PD_DOF (17)
00024 #define HG_DOF (12)
00025 #define TIMESTEP 0.002
00026 
00027 #define WAIST_P             26
00028 #define WAIST_R             27
00029 #define CHEST               28
00030 #define LARM_SHOULDER_P     19
00031 #define LARM_SHOULDER_R     20
00032 #define LARM_SHOULDER_Y     21  
00033 #define LARM_ELBOW          22
00034 #define LARM_WRIST_Y        23
00035 #define LARM_WRIST_P        24  
00036 #define LARM_WRIST_R        25
00037 #define RARM_SHOULDER_P     6
00038 #define RARM_SHOULDER_R     7
00039 #define RARM_SHOULDER_Y     8
00040 #define RARM_ELBOW          9
00041 #define RARM_WRIST_Y        10
00042 #define RARM_WRIST_P        11  
00043 #define RARM_WRIST_R        12
00044 #define LLEG_HIP_R          13
00045 #define LLEG_HIP_P          14
00046 #define LLEG_HIP_Y          15
00047 #define LLEG_KNEE           16
00048 #define LLEG_ANKLE_P        17
00049 #define LLEG_ANKLE_R        18
00050 #define RLEG_HIP_R          0
00051 #define RLEG_HIP_P          1   
00052 #define RLEG_HIP_Y          2
00053 #define RLEG_KNEE           3
00054 #define RLEG_ANKLE_P        4
00055 #define RLEG_ANKLE_R        5
00056 
00057 #define ANGLE_FILE "etc/angle.dat"
00058 #define VEL_FILE   "etc/vel.dat"
00059 #define ACC_FILE   "etc/acc.dat"
00060 
00061 #define GAIN_FILE  "etc/PDgain.dat"
00062 
00063 namespace {
00064   const bool CONTROLLER_BRIDGE_DEBUG = false;
00065 }
00066 
00067 
00068 // Module specification
00069 // <rtc-template block="module_spec">
00070 static const char* SamplePD_HG_spec[] =
00071   {
00072     "implementation_id", "SamplePD_HG",
00073     "type_name",         "SamplePD_HG",
00074     "description",       "Sample PD component",
00075     "version",           "0.1",
00076     "vendor",            "AIST",
00077     "category",          "Generic",
00078     "activity_type",     "DataFlowComponent",
00079     "max_instance",      "10",
00080     "language",          "C++",
00081     "lang_type",         "compile",
00082     // Configuration variables
00083 
00084     ""
00085   };
00086 // </rtc-template>
00087 
00088 SamplePD_HG::SamplePD_HG(RTC::Manager* manager)
00089   : RTC::DataFlowComponentBase(manager),
00090     // <rtc-template block="initializer">
00091     m_angle_inIn("angle_in", m_angle_in),
00092     m_torqueOut("torque", m_torque),
00093     m_angle_outOut("angle_out", m_angle_out),
00094     m_velOut("vel", m_vel),
00095     m_accOut("acc", m_acc),
00096       
00097     // </rtc-template>
00098     dummy(0),
00099     qold(DOF)
00100 {
00101   if( CONTROLLER_BRIDGE_DEBUG )
00102   {
00103     std::cout << "SamplePD_HG::SamplePD_HG" << std::endl;
00104   }
00105 
00106   // Registration: InPort/OutPort/Service
00107   // <rtc-template block="registration">
00108   
00109   // Set service provider to Ports
00110   
00111   // Set service consumers to Ports
00112   
00113   // Set CORBA Service Ports
00114   
00115   // </rtc-template>
00116 }
00117 
00118 SamplePD_HG::~SamplePD_HG()
00119 {
00120   closeFiles();
00121   delete [] Pgain;
00122   delete [] Dgain;
00123 }
00124 
00125 
00126 RTC::ReturnCode_t SamplePD_HG::onInitialize()
00127 {
00128   // <rtc-template block="bind_config">
00129   // Bind variables and configuration variable
00130   if( CONTROLLER_BRIDGE_DEBUG )
00131   {
00132     std::cout << "onInitialize" << std::endl;
00133   }
00134 
00135   // Set InPort buffers
00136   addInPort("angle_in", m_angle_inIn);
00137   
00138   // Set OutPort buffer
00139   addOutPort("torque", m_torqueOut);
00140   addOutPort("angle_out", m_angle_outOut);
00141   addOutPort("vel", m_velOut);
00142   addOutPort("acc", m_accOut);
00143 
00144   Pgain = new double[DOF];
00145   Dgain = new double[DOF];
00146 
00147   gain.open(GAIN_FILE);
00148   if (gain.is_open()){
00149     for (int i=0; i<DOF; i++){
00150       gain >> Pgain[i];
00151       gain >> Dgain[i];
00152     }
00153     gain.close();
00154   }else{
00155     std::cerr << GAIN_FILE << " not opened" << std::endl;
00156   }
00157   // </rtc-template>
00158 
00159   m_angle_in.data.length(DOF);
00160   m_angle_out.data.length(HG_DOF);
00161   m_vel.data.length(HG_DOF);
00162   m_acc.data.length(HG_DOF);
00163   m_torque.data.length(PD_DOF);
00164 
00165   return RTC::RTC_OK;
00166 }
00167 
00168 
00169 
00170 /*
00171 RTC::ReturnCode_t SamplePD_HG::onFinalize()
00172 {
00173   return RTC::RTC_OK;
00174 }
00175 */
00176 
00177 /*
00178 RTC::ReturnCode_t SamplePD_HG::onStartup(RTC::UniqueId ec_id)
00179 {
00180   return RTC::RTC_OK;
00181 }
00182 */
00183 
00184 /*
00185 RTC::ReturnCode_t SamplePD_HG::onShutdown(RTC::UniqueId ec_id)
00186 {
00187   return RTC::RTC_OK;
00188 }
00189 */
00190 
00191 RTC::ReturnCode_t SamplePD_HG::onActivated(RTC::UniqueId ec_id)
00192 {
00193   std::cout << "on Activated" << std::endl;
00194   openFiles();
00195 
00196   if(m_angle_inIn.isNew()){
00197     m_angle_inIn.read();
00198   }
00199 
00200   for(int i=0; i < DOF; ++i){
00201     qold[i] = m_angle_in.data[i];
00202     q_ref[i] = dq_ref[i] = ddq_ref[i] = 0.0;
00203   }
00204 
00205   return RTC::RTC_OK;
00206 }
00207 
00208 
00209 RTC::ReturnCode_t SamplePD_HG::onDeactivated(RTC::UniqueId ec_id)
00210 {
00211   std::cout << "on Deactivated" << std::endl;
00212   closeFiles();
00213   return RTC::RTC_OK;
00214 }
00215 
00216 
00217 
00218 RTC::ReturnCode_t SamplePD_HG::onExecute(RTC::UniqueId ec_id)
00219 {
00220   if( CONTROLLER_BRIDGE_DEBUG )
00221   {
00222     std::cout << "onExecute" << std::endl;
00223     std::string localStr;
00224     std::cin >> localStr; 
00225   }
00226 
00227   if(m_angle_inIn.isNew()){
00228     m_angle_inIn.read();
00229   }
00230 
00231   if(!angle.eof()){
00232     angle >> q_ref[0]; vel >> dq_ref[0]; acc >> ddq_ref[0];// skip time
00233     for (int i=0; i<DOF; i++){
00234       angle >> q_ref[i];
00235       vel >> dq_ref[i];
00236       acc >> ddq_ref[i];
00237     }
00238   }
00239 
00240   double tor_ref[DOF];
00241   for(int i=0; i<DOF; i++){
00242     double q = m_angle_in.data[i];
00243     double dq = (q - qold[i]) / TIMESTEP;
00244     qold[i] = q;
00245     
00246     tor_ref[i] = -(q - q_ref[i]) * Pgain[i] - (dq - dq_ref[i]) * Dgain[i];
00247   }
00248 
00249   m_torque.data[0] = tor_ref[WAIST_P];
00250   m_torque.data[1] = tor_ref[WAIST_R];
00251   m_torque.data[2] = tor_ref[CHEST];
00252   m_torque.data[3] = tor_ref[LARM_SHOULDER_P];
00253   m_torque.data[4] = tor_ref[LARM_SHOULDER_R];
00254   m_torque.data[5] = tor_ref[LARM_SHOULDER_Y];
00255   m_torque.data[6] = tor_ref[LARM_ELBOW];
00256   m_torque.data[7] = tor_ref[LARM_WRIST_Y];
00257   m_torque.data[8] = tor_ref[LARM_WRIST_P];
00258   m_torque.data[9] = tor_ref[LARM_WRIST_R];
00259   m_torque.data[10] = tor_ref[RARM_SHOULDER_P];
00260   m_torque.data[11] = tor_ref[RARM_SHOULDER_R];
00261   m_torque.data[12] = tor_ref[RARM_SHOULDER_Y];
00262   m_torque.data[13] = tor_ref[RARM_ELBOW];
00263   m_torque.data[14] = tor_ref[RARM_WRIST_Y];
00264   m_torque.data[15] = tor_ref[RARM_WRIST_P];
00265   m_torque.data[16] = tor_ref[RARM_WRIST_R];
00266 
00267   m_angle_out.data[0] = q_ref[LLEG_HIP_R];
00268   m_angle_out.data[1] = q_ref[LLEG_HIP_P];
00269   m_angle_out.data[2] = q_ref[LLEG_HIP_Y];
00270   m_angle_out.data[3] = q_ref[LLEG_KNEE];
00271   m_angle_out.data[4] = q_ref[LLEG_ANKLE_P];
00272   m_angle_out.data[5] = q_ref[LLEG_ANKLE_R];
00273   m_angle_out.data[6] = q_ref[RLEG_HIP_R];
00274   m_angle_out.data[7] = q_ref[RLEG_HIP_P];
00275   m_angle_out.data[8] = q_ref[RLEG_HIP_Y];
00276   m_angle_out.data[9] = q_ref[RLEG_KNEE];
00277   m_angle_out.data[10] = q_ref[RLEG_ANKLE_P];
00278   m_angle_out.data[11] = q_ref[RLEG_ANKLE_R];
00279   m_vel.data[0] = dq_ref[LLEG_HIP_R];
00280   m_vel.data[1] = dq_ref[LLEG_HIP_P];
00281   m_vel.data[2] = dq_ref[LLEG_HIP_Y];
00282   m_vel.data[3] = dq_ref[LLEG_KNEE];
00283   m_vel.data[4] = dq_ref[LLEG_ANKLE_P];
00284   m_vel.data[5] = dq_ref[LLEG_ANKLE_R];
00285   m_vel.data[6] = dq_ref[RLEG_HIP_R];
00286   m_vel.data[7] = dq_ref[RLEG_HIP_P];
00287   m_vel.data[8] = dq_ref[RLEG_HIP_Y];
00288   m_vel.data[9] = dq_ref[RLEG_KNEE];
00289   m_vel.data[10] = dq_ref[RLEG_ANKLE_P];
00290   m_vel.data[11] = dq_ref[RLEG_ANKLE_R];
00291   m_acc.data[0] = ddq_ref[LLEG_HIP_R];
00292   m_acc.data[1] = ddq_ref[LLEG_HIP_P];
00293   m_acc.data[2] = ddq_ref[LLEG_HIP_Y];
00294   m_acc.data[3] = ddq_ref[LLEG_KNEE];
00295   m_acc.data[4] = ddq_ref[LLEG_ANKLE_P];
00296   m_acc.data[5] = ddq_ref[LLEG_ANKLE_R];
00297   m_acc.data[6] = ddq_ref[RLEG_HIP_R];
00298   m_acc.data[7] = ddq_ref[RLEG_HIP_P];
00299   m_acc.data[8] = ddq_ref[RLEG_HIP_Y];
00300   m_acc.data[9] = ddq_ref[RLEG_KNEE];
00301   m_acc.data[10] = ddq_ref[RLEG_ANKLE_P];
00302   m_acc.data[11] = ddq_ref[RLEG_ANKLE_R];
00303 
00304   m_torqueOut.write();
00305   m_angle_outOut.write();
00306   m_velOut.write();
00307   m_accOut.write();
00308 
00309   return RTC::RTC_OK;
00310 }
00311 
00312 
00313 /*
00314   RTC::ReturnCode_t SamplePD_HG::onAborting(RTC::UniqueId ec_id)
00315   {
00316   return RTC::RTC_OK;
00317   }
00318 */
00319 
00320 /*
00321   RTC::ReturnCode_t SamplePD_HG::onError(RTC::UniqueId ec_id)
00322   {
00323   return RTC::RTC_OK;
00324   }
00325 */
00326 
00327 /*
00328   RTC::ReturnCode_t SamplePD_HG::onReset(RTC::UniqueId ec_id)
00329   {
00330   return RTC::RTC_OK;
00331   }
00332 */
00333 
00334 /*
00335   RTC::ReturnCode_t SamplePD_HG::onStateUpdate(RTC::UniqueId ec_id)
00336   {
00337   return RTC::RTC_OK;
00338   }
00339 */
00340 
00341 /*
00342   RTC::ReturnCode_t SamplePD_HG::onRateChanged(RTC::UniqueId ec_id)
00343   {
00344   return RTC::RTC_OK;
00345   }
00346 */
00347 
00348 void SamplePD_HG::openFiles()
00349 {
00350   angle.open(ANGLE_FILE);
00351   if (!angle.is_open()){
00352     std::cerr << ANGLE_FILE << " not opened" << std::endl;
00353   }
00354 
00355   vel.open(VEL_FILE);
00356   if (!vel.is_open()){
00357     std::cerr << VEL_FILE << " not opened" << std::endl;
00358   }
00359 
00360   acc.open(ACC_FILE);
00361   if (!acc.is_open())
00362   {
00363     std::cerr << ACC_FILE << " not opend" << std::endl;
00364   }
00365 }
00366 
00367 void SamplePD_HG::closeFiles()
00368 {
00369     if( angle.is_open() ){
00370         angle.close();
00371         angle.clear();
00372     }
00373     if( vel.is_open() ){
00374         vel.close();
00375         vel.clear();
00376     }
00377     if( acc.is_open() ){
00378         acc.close();
00379         acc.clear();
00380     }
00381 }
00382 
00383 
00384 extern "C"
00385 {
00386 
00387   DLL_EXPORT void SamplePD_HGInit(RTC::Manager* manager)
00388   {
00389     coil::Properties profile(SamplePD_HG_spec);
00390     manager->registerFactory(profile,
00391                              RTC::Create<SamplePD_HG>,
00392                              RTC::Delete<SamplePD_HG>);
00393   }
00394 
00395 };
00396 


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sun Apr 2 2017 03:43:56