camera_prosilica_gige
CamGigEProsilica.h
Go to the documentation of this file.
1 /*
2  * File: CameraProsilica.h
3  * Author: Alexander Duda
4  *
5  * Created on February 9, 2010, 8:39 AM
6  */
7 
8 #ifndef _CAMGIGEPROSILICA_H
9 #define _CAMGIGEPROSILICA_H
10 
11 #include <camera_interface/CamInterface.h>
12 #include "arch.h"
13 #include "PvApi.h"
14 #include <list>
15 
16 namespace camera
17 {
18  struct ProFrame
19  {
20  private:
21  std::vector<uint8_t> data;
22  public:
23  tPvFrame frame;
24  base::Time timestamp_received;
25 
26  ProFrame( int frame_size_in_byte)
27  {
28  resize(frame_size_in_byte);
29 
30  }
31 
32  inline void swap(base::samples::frame::Frame &other)
33  {
34  if(data.size() != other.image.size())
35  throw std::runtime_error("Frame size mismatch. "
36  " Can not swap data.");
37  other.image.swap(data);
38  frame.ImageBuffer = &data[0];
39 
40  other.attributes.clear();
41  if(frame.Status == ePvErrSuccess)
42  other.setStatus(base::samples::frame::STATUS_VALID);
43  else
44  other.setStatus(base::samples::frame::STATUS_INVALID);
45 
46  //Rolling frame counter. For GigE Vision cameras, this
47  //corresponds to the block number, which rolls from 1 to 0xFFFF
48  other.setAttribute<uint16_t>("FrameCount",frame.FrameCount);
49  other.time = timestamp_received;
50  other.received_time = timestamp_received;
51 
52  switch(frame.Format)
53  {
54  //check bayer pattern
55  case ePvFmtBayer8:
56  case ePvFmtBayer16:
57  switch (frame.BayerPattern)
58  {
59  case ePvBayerRGGB:
60  if(other.getFrameMode() != base::samples::frame::MODE_BAYER_RGGB)
61  {
62  if(other.getFrameMode() == base::samples::frame::MODE_BAYER)
63  other.frame_mode = base::samples::frame::MODE_BAYER_RGGB;
64  else
65  throw std::runtime_error("Bayer Pattern is MODE_BAYER_RGGB but frame has a "
66  "different format. Use MODE_BAYER if you want to use auto discover.");
67  }
68  break;
69 
70  case ePvBayerGBRG:
71  if(other.getFrameMode() != base::samples::frame::MODE_BAYER_GBRG)
72  {
73  if(other.getFrameMode() == base::samples::frame::MODE_BAYER)
74  other.frame_mode = base::samples::frame::MODE_BAYER_GBRG;
75  else
76  throw std::runtime_error("Bayer Pattern is MODE_BAYER_GBRG but frame has a "
77  "different format. Use MODE_BAYER if you want to use auto discover.");
78  }
79  break;
80 
81  case ePvBayerGRBG:
82  if(other.getFrameMode() != base::samples::frame::MODE_BAYER_GRBG)
83  {
84  if(other.getFrameMode() == base::samples::frame::MODE_BAYER)
85  other.frame_mode = base::samples::frame::MODE_BAYER_GRBG;
86  else
87  throw std::runtime_error("Bayer Pattern is MODE_BAYER_GRBG but frame has a "
88  "different format. Use MODE_BAYER if you want to use auto discover.");
89  }
90  break;
91 
92  case ePvBayerBGGR:
93  if(other.getFrameMode() != base::samples::frame::MODE_BAYER_BGGR)
94  {
95  if(other.getFrameMode() == base::samples::frame::MODE_BAYER)
96  other.frame_mode = base::samples::frame::MODE_BAYER_BGGR;
97  else
98  throw std::runtime_error("Bayer Pattern is MODE_BAYER_BGGR but frame has a "
99  "different format. Use MODE_BAYER if you want to use auto discover.");
100  }
101  break;
102 
103  default: //unknown bayer pattern
104  throw std::runtime_error("Unknown Bayer Patter");
105  }
106  break;
107 
108  case ePvFmtMono8:
109  case ePvFmtMono16:
110  if(other.getFrameMode() != base::samples::frame::MODE_GRAYSCALE)
111  throw std::runtime_error("Color format is MONO but frame has a different format.");
112  break;
113 
114  case ePvFmtRgb24:
115  case ePvFmtRgb48:
116  if(other.getFrameMode() != base::samples::frame::MODE_RGB)
117  throw std::runtime_error("Color format is RGB but frame has a different format.");
118  break;
119 
120  default:
121  {
122  if(other.getFrameMode() != base::samples::frame::MODE_UNDEFINED)
123  throw std::runtime_error("Unknown frame color format!!!");
124  }
125  }
126  }
127 
128  inline void resize( int frame_size_in_byte)
129  {
130  data.resize(frame_size_in_byte);
131  frame.ImageBuffer = &data[0];
132  frame.ImageBufferSize = frame_size_in_byte;
133  frame.AncillaryBuffer = NULL;
134  frame.Context[0] = &timestamp_received;
135  // frame.Context[1] is used for user callback
136 
137  //using AncillaryBufferSize to indicate if frame is done
138  //0 --> frame is done
139  //1 --> frame is not done
140  //faster than PvCaptureWaitForFrameDone
141  frame.AncillaryBufferSize = 0;
142  }
143  };
144 
145  class CamGigEProsilica : public CamInterface
146  {
147  private:
148  tPvHandle camera_handle_;
149  CamInfo cam_info_;
150  static int instance_count_;
151  std::list<ProFrame*> frame_queue_;
152  int frame_size_in_byte_;
153  AccessMode access_mode_;
154  void (*pcallback_function_)(const void* p);
155  void *pass_through_pointer_;
156  double timestamp_factor;
157  uint64_t timestamp_offset_camera_system; //in micro seconds
158 
159  uint32_t max_package_size_t; //max package size of the ethernet packages (mtu) in byte (defaul is 16110)
160 
161  public:
162  CamGigEProsilica(uint32_t max_package_size = 16110);
164 
165  public:
166  int listCameras(std::vector<CamInfo>&cam_infos)const;
167  bool open(const CamInfo &cam,const AccessMode mode);
168  bool open(const std::string &ip,const AccessMode mode);
169  bool isOpen()const;
170  bool close();
171  const CamInfo *getCameraInfo()const;
172 
173  bool grab(const GrabMode mode, const int buffer_len);
174  bool retrieveFrame(base::samples::frame::Frame &frame,const int timeout);
175  bool setFrameSettings(const base::samples::frame::frame_size_t size,
176  const base::samples::frame::frame_mode_t mode,
177  const uint8_t color_depth,
178  const bool resize_frames);
179  bool isFrameAvailable();
180  int skipFrames();
181 
182  bool setAttrib(const int_attrib::CamAttrib attrib,const int value);
183  bool setAttrib(const double_attrib::CamAttrib attrib,const double value);
184  bool setAttrib(const enum_attrib::CamAttrib attrib);
185  bool setAttrib(const str_attrib::CamAttrib attrib,const std::string &string);
186 
187  bool isAttribAvail(const int_attrib::CamAttrib attrib);
188  bool isAttribAvail(const str_attrib::CamAttrib attrib);
189  bool isAttribAvail(const double_attrib::CamAttrib attrib);
190  bool isAttribAvail(const enum_attrib::CamAttrib attrib);
191 
192  int getAttrib(const int_attrib::CamAttrib attrib);
193  double getAttrib(const double_attrib::CamAttrib attrib);
194  std::string getAttrib(const str_attrib::CamAttrib attrib);
195  bool isAttribSet(const enum_attrib::CamAttrib attrib);
196 
197  bool getFrameSettings(base::samples::frame::frame_size_t &size,
198  base::samples::frame::frame_mode_t &mode,
199  uint8_t &color_depth);
200 
201  bool triggerFrame();
202 
203  bool setCallbackFcn(void (*pcallback_function)(const void* p),void *p);
204  void callUserCallbackFcn()const;
205  void synchronizeWithSystemTime(uint32_t time_interval);
206  void saveConfiguration(uint8_t index);
207  void loadConfiguration(uint8_t index);
208 
209  void getRange(const double_attrib::CamAttrib attrib,double &dmin,double &dmax);
210  void getRange(const int_attrib::CamAttrib attrib,int &imin,int &imax);
211 
212  private:
213  //helpers
214  void copy_tPvCameraInfo_To_tCamInfo
215  (const tPvCameraInfo& source, CamInfo& dest) const;
216  bool fillCameraIpSettings(CamInfo& cam)const;
217  bool prepareQueueForGrabbing(const int queue_len);
218  inline bool isFrameQueued(const ProFrame* frame);
219  inline bool queueFrame(ProFrame* frame);
220  static void callBack(tPvFrame * frame);
221  static void callBack2(tPvFrame * frame);
222 
223  void checkCameraStatus()const;
224  void setConfigFileIndex(uint8_t index);
225  };
226 }
227 
228 
229 #endif /* _CAMGIGEPROSILICA_H */
230 
ProFrame(int frame_size_in_byte)
Definition: CamGigEProsilica.h:26
bool setCallbackFcn(void(*pcallback_function)(const void *p), void *p)
Definition: CamGigEProsilica.cpp:112
tPvFrame frame
Definition: CamGigEProsilica.h:23
void synchronizeWithSystemTime(uint32_t time_interval)
Definition: CamGigEProsilica.cpp:1023
void loadConfiguration(uint8_t index)
Definition: CamGigEProsilica.cpp:972
bool isAttribSet(const enum_attrib::CamAttrib attrib)
Definition: CamGigEProsilica.cpp:884
bool setAttrib(const int_attrib::CamAttrib attrib, const int value)
Definition: CamGigEProsilica.cpp:709
void saveConfiguration(uint8_t index)
Definition: CamGigEProsilica.cpp:964
int listCameras(std::vector< CamInfo > &cam_infos) const
Definition: CamGigEProsilica.cpp:78
bool getFrameSettings(base::samples::frame::frame_size_t &size, base::samples::frame::frame_mode_t &mode, uint8_t &color_depth)
Definition: CamGigEProsilica.cpp:939
bool isAttribAvail(const int_attrib::CamAttrib attrib)
Definition: CamGigEProsilica.cpp:773
void callUserCallbackFcn() const
Definition: CamGigEProsilica.cpp:125
Definition: CamGigEProsilica.h:145
bool triggerFrame()
Definition: CamGigEProsilica.cpp:929
bool isFrameAvailable()
Definition: CamGigEProsilica.cpp:335
bool retrieveFrame(base::samples::frame::Frame &frame, const int timeout)
Definition: CamGigEProsilica.cpp:363
bool setFrameSettings(const base::samples::frame::frame_size_t size, const base::samples::frame::frame_mode_t mode, const uint8_t color_depth, const bool resize_frames)
Definition: CamGigEProsilica.cpp:568
void resize(int frame_size_in_byte)
Definition: CamGigEProsilica.h:128
int skipFrames()
Definition: CamGigEProsilica.cpp:342
void swap(base::samples::frame::Frame &other)
Definition: CamGigEProsilica.h:32
int getAttrib(const int_attrib::CamAttrib attrib)
Definition: CamGigEProsilica.cpp:845
base::Time timestamp_received
Definition: CamGigEProsilica.h:24
bool close()
Definition: CamGigEProsilica.cpp:427
Definition: CamGigEProsilica.h:18
const CamInfo * getCameraInfo() const
Definition: CamGigEProsilica.cpp:130
bool isOpen() const
Definition: CamGigEProsilica.cpp:271
bool grab(const GrabMode mode, const int buffer_len)
Definition: CamGigEProsilica.cpp:278
CamGigEProsilica(uint32_t max_package_size=16110)
Definition: CamGigEProsilica.cpp:35
void getRange(const double_attrib::CamAttrib attrib, double &dmin, double &dmax)
Definition: CamGigEProsilica.cpp:903
bool open(const CamInfo &cam, const AccessMode mode)
Definition: CamGigEProsilica.cpp:165
~CamGigEProsilica()
Definition: CamGigEProsilica.cpp:66