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


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Wed May 15 2019 05:02:17