firewire_camera_driver.cpp
Go to the documentation of this file.
00001 #include "firewire_camera_driver.h"
00002 
00003 FirewireCameraDriver::FirewireCameraDriver()
00004 {
00005   //setDriverId(driver string id);
00006   this->server=CFirewireServer::instance();
00007   this->server->init();
00008   this->camera=NULL;
00009   this->camera_id=-1;
00010   this->ISO_speed=400;
00011   // desired configuration
00012   this->desired_conf.width=-1;
00013   this->desired_conf.height=-1;
00014   this->desired_conf.left_offset=-1;
00015   this->desired_conf.top_offset=-1;
00016   this->desired_conf.depth=(depths_t)-1;
00017   this->desired_conf.coding=(codings_t)-1;
00018   this->desired_conf.framerate=0.0;
00019   // current configuration
00020   this->current_conf.width=-1;
00021   this->current_conf.height=-1;
00022   this->current_conf.left_offset=-1;
00023   this->current_conf.top_offset=-1;
00024   this->current_conf.depth=(depths_t)-1;
00025   this->current_conf.coding=(codings_t)-1;
00026   this->current_conf.framerate=0.0;
00027   // default configuration
00028   this->default_conf.width=-1;
00029   this->default_conf.height=-1;
00030   this->default_conf.left_offset=-1;
00031   this->default_conf.top_offset=-1;
00032   this->default_conf.depth=(depths_t)-1;
00033   this->default_conf.coding=(codings_t)-1;
00034   this->default_conf.framerate=0.0;
00035   // default calibration file
00036   this->calibration_file="";
00037   // default frame id
00038   this->frame_id="";
00039 }
00040 
00041 bool FirewireCameraDriver::openDriver(void)
00042 {
00043   //setDriverId(driver string id);
00044   if(this->camera_id!=-1)
00045   {
00046     this->server->init();
00047     if(this->server->get_num_cameras()>0)
00048     {
00049       this->server->get_camera(this->camera_id,&this->camera);
00050       this->camera->set_ISO_speed(this->ISO_speed);
00051       this->camera->get_config(&this->default_conf.left_offset,&this->default_conf.top_offset,&this->default_conf.width,
00052                                &this->default_conf.height,&this->default_conf.framerate,&this->default_conf.depth,
00053                                &this->default_conf.coding);
00054       this->change_config(&this->desired_conf);
00055       ROS_INFO("Driver opened");
00056       return true;
00057     }
00058     else
00059     {   
00060       ROS_INFO("No cameras available");
00061       return false;
00062     }
00063   }
00064   else
00065   {
00066     ROS_INFO("Waiting for a valid id ...");
00067     return false;
00068   
00069   }
00070 }
00071 
00072 bool FirewireCameraDriver::closeDriver(void)
00073 { 
00074   if(this->camera!=NULL)
00075   {  
00076     delete this->camera;
00077     this->camera=NULL;
00078   }
00079 
00080   return true;
00081 }
00082 
00083 bool FirewireCameraDriver::startDriver(void)
00084 {
00085   if(this->camera!=NULL)
00086   {
00087     try{
00088       this->lock();
00089       this->camera->start();
00090       this->unlock();
00091       ROS_INFO("Driver started");
00092       return true;
00093     }catch(CException &e){
00094       this->unlock();
00095       //ROS_INFO(e.what().data());
00096       return false;
00097     }
00098   }
00099   else
00100     return false;
00101 }
00102 
00103 bool FirewireCameraDriver::stopDriver(void)
00104 {
00105   if(this->camera!=NULL)
00106   {
00107     try{
00108       this->lock();
00109       this->camera->stop();
00110       this->unlock();
00111       ROS_INFO("Driver stopped");
00112       return true;
00113     }catch(CException &e){
00114       this->unlock();
00115       //ROS_INFO(e.what().data());
00116       return false;
00117     }
00118   }
00119   else
00120     return false;
00121 }
00122 
00123 void FirewireCameraDriver::config_update(Config& new_cfg, uint32_t level)
00124 {
00125   TCameraConfig conf;
00126 
00127   // depending on current state
00128   // update driver with new_cfg data
00129   switch(this->getState())
00130   {
00131     case FirewireCameraDriver::CLOSED:
00132       this->lock();
00133       this->camera_id=new_cfg.Camera_node;
00134       this->ISO_speed=new_cfg.ISO_speed;
00135       this->dyn_rec_to_camera(new_cfg,this->desired_conf);
00136       // change the calibration file
00137       this->calibration_file=new_cfg.cal_file;
00138       // update the frame identifier
00139       this->frame_id=new_cfg.frame_id;
00140       this->unlock();
00141       break;
00142 
00143     case FirewireCameraDriver::OPENED:
00144       if(new_cfg.Camera_node!=-1)
00145       {
00146         try{
00147           this->lock();
00148           // update the desired ISO speed
00149           this->ISO_speed=new_cfg.ISO_speed;
00150           if(this->camera_id!=new_cfg.Camera_node)
00151           {
00152             this->close();
00153             this->camera_id=new_cfg.Camera_node;
00154             this->open();
00155           }
00156           this->dyn_rec_to_camera(new_cfg,conf);
00157           this->change_config(&conf);
00158           // write the configuration back to the dynamic reconfigure window
00159           this->camera_to_dyn_rec(conf,new_cfg);
00160           // white balance feature
00161           this->set_white_balance(&new_cfg.White_balance_enabled,&new_cfg.White_balance_mode,&new_cfg.White_balance_u_b_value,
00162                                   &new_cfg.White_balance_v_r_value);
00163           // shutter feature
00164           this->set_shutter(&new_cfg.Shutter_enabled,&new_cfg.Shutter_mode,&new_cfg.Shutter_value);
00165           // gain feature
00166           this->set_gain(&new_cfg.Gain_enabled,&new_cfg.Gain_mode,&new_cfg.Gain_value);
00167           // change the calibration file
00168           this->calibration_file=new_cfg.cal_file;
00169           // update the frame identifier
00170           this->frame_id=new_cfg.frame_id;
00171           this->unlock();
00172         }catch(CFirewireCameraException &e){
00173           ROS_INFO("Invalid configuration. Setting back the default configuration ...");
00174           this->change_config(&this->default_conf);
00175           this->camera_to_dyn_rec(this->default_conf,new_cfg);
00176           this->unlock();
00177         }
00178       }
00179       break;
00180 
00181     case FirewireCameraDriver::RUNNING:
00182       break;
00183   }
00184 
00185   // save the current configuration
00186   this->config_=new_cfg;
00187 }
00188 
00189 void FirewireCameraDriver::dyn_rec_to_camera(iri_firewire_camera::FirewireCameraConfig &dyn_rec_config,TCameraConfig &camera_config)
00190 {
00191   camera_config.left_offset=dyn_rec_config.Left_offset;
00192   camera_config.top_offset=dyn_rec_config.Top_offset;
00193   camera_config.width=dyn_rec_config.Image_width;
00194   camera_config.height=dyn_rec_config.Image_height;
00195   camera_config.framerate=dyn_rec_config.Framerate;
00196   switch(dyn_rec_config.Color_coding)
00197   {
00198     case 0: camera_config.depth=DEPTH8;
00199             camera_config.coding=MONO;
00200             break;
00201     case 1: camera_config.depth=DEPTH16;
00202             camera_config.coding=YUV;
00203             break;
00204     case 2: camera_config.depth=DEPTH24;
00205             camera_config.coding=YUV;
00206             break;
00207     case 3: camera_config.depth=DEPTH24;
00208             camera_config.coding=RGB;
00209             break;
00210     case 4: camera_config.depth=DEPTH16;
00211             camera_config.coding=MONO;
00212             break;
00213     case 5: camera_config.depth=DEPTH48;
00214             camera_config.coding=RGB;
00215             break;
00216     case 6: camera_config.depth=DEPTH8;
00217             camera_config.coding=RAW;
00218             break;
00219     case 7: camera_config.depth=DEPTH16;
00220             camera_config.coding=RAW;
00221             break;
00222     default: camera_config.depth=DEPTH24;
00223              camera_config.coding=RGB;
00224              break;
00225   }
00226 }
00227 
00228 void FirewireCameraDriver::camera_to_dyn_rec(TCameraConfig &camera_config,iri_firewire_camera::FirewireCameraConfig &dyn_rec_config)
00229 {
00230   dyn_rec_config.Left_offset=camera_config.left_offset;
00231   dyn_rec_config.Top_offset=camera_config.top_offset;
00232   dyn_rec_config.Image_width=camera_config.width;
00233   dyn_rec_config.Image_height=camera_config.height;
00234   dyn_rec_config.Framerate=camera_config.framerate;
00235   if(camera_config.depth==DEPTH8 && camera_config.coding==MONO)
00236     dyn_rec_config.Color_coding=0;
00237   else if(camera_config.depth==DEPTH16 && camera_config.coding==YUV)
00238     dyn_rec_config.Color_coding=1;
00239   else if(camera_config.depth==DEPTH24 && camera_config.coding==YUV)
00240     dyn_rec_config.Color_coding=2;
00241   else if(camera_config.depth==DEPTH24 && camera_config.coding==RGB)
00242     dyn_rec_config.Color_coding=3;
00243   else if(camera_config.depth==DEPTH16 && camera_config.coding==MONO)
00244     dyn_rec_config.Color_coding=4;
00245   else if(camera_config.depth==DEPTH48 && camera_config.coding==RGB)
00246     dyn_rec_config.Color_coding=5;
00247   else if(camera_config.depth==DEPTH8 && camera_config.coding==RAW)
00248     dyn_rec_config.Color_coding=6;
00249   else if(camera_config.depth==DEPTH16 && camera_config.coding==RAW)
00250     dyn_rec_config.Color_coding=0;
00251   else
00252     dyn_rec_config.Color_coding=3;
00253 }
00254 
00255 void FirewireCameraDriver::change_config(TCameraConfig *new_conf)
00256 {
00257   try{
00258     if(this->camera!=NULL)
00259     {
00260       this->current_conf.left_offset=new_conf->left_offset;
00261       this->current_conf.top_offset=new_conf->top_offset;
00262       this->current_conf.width=new_conf->width;
00263       this->current_conf.height=new_conf->height;
00264       this->current_conf.framerate=new_conf->framerate;
00265       this->current_conf.depth=new_conf->depth;
00266       this->current_conf.coding=new_conf->coding;
00267       this->camera->set_config(&this->current_conf.left_offset,&this->current_conf.top_offset,&this->current_conf.width,
00268                                &this->current_conf.height,&this->current_conf.framerate,this->current_conf.depth,
00269                                this->current_conf.coding);
00270       this->desired_conf.left_offset=new_conf->left_offset;
00271       new_conf->left_offset=current_conf.left_offset;
00272       this->desired_conf.top_offset=new_conf->top_offset;
00273       new_conf->top_offset=current_conf.top_offset;
00274       this->desired_conf.width=new_conf->width;
00275       new_conf->width=current_conf.width;
00276       this->desired_conf.height=new_conf->height;
00277       new_conf->height=current_conf.height;
00278       this->desired_conf.framerate=new_conf->framerate;
00279       new_conf->framerate=current_conf.framerate;
00280       this->desired_conf.depth=new_conf->depth;
00281       new_conf->depth=current_conf.depth;
00282       this->desired_conf.coding=new_conf->coding;
00283       new_conf->coding=current_conf.coding;
00284     }
00285   }catch(CFirewireCameraException &e){
00286     ROS_INFO("Invalid configuration. Setting back the default configuration ...");
00287     this->change_config(&this->default_conf);
00288     new_conf->left_offset=this->default_conf.left_offset;
00289     new_conf->top_offset=this->default_conf.top_offset;
00290     new_conf->width=this->default_conf.width;
00291     new_conf->height=this->default_conf.height;
00292     new_conf->framerate=this->default_conf.framerate;
00293     new_conf->depth=this->default_conf.depth;
00294     new_conf->coding=this->default_conf.coding;
00295   }catch(CFirewireInternalException &e){
00296     throw;
00297   }
00298 }
00299 
00300 void FirewireCameraDriver::set_white_balance(bool *enable,int *mode, int *u_b_value, int *v_r_value)
00301 {
00302   try{
00303     if(this->camera->is_feature_present(DC1394_FEATURE_WHITE_BALANCE))
00304     {
00305       if(*enable)
00306       {
00307         try{
00308           if(!this->camera->is_feature_enabled(DC1394_FEATURE_WHITE_BALANCE))
00309             this->camera->enable_feature(DC1394_FEATURE_WHITE_BALANCE);
00310         }catch(CFirewireFeatureException &e){
00311           (*enable)=false;
00312           ROS_INFO("The white balance feature can not be enabled");
00313           return;
00314         }
00315         if((*mode)==0)
00316         {
00317           try{
00318             this->camera->set_feature_auto(DC1394_FEATURE_WHITE_BALANCE);
00319           }catch(CFirewireFeatureException &e){
00320             (*mode)=1;
00321             ROS_INFO("The white balance feature can not be set in auto mode");
00322             return;
00323           }
00324         }
00325         else
00326         {
00327           try{
00328             this->camera->set_feature_manual(DC1394_FEATURE_WHITE_BALANCE);
00329           }catch(CFirewireFeatureException &e){
00330             (*mode)=0;
00331             ROS_INFO("The white balance feature can not be set in manual mode");
00332             return;
00333           }
00334           try{
00335             this->camera->set_white_balance_value((*u_b_value),(*v_r_value));
00336           }catch(CFirewireFeatureException &e){
00337             this->camera->get_white_balance_value((unsigned int *)u_b_value,(unsigned int *)v_r_value);
00338             ROS_INFO("The white balance value is out of range");
00339             return;
00340           }
00341         }
00342       }
00343       else
00344       {
00345         try{
00346           if(this->camera->is_feature_enabled(DC1394_FEATURE_WHITE_BALANCE))
00347             this->camera->disable_feature(DC1394_FEATURE_WHITE_BALANCE);
00348         }catch(CFirewireFeatureException &e){
00349           (*enable)=true;
00350           ROS_INFO("The white balance feature can not be disabled");
00351           return;
00352         }
00353       }
00354     }
00355     else
00356     {
00357       (*enable)=false;
00358       ROS_INFO("The white balance feature is not present");
00359     }
00360   }catch(CFirewireInternalException &e){
00361     ROS_INFO("Impossible to set the white balance feature");
00362   }
00363 }
00364 
00365 void FirewireCameraDriver::set_shutter(bool *enable,int *mode, int *value)
00366 {
00367   try{
00368     if(this->camera->is_feature_present(DC1394_FEATURE_SHUTTER))
00369     {
00370       if(*enable)
00371       {
00372         try{
00373           if(!this->camera->is_feature_enabled(DC1394_FEATURE_SHUTTER))
00374             this->camera->enable_feature(DC1394_FEATURE_SHUTTER);
00375         }catch(CFirewireFeatureException &e){
00376           (*enable)=false;
00377           ROS_INFO("The shutter feature is not present");
00378           return;
00379         }
00380         if((*mode)==0)
00381         {
00382           try{
00383             this->camera->set_feature_auto(DC1394_FEATURE_SHUTTER);
00384           }catch(CFirewireFeatureException &e){
00385             (*mode)=1;
00386             ROS_INFO("The shutter feature can not be set in auto mode");
00387             return;
00388           }
00389         }
00390         else
00391         {
00392           try{
00393             this->camera->set_feature_manual(DC1394_FEATURE_SHUTTER);
00394           }catch(CFirewireFeatureException &e){
00395             (*mode)=0;
00396             ROS_INFO("The shutter feature can not be set in manual mode");
00397             return;
00398           }
00399           try{
00400             this->camera->set_feature_value(DC1394_FEATURE_SHUTTER,(*value));
00401           }catch(CFirewireFeatureException &e){
00402             (*value)=this->camera->get_feature_value(DC1394_FEATURE_SHUTTER);
00403             ROS_INFO("The shutter value is out of range");
00404             return;
00405           }
00406         }
00407       }
00408       else
00409       {
00410         try{
00411           if(this->camera->is_feature_enabled(DC1394_FEATURE_SHUTTER))
00412             this->camera->disable_feature(DC1394_FEATURE_SHUTTER);
00413         }catch(CFirewireFeatureException &e){
00414           (*enable)=true;
00415           ROS_INFO("The shutter feature can not be disabled");
00416           return;
00417         }
00418       }
00419     }
00420     else
00421     {
00422       (*enable)=false;
00423       ROS_INFO("The shutter feature is not present");
00424     }
00425   }catch(CFirewireInternalException &e){
00426     ROS_INFO("Impossible to set the shutter feature");
00427   }
00428 }
00429 
00430 void FirewireCameraDriver::set_gain(bool *enable, int *mode, int *value)
00431 {
00432   try{
00433     if(this->camera->is_feature_present(DC1394_FEATURE_GAIN))
00434     {
00435       if(*enable)
00436       {
00437         try{
00438           if(!this->camera->is_feature_enabled(DC1394_FEATURE_GAIN))
00439             this->camera->enable_feature(DC1394_FEATURE_GAIN);
00440         }catch(CFirewireFeatureException &e){
00441           (*enable)=false;
00442           ROS_INFO("The gain feature is not present");
00443           return;
00444         }
00445         if((*mode)==0)
00446         {
00447           try{
00448             this->camera->set_feature_auto(DC1394_FEATURE_GAIN);
00449           }catch(CFirewireFeatureException &e){
00450             (*mode)=1;
00451             ROS_INFO("The gain feature can not be set in auto mode");
00452             return;
00453           }
00454         }
00455         else
00456         {
00457           try{
00458             this->camera->set_feature_manual(DC1394_FEATURE_GAIN);
00459           }catch(CFirewireFeatureException &e){
00460             (*mode)=0;
00461             ROS_INFO("The gain feature can not be set in manual mode");
00462             return;
00463           }
00464           try{
00465             this->camera->set_feature_value(DC1394_FEATURE_GAIN,(*value));
00466           }catch(CFirewireFeatureException &e){
00467             (*value)=this->camera->get_feature_value(DC1394_FEATURE_GAIN);
00468             ROS_INFO("The gain value is out of range");
00469             return;
00470           }
00471         }
00472       }
00473       else
00474       {
00475         try{
00476           if(this->camera->is_feature_enabled(DC1394_FEATURE_GAIN))
00477             this->camera->disable_feature(DC1394_FEATURE_GAIN);
00478         }catch(CFirewireFeatureException &e){
00479           (*enable)=true;
00480           ROS_INFO("The gain feature can not be disabled");
00481           return;
00482         }
00483       }
00484     }
00485     else
00486     {
00487       (*enable)=false;
00488       ROS_INFO("The gain feature is not present");
00489     }
00490   }catch(CFirewireInternalException &e){
00491     ROS_INFO("Impossible to set the gain feature");
00492   }
00493 }
00494 
00495 void FirewireCameraDriver::get_image(char **image_data)
00496 {
00497   if(this->camera!=NULL)
00498     this->camera->get_image(image_data);
00499   else
00500     *image_data=NULL;
00501 }
00502 
00503 void FirewireCameraDriver::set_ISO_speed(int iso_speed)
00504 {
00505   this->ISO_speed=iso_speed;
00506 }
00507 
00508 void FirewireCameraDriver::set_config(TCameraConfig *new_conf)
00509 {
00510   try{
00511     this->lock();
00512     this->change_config(new_conf);
00513     this->unlock();
00514   }catch(CException &e){
00515     this->unlock();
00516     throw;
00517   }
00518 }
00519 
00520 void FirewireCameraDriver::get_current_config(TCameraConfig *current_conf)
00521 {
00522   current_conf->left_offset=this->current_conf.left_offset;
00523   current_conf->top_offset=this->current_conf.top_offset;
00524   current_conf->width=this->current_conf.width;
00525   current_conf->height=this->current_conf.height;
00526   current_conf->framerate=this->current_conf.framerate;
00527   current_conf->depth=this->current_conf.depth;
00528   current_conf->coding=this->current_conf.coding;
00529 }
00530 
00531 void FirewireCameraDriver::get_desired_config(TCameraConfig *desired_conf)
00532 {
00533   desired_conf->left_offset=this->desired_conf.left_offset;
00534   desired_conf->top_offset=this->desired_conf.top_offset;
00535   desired_conf->width=this->desired_conf.width;
00536   desired_conf->height=this->desired_conf.height;
00537   desired_conf->framerate=this->desired_conf.framerate;
00538   desired_conf->depth=this->desired_conf.depth;
00539   desired_conf->coding=this->desired_conf.coding;
00540 }
00541 
00542 void FirewireCameraDriver::get_default_config(TCameraConfig *default_conf)
00543 {
00544   default_conf->left_offset=this->default_conf.left_offset;
00545   default_conf->top_offset=this->default_conf.top_offset;
00546   default_conf->width=this->default_conf.width;
00547   default_conf->height=this->default_conf.height;
00548   default_conf->framerate=this->default_conf.framerate;
00549   default_conf->depth=this->default_conf.depth;
00550   default_conf->coding=this->default_conf.coding;
00551 }
00552 
00553 std::string FirewireCameraDriver::get_new_frame_event(void)
00554 {  
00555   std::string event="";
00556 
00557   if(this->camera!=NULL) 
00558     this->camera->get_new_frame_event_id(event);
00559 
00560   return event;
00561 }
00562 
00563 std::string FirewireCameraDriver::get_calibration_file(void)
00564 {
00565   return this->calibration_file;
00566 }
00567 
00568 std::string FirewireCameraDriver::get_frame_id(void)
00569 {
00570   return this->frame_id;
00571 }
00572 
00573 FirewireCameraDriver::~FirewireCameraDriver()
00574 {
00575   this->server->close();
00576 }


iri_firewire_camera
Author(s): Sergi Hernandez Juan
autogenerated on Fri Dec 6 2013 22:28:49