00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "CaptureCamera.hpp"
00020 #include <libdc1394/dc1394_control.h>
00021 #include <ocl/ComponentLoader.hpp>
00022
00023
00024 ORO_CREATE_COMPONENT(OCL::CaptureCamera)
00025
00026 namespace OCL
00027 {
00028
00029 using namespace RTT;
00030 using namespace std;
00031
00032 CaptureCamera::CaptureCamera(string name):
00033 TaskContext(name, PreOperational),
00034 _image("RawImage"),
00035 _capture_time("CaptureTimestamp"),
00036 _newImage("updateImage",&CaptureCamera::updateImage,&CaptureCamera::updateImageFinished, this),
00037 _setProperties("setProperties",&CaptureCamera::setProperties,&CaptureCamera::setPropertiesFinished, this),
00038 _camera_index("camera_index","Camera Index",-1),
00039 _video_driver("video_driver","Video driver: autodetect, v4l or firewire.","firewire"),
00040 _capture_mode("mode","Capture Mode (firewire), check dc1394 for values",MODE_640x480_MONO),
00041 _capture_shutter("shutter","Capture Shutter in ms(firewire)",500),
00042 _capture_gain("gain","Capture Gain",200),
00043 _capture_convert("convert","Capture Convert to RGB(firewire)",0),
00044 _capture_fps("fps","Capture Framerate (firewire)",30),
00045 _capture_width("image_width","Capture frame width (V4L)",640),
00046 _capture_height("image_height","Capture frame height (V4L)",480),
00047 _show_time("show_time","True if i have to log capture times (WARNING: only for debugging)",false),
00048 _show_image("show_image","True if i have to show captured image (WARNING: only for debugging)",false),
00049 _update(false)
00050 {
00051
00052
00053
00054 this->ports()->addPort(&_image);
00055 this->ports()->addPort(&_capture_time);
00056
00057
00058
00059 this->properties()->addProperty( &_camera_index );
00060 this->properties()->addProperty( &_video_driver );
00061 this->properties()->addProperty( &_capture_mode );
00062 this->properties()->addProperty( &_capture_shutter );
00063 this->properties()->addProperty( &_capture_gain );
00064 this->properties()->addProperty( &_capture_convert );
00065 this->properties()->addProperty( &_capture_fps );
00066 this->properties()->addProperty( &_capture_width );
00067 this->properties()->addProperty( &_capture_height );
00068 this->properties()->addProperty( &_show_time );
00069 this->properties()->addProperty( &_show_image );
00070
00071
00072
00073 this->commands()->addCommand( &_newImage,"Grab new image and store in DataPort");
00074 this->commands()->addCommand( &_setProperties,"Commit new capture properties to camera");
00075
00076 }
00077
00078 CaptureCamera::~CaptureCamera()
00079 {
00080
00081 }
00082
00083
00084 bool CaptureCamera::configureHook()
00085 {
00086 Logger::In in(this->getName().data());
00087
00088
00089
00090 if ( this->marshalling()->readProperties( this->getName() + ".cpf" ) == false)
00091 return false;
00092
00093
00094
00095 if (_video_driver.value()=="firewire"){
00096 _capture = cvCaptureFromCAM(_camera_index.value()+CV_CAP_IEEE1394);
00097 }else if(_video_driver.value()=="v4l"){
00098 _capture = cvCaptureFromCAM(_camera_index.value()+CV_CAP_V4L);
00099 }else if(_video_driver.value()=="autodetect"){
00100 _capture = cvCaptureFromCAM(_camera_index.value());
00101 }else{
00102 log(Error) << "Unkown video driver! Please choose firewire, v4l or autodetect." << endlog();
00103 return false;
00104 }
00105
00106 if( !_capture ){
00107 log(Error) << "Could not initialize capturing..." << endlog();
00108 return false;
00109 }
00110
00111
00112
00113 this->setProperties();
00114
00115
00116
00117 _frame=cvQueryFrame(_capture);
00118 _image.Set(*_frame);
00119 if(_show_image.value()){
00120 cvNamedWindow(this->getName().c_str(),CV_WINDOW_AUTOSIZE);
00121 cvWaitKey(3);
00122 cvShowImage(this->getName().c_str(),_frame);
00123 cvWaitKey(3);
00124 }
00125
00126 log(Info) << "Capturing initialized..." << endlog();
00127 return true;
00128 }
00129
00130
00131
00132 bool CaptureCamera::startHook()
00133 {
00134
00135
00136 _capture_time.Set(TimeService::Instance()->ticksGet());
00137
00138 return true;
00139 }
00140
00141 void CaptureCamera::cleanupHook()
00142 {
00143 Logger::In in(this->getName().data());
00144
00145
00146 if(_show_image.value()){
00147 cvDestroyWindow(this->getName().c_str());
00148 cvWaitKey(3);
00149 }
00150
00151
00152 cvReleaseCapture( &_capture );
00153 }
00154
00155 bool CaptureCamera::updateImage()
00156 {
00157 return true;
00158 }
00159
00160 bool CaptureCamera::updateImageFinished()const
00161 {
00162 return _update;
00163 }
00164
00165 void CaptureCamera::updateHook()
00166 {
00167 Logger::In in(this->getName().data());
00168
00169 _update = false;
00170
00171
00172 if(_show_time.value()){
00173 _elapsed = TimeService::Instance()->secondsSince( _timestamp );
00174 log(Info) << "time since last capture: "<< _elapsed << endlog();
00175 _timestamp = TimeService::Instance()->ticksGet();
00176 }
00177
00178
00179 _capture_time.Set(TimeService::Instance()->ticksGet());
00180
00181 if(!cvGrabFrame(_capture)){
00182 log(Error) << "Failed to grab a new frame." << endlog();
00183 this->error();
00184 }
00185 _frame = cvRetrieveFrame(_capture);
00186
00187 _image.Set(*_frame);
00188
00189
00190 if(_show_image.value()){
00191 cvShowImage(this->getName().c_str(),_frame);
00192 cvWaitKey(3);
00193 }
00194
00195
00196 if(_show_time.value()){
00197 _elapsed = TimeService::Instance()->secondsSince( _timestamp );
00198 log(Info) << "time used for capturing: "<< _elapsed << endlog();
00199 _timestamp = TimeService::Instance()->ticksGet();
00200 }
00201
00202 _update = true;
00203 }
00204
00205
00206 bool CaptureCamera::setProperties()
00207 {
00208 Logger::In in(this->getName().data());
00209
00210 _update_properties=false;
00211
00212
00213
00214
00215
00216 if(cvSetCaptureProperty(_capture,CV_CAP_PROP_GAIN,_capture_gain.value())<1)
00217 log(Warning) << "Could not set the capturing gain property!" << endlog();
00218
00219
00220 if(_video_driver.value()=="firewire"){
00221 if(cvSetCaptureProperty(_capture,CV_CAP_PROP_MODE,_capture_mode.value())<1)
00222 log(Warning) << "Could not set the capturing mode property!" << endlog();
00223 if(cvSetCaptureProperty(_capture,FEATURE_SHUTTER,_capture_shutter.value())<1)
00224 log(Warning) << "Could not set the shutter speed property!" << endlog();
00225 if(cvSetCaptureProperty(_capture,CV_CAP_PROP_CONVERT_RGB,_capture_convert.value())<1)
00226 log(Warning) << "Could not set the RGB conversion property!" << endlog();
00227 if(cvSetCaptureProperty(_capture,CV_CAP_PROP_FPS,_capture_fps.value())<1)
00228 log(Warning) << "Could not set the fps property!" << endlog();
00229 }
00230
00231
00232 if(_video_driver.value()=="v4l"){
00233
00234 cvSetCaptureProperty(_capture,CV_CAP_PROP_FRAME_WIDTH,_capture_width.value());
00235 if(cvSetCaptureProperty(_capture,CV_CAP_PROP_FRAME_HEIGHT,_capture_height.value())<1)
00236 log(Warning) <<"Could not set the frame width and height property!" << endlog();
00237 }
00238
00239 _update_properties = true;
00240 return true;
00241 }
00242
00243 bool CaptureCamera::setPropertiesFinished()const
00244 {
00245 return _update_properties;
00246 }
00247 }
00248
00249