CameraDriverPtGrey.cc
Go to the documentation of this file.00001 #ifdef __linux__
00002 #ifdef HAVE_FLYCAP
00003
00004 #include "CameraDriverPtGrey.h"
00005 #include "Shared/debuget.h"
00006 #include "Shared/MarkScope.h"
00007
00008 using namespace std;
00009
00010 const std::string CameraDriverPtGrey::autoRegisterCameraDriverPtGrey = DeviceDriver::getRegistry().registerType<CameraDriverPtGrey>("CameraPtGrey");
00011
00012 bool CameraDriverPtGrey::advance()
00013 {
00014 MarkScope l(lock);
00015
00016
00017
00018 if (!cam.IsConnected()) {
00019 openCam();
00020
00021 if (!cam.IsConnected()) {
00022 return false;
00023 }
00024 }
00025
00026 FlyCapture2::Error error;
00027 FlyCapture2::Image rawImage;
00028
00029
00030 error = cam.RetrieveBuffer( &rawImage );
00031 if (error != FlyCapture2::PGRERROR_OK) {
00032 cout << "CAMERA DRIVER: error capturing image: " << error.GetDescription() << endl;
00033 return false;
00034 }
00035
00036 if (!convertImage(rawImage)) {
00037 return false;
00038 }
00039
00040 ++frameCount;
00041 return true;
00042 }
00043
00044 void CameraDriverPtGrey::registerSource() {
00045 openCam();
00046 index.addPrimitiveListener(this);
00047 }
00048
00049 void CameraDriverPtGrey::deregisterSource() {
00050 index.removePrimitiveListener(this);
00051 closeCam();
00052 }
00053
00054 void CameraDriverPtGrey::doUnfreeze() {
00055 thread.start();
00056 }
00057
00058 void CameraDriverPtGrey::doFreeze() {
00059 if(thread.isStarted())
00060 thread.stop().join();
00061 }
00062
00063 void CameraDriverPtGrey::threadrun() {
00064 while(advance())
00065 Thread::testCurrentCancel();
00066 }
00067
00068
00069 void CameraDriverPtGrey::plistValueChanged(const plist::PrimitiveBase& pl)
00070 {
00071 if (&pl == &index) {
00072 MarkScope l(lock);
00073 openCam();
00074 }
00075 }
00076
00077 void CameraDriverPtGrey::openCam()
00078 {
00079 if (cam.IsConnected())
00080 closeCam();
00081
00082 FlyCapture2::BusManager busMgr;
00083 FlyCapture2::Error error;
00084
00085 unsigned int numCameras;
00086 error = busMgr.GetNumOfCameras(&numCameras);
00087 if (error != FlyCapture2::PGRERROR_OK) {
00088 cout << "CAMERA DRIVER: error detecting number of cameras: " << error.GetDescription() << endl;
00089 return;
00090 }
00091
00092 cout << "CAMERA DRIVER: number of cameras detected: " << numCameras << endl;
00093
00094 if (index >= numCameras) {
00095 cout << "CAMERA DRIVER: index out of bounds: " << index << endl;
00096 return;
00097 }
00098
00099 FlyCapture2::PGRGuid guid;
00100 error = busMgr.GetCameraFromIndex(index, &guid);
00101 if (error != FlyCapture2::PGRERROR_OK) {
00102 cout << "CAMERA DRIVER: error getting camera GUID: " << error.GetDescription() << endl;
00103 return;
00104 }
00105
00106
00107 error = cam.Connect(&guid);
00108 if (error != FlyCapture2::PGRERROR_OK) {
00109 cout << "CAMERA DRIVER: error connecting camera: " << error.GetDescription() << endl;
00110 cam.Disconnect();
00111 return;
00112 }
00113
00114
00115 FlyCapture2::CameraInfo camInfo;
00116 error = cam.GetCameraInfo(&camInfo);
00117 if (error != FlyCapture2::PGRERROR_OK) {
00118 cout << "CAMERA DRIVER: error getting camera info: " << error.GetDescription() << endl;
00119 }
00120 else {
00121 printf(
00122 "\nCAMERA DRIVER: Using camera:\n"
00123 "Serial number - %u\n"
00124 "Camera model - %s\n"
00125 "Camera vendor - %s\n"
00126 "Sensor - %s\n"
00127 "Resolution - %s\n"
00128 "Firmware version - %s\n"
00129 "Firmware build time - %s\n\n",
00130 camInfo.serialNumber,
00131 camInfo.modelName,
00132 camInfo.vendorName,
00133 camInfo.sensorInfo,
00134 camInfo.sensorResolution,
00135 camInfo.firmwareVersion,
00136 camInfo.firmwareBuildTime );
00137 }
00138
00139
00140 error = cam.SetVideoModeAndFrameRate(FlyCapture2::VIDEOMODE_640x480Y8, FlyCapture2::FRAMERATE_30);
00141 if (error != FlyCapture2::PGRERROR_OK) {
00142 cout << "CAMERA DRIVER: error setting capture mode: " << error.GetDescription() << endl;
00143 cam.Disconnect();
00144 return;
00145 }
00146
00147
00148 error = cam.StartCapture();
00149 if (error != FlyCapture2::PGRERROR_OK) {
00150 cout << "CAMERA DRIVER: error starting capture: " << error.GetDescription() << endl;
00151 cam.Disconnect();
00152 return;
00153 }
00154 }
00155
00156 void CameraDriverPtGrey::closeCam()
00157 {
00158 FlyCapture2::Error error;
00159
00160 error = cam.StopCapture();
00161 if (error != FlyCapture2::PGRERROR_OK) {
00162 cout << "CAMERA DRIVER: error stopping capture: " << error.GetDescription() << endl;
00163 }
00164
00165 error = cam.Disconnect();
00166 if (error != FlyCapture2::PGRERROR_OK) {
00167 cout << "CAMERA DRIVER: error disconnecting camera: " << error.GetDescription() << endl;
00168 }
00169 }
00170
00171 bool CameraDriverPtGrey::convertImage(FlyCapture2::Image & rawImage)
00172 {
00173 FlyCapture2::Error error;
00174
00175
00176 FlyCapture2::Image convertedImage;
00177
00178
00179 error = rawImage.Convert( FlyCapture2::PIXEL_FORMAT_RGB8, &convertedImage );
00180 if (error != FlyCapture2::PGRERROR_OK) {
00181 cout << "CAMERA DRIVER: error converting image: " << error.GetDescription() << endl;
00182 return false;
00183 }
00184
00185 unsigned int layer = 0;
00186 unsigned int components = 3;
00187 unsigned int width = convertedImage.GetCols();
00188 unsigned int height = convertedImage.GetRows();
00189
00190 ssize_t reqSize = sizeof(ImageHeader) + width * height * components;
00191 RCRegion* region = getUnusedRegion(reqSize,0);
00192 unsigned char * buf = reinterpret_cast<unsigned char*>(region->Base());
00193 new (buf) ImageHeader(0, layer, width, height, components, frameCount, timestamp, nextName());
00194
00195 unsigned char * data = convertedImage.GetData();
00196 unsigned int dataSize = convertedImage.GetDataSize();
00197
00198
00199
00200
00201
00202
00203 unsigned char * dst = buf + sizeof(ImageHeader);
00204 unsigned char * src = data;
00205
00206 while (src != (data + dataSize)) {
00207 int r, g, b;
00208
00209 r=*src;
00210 ++src;
00211
00212 g=*src;
00213 ++src;
00214
00215 b=*src;
00216 ++src;
00217
00218
00219 *dst++ = ((66*r + 129*g + 25*b + 128) >> 8) + 16;
00220 *dst++ = ((-38*r - 74*g + 112*b + 128) >> 8) + 128;
00221 *dst++ = ((112*r - 94*g - 18*b + 128) >> 8) + 128;
00222 }
00223
00224 setImage(region);
00225 return true;
00226 }
00227
00228
00229
00230
00231
00232
00233
00234 #endif // ifdef HAVE_FLYCAP
00235
00236 #endif // ifdef __linux__