COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
gige_snap.cpp File Reference
#include "device/general/gige_lib.h"
#include "agent/agentclass.h"
#include "support/datalib.h"
#include "time.h"
Include dependency graph for gige_snap.cpp:

Macros

#define A35COUNT   1
 

Functions

int main (int argc, char *argv[])
 

Variables

uint8_t image [110000000]
 

Macro Definition Documentation

#define A35COUNT   1

Function Documentation

int main ( int  argc,
char *  argv[] 
)
41 {
43  int32_t tbytes;
44  int32_t expbytes;
45  int32_t iretn;
46  uint32_t width, height;
47  //uint32_t faddress, fsize;
48  uint32_t bsize;
49  FILE *fp;
50  std::vector<gige_acknowledge_ack> gige_list;
51 
52  std::string extra = "";
53  uint32_t exposure=1;
54  uint32_t gain=1;
55  uint32_t binning=1;
56  char ipaddress[20];
57 
58  switch (argc)
59  {
60  case 6:
61  binning=atol(argv[5]);
62  case 5:
63  gain = atol(argv[4]);
64  case 4:
65  exposure = atol(argv[3]);
66  case 3:
67  extra = argv[2];
68  case 2:
69  strcpy(ipaddress, argv[1]);
70  break;
71  default:
72  printf("Usage: gigesnap ipaddress [extraname [exposure [gain [binning]]]]\n");
73  exit (1);
74  break;
75  }
76 
77  gige_list = gige_discover();
78  if (!gige_list.size())
79  {
80  printf("Couldn't find any cameras\n");
81  exit(1);
82  }
83 
84 
85  //define variables to be read by the prosilica - e.g., to check exposure, etc.
86  uint32_t used_exposure=0;
87  uint32_t used_imagewidth=0;
88  uint32_t used_imageheight=0;
89 
90  std::string imagename = "testimage.imgdata";
91  std::string metafilename = "testimage.imgmeta";
92  std::string fringename = "testimage.imgfringe";
93 
94 
95 
96 
97  //Time stuff. Probably superfluous.
98  time_t current_time;
99  char* c_time_string;
100 
101  /* Obtain current time as seconds elapsed since the Epoch. */
102  current_time = time(NULL);
103 
104 
105  if (current_time == ((time_t)-1))
106 
107  {
108  (void) fprintf(stderr, "Failure to compute the current time.");
109  return EXIT_FAILURE;
110  }
111  /* Convert to local time format. */
112  c_time_string = ctime(&current_time);
113 
114  if (c_time_string == NULL)
115  {
116  (void) fprintf(stderr, "Failure to convert the current time.");
117  return EXIT_FAILURE;
118  }
119 
120  /* Print to stdout. */
121  // (void) printf("Current time is %s", c_time_string);
122  // return EXIT_SUCCESS;
123 
124 
125 
126 
127  if ((handle=gige_open(ipaddress,0x02,40000,5000,7500000)) == NULL)
128  {
129  if((handle = gige_open(gige_value_to_address(gige_list[0].address),0x02,40000,5000,7500000)) == NULL)
130  {
131  printf("Couldn't open camera\n");
132  exit(1);
133  }
134  }
135 
136  iretn = gige_readreg(handle,GIGE_REG_CCP);
137  printf("Read CCP: %u\n",iretn);
138 
139  iretn = gige_readreg(handle,GIGE_REG_DEVICE_MAC_HIGH);
140  printf("Read DEVICE_MAC_HIGH: %0x\n",iretn);
141 
142  iretn = gige_readreg(handle,GIGE_REG_DEVICE_MAC_LOW);
143  printf("Read DEVICE_MAC_LOW: %0x\n",iretn);
144 
145  iretn = gige_readreg(handle,GIGE_REG_CCP);
146  printf("Read CCP: %u\n",iretn);
147 
148  iretn = gige_readreg(handle,GIGE_REG_VERSION);
149  printf("Read VERSION: %u\n",iretn);
150 
151  iretn = gige_readreg(handle,GIGE_REG_DEVICE_MODE);
152  printf("Read DEVICE_MODE: %u\n",iretn);
153 
155  printf("Read GVCP_HEARTBEAT_TIMEOUT %u\n",iretn);
156 
158  printf("Read PRIMARY_APPLICATION_IP_ADDRESS %x\n",iretn);
159 
160  /*
161  iretn = gige_readmem(handle,GIGE_REG_FIRST_URL,512);
162  char *ptr = (char *)handle->cack_mem.data;
163  do
164  {
165  ptr++;
166  } while (*ptr != ':');
167  char *ptr1 = ++ptr;
168 
169  do
170  {
171  ptr++;
172  } while (*ptr != ';');
173 
174  char fname[100];
175  strncpy(fname,ptr1,ptr-ptr1);
176  fname[ptr-ptr1] = 0;
177  uint32_t faddress, fsize;
178  sscanf((char *)ptr,";%x;%x",&faddress,&fsize);
179  printf("Read GIGE_REG_FIRST_URL %u %s;%u;%u\n",iretn,fname,faddress,fsize);
180 
181  iretn = gige_readmem(handle,GIGE_REG_SECOND_URL,512);
182  printf("Read GIGE_REG_FIRST_SECOND %u %s\n",iretn,handle->cack_mem.data);
183 
184  fp = fopen(fname,"w");
185  uint32_t count=0;
186  for (uint32_t i=0; i<fsize/536; ++i)
187  {
188  iretn = gige_readmem(handle,faddress+count,536);
189  fwrite((void *)handle->cack_mem.data,iretn,1,fp);
190  count += iretn;
191  printf("Read GIGE_REG_XML %u %d\r",iretn,count);
192  }
193  iretn = gige_readmem(handle,faddress+count,fsize-count);
194  fwrite((void *)handle->cack_mem.data,iretn,1,fp);
195  count += iretn;
196  printf("Read GIGE_REG_XML %u %d\n",iretn,count);
197  fclose(fp);
198 
199 */
200 
201 
202  // configure camera- moved up to reflect accurate data down below
204  printf("Model: %s\n", (char *)handle->cack_mem.data);
205  bsize = handle->bestsize;
206  if( strncmp((char *)handle->cack_mem.data, "FLIR AX5", 8) == 0 )
207  {
208  iretn = gige_readreg(handle,A35_PIXELFORMAT);
209  // printf("Read A35_PIXELFORMAT %d\n",iretn);
210 
211  width = 336;
212  printf("Read A35_WIDTH %d\n",width);
213 
214  height = 256;
215  printf("Read A35_HEIGHT %d\n",height);
216 
217  iretn = a35_config(handle, width, height, A35_SENSORVIDEOSTANDARD_30HZ);
218  expbytes = width * height * exposure * 2;
219  tbytes = a35_image(handle, exposure, image, bsize);
220 
221  if (expbytes == tbytes)
222  {
223  fringename = data_type_path("hiakasat", "temp", "gige", currentmjd(0.), extra, "imgfringe");
224  FILE *fp = fopen(fringename.c_str(), "w");
225  fprintf(fp, "Fringes\n");
226  uint16_t *cube = (uint16_t *)image;
227  float mean[336][256];
228  float high[336];
229  float fringe[256];
230  for (uint32_t icol=0; icol<336; ++icol)
231  {
232  high[icol]=0.;
233  for (uint32_t irow=0; irow<256; ++irow)
234  {
235  mean[icol][irow] = 0.;
236  for (uint32_t ilayer=0; ilayer<exposure; ++ilayer)
237  {
238  mean[icol][irow] += cube[ilayer*336*256+irow*336+icol];
239  }
240  mean[icol][irow] /= (float)exposure;
241  if (mean[icol][irow] > high[icol])
242  {
243  high[icol] = mean[icol][irow];
244  printf("[%d,%d] %f\r",icol,irow,high[icol]);
245  }
246  }
247  printf("\n");
248  }
249 
250  for (uint32_t irow=0; irow<256; ++irow)
251  {
252  fringe[irow] = 0.;
253  for (uint32_t icol=0; icol<336; ++icol)
254  {
255  fringe[irow] += mean[icol][irow] / mean[icol][0];
256  }
257  fringe[irow] /= 336.;
258  fprintf(fp,"%.4g\n",fringe[irow]);
259  }
260  fclose(fp);
261 
262  printf("A35_TEMPERATURE: %u A35_TEMPERATUREFPA: %f\n", gige_readreg(handle, A35_TEMPERATURE), gige_readreg(handle, A35_TEMPERATUREFPA)/10.);
263  }
264  }
265  else if (strncmp((char *)handle->cack_mem.data, "PT1000", 6) == 0 )
266  {
267  iretn = gige_readreg(handle,PT1000::PixelFormatReg);
268  printf("Read PT1000PIXELFORMAT %d\n",iretn);
269 
270  width = gige_readreg(handle,PT1000::WidthReg);
271  printf("Read PT1000_WIDTH %d\n",width);
272 // width = 320;
273 
274  height = gige_readreg(handle,PT1000::HeightReg);
275  printf("Read PT1000_HEIGHT %d\n",height);
276 // height = 240;
277 
278  iretn = pt1000_config(handle, width, height);
279  if (iretn < 0)
280  {
281  printf("Error configuring: %s\n", cosmos_error_string(iretn).c_str());
282  }
283  expbytes = width * height * exposure * 2;
284  tbytes = pt1000_image(handle, exposure, image, bsize);
285 
286  if (expbytes == tbytes)
287  {
288  FILE *fp;
289  fp = fopen("pt1000_test.img","wb");
290  fwrite((void *)image,tbytes,1,fp);
291  fclose(fp);
292 
293  fp = fopen("pt1000_test", "w");
294  uint16_t *cube = (uint16_t *)image;
295  vector<vector<float>> mean;
296  mean.resize(width);
297  vector<float> high;
298  high.resize(width);
299  for (uint32_t icol=0; icol<width; ++icol)
300  {
301  high[icol]=0.;
302  mean[icol].resize(height);
303  for (uint32_t irow=0; irow<height; ++irow)
304  {
305  mean[icol][irow] = 0.;
306  for (uint32_t ilayer=0; ilayer<exposure; ++ilayer)
307  {
308  mean[icol][irow] += cube[ilayer*width*height+irow*width+icol];
309  }
310  mean[icol][irow] /= (float)exposure;
311  if (mean[icol][irow] > high[icol])
312  {
313  high[icol] = mean[icol][irow];
314  printf("[%d,%d] %f\r",icol,irow,high[icol]);
315  }
316  }
317  printf("\n");
318  }
319 
320  fclose(fp);
321  }
322 
324  printf("Read PT1000_AcquisitionMode %d\n",iretn);
325 
326  iretn = gige_readreg(handle,PT1000::PixelFormatReg);
327  printf("Read PT1000_PixelFormat %d\n",iretn);
328 
329  iretn = gige_readreg(handle,PT1000::SensorWidthReg);
330  printf("Read PT1000_SensorWidth %d\n",iretn);
331 
332  iretn = gige_readreg(handle,PT1000::SensorHeightReg);
333  printf("Read PT1000_SensorHeight %d\n",iretn);
334 
335  iretn = gige_readreg(handle,PT1000::WidthReg);
336  printf("Read PT1000_Width %d\n",iretn);
337 
338  iretn = gige_readreg(handle,PT1000::HeightReg);
339  printf("Read PT1000_Height %d\n",iretn);
340 
341  }
342  else
343  {
344 
345 
346  iretn = prosilica_config(handle, PROSILICA_PixelFormat_BayerRG12, binning, binning, 2448, 2050, 0, 0);
348  // printf("Read PROSILICA_StreamBytesPerSec %d\n",iretn);
349 
350 
351 
352 
353 
354 
355  iretn = gige_readreg(handle,PROSILICA_BinningXValue);
356  // printf("Read PROSILICA_BinningXValue %d\n",iretn);
357 
358  iretn = gige_readreg(handle,PROSILICA_BinningYValue);
359  // printf("Read PROSILICA_BinningYValue %d\n",iretn);
360 
361  iretn = gige_readreg(handle,PROSILICA_RegionX);
362  // printf("Read PROSILICA_RegionX %d\n",iretn);
363 
364  iretn = gige_readreg(handle,PROSILICA_RegionY);
365  // printf("Read PROSILICA_RegionY %d\n",iretn);
366 
367  iretn = gige_readreg(handle,PROSILICA_PixelFormat);
368  // printf("Read PROSILICA_PixelFormat %d\n",iretn);
369 
370  width = gige_readreg(handle,PROSILICA_MaxWidth);
371  printf("Read PROSILICA_MaxWidth %d\n",width);
372 
373  height = gige_readreg(handle,PROSILICA_MaxHeight);
374  printf("Read PROSILICA_MaxHeight %d\n",height);
375 
376  iretn = gige_readreg(handle,PROSILICA_SensorWidth);
377  // printf("Read PROSILICA_SensorWidth %d\n",iretn);
378 
379  used_imagewidth = gige_readreg(handle,PROSILICA_Width);
380  // printf("Read PROSILICA_Width %d\n",used_imagewidth);
381 
382  iretn = gige_readreg(handle,PROSILICA_SensorHeight);
383  // printf("Read PROSILICA_SensorHeight %d\n",iretn);
384 
385  used_imageheight = gige_readreg(handle,PROSILICA_Height);
386  // printf("Read PROSILICA_Height %d\n",used_imageheight);
387 
388  width = gige_readreg(handle,PROSILICA_SensorWidth);
389 
390  height = gige_readreg(handle,PROSILICA_SensorHeight);
391 
392  expbytes = width * height * 2;
393  tbytes = prosilica_image(handle, (exposure==0?PROSILICA_ExposureMode_Auto:PROSILICA_ExposureMode_AutoOff), exposure, gain, image, bsize);
394 
395  used_exposure = gige_readreg(handle,PROSILICA_ExposureValue);
396  printf("Read PROSILICA_ExposureValue %d\n",used_exposure);
397 
398  iretn = gige_readreg(handle,PROSILICA_AcquisitionMode);
399  // printf("Read PROSILICA_AcquisitionMode %d\n",iretn);
400 
401  iretn = gige_readreg(handle,PROSILICA_PayloadSize);
402  // printf("Read PROSILICA_PayloadSize %d\n",iretn);
403 
404  }
405 
406  if (expbytes == tbytes)
407  {
408  //sudo histogram
409  printf("\nRead: %d bytes ",tbytes);
410  uint32_t counts[4]={0,0,0,0};
411  uint16_t *array=(uint16_t *)image;
412  double mean=0.;
413  double std=0.;
414  double snr=0.;
415  int16_t min_value=4095.;
416  int16_t max_value=0.;
417 
418 
419 
420  for (int32_t i=0; i<tbytes/2; ++i)
421  {
422  mean += array[i];
423  std += (array[i]*array[i]);
424  ++counts[array[i]/1024];
425 
426  if (array[i]<min_value){
427  min_value=array[i];
428  }
429 
430  if (array[i]>max_value)
431  {
432  max_value=array[i];
433  }
434  }
435  std = sqrt((std-(mean*mean/(tbytes/2)))/(tbytes/2)-1);
436  mean /= (tbytes/2);
437  snr = (mean/std);
438 
439  printf("%u %u %u %u %f %f %f\n\n",counts[0],counts[1],counts[2],counts[3],mean,std, snr);
440  iretn = gige_readreg(handle,GIGE_REG_SCPS);
441  printf("Read GIGE_REG_STREAM_CHANNEL_PACKET_SIZE %d\n",iretn);
442 
443  iretn = gige_readreg(handle,GIGE_REG_SCDA);
444  printf("Read GIGE_STREAM_CHANNEL_DESTINATION_ADDRESS %x\n",iretn);
445 
446  // Separate and print out some of the image quality metrics:
447  printf("___________________________ \n\n");
448  printf("Mean Image Value = %f\n",mean);
449  printf("Standard Deviation = %f\n",std);
450  printf("SNR = %f\n",snr);
451  printf("Min/Max = %d, %d \n",min_value, max_value);
452 
453 
454  gige_close(handle);
455 
456  // write image:
457 
458  imagename = data_type_path("hiakasat", "temp", "gige", currentmjd(0.), extra, "imgdata");
459  fp = fopen(imagename.c_str(),"wb");
460  fwrite((void *)image,tbytes,1,fp);
461  fclose(fp);
462 
463  // write metadata file:
464 
465 
466  metafilename = data_type_path("hiakasat", "temp", "gige", currentmjd(0.), extra, "imgmeta");
467  FILE *f = fopen(metafilename.c_str(), "wb");
468  if (f == NULL)
469  {
470  printf("Error opening file!\n");
471  exit(1);
472  }
473 
474  /* print image metrics to file */
475  fprintf(f, "Image Name = %s\n",imagename.c_str());
476  fprintf(f, "IP Address = %s\n",ipaddress);
477  fprintf(f, "Acq Timestamp = %f\n",currentmjd(0.));
478  fprintf(f, "Acq Date = %s",c_time_string);
479  fprintf(f, "Exposure = %d\n",used_exposure);
480  fprintf(f, "Pixel Binning = %d\n",binning);
481  fprintf(f, "Image Width = %d\n",used_imagewidth);
482  fprintf(f, "Image Height = %d\n",used_imageheight);
483  fprintf(f, "Mean Image Value = %f\n",mean);
484  fprintf(f, "Standard Deviation = %f\n",std);
485  fprintf(f, "SNR = %f\n",snr);
486  fprintf(f, "Min/Max = %d, %d \n",min_value, max_value);
487 
488  fclose(f);
489  }
490  else
491  {
492  printf("Error: Expected %d bytes, Read %d bytes\n", expbytes, tbytes);
493  }
494 
495 
496  exit(0);
497 }
#define PROSILICA_PixelFormat_BayerRG12
Definition: gige_lib.h:191
int pt1000_image(gige_handle *handle, uint32_t frames, uint8_t *buffer, uint16_t bsize)
Take PT1000 image stream.
Definition: gige_lib.cpp:574
gige_acknowledge_mem cack_mem
Definition: gige_lib.h:349
uint8_t image[110000000]
Definition: gige_snap.cpp:38
#define A35_TEMPERATUREFPA
Definition: gige_lib.h:427
#define GIGE_REG_DEVICE_MODE
Definition: gige_lib.h:81
uint16_t bsize
Definition: netperf_listen.cpp:71
Definition: gige_lib.h:480
int i
Definition: rw_test.cpp:37
int prosilica_image(gige_handle *handle, uint16_t emode, uint32_t exposure, uint32_t gain, uint8_t *buffer, uint16_t bsize)
Take Prosilica image.
Definition: gige_lib.cpp:687
int a35_config(gige_handle *handle, uint32_t xsize, uint32_t ysize, uint32_t video_rate)
Configure a35 camera.
Definition: gige_lib.cpp:441
Definition: gige_lib.h:476
#define GIGE_REG_SCDA
Definition: gige_lib.h:143
int iretn
Definition: rw_test.cpp:37
#define PROSILICA_PayloadSize
Definition: gige_lib.h:196
#define PROSILICA_Width
Definition: gige_lib.h:192
vector< gige_acknowledge_ack > gige_discover()
Discover GIGE Camera.
Definition: gige_lib.cpp:332
#define PROSILICA_RegionX
Definition: gige_lib.h:194
#define PROSILICA_Height
Definition: gige_lib.h:193
#define PROSILICA_ExposureValue
Definition: gige_lib.h:220
string cosmos_error_string(int32_t cosmos_errno)
Definition: cosmos-errno.cpp:45
Definition: gige_lib.h:475
#define A35_PIXELFORMAT
Definition: gige_lib.h:420
Definition: gige_lib.h:482
char address[]
Definition: netperf_listen.cpp:69
#define A35_SENSORVIDEOSTANDARD_30HZ
Definition: gige_lib.h:447
string data_type_path(string node, string location, string agent, double mjd, string type)
Create data file path.
Definition: datalib.cpp:910
#define PROSILICA_BinningYValue
Definition: gige_lib.h:178
#define GIGE_REG_VERSION
Definition: gige_lib.h:80
int prosilica_config(gige_handle *handle, uint32_t format, uint32_t xbin, uint32_t ybin, uint32_t xsize, uint32_t ysize, uint32_t xoffset, uint32_t yoffset)
Configure Prosilica camera.
Definition: gige_lib.cpp:640
#define GIGE_MAX_MODEL_NAME
Definition: gige_lib.h:155
#define GIGE_REG_PRIMARY_APPLICATION_IP_ADDRESS
Definition: gige_lib.h:128
char * gige_value_to_address(uint32_t value)
IP Value to address.
Definition: gige_lib.cpp:418
#define PROSILICA_RegionY
Definition: gige_lib.h:195
uint32_t gige_readreg(gige_handle *handle, uint32_t address)
Read GIGE Register.
Definition: gige_lib.cpp:237
#define PROSILICA_MaxHeight
Definition: gige_lib.h:181
#define GIGE_REG_CCP
Definition: gige_lib.h:126
uint32_t gige_readmem(gige_handle *handle, uint32_t address, uint32_t size)
Read GIGE memory.
Definition: gige_lib.cpp:284
gige_handle * handle
Definition: kpc9612p_recv.cpp:33
#define PROSILICA_ExposureMode_AutoOff
Do not automatically adjust exposure.
Definition: gige_lib.h:213
#define GIGE_REG_GVCP_HEARTBEAT_TIMEOUT
Definition: gige_lib.h:110
#define PROSILICA_AcquisitionMode
Definition: gige_lib.h:201
uint8_t data[600-12]
Definition: gige_lib.h:278
double currentmjd(double offset)
Current UTC in Modified Julian Days.
Definition: timelib.cpp:65
int pt1000_config(gige_handle *handle, uint32_t xsize, uint32_t ysize)
Configure pt1000 camera.
Definition: gige_lib.cpp:541
gige_handle * gige_open(char address[18], uint8_t privilege, uint32_t heartbeat_msec, uint32_t socket_usec, uint32_t streambps)
Connect to camera.
Definition: gige_lib.cpp:64
Definition: gige_lib.h:477
int a35_image(gige_handle *handle, uint32_t frames, uint8_t *buffer, uint16_t bsize)
Take A35 image stream.
Definition: gige_lib.cpp:479
Definition: gige_lib.h:474
#define A35_TEMPERATURE
Definition: gige_lib.h:426
#define GIGE_REG_SCPS
Definition: gige_lib.h:139
FILE * fp
Definition: rw_test.cpp:38
#define PROSILICA_ExposureMode_Auto
Adjust exposure on each frame.
Definition: gige_lib.h:217
#define GIGE_REG_DEVICE_MAC_HIGH
Definition: gige_lib.h:82
Definition: gige_lib.h:336
#define PROSILICA_SensorWidth
Definition: gige_lib.h:173
#define EXIT_FAILURE
Definition: cdjpeg.h:169
#define PROSILICA_PixelFormat
Definition: gige_lib.h:182
#define PROSILICA_StreamBytesPerSec
Definition: gige_lib.h:179
#define GIGE_REG_DEVICE_MAC_LOW
Definition: gige_lib.h:83
#define PROSILICA_BinningXValue
Definition: gige_lib.h:176
#define PROSILICA_MaxWidth
Definition: gige_lib.h:180
#define PROSILICA_SensorHeight
Definition: gige_lib.h:174
#define GIGE_REG_MODEL_NAME
Definition: gige_lib.h:90
uint16_t bestsize
Best packet size.
Definition: gige_lib.h:359
void gige_close(gige_handle *handle)
Close GigE Camera.
Definition: gige_lib.cpp:177

Variable Documentation

uint8_t image[110000000]