Rev 121 | Rev 134 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#include "CameraPointGrey.h"
#include <cstring>
//#include <sys/ioctl.h>
//#include <linux/usbdevice_fs.h>
void PrintError(FlyCapture2::Error error){
error.PrintErrorTrace();
}
vector<CameraInfo> CameraPointGrey::getCameraList(){
FlyCapture2::Error error;
FlyCapture2::BusManager busManager;
unsigned int numCameras;
error = busManager.GetNumOfCameras(&numCameras);
vector<CameraInfo> ret;
if (error != FlyCapture2::PGRERROR_OK){
PrintError(error);
return ret;
}
for (unsigned int i=0; i < numCameras; i++){
FlyCapture2::PGRGuid guid;
error = busManager.GetCameraFromIndex(i, &guid);
if (error != FlyCapture2::PGRERROR_OK)
PrintError(error);
// Connect to camera
FlyCapture2::Camera cam;
error = cam.Connect(&guid);
if (error != FlyCapture2::PGRERROR_OK)
PrintError( error );
// Get camera information
FlyCapture2::CameraInfo camInfo;
error = cam.GetCameraInfo(&camInfo);
if (error != FlyCapture2::PGRERROR_OK)
PrintError( error );
CameraInfo camera;
camera.vendor = "Point Grey";
camera.model = camInfo.modelName;
camera.busID = camInfo.nodeNumber;
camera.serialNumber = camInfo.serialNumber;
ret.push_back(camera);
}
return ret;
}
CameraPointGrey::CameraPointGrey(unsigned int camNum, CameraTriggerMode triggerMode) : Camera(triggerMode){
// // Reset USB bus
// std::cout << "Resetting USB device %s\n";
// int rc = ioctl(fd, USBDEVFS_RESET, 0);
// if (rc < 0)
// perror("Error in ioctl");
// else
// printf("Reset successful\n");
FlyCapture2::Error error;
// Connect to camera
FlyCapture2::BusManager busManager;
FlyCapture2::PGRGuid camGuid;
busManager.GetCameraFromIndex(camNum, &camGuid);
error = cam.Connect(&camGuid);
if (error != FlyCapture2::PGRERROR_OK)
PrintError(error);
// // Configure DCAM video mode and frame rate
// FlyCapture2::VideoMode videoMode = FlyCapture2::VIDEOMODE_1280x960Y8;
// FlyCapture2::FrameRate frameRate = FlyCapture2::FRAMERATE_7_5;
// error = cam.SetVideoModeAndFrameRate(videoMode, frameRate);
// if (error != FlyCapture2::PGRERROR_OK)
// PrintError(error);
// Configure Format7 mode
FlyCapture2::Format7ImageSettings format7Settings;
format7Settings.mode = FlyCapture2::MODE_0;
format7Settings.pixelFormat = FlyCapture2::PIXEL_FORMAT_RAW8;
format7Settings.width = 3376;
format7Settings.height = 2704;
format7Settings.offsetX = 0;
format7Settings.offsetY = 0;
// Validate and set mode
FlyCapture2::Format7PacketInfo packetInfo;
bool valid;
error = cam.ValidateFormat7Settings(&format7Settings, &valid, &packetInfo);
if (error != FlyCapture2::PGRERROR_OK)
PrintError(error);
// packetsize configures maximum frame rate
error = cam.SetFormat7Configuration(&format7Settings, packetInfo.recommendedBytesPerPacket);
if (error != FlyCapture2::PGRERROR_OK)
PrintError(error);
// Configure general
FlyCapture2::FC2Config config;
config.numBuffers = 5;
config.grabTimeout = 1000; // retrieveBuffer() timeout in ms
config.grabMode = FlyCapture2::DROP_FRAMES;
config.isochBusSpeed = FlyCapture2::BUSSPEED_S_FASTEST;
config.highPerformanceRetrieveBuffer = true;
//config.asyncBusSpeed = FlyCapture2::BUSSPEED_S_FASTEST;
error = cam.SetConfiguration(&config);
if (error != FlyCapture2::PGRERROR_OK)
PrintError(error);
// Get the camera information
FlyCapture2::CameraInfo camInfo;
error = cam.GetCameraInfo(&camInfo);
if (error != FlyCapture2::PGRERROR_OK)
PrintError(error);
std::cout << camInfo.vendorName << " " << camInfo.modelName << " " << camInfo.serialNumber << std::endl;
// Turn off auto settings
FlyCapture2::Property property;
property.type = FlyCapture2::AUTO_EXPOSURE;
property.onOff = false;
error = cam.SetProperty(&property);
if (error != FlyCapture2::PGRERROR_OK)
PrintError(error);
property.type = FlyCapture2::GAMMA;
property.onOff = true;
property.absControl = true;
property.absValue = 1.0;
error = cam.SetProperty(&property);
if (error != FlyCapture2::PGRERROR_OK)
PrintError(error);
// Set frame rate to 6
property.type = FlyCapture2::FRAME_RATE;
property.onOff = true;
property.absControl = true;
property.autoManualMode = false;
property.absValue = 6.0;
error = cam.SetProperty(&property);
if (error != FlyCapture2::PGRERROR_OK)
PrintError(error);
// Set white balance to match LED projector light
property.type = FlyCapture2::WHITE_BALANCE;
property.onOff = true;
property.absControl = false;
property.autoManualMode = false;
property.valueA = 666; //red
property.valueB = 777; //blue
error = cam.SetProperty(&property);
if (error != FlyCapture2::PGRERROR_OK)
PrintError(error);
// Set reasonable default settings
CameraSettings settings;
//settings.shutter = 8.33;
settings.shutter = 66.66;
settings.gain = 0.0;
this->setCameraSettings(settings);
return;
}
CameraSettings CameraPointGrey::getCameraSettings(){
FlyCapture2::Property property;
// Get settings:
CameraSettings settings;
property.type = FlyCapture2::SHUTTER;
cam.GetProperty(&property);
settings.shutter = property.absValue;
property.type = FlyCapture2::GAIN;
cam.GetProperty(&property);
settings.gain = property.absValue;
return settings;
}
void CameraPointGrey::setCameraSettings(CameraSettings settings){
FlyCapture2::Property property;
property.onOff = true;
property.absControl = true;
property.type = FlyCapture2::SHUTTER;
property.absValue = settings.shutter;
cam.SetProperty(&property);
property.type = FlyCapture2::GAIN;
property.absValue = settings.gain;
cam.SetProperty(&property);
}
void CameraPointGrey::startCapture(){
FlyCapture2::Error error;
CameraSettings settings = this->getCameraSettings();
std::cout << "\tShutter: " << settings.shutter << "ms" << std::endl;
std::cout << "\tGain: " << settings.gain << "dB" << std::endl;
if(triggerMode == triggerModeHardware){
// Configure for hardware trigger
FlyCapture2::TriggerMode triggerMode;
triggerMode.onOff = true;
triggerMode.polarity = 0;
triggerMode.source = 0;
triggerMode.mode = 14;
error = cam.SetTriggerMode(&triggerMode);
if (error != FlyCapture2::PGRERROR_OK)
PrintError(error);
} else if(triggerMode == triggerModeSoftware){
// Configure software trigger
FlyCapture2::TriggerMode triggerMode;
triggerMode.onOff = true;
triggerMode.polarity = 0;
triggerMode.source = 7; // software
triggerMode.mode = 0;
error = cam.SetTriggerMode(&triggerMode);
if (error != FlyCapture2::PGRERROR_OK)
PrintError(error);
}
// Set the trigger timeout to 1000 ms
FlyCapture2::FC2Config config;
config.grabTimeout = 1000;
error = cam.SetConfiguration(&config);
if (error != FlyCapture2::PGRERROR_OK)
PrintError(error);
error = cam.StartCapture();
if (error != FlyCapture2::PGRERROR_OK)
PrintError(error);
capturing = true;
}
void CameraPointGrey::stopCapture(){
FlyCapture2::Error error = cam.StopCapture();
if (error != FlyCapture2::PGRERROR_OK)
PrintError(error);
capturing = false;
}
void CameraPointGrey::trigger(){
FlyCapture2::Error error;
// Fire software trigger
// broadcasting not supported on some platforms
error = cam.FireSoftwareTrigger(false);
if (error != FlyCapture2::PGRERROR_OK)
PrintError(error);
}
CameraFrame CameraPointGrey::getFrame(){
FlyCapture2::Error error;
// Retrieve the image
FlyCapture2::Image rawImage;
error = cam.RetrieveBuffer(&rawImage);
if (error != FlyCapture2::PGRERROR_OK)
PrintError(error);
// rawImage.SetColorProcessing(FlyCapture2::IPP);
// // de-Bayer
// rawImage.Convert(FlyCapture2::PIXEL_FORMAT_RGB8, ¤tImage);
// CameraFrame frame;
// frame.timeStamp = currentImage.GetTimeStamp().cycleCount;
// frame.height = currentImage.GetRows();
// frame.width = currentImage.GetCols();
// frame.bitDepth = 8;
// frame.channels = 3;
// frame.memory = (unsigned short*)currentImage.GetData();
CameraFrame frame;
frame.timeStamp = rawImage.GetTimeStamp().cycleCount;
frame.height = rawImage.GetRows();
frame.width = rawImage.GetCols();
frame.bitDepth = 16;
frame.channels = 1;
frame.memory = rawImage.GetData();
return frame;
}
size_t CameraPointGrey::getFrameSizeBytes(){
FlyCapture2::Format7ImageSettings format7Settings;
cam.GetFormat7Configuration(&format7Settings, NULL, NULL);
return format7Settings.height*format7Settings.width*3*2;
}
size_t CameraPointGrey::getFrameWidth(){
FlyCapture2::Format7ImageSettings format7Settings;
unsigned int dummy1;
float dummy2;
FlyCapture2::Error error = cam.GetFormat7Configuration(&format7Settings, &dummy1, &dummy2);
if (error != FlyCapture2::PGRERROR_OK)
PrintError(error);
return format7Settings.width;
}
size_t CameraPointGrey::getFrameHeight(){
FlyCapture2::Format7ImageSettings format7Settings;
unsigned int dummy1;
float dummy2;
FlyCapture2::Error error = cam.GetFormat7Configuration(&format7Settings, &dummy1, &dummy2);
if (error != FlyCapture2::PGRERROR_OK)
PrintError(error);
return format7Settings.height;
}
CameraPointGrey::~CameraPointGrey(){
if(capturing){
// Stop camera transmission
stopCapture();
}
// Gracefulle destruct the camera
cam.Disconnect();
std::cout << "Closed camera!" << std::endl << std::flush;
}