00001 #ifndef __PORT_HANDLER_H__ 00002 #define __PORT_HANDLER_H__ 00003 00004 #include <rtm/idl/InterfaceDataTypes.hh> 00005 #include "hrpsys/idl/HRPDataTypes.hh" 00006 #include "hrpsys/idl/pointcloud.hh" 00007 #include "BodyRTC.h" 00008 00009 namespace hrp{ 00010 class ForceSensor; 00011 class RateGyroSensor; 00012 class AccelSensor; 00013 class RangeSensor; 00014 class VisionSensor; 00015 }; 00016 00017 class InPortHandlerBase 00018 { 00019 public: 00020 virtual void update()=0; 00021 }; 00022 00023 class OutPortHandlerBase 00024 { 00025 public: 00026 virtual void update(double time)=0; 00027 }; 00028 00029 template<class T> 00030 class InPortHandler : public InPortHandlerBase 00031 { 00032 public: 00033 InPortHandler(RTC::DataFlowComponentBase *i_rtc, const char *i_portName) : 00034 m_port(i_portName, m_data){ 00035 i_rtc->addInPort(i_portName, m_port); 00036 } 00037 protected: 00038 T m_data; 00039 RTC::InPort<T> m_port; 00040 }; 00041 00042 template<class T> 00043 class OutPortHandler : public OutPortHandlerBase 00044 { 00045 public: 00046 OutPortHandler(RTC::DataFlowComponentBase *i_rtc, 00047 const char *i_portName) : 00048 m_port(i_portName, m_data){ 00049 i_rtc->addOutPort(i_portName, m_port); 00050 } 00051 void write(double time){ 00052 m_data.tm.sec = time; 00053 m_data.tm.nsec = (time - m_data.tm.sec)*1e9; 00054 m_port.write(); 00055 } 00056 protected: 00057 T m_data; 00058 RTC::OutPort<T> m_port; 00059 }; 00060 00061 class JointInPortHandler : public InPortHandler<RTC::TimedDoubleSeq> 00062 { 00063 public: 00064 JointInPortHandler(RTC::DataFlowComponentBase *i_rtc, 00065 const char *i_portName, 00066 const std::vector<hrp::Link *> &i_joints, 00067 std::vector<OpenHRP::RobotHardwareService::SwitchStatus> *i_servo); 00068 protected: 00069 std::vector<hrp::Link *> m_joints; 00070 std::vector<OpenHRP::RobotHardwareService::SwitchStatus> &m_servo; 00071 }; 00072 00073 class JointOutPortHandler : public OutPortHandler<RTC::TimedDoubleSeq> 00074 { 00075 public: 00076 JointOutPortHandler(RTC::DataFlowComponentBase *i_rtc, 00077 const char *i_portName, 00078 const std::vector<hrp::Link *> &i_joints); 00079 protected: 00080 std::vector<hrp::Link *> m_joints; 00081 }; 00082 00083 class JointValueInPortHandler : public JointInPortHandler 00084 { 00085 public: 00086 JointValueInPortHandler(RTC::DataFlowComponentBase *i_rtc, 00087 const char *i_portName, 00088 const std::vector<hrp::Link *> &i_joints, 00089 std::vector<OpenHRP::RobotHardwareService::SwitchStatus> *i_servo); 00090 void update(); 00091 }; 00092 00093 class JointValueOutPortHandler : public JointOutPortHandler 00094 { 00095 public: 00096 JointValueOutPortHandler(RTC::DataFlowComponentBase *i_rtc, 00097 const char *i_portName, 00098 const std::vector<hrp::Link *> &i_joints); 00099 void update(double time); 00100 }; 00101 00102 class JointVelocityInPortHandler : public JointInPortHandler 00103 { 00104 public: 00105 JointVelocityInPortHandler(RTC::DataFlowComponentBase *i_rtc, 00106 const char *i_portName, 00107 const std::vector<hrp::Link *> &i_joints, 00108 std::vector<OpenHRP::RobotHardwareService::SwitchStatus> *i_servo); 00109 void update(); 00110 }; 00111 00112 class JointVelocityOutPortHandler : public JointOutPortHandler 00113 { 00114 public: 00115 JointVelocityOutPortHandler(RTC::DataFlowComponentBase *i_rtc, 00116 const char *i_portName, 00117 const std::vector<hrp::Link *> &i_joints); 00118 void update(double time); 00119 }; 00120 00121 class JointAccelerationInPortHandler : public JointInPortHandler 00122 { 00123 public: 00124 JointAccelerationInPortHandler(RTC::DataFlowComponentBase *i_rtc, 00125 const char *i_portName, 00126 const std::vector<hrp::Link *> &i_joints, 00127 std::vector<OpenHRP::RobotHardwareService::SwitchStatus> *i_servo); 00128 void update(); 00129 }; 00130 00131 class JointAccelerationOutPortHandler : public JointOutPortHandler 00132 { 00133 public: 00134 JointAccelerationOutPortHandler(RTC::DataFlowComponentBase *i_rtc, 00135 const char *i_portName, 00136 const std::vector<hrp::Link *> &i_joints); 00137 void update(double time); 00138 }; 00139 00140 class JointTorqueInPortHandler : public JointInPortHandler 00141 { 00142 public: 00143 JointTorqueInPortHandler(RTC::DataFlowComponentBase *i_rtc, 00144 const char *i_portName, 00145 const std::vector<hrp::Link *> &i_joints, 00146 std::vector<OpenHRP::RobotHardwareService::SwitchStatus> *i_servo); 00147 void update(); 00148 }; 00149 00150 class JointTorqueOutPortHandler : public JointOutPortHandler 00151 { 00152 public: 00153 JointTorqueOutPortHandler(RTC::DataFlowComponentBase *i_rtc, 00154 const char *i_portName, 00155 const std::vector<hrp::Link *> &i_joints); 00156 void update(double time); 00157 private: 00158 hrp::Link *m_link; 00159 }; 00160 00161 class AbsTransformInPortHandler : public InPortHandler<RTC::TimedPose3D> 00162 { 00163 public: 00164 AbsTransformInPortHandler(RTC::DataFlowComponentBase *i_rtc, 00165 const char *i_portName, 00166 hrp::Link *i_link); 00167 void update(); 00168 private: 00169 hrp::Link *m_link; 00170 }; 00171 00172 class AbsVelocityInPortHandler : public InPortHandler<RTC::TimedDoubleSeq> 00173 { 00174 public: 00175 AbsVelocityInPortHandler(RTC::DataFlowComponentBase *i_rtc, 00176 const char *i_portName, 00177 hrp::Link *i_link); 00178 void update(); 00179 private: 00180 hrp::Link *m_link; 00181 }; 00182 00183 class AbsAccelerationInPortHandler : public InPortHandler<RTC::TimedDoubleSeq> 00184 { 00185 public: 00186 AbsAccelerationInPortHandler(RTC::DataFlowComponentBase *i_rtc, 00187 const char *i_portName, 00188 hrp::Link *i_link); 00189 void update(); 00190 private: 00191 hrp::Link *m_link; 00192 }; 00193 00194 class FrameRateInPortHandler : public InPortHandler<RTC::TimedDouble> 00195 { 00196 public: 00197 FrameRateInPortHandler(RTC::DataFlowComponentBase *i_rtc, 00198 const char *i_portName, 00199 hrp::VisionSensor *i_sensor); 00200 void update(); 00201 private: 00202 hrp::VisionSensor *m_sensor; 00203 }; 00204 00205 class LightSwitchInPortHandler : public InPortHandler<RTC::TimedBoolean> 00206 { 00207 public: 00208 LightSwitchInPortHandler(RTC::DataFlowComponentBase *i_rtc, 00209 const char *i_portName, 00210 hrp::Light *i_light); 00211 void update(); 00212 private: 00213 hrp::Light *m_light; 00214 }; 00215 00216 class AbsTransformOutPortHandler : public OutPortHandler<RTC::TimedPose3D> 00217 { 00218 public: 00219 AbsTransformOutPortHandler(RTC::DataFlowComponentBase *i_rtc, 00220 const char *i_portName, 00221 hrp::Link *i_link); 00222 AbsTransformOutPortHandler(RTC::DataFlowComponentBase *i_rtc, 00223 const char *i_portName, 00224 hrp::Sensor *i_sensor); 00225 void update(double time); 00226 private: 00227 hrp::Link *m_link; 00228 hrp::Sensor *m_sensor; 00229 }; 00230 00231 class AbsVelocityOutPortHandler : public OutPortHandler<RTC::TimedDoubleSeq> 00232 { 00233 public: 00234 AbsVelocityOutPortHandler(RTC::DataFlowComponentBase *i_rtc, 00235 const char *i_portName, 00236 hrp::Link *i_link); 00237 void update(double time); 00238 private: 00239 hrp::Link *m_link; 00240 }; 00241 00242 class AbsAccelerationOutPortHandler : 00243 public OutPortHandler<RTC::TimedDoubleSeq> 00244 { 00245 public: 00246 AbsAccelerationOutPortHandler(RTC::DataFlowComponentBase *i_rtc, 00247 const char *i_portName, 00248 hrp::Link *i_link); 00249 void update(double time); 00250 private: 00251 hrp::Link *m_link; 00252 }; 00253 00254 template<class T, class S> 00255 class SensorPortHandler : public OutPortHandler<S> 00256 { 00257 public: 00258 SensorPortHandler(RTC::DataFlowComponentBase *i_rtc, 00259 const char *i_portName, T *i_sensor) : 00260 OutPortHandler<S>(i_rtc, i_portName), 00261 m_sensor(i_sensor){ 00262 } 00263 protected: 00264 T *m_sensor; 00265 }; 00266 00267 class ForceSensorPortHandler : 00268 public SensorPortHandler<hrp::ForceSensor, RTC::TimedDoubleSeq> 00269 { 00270 public: 00271 ForceSensorPortHandler(RTC::DataFlowComponentBase *i_rtc, 00272 const char *i_portName, 00273 hrp::ForceSensor *i_sensor); 00274 void update(double time); 00275 }; 00276 00277 class RateGyroSensorPortHandler : 00278 public SensorPortHandler<hrp::RateGyroSensor, RTC::TimedAngularVelocity3D> 00279 { 00280 public: 00281 RateGyroSensorPortHandler(RTC::DataFlowComponentBase *i_rtc, 00282 const char *i_portName, 00283 hrp::RateGyroSensor *i_sensor); 00284 void update(double time); 00285 private: 00286 }; 00287 00288 class AccelSensorPortHandler : 00289 public SensorPortHandler<hrp::AccelSensor, RTC::TimedAcceleration3D> 00290 { 00291 public: 00292 AccelSensorPortHandler(RTC::DataFlowComponentBase *i_rtc, 00293 const char *i_portName, 00294 hrp::AccelSensor *i_sensor); 00295 void update(double time); 00296 private: 00297 }; 00298 00299 class RangeSensorPortHandler : 00300 public SensorPortHandler<hrp::RangeSensor, RTC::RangeData> 00301 { 00302 public: 00303 RangeSensorPortHandler(RTC::DataFlowComponentBase *i_rtc, 00304 const char *i_portName, 00305 hrp::RangeSensor *i_sensor); 00306 void update(double time); 00307 private: 00308 }; 00309 00310 class VisionSensorPortHandler : 00311 public SensorPortHandler<hrp::VisionSensor, Img::TimedCameraImage> 00312 { 00313 public: 00314 VisionSensorPortHandler(RTC::DataFlowComponentBase *i_rtc, 00315 const char *i_portName, 00316 hrp::VisionSensor *i_sensor); 00317 void update(double time); 00318 private: 00319 }; 00320 00321 class PointCloudPortHandler : 00322 public SensorPortHandler<hrp::VisionSensor, PointCloudTypes::PointCloud> 00323 { 00324 public: 00325 PointCloudPortHandler(RTC::DataFlowComponentBase *i_rtc, 00326 const char *i_portName, 00327 hrp::VisionSensor *i_sensor); 00328 void update(double time); 00329 private: 00330 std::string m_pcFormat; 00331 }; 00332 00333 class EmergencySignalPortHandler : public OutPortHandler<RTC::TimedLong> 00334 { 00335 public: 00336 EmergencySignalPortHandler(RTC::DataFlowComponentBase *i_rtc, 00337 const char *i_portName, 00338 BodyRTC *i_body); 00339 void update(double time); 00340 protected: 00341 BodyRTC *m_body; 00342 }; 00343 00344 class ServoStatePortHandler : public OutPortHandler<OpenHRP::TimedLongSeqSeq> 00345 { 00346 public: 00347 ServoStatePortHandler(RTC::DataFlowComponentBase *i_rtc, 00348 const char *i_portName, 00349 BodyRTC *i_body); 00350 void update(double time); 00351 protected: 00352 BodyRTC *m_body; 00353 OpenHRP::RobotHardwareService::RobotState* rs; 00354 }; 00355 00356 #endif