VideoCapture.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00010 #include "hrpsys/util/VectorConvert.h"
00011 #include "VideoCapture.h"
00012 
00013 // Module specification
00014 // <rtc-template block="module_spec">
00015 static const char* videocapture_spec[] =
00016   {
00017     "implementation_id", "VideoCapture",
00018     "type_name",         "VideoCapture",
00019     "description",       "video capture component",
00020     "version",           HRPSYS_PACKAGE_VERSION,
00021     "vendor",            "JSK",
00022     "category",          "example",
00023     "activity_type",     "DataFlowComponent",
00024     "max_instance",      "10",
00025     "language",          "C++",
00026     "lang_type",         "compile",
00027     // Configuration variables
00028     "conf.default.initialMode", "continuous",
00029     "conf.default.devIds", "0",
00030     "conf.default.width", "640",
00031     "conf.default.height", "480",
00032     "conf.default.frameRate", "1",
00033 
00034     ""
00035   };
00036 // </rtc-template>
00037 
00038 VideoCapture::VideoCapture(RTC::Manager* manager)
00039   : RTC::DataFlowComponentBase(manager),
00040     // <rtc-template block="initializer">
00041     m_MultiCameraImagesOut ("MultiCameraImages", m_MultiCameraImages),
00042     m_CameraImageOut ("CameraImage", m_CameraImage),
00043     m_CameraCaptureServicePort("CameraCaptureService"),
00044     m_CameraCaptureService(this),
00045     // </rtc-template>
00046     m_mode(CONTINUOUS),
00047     m_needToReactivate(false)
00048 {
00049 }
00050 
00051 VideoCapture::~VideoCapture()
00052 {
00053 }
00054 
00055 
00056 
00057 RTC::ReturnCode_t VideoCapture::onInitialize()
00058 {
00059   std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
00060 
00061   // <rtc-template block="bind_config">
00062   // Bind variables and configuration variable
00063   bindParameter("initialMode", m_initialMode, "continuous");
00064   bindParameter("devIds", m_devIds, "0");
00065   bindParameter("width", m_width, "640");
00066   bindParameter("height", m_height, "480");
00067   bindParameter("frameRate", m_frameRate, "1");
00068   
00069   // </rtc-template>
00070 
00071   // Registration: InPort/OutPort/Service
00072   // <rtc-template block="registration">
00073   // Set InPort buffers
00074 
00075   // Set OutPort buffer
00076   if (m_devIds.size() == 1){
00077     addOutPort ("CameraImage", m_CameraImageOut);
00078   }else{
00079     addOutPort ("MultiCameraImages", m_MultiCameraImagesOut);
00080   }
00081   
00082   // Set service provider to Ports
00083   m_CameraCaptureServicePort.registerProvider("service0", "CameraCaptureService", m_CameraCaptureService);
00084   
00085   // Set service consumers to Ports
00086   
00087   // Set CORBA Service Ports
00088   addPort(m_CameraCaptureServicePort);
00089   
00090   // </rtc-template>
00091 
00092   return RTC::RTC_OK;
00093 }
00094 
00095 
00096 
00097 /*
00098 RTC::ReturnCode_t VideoCapture::onFinalize()
00099 {
00100   return RTC::RTC_OK;
00101 }
00102 */
00103 
00104 RTC::ReturnCode_t VideoCapture::onStartup(RTC::UniqueId ec_id)
00105 {
00106   return RTC::RTC_OK;
00107 }
00108 
00109 /*
00110 RTC::ReturnCode_t VideoCapture::onShutdown(RTC::UniqueId ec_id)
00111 {
00112   return RTC::RTC_OK;
00113 }
00114 */
00115 
00116 RTC::ReturnCode_t VideoCapture::onActivated(RTC::UniqueId ec_id)
00117 {
00118   std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
00119   m_tOld = (double)(coil::gettimeofday());
00120   if (m_initialMode == "continuous"){
00121     m_mode = CONTINUOUS;
00122   }else{
00123     m_mode = SLEEP;
00124   }
00125 
00126   if (m_devIds.size() == 1){
00127     std::cout << "** devId:" << m_devIds[0] << std::endl;
00128     v4l_capture *cam = new v4l_capture ();
00129     if (cam->init(m_width, m_height, m_devIds[0]) != 0) return RTC::RTC_ERROR;
00130     m_cameras.push_back (cam);
00131     m_CameraImage.data.image.format = Img::CF_RGB;
00132     m_CameraImage.data.image.width = cam->getWidth ();
00133     m_CameraImage.data.image.height = cam->getHeight ();
00134     m_CameraImage.data.image.raw_data.length (cam->getWidth () * cam->getHeight () * 3);
00135   }else{
00136     m_MultiCameraImages.data.image_seq.length (m_devIds.size ());
00137     m_MultiCameraImages.data.camera_set_id = 0;
00138     for (unsigned int i = 0; i < m_devIds.size (); i++)
00139       {
00140         std::cout << "** devId:" << m_devIds[i] << std::endl;
00141         v4l_capture *cam = new v4l_capture ();
00142         cam->init(m_width, m_height, m_devIds[i]);
00143         m_cameras.push_back (cam);
00144         m_MultiCameraImages.data.image_seq[i].image.format = Img::CF_RGB;
00145         m_MultiCameraImages.data.image_seq[i].image.width = cam->getWidth ();
00146         m_MultiCameraImages.data.image_seq[i].image.height = cam->getHeight ();
00147         m_MultiCameraImages.data.image_seq[i].image.raw_data.length (cam->getWidth () * cam->getHeight () * 3);
00148       }
00149   }
00150 
00151   return RTC::RTC_OK;
00152 }
00153 
00154 RTC::ReturnCode_t VideoCapture::onDeactivated(RTC::UniqueId ec_id)
00155 {
00156   std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
00157   for (unsigned int i=0; i< m_cameras.size(); i++){
00158       delete m_cameras[i];
00159   } 
00160   m_cameras.clear();
00161   return RTC::RTC_OK;
00162 }
00163 
00164 RTC::ReturnCode_t VideoCapture::onExecute(RTC::UniqueId ec_id)
00165 {
00166   //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")" << std::endl;
00167   if (m_needToReactivate){
00168     if (onActivated(ec_id) == RTC::RTC_OK){
00169       m_needToReactivate = false;
00170     }
00171   }
00172   if (!capture()){
00173     std::cerr << m_profile.instance_name << ": failed to capture." << std::endl;
00174     onDeactivated(ec_id);
00175     m_needToReactivate = true;
00176     return RTC::RTC_OK;
00177   }
00178 
00179   double tNew = (double)(coil::gettimeofday());
00180   double dt = (double)(tNew - m_tOld);
00181   if (dt > 1.0/m_frameRate){
00182     m_tOld = tNew;
00183   }else{
00184     return RTC::RTC_OK;
00185   }
00186 
00187   if (m_mode == SLEEP) return RTC::RTC_OK;
00188 
00189   if (m_cameras.size() == 1){
00190     m_CameraImageOut.write();
00191   }else{
00192     m_MultiCameraImagesOut.write();
00193   }
00194 
00195   if (m_mode == ONESHOT) m_mode = SLEEP;
00196 
00197   return RTC::RTC_OK;
00198 }
00199 
00200 bool VideoCapture::capture()
00201 {
00202   if (m_cameras.size() == 1){
00203     m_CameraImage.error_code = 0;
00204     uchar *imgFrom = m_cameras[0]->capture();
00205     if (!imgFrom) return false;
00206     memcpy(m_CameraImage.data.image.raw_data.get_buffer(), imgFrom,
00207            m_CameraImage.data.image.raw_data.length() * sizeof(uchar));
00208   }else{
00209     m_MultiCameraImages.error_code = 0;
00210     for (unsigned int i = 0; i < m_cameras.size (); i++)
00211       {
00212         uchar *imgFrom = m_cameras[i]->capture();
00213         if (!imgFrom) return false;
00214         memcpy (m_MultiCameraImages.data.image_seq[i].image.raw_data.get_buffer(), imgFrom,
00215                 m_MultiCameraImages.data.image_seq[i].image.raw_data.length() * sizeof (uchar));
00216       }
00217   }
00218   return true;
00219 }
00220 
00221 /*
00222 RTC::ReturnCode_t VideoCapture::onAborting(RTC::UniqueId ec_id)
00223 {
00224   return RTC::RTC_OK;
00225 }
00226 */
00227 
00228 /*
00229 RTC::ReturnCode_t VideoCapture::onError(RTC::UniqueId ec_id)
00230 {
00231   return RTC::RTC_OK;
00232 }
00233 */
00234 
00235 /*
00236 RTC::ReturnCode_t VideoCapture::onReset(RTC::UniqueId ec_id)
00237 {
00238   return RTC::RTC_OK;
00239 }
00240 */
00241 
00242 /*
00243 RTC::ReturnCode_t VideoCapture::onStateUpdate(RTC::UniqueId ec_id)
00244 {
00245   return RTC::RTC_OK;
00246 }
00247 */
00248 
00249 /*
00250 RTC::ReturnCode_t VideoCapture::onRateChanged(RTC::UniqueId ec_id)
00251 {
00252   return RTC::RTC_OK;
00253 }
00254 */
00255 
00256 void VideoCapture::take_one_frame()
00257 {
00258   m_mode = ONESHOT;
00259 }
00260 
00261 void VideoCapture::start_continuous()
00262 {
00263   m_mode = CONTINUOUS;
00264 }
00265 
00266 void VideoCapture::stop_continuous()
00267 {
00268   m_mode = SLEEP;
00269 }
00270 
00271 
00272 extern "C"
00273 {
00274 
00275   void VideoCaptureInit(RTC::Manager* manager)
00276   {
00277     RTC::Properties profile(videocapture_spec);
00278     manager->registerFactory(profile,
00279                              RTC::Create<VideoCapture>,
00280                              RTC::Delete<VideoCapture>);
00281   }
00282 
00283 };
00284 
00285 


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