SampleLF.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 "SampleLF.h"
00020 
00021 #include <iostream>
00022 #include <math.h>
00023 
00024 #define DOF (29)
00025 #define TIMESTEP 0.002
00026 
00027 #define GAIN_FILE  "etc/PDgain.dat"
00028 
00029 
00030 namespace {
00031   const bool CONTROLLER_BRIDGE_DEBUG = false;
00032 }
00033 
00034 
00035 // Module specification
00036 // <rtc-template block="module_spec">
00037 static const char* samplepd_spec[] =
00038   {
00039     "implementation_id", "SampleLF",
00040     "type_name",         "SampleLF",
00041     "description",       "Sample LF component",
00042     "version",           "0.1",
00043     "vendor",            "AIST",
00044     "category",          "Generic",
00045     "activity_type",     "DataFlowComponent",
00046     "max_instance",      "10",
00047     "language",          "C++",
00048     "lang_type",         "compile",
00049     // Configuration variables
00050 
00051     ""
00052   };
00053 // </rtc-template>
00054 
00055 SampleLF::SampleLF(RTC::Manager* manager)
00056   : RTC::DataFlowComponentBase(manager),
00057     // <rtc-template block="initializer">
00058     m_angleIn("angle", m_angle),
00059     m_torqueInL("r_torque_out",m_torqueL),
00060     m_torqueInR("l_torque_out",m_torqueR),
00061     m_torqueOut("torque", m_torque),
00062     
00063     // </rtc-template>
00064     dummy(0),
00065     qold(DOF),
00066     qref_old(DOF)
00067 {
00068   if( CONTROLLER_BRIDGE_DEBUG )
00069   {
00070     std::cout << "SampleLF::SampleLF" << std::endl;
00071   }
00072   // Registration: InPort/OutPort/Service
00073   // <rtc-template block="registration">
00074 
00075   // Set service provider to Ports
00076   
00077   // Set service consumers to Ports
00078   
00079   // Set CORBA Service Ports
00080   
00081   // </rtc-template>
00082 }
00083 
00084 SampleLF::~SampleLF()
00085 {
00086   closeFiles();
00087   delete [] Pgain;
00088   delete [] Dgain;
00089 }
00090 
00091 
00092 RTC::ReturnCode_t SampleLF::onInitialize()
00093 {
00094   // <rtc-template block="bind_config">
00095   // Bind variables and configuration variable
00096   if( CONTROLLER_BRIDGE_DEBUG )
00097   {
00098     std::cout << "onInitialize" << std::endl;
00099   }
00100 
00101   // Set InPort buffers
00102   addInPort("angle", m_angleIn);
00103   addInPort("r_torque_out", m_torqueInL);
00104   addInPort("l_torque_out", m_torqueInR);
00105 
00106   // Set OutPort buffer
00107   addOutPort("torque", m_torqueOut);
00108   // </rtc-template>
00109 
00110   Pgain = new double[DOF];
00111   Dgain = new double[DOF];
00112 
00113   gain.open(GAIN_FILE);
00114   if (gain.is_open()){
00115     for (int i=0; i<DOF; i++){
00116       gain >> Pgain[i];
00117       gain >> Dgain[i];
00118     }
00119     gain.close();
00120   }else{
00121     std::cerr << GAIN_FILE << " not opened" << std::endl;
00122   }
00123   m_torque.data.length(DOF);
00124   m_torqueL.data.length(1);
00125   m_torqueR.data.length(1);
00126   m_angle.data.length(DOF);
00127 
00128   return RTC::RTC_OK;
00129 }
00130 
00131 
00132 /*
00133 RTC::ReturnCode_t SampleLF::onFinalize()
00134 {
00135   return RTC::RTC_OK;
00136 }
00137 */
00138 
00139 /*
00140 RTC::ReturnCode_t SampleLF::onStartup(RTC::UniqueId ec_id)
00141 {
00142   return RTC::RTC_OK;
00143 }
00144 */
00145 
00146 /*
00147 RTC::ReturnCode_t SampleLF::onShutdown(RTC::UniqueId ec_id)
00148 {
00149   return RTC::RTC_OK;
00150 }
00151 */
00152 
00153 RTC::ReturnCode_t SampleLF::onActivated(RTC::UniqueId ec_id)
00154 {
00155 
00156   std::cout << "on Activated" << std::endl;
00157   check = true;
00158   file = 1;
00159 
00160   openFiles();
00161 
00162   if(m_angleIn.isNew()){
00163     m_angleIn.read();
00164   }
00165 
00166   for(int i=0; i < DOF; ++i){
00167     qold[i] = m_angle.data[i];
00168     qref_old[i] = 0.0;
00169   }
00170 
00171   return RTC::RTC_OK;
00172 }
00173 
00174 
00175 RTC::ReturnCode_t SampleLF::onDeactivated(RTC::UniqueId ec_id)
00176 {
00177   std::cout << "on Deactivated" << std::endl;
00178   closeFiles();
00179   return RTC::RTC_OK;
00180 }
00181 
00182 
00183 
00184 RTC::ReturnCode_t SampleLF::onExecute(RTC::UniqueId ec_id)
00185 {
00186   if( CONTROLLER_BRIDGE_DEBUG )
00187   {
00188     std::cout << "SampleLF::onExecute" << std::endl;
00189   }
00190 
00191   // この関数の振る舞いはController_impl::controlの派生先仮想関数に対応する //
00192   if(m_angleIn.isNew()){
00193     m_angleIn.read();
00194   }
00195 
00196   if(m_torqueInL.isNew()){
00197     m_torqueInL.read();
00198   }
00199 
00200   if(m_torqueInR.isNew()){
00201     m_torqueInR.read();
00202   }
00203 
00204   double q_ref, dq_ref;
00205   double threshold = 30.0;
00206 
00207 
00208   // *.datの読み込み //
00209   // 行頭の時間データのスキップと行の存在チェックを兼ねた処理 //
00210   // 行が存在しなければ次の行を読み込む //
00211   if(file==1)
00212   {
00213     if( !(angle1 >> dq_ref &&  vel1 >> dq_ref) )// skip time
00214     {
00215       file=2;
00216     }
00217   }
00218 
00219   if(file==2)
00220   {
00221     if( !(angle2 >> dq_ref &&  vel2 >> dq_ref) )// skip time
00222     {
00223       file=3;
00224     }
00225 
00226     if( ( fabs( m_torqueR.data[0] ) < threshold || fabs( m_torqueL.data[0] ) < threshold ) &&  check )
00227     {
00228       file=3;
00229     }
00230 
00231     check = false;
00232   }
00233 
00234   // *.dat一行の読み込み//
00235   for (int i=0; i<DOF; i++)
00236   {
00237     switch(file)
00238     {
00239       case 1:
00240         angle1 >> q_ref;
00241         vel1 >> dq_ref;
00242         break;
00243       case 2:
00244         angle2 >> q_ref;
00245         vel2 >> dq_ref;
00246         break;
00247       case 3:
00248         q_ref = qref_old[i];
00249         dq_ref=0.0;
00250         break;
00251     }//switch(file)
00252     double q = m_angle.data[i];
00253     double dq = (q - qold[i]) / TIMESTEP;
00254 
00255     m_torque.data[i] = -(q - q_ref) * Pgain[i] - (dq - dq_ref) * Dgain[i];
00256 
00257     qold[i] = q;
00258     if(file !=3)
00259       qref_old[i] = q_ref;
00260   }//for (int i=0; i<DOF; i++)
00261 
00262   m_torqueOut.write();
00263   
00264   return RTC::RTC_OK;
00265 }
00266 
00267 
00268 /*
00269   RTC::ReturnCode_t SampleLF::onAborting(RTC::UniqueId ec_id)
00270   {
00271   return RTC::RTC_OK;
00272   }
00273 */
00274 
00275 /*
00276   RTC::ReturnCode_t SampleLF::onError(RTC::UniqueId ec_id)
00277   {
00278   return RTC::RTC_OK;
00279   }
00280 */
00281 
00282 /*
00283   RTC::ReturnCode_t SampleLF::onReset(RTC::UniqueId ec_id)
00284   {
00285   return RTC::RTC_OK;
00286   }
00287 */
00288 
00289 /*
00290   RTC::ReturnCode_t SampleLF::onStateUpdate(RTC::UniqueId ec_id)
00291   {
00292   return RTC::RTC_OK;
00293   }
00294 */
00295 
00296 /*
00297   RTC::ReturnCode_t SampleLF::onRateChanged(RTC::UniqueId ec_id)
00298   {
00299   return RTC::RTC_OK;
00300   }
00301 */
00302 void SampleLF::openFiles()
00303 {
00304   angle1.open("etc/LFangle1.dat");
00305   if (!angle1.is_open()){
00306     std::cerr << "etc/LFangle1.dat not opened" << std::endl;
00307   }
00308 
00309   angle2.open("etc/LFangle2.dat");
00310   if (!angle2.is_open()){
00311     std::cerr << "etc/LFangle2.dat not opened" << std::endl;
00312   }
00313 
00314   vel1.open("etc/LFvel1.dat");
00315   if (!vel1.is_open()){
00316     std::cerr << "etc/LFvel1.dat not opened" << std::endl;
00317   }
00318 
00319   vel2.open("etc/LFvel2.dat");
00320   if (!vel2.is_open()){
00321     std::cerr << "etc/LFvel2.dat not opened" << std::endl;
00322   }
00323 }
00324 
00325 void SampleLF::closeFiles()
00326 {
00327   if( angle1.is_open() ){
00328     angle1.close();
00329     angle1.clear();
00330   }
00331   if( angle2.is_open() ){
00332     angle2.close();
00333     angle2.clear();
00334   }
00335   if( vel1.is_open() ){
00336     vel1.close();
00337     vel1.clear();
00338   }
00339   if( vel2.is_open() ){
00340     vel2.close();
00341     vel2.clear();
00342   }
00343 }
00344 
00345 
00346 
00347 extern "C"
00348 {
00349 
00350   DLL_EXPORT void SampleLFInit(RTC::Manager* manager)
00351   {
00352     coil::Properties profile(samplepd_spec);
00353     manager->registerFactory(profile,
00354                              RTC::Create<SampleLF>,
00355                              RTC::Delete<SampleLF>);
00356   }
00357 
00358 };
00359 


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