Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vp1394CMUGrabber.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Firewire cameras video capture based on CMU 1394 Digital Camera SDK.
32  *
33  * Authors:
34  * Lucas Lopes Lemos FEMTO-ST, AS2M departement, Besancon
35  * Guillaume Laurent FEMTO-ST, AS2M departement, Besancon
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
40 #include <visp3/core/vpConfig.h>
41 
42 #ifdef VISP_HAVE_CMU1394
43 
44 #include <iostream>
45 
46 #include <visp3/core/vpImageConvert.h>
47 #include <visp3/sensor/vp1394CMUGrabber.h>
48 
53 {
54  // public members
55  init = false;
56 
57  // protected members
58  width = height = -1;
59 
60  // private members
61  camera = new C1394Camera;
62  index = 0; // If a camera was not selected the first one (index = 0) will be used
63  _format = _mode = _fps = -1;
64  _modeauto=true;
65 }
66 
71 {
72  close();
73  // delete camera instance
74  if (camera) {
75  delete camera;
76  camera = NULL;
77  }
78 }
79 
84 void
86 {
87  int camerror;
88 
89  index = cam_id ;
90 
91  camerror = camera->SelectCamera(index);
92  if ( camerror!= CAM_SUCCESS)
93  {
94  switch (camerror)
95  {
96  case CAM_ERROR_PARAM_OUT_OF_RANGE:
97  vpERROR_TRACE("vp1394CMUGrabber error: Found no camera number %i",index);
98  throw (vpFrameGrabberException(vpFrameGrabberException::initializationError,"The required camera is not present") );
99  break;
100  case CAM_ERROR_BUSY:
101  vpERROR_TRACE("vp1394CMUGrabber error: The camera %i is busy",index);
102  throw (vpFrameGrabberException(vpFrameGrabberException::initializationError,"The required camera is in use by other application") );
103  break;
104  case CAM_ERROR:
105  vpERROR_TRACE("vp1394CMUGrabber error: General I/O error when selecting camera number %i",index);
106  throw (vpFrameGrabberException(vpFrameGrabberException::initializationError,"Resolve camera can not be used") );
107  break;
108  }
109  close();
110  }
111 } // end camera select
112 
116 void
117 vp1394CMUGrabber::initCamera()
118 {
119  if (init == false)
120  {
121  int camerror;
122 
123  if (camera->CheckLink() != CAM_SUCCESS)
124  {
125  vpERROR_TRACE("C1394Camera error: Found no cameras on the 1394 bus");
126  throw (vpFrameGrabberException(vpFrameGrabberException::initializationError,"The is no detected camera") );
127  }
128 
129  camerror = camera->InitCamera();
130  if ( camerror != CAM_SUCCESS )
131  {
132  switch (camerror)
133  {
134  case CAM_ERROR_NOT_INITIALIZED:
135  vpERROR_TRACE("vp1394CMUGrabber error: No camera selected",index);
136  throw (vpFrameGrabberException(vpFrameGrabberException::initializationError,"The is no selected camera") );
137  break;
138  case CAM_ERROR_BUSY:
139  vpERROR_TRACE("vp1394CMUGrabber error: The camera %i is busy",index);
140  throw (vpFrameGrabberException(vpFrameGrabberException::initializationError,"The required camera is in use by other application") );
141  break;
142  case CAM_ERROR:
143  vpERROR_TRACE("vp1394CMUGrabber error: General I/O error when selecting camera number %i",index);
144  throw (vpFrameGrabberException(vpFrameGrabberException::initializationError,"Resolve camera can not be used") );
145  break;
146  }
147  close();
148  }
149 
150  if (camera->Has1394b())
151  camera->Set1394b(TRUE);
152 
153  // Get the current settings
154  _format = camera->GetVideoFormat();
155  _mode = camera->GetVideoMode();
156  _color = getVideoColorCoding();
157  //std::cout << "format: " << _format << std::endl;
158  //std::cout << "mode: " << _mode << std::endl;
159  //std::cout << "color coding: " << _color << std::endl;
160 
161  // Set trigger off
162  camera->GetCameraControlTrigger()->SetOnOff(false);
163 
164  unsigned long w, h;
165  camera->GetVideoFrameDimensions(&w, &h);
166  this->width = w;
167  this->height = h;
168 
169  // start acquisition
170  if (camera->StartImageAcquisition() != CAM_SUCCESS)
171  {
172  close();
173  vpERROR_TRACE("vp1394CMUGrabber error: Can't start image acquisition from IEEE 1394 camera number %i",index);
175  "Error while starting image acquisition") );
176  }
177 
178  init = true;
179  }
180 
181 } // end camera init
182 
183 
188 void
190 {
191  initCamera();
192  I.resize(this->height, this->width);
193 }
194 
199 void
201 {
202  initCamera();
203  I.resize(this->height, this->width);
204 }
205 
213 void
215 {
216  // get image data
217  unsigned long length;
218  unsigned char *rawdata = NULL ;
219  int dropped;
220  unsigned int size;
221 
222  open(I);
223 
224  camera->AcquireImageEx(TRUE,&dropped);
225  rawdata = camera->GetRawData(&length);
226 
227  size = I.getSize();
228  switch(_color) {
230  memcpy(I.bitmap, (unsigned char *) rawdata, size);
231  break;
233  vpImageConvert::MONO16ToGrey(rawdata, I.bitmap, size);
234  break;
235 
237  vpImageConvert::YUV411ToGrey(rawdata, I.bitmap, size);
238  break;
239 
241  vpImageConvert::YUV422ToGrey(rawdata, I.bitmap, size);
242  break;
243 
245  vpImageConvert::YUV444ToGrey(rawdata, I.bitmap, size);
246  break;
247 
249  vpImageConvert::RGBToGrey(rawdata, I.bitmap, size);
250  break;
251 
252  default:
253  close();
254  vpERROR_TRACE("Format conversion not implemented. Acquisition failed.");
256  "Format conversion not implemented. "
257  "Acquisition failed.") );
258  break;
259  };
260 
261  //unsigned short depth = 0;
262  //camera->GetVideoDataDepth(&depth);
263  //std::cout << "depth: " << depth << " computed: " << (float)(length/(I.getHeight() * I.getWidth())) << std::endl;
264 
265 
266  //memcpy(I.bitmap,rawdata,length);
267 
268 }
269 
278 void
280 {
281  // get image data
282  unsigned long length;
283  unsigned char *rawdata = NULL;
284  int dropped;
285  unsigned int size;
286 
287  open(I);
288 
289  camera->AcquireImageEx(TRUE,&dropped);
290  rawdata = camera->GetRawData(&length);
291  size = I.getWidth() * I.getHeight();
292 
293  switch (_color) {
295  vpImageConvert::GreyToRGBa(rawdata, (unsigned char *)I.bitmap, size);
296  break;
297 
299  vpImageConvert::MONO16ToRGBa(rawdata, (unsigned char *)I.bitmap, size);
300  break;
301 
303  vpImageConvert::YUV411ToRGBa(rawdata, (unsigned char *)I.bitmap, size);
304  break;
305 
307  vpImageConvert::YUV422ToRGBa(rawdata, (unsigned char *)I.bitmap, size);
308  break;
309 
311  vpImageConvert::YUV444ToRGBa(rawdata, (unsigned char *)I.bitmap, size);
312  break;
313 
315  size = length / 3;
316  vpImageConvert::RGBToRGBa(rawdata, (unsigned char *)I.bitmap, size);
317  break;
318 
319  default:
320  close();
321  vpERROR_TRACE("Format conversion not implemented. Acquisition failed.");
323  "Format conversion not implemented. "
324  "Acquisition failed.") );
325  break;
326  };
327 }
328 
332 void
334 {
335  // stop acquisition
336  if (camera->IsAcquiring()) {
337  // stop acquisition
338  if (camera->StopImageAcquisition() != CAM_SUCCESS)
339  {
340  close();
341  vpERROR_TRACE("vp1394CMUGrabber error: Can't stop image acquisition from IEEE 1394 camera number %i",index);
343  "Error while stopping image acquisition") );
344  }
345  }
346 
347  init = false;
348 }
349 
354 void
355 vp1394CMUGrabber::setControl(unsigned short gain, unsigned short shutter)
356 {
357  setShutter(shutter);
358  setGain(gain);
359 }
360 
364 int
366 {
367  int n_cam = camera->RefreshCameraList();
368 
369  return n_cam;
370 }
371 
377 void vp1394CMUGrabber::getGainMinMax(unsigned short &min, unsigned short &max)
378 {
379  initCamera();
380 
381  C1394CameraControl *Control;
382  Control = camera->GetCameraControl(FEATURE_GAIN);
383  Control->Inquire();
384  Control->GetRange(&min, &max);
385 }
392 {
393  initCamera();
394  camera->GetCameraControl(FEATURE_GAIN)->SetAutoMode(true);
395 }
401 void vp1394CMUGrabber::setGain(unsigned short gain)
402 {
403  initCamera();
404  _gain = gain;
405 
406  unsigned short min,max;
407  C1394CameraControl *Control;
408 
409  Control = camera->GetCameraControl(FEATURE_GAIN);
410  Control->Inquire();
411  Control->GetRange(&min,&max);
412 
413  if (_gain < min)
414  {
415  _gain = min;
416  std::cout << "vp1394CMUGrabber warning: Desired gain register value of IEEE 1394 camera number " << index << " can't be less than " << _gain << std::endl;
417  }
418  else if (_gain > max)
419  {
420  _gain = max;
421  std::cout << "vp1394CMUGrabber warning: Desired gain register value of IEEE 1394 camera number " << index << " can't be greater than " << _gain << std::endl;
422  }
423 
424  Control->SetAutoMode(false);
425  if(Control->SetValue(_gain) != CAM_SUCCESS)
426  {
427  std::cout << "vp1394CMUGrabber warning: Can't set gain register value of IEEE 1394 camera number " << index << std::endl;
428  }
429 }
430 
436 void vp1394CMUGrabber::getShutterMinMax(unsigned short &min, unsigned short &max)
437 {
438  initCamera();
439 
440  C1394CameraControl *Control;
441  Control = camera->GetCameraControl(FEATURE_SHUTTER);
442  Control->Inquire();
443  Control->GetRange(&min, &max);
444 }
445 
452 {
453  initCamera();
454  camera->GetCameraControl(FEATURE_SHUTTER)->SetAutoMode(true);
455 }
461 void vp1394CMUGrabber::setShutter(unsigned short shutter)
462 {
463  initCamera();
464 
465  _shutter = shutter;
466 
467  unsigned short min,max;
468  C1394CameraControl *Control;
469 
470  Control = camera->GetCameraControl(FEATURE_SHUTTER);
471  Control->Inquire();
472  Control->GetRange(&min,&max);
473 
474  if (_shutter < min)
475  {
476  _shutter = min;
477  std::cout << "vp1394CMUGrabber warning: Desired exposure time register value of IEEE 1394 camera number " << index << " can't be less than " << _shutter << std::endl;
478  }
479  else if (_shutter > max)
480  {
481  _shutter = max;
482  std::cout << "vp1394CMUGrabber warning: Desired exposure time register value of IEEE 1394 camera number " << index << " can't be greater than " << _shutter << std::endl;
483  }
484  Control->SetAutoMode(false);
485  if(Control->SetValue(_shutter) != CAM_SUCCESS)
486  {
487  std::cout << "vp1394CMUGrabber warning: Can't set exposure time register value of IEEE 1394 camera number " << index << std::endl;
488  }
489 }
490 
494 void
496 {
497  if( camera->GetNumberCameras() > cam_id )
498  {
499  char buf[512];
500  camera->GetNodeDescription(cam_id,buf,512);
501  std::cout << "Camera " << cam_id << ": " << buf << std::endl ;
502 
503  }
504  else {
505  std::cout << "Camera " << cam_id << ": camera not found" << std::endl ;
506  }
507 }
508 
509 
513 void
515 {
516  char vendor[256] , model[256] , buf[256];
517  LARGE_INTEGER ID;
518 
519  camera->GetCameraName(model,sizeof(model));
520  camera->GetCameraVendor(vendor,sizeof(vendor));
521  camera->GetCameraUniqueID(&ID);
522 
523  std::cout << "Vendor: " << vendor << std::endl;
524  std::cout << "Model: " << model << std::endl;
525 
526  sprintf(buf,"%08X%08X",ID.HighPart,ID.LowPart);
527  std::cout << "UniqueID: " << buf << std::endl;
528 
529 }
530 
531 
570 void
571 vp1394CMUGrabber::setVideoMode( unsigned long format, unsigned long mode )
572 {
573  initCamera();
574 
575  _format = format ;
576  _mode = mode ;
577 
578  // Set format and mode
579  if ((_format != -1) && (_mode != -1))
580  {
581  if (!camera->HasVideoMode(_format, _mode))
582  {
583  close();
584  vpERROR_TRACE("vp1394CMUGrabber error: The image format is not supported by the IEEE 1394 camera number %i",index);
585  throw (vpFrameGrabberException(vpFrameGrabberException::settingError,"Video mode not supported") );
586  }
587 
588  if (camera->IsAcquiring()) {
589  // stop acquisition
590  if (camera->StopImageAcquisition() != CAM_SUCCESS)
591  {
592  close();
593  vpERROR_TRACE("vp1394CMUGrabber error: Can't stop image acquisition from IEEE 1394 camera number %i",index);
595  "Error while stopping image acquisition") );
596  }
597  }
598 
599  if (camera->SetVideoFormat(_format) != CAM_SUCCESS)
600  {
601  close();
602  vpERROR_TRACE("vp1394CMUGrabber error: Can't set video format of IEEE 1394 camera number %i",index);
603  throw (vpFrameGrabberException(vpFrameGrabberException::settingError,"Can't set video format") );
604  }
605 
606  if (camera->SetVideoMode(_mode) != CAM_SUCCESS)
607  {
608  close();
609  vpERROR_TRACE("vp1394CMUGrabber error: Can't set video mode of IEEE 1394 camera number %i",index);
610  throw (vpFrameGrabberException(vpFrameGrabberException::settingError,"Can't set video mode") );
611  }
612 
613  // start acquisition
614  if (camera->StartImageAcquisition() != CAM_SUCCESS)
615  {
616  close();
617  vpERROR_TRACE("vp1394CMUGrabber error: Can't start image acquisition from IEEE 1394 camera number %i",index);
619  "Error while starting image acquisition") );
620  }
621 
622  // Update Image dimension
623  unsigned long w, h;
624  camera->GetVideoFrameDimensions(&w, &h);
625  this->width = w;
626  this->height = h;
627 
628  // Update the color coding
629  _color = getVideoColorCoding();
630  }
631 }
632 
654 void
656 {
657  initCamera();
658 
659  _fps = fps;
660 
661  // Set fps
662  if (_fps!=-1)
663  {
664  if (!camera->HasVideoFrameRate(_format,_mode,_fps))
665  {
666  close();
667  vpERROR_TRACE("vp1394CMUGrabber error: The frame rate is not supported by the IEEE 1394 camera number %i for the selected image format",index);
668  throw (vpFrameGrabberException(vpFrameGrabberException::settingError,"The frame rate is not supported") );
669  }
670 
671  if (camera->IsAcquiring()) {
672  // stop acquisition
673  if (camera->StopImageAcquisition() != CAM_SUCCESS)
674  {
675  close();
676  vpERROR_TRACE("vp1394CMUGrabber error: Can't stop image acquisition from IEEE 1394 camera number %i",index);
678  "Error while stopping image acquisition") );
679  }
680  }
681  if (camera->SetVideoFrameRate(_fps) != CAM_SUCCESS)
682  {
683  close();
684  vpERROR_TRACE("vp1394CMUGrabber error: Can't set video frame rate of IEEE 1394 camera number %i",index);
685  throw (vpFrameGrabberException(vpFrameGrabberException::settingError,"Can't set video frame rate") );
686  }
687  // start acquisition
688  if (camera->StartImageAcquisition() != CAM_SUCCESS)
689  {
690  close();
691  vpERROR_TRACE("vp1394CMUGrabber error: Can't start image acquisition from IEEE 1394 camera number %i",index);
693  "Error while starting image acquisition") );
694  }
695 
696  }
697 }
719 int
721 {
722  initCamera();
723  int fps = camera->GetVideoFrameRate();
724  return fps;
725 }
726 
744 {
745  this->acquire(I);
746  return *this;
747 }
748 
766 {
767  this->acquire(I);
768  return *this;
769 }
770 
771 #elif !defined(VISP_BUILD_SHARED_LIBS)
772 // Work arround to avoid warning: libvisp_sensor.a(vp1394CMUGrabber.cpp.o) has no symbols
773 void dummy_vp1394CMUGrabber() {};
774 #endif
775 
int getNumberOfConnectedCameras() const
void setVideoMode(unsigned long format, unsigned long mode)
static void RGBToGrey(unsigned char *rgb, unsigned char *grey, unsigned int size)
void setGain(unsigned short gain)
unsigned int getWidth() const
Definition: vpImage.h:226
void open(vpImage< unsigned char > &I)
static void RGBToRGBa(unsigned char *rgb, unsigned char *rgba, unsigned int size)
Type * bitmap
points toward the bitmap
Definition: vpImage.h:134
static void MONO16ToGrey(unsigned char *grey16, unsigned char *grey, unsigned int size)
vpColorCodingType getVideoColorCoding() const
Get the video color coding format.
#define vpERROR_TRACE
Definition: vpDebug.h:391
static void GreyToRGBa(unsigned char *grey, unsigned char *rgba, unsigned int size)
static void YUV422ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size)
Error that can be emited by the vpFrameGrabber class and its derivates.
void setShutter(unsigned short shutter)
void displayCameraDescription(int cam_id)
static void YUV444ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size)
static void YUV422ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size)
virtual ~vp1394CMUGrabber()
void getGainMinMax(unsigned short &min, unsigned short &max)
Firewire cameras video capture based on CMU 1394 Digital Camera SDK.
void setFramerate(unsigned long fps)
unsigned int getSize() const
Definition: vpImage.h:212
void acquire(vpImage< unsigned char > &I)
unsigned int height
Number of rows in the image.
static void MONO16ToRGBa(unsigned char *grey16, unsigned char *rgba, unsigned int size)
vp1394CMUGrabber & operator>>(vpImage< unsigned char > &I)
void resize(const unsigned int h, const unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:903
bool init
Set to true if the frame grabber has been initialized.
static void YUV444ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size)
void getShutterMinMax(unsigned short &min, unsigned short &max)
static void YUV411ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size)
unsigned int getHeight() const
Definition: vpImage.h:175
static void YUV411ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size)
unsigned int width
Number of columns in the image.
void setControl(unsigned short gain, unsigned short shutter)
void selectCamera(int cam_id)