pike.cpp

00001 
00002 /***************************************************************************
00003  *  pike.cpp - Allied Vision Technologies Pike camera
00004  *
00005  *  Generated: Tue Mar 16 15:27:32 2010
00006  *  Copyright  2010  Daniel Beck
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #include <cams/pike.h>
00025 #include <cams/cam_exceptions.h>
00026 
00027 #include <fvutils/system/camargp.h>
00028 
00029 #include <cstring>
00030 #include <cstdlib>
00031 
00032 using namespace std;
00033 using namespace fawkes;
00034 
00035 namespace firevision {
00036 #if 0 /* just to make Emacs auto-indent happy */
00037 }
00038 #endif
00039 
00040 /** @class PikeCamera <cams/pike.h>
00041  * Pike camera.
00042  * Allows to access some special features of the Pike camera made by
00043  * Allied Vision Technologies.
00044  */
00045 
00046 // AVT specific registers
00047 /** Register for white balance settings */
00048 #define AVT_WHITE_BALANCE_REGISTER     (0x0F0080C)
00049 
00050 /** Registers for area of interest settings */
00051 #define AVT_AUTOFNC_AOI_REGISTER       (0x0390)
00052 #define AVT_AF_AREA_POSITION_REGISTER  (0x0394)
00053 #define AVT_AF_AREA_SIZE_REGISTER      (0x0398)
00054 
00055 /** Extended version information registerst */
00056 #define AVT_VERSION_INFO1_REGISTER     (0x1000010)
00057 #define AVT_VERSION_INFO3_REGISTER     (0x1000018)
00058 
00059 // AVT specific data structures
00060 // /** White balance settings data structure */
00061 // typedef struct {
00062 //   uint32_t abs_control  :  1;
00063 //   uint32_t reserved     :  3;
00064 //   uint32_t one_push     :  1;
00065 //   uint32_t on_off       :  1;
00066 //   uint32_t a_m_mode     :  1;
00067 //   uint32_t ub_value     : 12;
00068 //   uint32_t vr_value     : 12;
00069 //   uint32_t presence_inq :  1;
00070 // } avt_white_balance_t;
00071 
00072 /** Datastructure for the autofunction AOI */
00073 typedef struct {
00074   uint32_t xuints         : 12; /**< X units of work area/pos. beginning with 0 (read only) */
00075   uint32_t yuints         : 12; /**< Y units of work area/pos. beginning with 0 (read only) */
00076   uint32_t reserved3      :  1; /**< Reserved. */
00077   uint32_t on_off         :  1; /**< Enable/disable AOI (see note above). */
00078   uint32_t reserved2      :  1; /**< Reserved. */
00079   uint32_t show_work_area :  1; /**< Show work area. */
00080   uint32_t reserved1      :  3; /**< Reserved. */
00081   uint32_t presence_inq   :  1; /**< Indicates presence of this feature (read only). */
00082 } avt_autofnc_aoi_t;
00083 
00084 /** Datastructure for the position of the autofunction AOI */
00085 typedef struct {
00086   uint32_t top  : 16; /**< Work area position (top coordinate). */
00087   uint32_t left : 16; /**< Work area position (left coordinate). */
00088 } avt_af_area_position_t;
00089 
00090 /** Datastructure for the size of the autofunction AOI */
00091 typedef struct {
00092   uint32_t height : 16; /**< Height of work area size. */
00093   uint32_t width  : 16; /**< Width of work area size. */
00094 } avt_af_area_size_t;
00095 
00096 /** Datastructure for version information of the uC */
00097 typedef struct {
00098   uint32_t uc_version : 16; /**< Bcd-coded version number. */
00099   uint32_t uc_type_id : 16; /**< Always 0. */
00100 } avt_version_info1_t;
00101 
00102 /** Datastructure for version information of the FGPA */
00103 typedef struct {
00104   uint32_t fpga_version   : 16; /**< Bcd-coded version number. */
00105   uint32_t camera_type_id : 16; /**< See Table 122: Camera type ID list on page 267 in the technical manual (v 4.3.0). */
00106 } avt_version_info3_t;
00107 
00108 
00109 /** Constructor.
00110  * @param cap Camera argument parser.
00111  */
00112 PikeCamera::PikeCamera(const CameraArgumentParser* cap)
00113   : FirewireCamera( cap )
00114 {
00115   __aoi_left   = 0;
00116   __aoi_top    = 0;
00117   __aoi_width  = 0;
00118   __aoi_height = 0;
00119   __aoi_show_work_area = false;
00120 
00121   __set_autofnc_aoi = false;
00122 
00123   if ( cap->has( "autofnc_aoi" ) )
00124   {
00125     __set_autofnc_aoi = true;
00126     parse_set_autofnc_aoi( cap->get( "autofnc_aoi" ).c_str() );
00127   }
00128 }
00129 
00130 /** Destructor. */
00131 PikeCamera::~PikeCamera()
00132 {
00133 }
00134 
00135 void
00136 PikeCamera::open()
00137 {
00138   try
00139   {
00140     FirewireCamera::open();
00141   } catch ( Exception &e )
00142   { throw; }
00143 
00144   if ( !_opened )
00145   { throw Exception( "PikeCamera::open: FirewireCamera::open dit not succed" ); }
00146 
00147   if ( !set_autofunction_aoi( __aoi_left, __aoi_top, __aoi_width, __aoi_height,
00148                               __aoi_show_work_area ) )
00149   {
00150     throw Exception( "PikeCamera::PikeCamera: setting autofnc AOI failed." );
00151   }
00152 }
00153 
00154 void
00155 PikeCamera::print_info()
00156 {
00157   FirewireCamera::print_info();
00158 
00159   uint32_t value;
00160   dc1394error_t err = dc1394_get_register( _camera,
00161                                            AVT_VERSION_INFO1_REGISTER,
00162                                            &value );
00163 
00164   if ( err != DC1394_SUCCESS )
00165   {
00166     throw Exception( "Pike::print_info; dc1394_get_register(AVT_VERSION_INFO1_REGISTER) failed\n" );
00167   }
00168 
00169   avt_version_info1_t version1;
00170   memcpy( (void*) &version1, (void*) &value, sizeof(uint32_t) );
00171 
00172   err = dc1394_get_register( _camera,
00173                              AVT_VERSION_INFO3_REGISTER,
00174                              &value );
00175 
00176   if ( err != DC1394_SUCCESS )
00177   {
00178     throw Exception( "Pike::print_info; dc1394_get_register(AVT_VERSION_INFO3_REGISTER) failed\n" );
00179   }
00180 
00181   avt_version_info3_t version3;
00182   memcpy( (void*) &version3, (void*) &value, sizeof(uint32_t) );
00183 
00184   printf( "uC type ID: %d  uC version: %x  camera type id: %d   FPGA version: %x\n",
00185           version1.uc_type_id, version1.uc_version, version3.camera_type_id, version3.fpga_version );
00186 }
00187 
00188 /** Set the area of interest (AOI) for the auto functions.
00189  * @param left offset form the left image border
00190  * @param top offset form the top image border
00191  * @param width width of the AOI
00192  * @param height height of the AOI
00193  * @param show_work_area highlight the work area in the image
00194  * @return true on success, false otherwise
00195  */
00196 bool
00197 PikeCamera::set_autofunction_aoi( unsigned int left,
00198                                   unsigned int top,
00199                                   unsigned int width,
00200                                   unsigned int height,
00201                                   bool show_work_area )
00202 {
00203   if ( !_opened )
00204   { return false; }
00205 
00206   if ( !__set_autofnc_aoi )
00207   { return true; }
00208 
00209   avt_autofnc_aoi_t aoi;
00210   avt_af_area_position_t position;
00211   avt_af_area_size_t size;
00212 
00213   aoi.show_work_area = show_work_area;
00214   aoi.on_off         = true;
00215 
00216   position.left = left;
00217   position.top  = top;
00218 
00219   size.width  = width;
00220   size.height = height;
00221 
00222   dc1394error_t err;
00223 
00224   uint32_t value = 0;
00225   memcpy( (void*) &value, (void*) &aoi, sizeof( value ) );
00226   
00227   err = dc1394_set_adv_control_register( _camera,
00228                                          AVT_AUTOFNC_AOI_REGISTER,
00229                                          value );
00230 
00231   if ( err != DC1394_SUCCESS )
00232   {
00233     throw Exception( "Pike::set_autofunction_aoi; dc1394_set_register(AVT_AUTOFNC_AOI_REGISTER) failed\n" );
00234   }
00235   
00236   memcpy( (void*) &value, (void*) &position, sizeof( value ) );
00237   err = dc1394_set_adv_control_register( _camera,
00238                                          AVT_AF_AREA_POSITION_REGISTER,
00239                                          value );
00240 
00241   if ( err != DC1394_SUCCESS )
00242   {
00243     throw Exception( "Pike::set_autofunction_aoi; dc1394_set_register(AVT_AF_AREA_POSITION_REGISTER) failed\n" );
00244   }
00245 
00246   memcpy( (void*) &value, (void*) &size, sizeof( value ) );
00247   err = dc1394_set_adv_control_register( _camera,
00248                                          AVT_AF_AREA_SIZE_REGISTER,
00249                                          value );
00250 
00251   if ( err != DC1394_SUCCESS )
00252   {
00253     throw Exception( "Pike::set_autofunction_aoi; dc1394_set_register(AVT_AF_AREA_SIZE_REGISTER) failed\n" );
00254   }
00255 
00256   err = dc1394_get_adv_control_register( _camera,
00257                                          AVT_AUTOFNC_AOI_REGISTER,
00258                                          &value );
00259   if ( err != DC1394_SUCCESS )
00260   {
00261     throw Exception( "Pike::set_autofunction_aoi; dc1394_get_register(AVT_AUTOFNC_AOI_REGISTER) failed\n" );
00262   }
00263 
00264   memcpy ( (void*) &aoi, (void*) &value, sizeof( value ) );
00265 
00266   return aoi.on_off;
00267 }
00268 
00269 /** Parse the autofnc_aoi parameter in the camera argument string.
00270  * The format ist <left>x<top>+<width>x<height>-<show>. "-<show>" is
00271  * optional.
00272  * @param aoi the parameter string of the autofnc_aoi parameter
00273  */
00274 void
00275 PikeCamera::parse_set_autofnc_aoi( const char* aoi )
00276 {
00277   // format: left x top + width x height - show
00278 
00279   string a = aoi;
00280   
00281   string::size_type pos;
00282 
00283   pos = a.find( "x", 0 );
00284   if ( pos == string::npos )
00285   { throw Exception( "Illegal autofnc AOI parameter" ); }
00286   string left = a.substr( 0, pos );
00287   a = a.substr( pos + 1 );
00288 
00289   pos = a.find( "+", 0 );
00290   if ( pos == string::npos )
00291   { throw Exception( "Illegal autofnc AOI parameter" ); }
00292   string top = a.substr( 0, pos );
00293   a = a.substr( pos + 1 );
00294 
00295   pos = a.find( "x", 0 );
00296   if ( pos == string::npos )
00297   { throw Exception( "Illegal autofnc AOI parameter" ); }
00298   string width = a.substr( 0, pos );
00299   a = a.substr( pos + 1 );
00300 
00301   string height;
00302   string show;
00303   pos = a.find( "-", 0 );
00304   if ( pos == string::npos )
00305   {
00306     height = a;
00307     __aoi_show_work_area = false;
00308   }
00309   else
00310   {
00311     height = a.substr( 0, pos );
00312     show = a.substr( pos + 1 );
00313 
00314     __aoi_show_work_area = ( show == "show" ) ? true : false;
00315   }
00316 
00317   __aoi_left   = atoi( left.c_str() );
00318   __aoi_top    = atoi( top.c_str() );
00319   __aoi_width  = atoi( width.c_str() );
00320   __aoi_height = atoi( height.c_str() );
00321 }
00322 
00323 } // end namespace firevision

Generated on Tue Feb 22 13:31:15 2011 for Fawkes API by  doxygen 1.4.7