00001 // -*- C++ -*- 00017 #ifndef MOTORCONTROL_H 00018 #define MOTORCONTROL_H 00019 00020 #include <rtm/Manager.h> 00021 #include <rtm/DataFlowComponentBase.h> 00022 #include <rtm/CorbaPort.h> 00023 #include <rtm/DataInPort.h> 00024 #include <rtm/DataOutPort.h> 00025 #include <rtm/idl/BasicDataTypeSkel.h> 00026 //#include <rtm/idl/IIS.h> 00027 00028 //--- [add] --------------- 00029 #include "IISStub.h" 00030 #include "IISSkel.h" 00031 #include "IIS.hh" 00032 00033 #define LOG 0 00034 #define DEBUG 0 00035 #define CALLBACK 0 00036 #define CALLBACK_DEBUG 0 00037 #include <iostream> 00038 #include <fstream> 00039 #include <math.h> 00040 00041 #if CALLBACK 00042 #include <rtm/RingBuffer.h> 00043 //-CallBackObject-- 00044 class Debug 00045 : public RTC::OnWriteConvert<RTC::TimedDoubleSeq> 00046 { 00047 //ofstream ofsCallBack; 00048 public: 00049 Debug(void) { 00050 //ofsCallBack.open("data/callback.dat"); 00051 }; 00052 RTC::TimedDoubleSeq operator()(const RTC::TimedDoubleSeq& value) 00053 { 00054 RTC::TimedDoubleSeq ret(value); 00055 #if CALLBACK_DEBUG 00056 double dummy_sec = value.tm.sec + 1.0e-9*value.tm.nsec; 00057 printf("--[CallBack_TIME(CURRENT)]: %lf\t[L]=%lf\t[R]=%lf\n",dummy_sec, value.data[0], value.data[1]); 00058 #endif 00059 return ret; 00060 }; 00061 00062 }; 00063 //-- 00064 #endif 00065 //--- [add] --------------- 00066 00067 // Service implementation headers 00068 // <rtc-template block="service_impl_h"> 00069 #include "MotorControlProSVC_impl.h" 00070 00071 // </rtc-template> 00072 00073 // Service Consumer stub headers 00074 // <rtc-template block="consumer_stub_h"> 00075 00076 // </rtc-template> 00077 00078 using namespace RTC; 00079 using namespace IIS; 00080 00104 class MotorControl 00105 : public RTC::DataFlowComponentBase 00106 { 00107 public: 00112 MotorControl(RTC::Manager* manager); 00113 00117 ~MotorControl(); 00118 00119 // <rtc-template block="public_attribute"> 00120 00121 // </rtc-template> 00122 00123 // <rtc-template block="public_operation"> 00124 00125 // </rtc-template> 00126 00127 /*** 00128 * set Configuration parameters 00129 * 00130 * The initialize action (on CREATED->ALIVE transition) 00131 * formaer rtc_init_entry() 00132 * 00133 * @return RTC::ReturnCode_t 00134 * 00135 * @pre none 00136 * @post none 00137 * 00138 */ 00139 virtual RTC::ReturnCode_t onInitialize(); 00140 00141 /*** 00142 * 00143 * 00144 * The finalize action (on ALIVE->END transition) 00145 * formaer rtc_exiting_entry() 00146 * 00147 * @return RTC::ReturnCode_t 00148 * 00149 * 00150 */ 00151 // virtual RTC::ReturnCode_t onFinalize(); 00152 00153 /*** 00154 * 00155 * The startup action when ExecutionContext startup 00156 * former rtc_starting_entry() 00157 * 00158 * @param ec_id target ExecutionContext Id 00159 * 00160 * @return RTC::ReturnCode_t 00161 * 00162 * 00163 */ 00164 // virtual RTC::ReturnCode_t onStartup(RTC::UniqueId ec_id); 00165 00166 /*** 00167 * 00168 * The shutdown action when ExecutionContext stop 00169 * former rtc_stopping_entry() 00170 * 00171 * @param ec_id target ExecutionContext Id 00172 * 00173 * @return RTC::ReturnCode_t 00174 * 00175 * 00176 */ 00177 // virtual RTC::ReturnCode_t onShutdown(RTC::UniqueId ec_id); 00178 00179 /*** 00180 * (1) update INPORT's data 00181 * (2) open files for debugging 00182 * (3) initialize 00183 * (4) get robot's parameter 00184 * 00185 * The activated action (Active state entry action) 00186 * former rtc_active_entry() 00187 * 00188 * @param ec_id target ExecutionContext Id 00189 * 00190 * @return RTC::ReturnCode_t 00191 * 00192 * @pre none 00193 * @post none 00194 * 00195 */ 00196 virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id); 00197 00198 /*** 00199 * (1)close files for debugging 00200 * 00201 * The deactivated action (Active state exit action) 00202 * former rtc_active_exit() 00203 * 00204 * @param ec_id target ExecutionContext Id 00205 * 00206 * @return RTC::ReturnCode_t 00207 * 00208 * @pre none 00209 * @post none 00210 * 00211 */ 00212 virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id); 00213 00214 /*** 00215 * (1)getting target trans/rot Velocity of robot's center data from DriveContr 00216 * olRTC 00217 * (2)calculating Torque values 00218 * (3)sending Torque data to Simulator or real Motor 00219 * (4)getting current Angle data (encoder dadta) from Simulator or Real Robot 00220 * (5)sending current Angle data to DriveControlRTC or OdometryRTC 00221 * 00222 * The execution action that is invoked periodically 00223 * former rtc_active_do() 00224 * 00225 * @param ec_id target ExecutionContext Id 00226 * 00227 * @return RTC::ReturnCode_t 00228 * 00229 * @pre none 00230 * @post none 00231 * 00232 */ 00233 virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id); 00234 00235 /*** 00236 * 00237 * The aborting action when main logic error occurred. 00238 * former rtc_aborting_entry() 00239 * 00240 * @param ec_id target ExecutionContext Id 00241 * 00242 * @return RTC::ReturnCode_t 00243 * 00244 * 00245 */ 00246 // virtual RTC::ReturnCode_t onAborting(RTC::UniqueId ec_id); 00247 00248 /*** 00249 * 00250 * The error action in ERROR state 00251 * former rtc_error_do() 00252 * 00253 * @param ec_id target ExecutionContext Id 00254 * 00255 * @return RTC::ReturnCode_t 00256 * 00257 * 00258 */ 00259 // virtual RTC::ReturnCode_t onError(RTC::UniqueId ec_id); 00260 00261 /*** 00262 * 00263 * The reset action that is invoked resetting 00264 * This is same but different the former rtc_init_entry() 00265 * 00266 * @param ec_id target ExecutionContext Id 00267 * 00268 * @return RTC::ReturnCode_t 00269 * 00270 * 00271 */ 00272 // virtual RTC::ReturnCode_t onReset(RTC::UniqueId ec_id); 00273 00274 /*** 00275 * 00276 * The state update action that is invoked after onExecute() action 00277 * no corresponding operation exists in OpenRTm-aist-0.2.0 00278 * 00279 * @param ec_id target ExecutionContext Id 00280 * 00281 * @return RTC::ReturnCode_t 00282 * 00283 * 00284 */ 00285 // virtual RTC::ReturnCode_t onStateUpdate(RTC::UniqueId ec_id); 00286 00287 /*** 00288 * 00289 * The action that is invoked when execution context's rate is changed 00290 * no corresponding operation exists in OpenRTm-aist-0.2.0 00291 * 00292 * @param ec_id target ExecutionContext Id 00293 * 00294 * @return RTC::ReturnCode_t 00295 * 00296 * 00297 */ 00298 // virtual RTC::ReturnCode_t onRateChanged(RTC::UniqueId ec_id); 00299 00300 00301 protected: 00302 // <rtc-template block="protected_attribute"> 00303 00304 // </rtc-template> 00305 00306 // <rtc-template block="protected_operation"> 00307 00308 // </rtc-template> 00309 00310 // Configuration variable declaration 00311 // <rtc-template block="config_declare"> 00317 double m_PGainL; 00323 double m_DGainL; 00329 double m_PGainR; 00335 double m_DGainR; 00341 double m_SimulatedOffsetX; 00347 double m_SimulatedOffsetY; 00353 double m_SimulatedOffsetAngle; 00361 int m_leftWheelID; 00369 int m_rightWheelID; 00377 double m_radiusOfLeftWheel; 00385 double m_radiusOfRightWheel; 00393 double m_lengthOfAxle; 00400 double m_radiusOfBodyArea; 00401 // </rtc-template> 00402 00403 // DataInPort declaration 00404 // <rtc-template block="inport_declare"> 00405 TimedVelocity m_TargetVelocity; 00417 InPort<TimedVelocity> m_TargetVelocityIn; 00418 TimedDoubleSeq m_InCurrentWheelAngle; 00428 InPort<TimedDoubleSeq> m_InCurrentWheelAngleIn; 00429 TimedDoubleSeq m_InSimulatedPosition; 00442 InPort<TimedDoubleSeq> m_InSimulatedPositionIn; 00443 00444 // </rtc-template> 00445 00446 00447 // DataOutPort declaration 00448 // <rtc-template block="outport_declare"> 00449 TimedDoubleSeq m_OutSimulatedPositionToInventGUI; 00462 OutPort<TimedDoubleSeq> m_OutSimulatedPositionToInventGUIOut; 00463 TimedDoubleSeq m_Torque; 00473 OutPort<TimedDoubleSeq> m_TorqueOut; 00474 TimedDoubleSeq m_OutCurrentWheelAngle; 00484 OutPort<TimedDoubleSeq> m_OutCurrentWheelAngleOut; 00485 TimedDoubleSeq m_OutSimulatedPositionToLocalization; 00498 OutPort<TimedDoubleSeq> m_OutSimulatedPositionToLocalizationOut; 00499 00500 // </rtc-template> 00501 00502 // CORBA Port declaration 00503 // <rtc-template block="corbaport_declare"> 00510 RTC::CorbaPort m_InventGUIProvPort; 00515 RTC::CorbaPort m_BumpProvPort; 00516 00517 // </rtc-template> 00518 00519 // Service declaration 00520 // <rtc-template block="service_declare"> 00532 MotorSVC_impl m_InventGUIMotor; 00540 MotorSVC_impl m_BumpMotor; 00541 00542 // </rtc-template> 00543 00544 // Consumer declaration 00545 // <rtc-template block="consumer_declare"> 00546 00547 // </rtc-template> 00548 00549 private: 00550 // <rtc-template block="private_attribute"> 00551 00552 // </rtc-template> 00553 00554 // <rtc-template block="private_operation"> 00555 00556 // </rtc-template> 00557 00558 //---[add]------------------------ 00559 const static int Dof = 2; 00560 //const static int WheelLeftId = 0; //!< jointID of left wheel 00561 //const static int WheelRightId = 1; //!< jointID of right wheel 00562 00563 double timeStep; 00564 double TimeStamp; 00565 double TimeStampOld; 00566 unsigned long sec; 00567 unsigned long nsec; 00568 int compSwitch; 00569 double qOld[Dof]; 00570 bool InitializeFlag; 00571 00573 struct BODY { 00574 int leftID; 00575 int rightID; 00576 double length; 00577 double wheelRadiusLeft; 00578 double wheelRadiusRight; 00579 double bodyRadius; 00580 }Body; 00581 00582 #if DEBUG 00583 std::ofstream ofs; // TODO:someday REMOVE ( for output the SimulatedPosition&Angle data to DEBUG file) 00584 #endif 00585 00586 #if CALLBACK 00587 //-- callback object 00588 Debug m_debug; 00589 #endif 00590 //---[add]------------------------ 00591 00592 }; 00593 00594 00595 extern "C" 00596 { 00597 DLL_EXPORT void MotorControlInit(RTC::Manager* manager); 00598 }; 00599 00600 #endif // MOTORCONTROL_H