AccelerationChecker.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00010 #include <math.h>
00011 #include <iomanip>
00012 #include <stdio.h>
00013 #include "AccelerationChecker.h"
00014 
00015 // Module specification
00016 // <rtc-template block="module_spec">
00017 static const char* spec[] =
00018   {
00019     "implementation_id", "AccelerationChecker",
00020     "type_name",         "AccelerationChecker",
00021     "description",       "Joint acceleration checker",
00022     "version",           HRPSYS_PACKAGE_VERSION,
00023     "vendor",            "AIST",
00024     "category",          "example",
00025     "activity_type",     "DataFlowComponent",
00026     "max_instance",      "10",
00027     "language",          "C++",
00028     "lang_type",         "compile",
00029     // Configuration variables
00030     "conf.default.thd", "1000",
00031     "conf.default.print", "0",
00032 
00033     ""
00034   };
00035 // </rtc-template>
00036 
00037 AccelerationChecker::AccelerationChecker(RTC::Manager* manager)
00038   : RTC::DataFlowComponentBase(manager),
00039     // <rtc-template block="initializer">
00040     m_qIn("qIn", m_q),
00041     m_qOut("qOut", m_q),
00042     // </rtc-template>
00043     dummy(0)
00044 {
00045 }
00046 
00047 AccelerationChecker::~AccelerationChecker()
00048 {
00049 }
00050 
00051 
00052 
00053 RTC::ReturnCode_t AccelerationChecker::onInitialize()
00054 {
00055   //std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
00056   // <rtc-template block="bind_config">
00057   // Bind variables and configuration variable
00058   bindParameter("thd", m_thd, "1000");
00059   bindParameter("print", m_print, "0");
00060   
00061   // </rtc-template>
00062 
00063   // Registration: InPort/OutPort/Service
00064   // <rtc-template block="registration">
00065   // Set InPort buffers
00066   addInPort("qIn", m_qIn);
00067 
00068   // Set OutPort buffer
00069   addOutPort("qOut", m_qOut);
00070   
00071   // Set service provider to Ports
00072   
00073   // Set service consumers to Ports
00074   
00075   // Set CORBA Service Ports
00076   
00077   // </rtc-template>
00078 
00079   RTC::Properties& prop = getProperties();
00080   m_dt = 0;
00081   coil::stringTo(m_dt, prop["dt"].c_str());
00082   if (m_dt == 0){
00083       std::cerr << m_profile.instance_name << ": dt is not defined in the conf"
00084                 << std::endl;
00085       return RTC::RTC_ERROR;
00086   }
00087 
00088   return RTC::RTC_OK;
00089 }
00090 
00091 
00092 
00093 /*
00094 RTC::ReturnCode_t AccelerationChecker::onFinalize()
00095 {
00096   return RTC::RTC_OK;
00097 }
00098 */
00099 
00100 /*
00101 RTC::ReturnCode_t AccelerationChecker::onStartup(RTC::UniqueId ec_id)
00102 {
00103   return RTC::RTC_OK;
00104 }
00105 */
00106 
00107 /*
00108 RTC::ReturnCode_t AccelerationChecker::onShutdown(RTC::UniqueId ec_id)
00109 {
00110   return RTC::RTC_OK;
00111 }
00112 */
00113 
00114 RTC::ReturnCode_t AccelerationChecker::onActivated(RTC::UniqueId ec_id)
00115 {
00116   std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
00117   return RTC::RTC_OK;
00118 }
00119 
00120 RTC::ReturnCode_t AccelerationChecker::onDeactivated(RTC::UniqueId ec_id)
00121 {
00122   std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
00123   return RTC::RTC_OK;
00124 }
00125 
00126 RTC::ReturnCode_t AccelerationChecker::onExecute(RTC::UniqueId ec_id)
00127 {
00128   //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")" << std::endl;
00129 
00130   if (m_qIn.isNew()){
00131     m_qIn.read();
00132     if (!m_dq.data.length()){ // first time
00133         m_qOld.data.length(m_q.data.length());
00134         m_qOld = m_q;
00135         m_dqOld.data.length(m_q.data.length());
00136         m_ddqMax.data.length(m_q.data.length());
00137         for (unsigned int i=0; i<m_dqOld.data.length(); i++){
00138             m_dqOld.data[i] = 0.0;
00139             m_ddqMax.data[i] = 0.0;
00140         }
00141         m_dq.data.length(m_q.data.length());
00142     }
00143     for (unsigned int i=0; i<m_q.data.length(); i++){
00144         m_dq.data[i] = (m_q.data[i] - m_qOld.data[i])/m_dt;
00145         double ddq = (m_dq.data[i] - m_dqOld.data[i])/m_dt;
00146         if (fabs(ddq) > m_ddqMax.data[i]) m_ddqMax.data[i] = fabs(ddq);
00147         if (fabs(ddq) > m_thd){
00148             std::cout << std::fixed << std::setprecision(3) << "[" 
00149                       << (double)coil::gettimeofday() 
00150                       << "] Warning: too big joint acceleration for "
00151                       << i << "th joint(" << ddq << "[rad/m^2])" << std::endl;
00152         }
00153     }
00154     m_qOld = m_q;
00155     m_dqOld = m_dq;
00156 
00157     if (m_print){
00158         printf("jid: max acc[rad/m^2]\n");
00159         for (unsigned int i=0; i<m_ddqMax.data.length(); i++){
00160             printf("%2d: %8f\n", i, m_ddqMax.data[i]);
00161         }
00162         m_print = false;
00163     }
00164 
00165     m_qOut.write();
00166   }
00167 
00168   return RTC::RTC_OK;
00169 }
00170 
00171 /*
00172 RTC::ReturnCode_t AccelerationChecker::onAborting(RTC::UniqueId ec_id)
00173 {
00174   return RTC::RTC_OK;
00175 }
00176 */
00177 
00178 /*
00179 RTC::ReturnCode_t AccelerationChecker::onError(RTC::UniqueId ec_id)
00180 {
00181   return RTC::RTC_OK;
00182 }
00183 */
00184 
00185 /*
00186 RTC::ReturnCode_t AccelerationChecker::onReset(RTC::UniqueId ec_id)
00187 {
00188   return RTC::RTC_OK;
00189 }
00190 */
00191 
00192 /*
00193 RTC::ReturnCode_t AccelerationChecker::onStateUpdate(RTC::UniqueId ec_id)
00194 {
00195   return RTC::RTC_OK;
00196 }
00197 */
00198 
00199 /*
00200 RTC::ReturnCode_t AccelerationChecker::onRateChanged(RTC::UniqueId ec_id)
00201 {
00202   return RTC::RTC_OK;
00203 }
00204 */
00205 
00206 
00207 
00208 extern "C"
00209 {
00210 
00211   void AccelerationCheckerInit(RTC::Manager* manager)
00212   {
00213     RTC::Properties profile(spec);
00214     manager->registerFactory(profile,
00215                              RTC::Create<AccelerationChecker>,
00216                              RTC::Delete<AccelerationChecker>);
00217   }
00218 
00219 };
00220 
00221 


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