Subversion Repositories seema-scanner

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include "CameraXIMEA.h"
#include <cstdio>

// Note: library header conflicts, and should only be included in cpp files
#include <xiApi.h>

#define HandleResult(res,place) if (res!=XI_OK) {printf("CameraXIMEA: Error at %s (%d)",place,res); fflush(stdout);}

std::vector<CameraInfo> CameraXIMEA::getCameraList(){

    XI_RETURN stat = XI_OK;
    DWORD numCams;
    stat = xiGetNumberDevices(&numCams);
    HandleResult(stat, "xiGetNumberDevices");

    std::vector<CameraInfo> ret(numCams);
    for(unsigned int i=0; i<numCams; i++){
        CameraInfo info;
        info.vendor = "Ximea";
        char name[20];
        xiGetDeviceInfoString(i, XI_PRM_DEVICE_NAME, name, 20);
        info.model = name;
        info.busID = i;
        ret[i] = info;
    }
    return ret;
}

CameraXIMEA::CameraXIMEA(unsigned int camNum) : camera(NULL){

    // Set debugging level
    xiSetParamInt(0, XI_PRM_DEBUG_LEVEL, XI_DL_FATAL);

    // Disable auto bandwidth determination
    xiSetParamInt(0, XI_PRM_AUTO_BANDWIDTH_CALCULATION, XI_ON);

    // Retrieve a handle to the camera device
    stat = xiOpenDevice(camNum, &camera);
    HandleResult(stat,"xiOpenDevice");

    // Configure safe buffers (copies data)
    xiSetParamInt(camera, XI_PRM_BUFFER_POLICY, XI_BP_SAFE);

//    // Output frame signal
//    xiSetParamInt(camera, XI_PRM_GPO_SELECTOR, 1);
//    xiSetParamInt(camera, XI_PRM_GPO_MODE, XI_GPO_ON);

    // Configure buffer size
    stat = xiSetParamInt(camera, XI_PRM_BUFFERS_QUEUE_SIZE, 10);
    HandleResult(stat,"xiSetParam (XI_PRM_BUFFERS_QUEUE_SIZE)");

    // Configure queue mode (0 = next frame in queue, 1 = most recent frame)
    stat = xiSetParamInt(camera, XI_PRM_RECENT_FRAME, 1);
    HandleResult(stat,"xiSetParam (XI_PRM_RECENT_FRAME)");

    // Configure image type
    stat = xiSetParamInt(camera, XI_PRM_IMAGE_DATA_FORMAT, XI_MONO8);
    HandleResult(stat,"xiSetParam (XI_PRM_IMAGE_DATA_FORMAT)");

    // Configure ouput pin 1 as trigger input
    xiSetParamInt(camera, XI_PRM_GPI_SELECTOR, 1);
    stat = xiSetParamInt(camera, XI_PRM_GPI_MODE, XI_GPI_TRIGGER);
    HandleResult(stat,"xiSetParam (XI_PRM_GPI_MODE)");

//    // Downsample to half size
//    stat = xiSetParamInt(camera, XI_PRM_DOWNSAMPLING_TYPE, XI_BINNING);
//    HandleResult(stat,"xiSetParam (XI_PRM_DOWNSAMPLING_TYPE)");
//    stat = xiSetParamInt(camera, XI_PRM_DOWNSAMPLING, 2);
//    HandleResult(stat,"xiSetParam (XI_PRM_DOWNSAMPLING)");

//    // Configure frame rate
//    stat = xiSetParamFloat(camera, XI_PRM_FRAMERATE, 10);
//    HandleResult(stat,"xiSetParam (XI_PRM_FRAMERATE)");

    // Define ROI
    stat = xiSetParamInt(camera, XI_PRM_WIDTH, 640);
    HandleResult(stat,"xiSetParam (XI_PRM_WIDTH)");
    stat = xiSetParamInt(camera, XI_PRM_HEIGHT, 512);
    HandleResult(stat,"xiSetParam (XI_PRM_HEIGHT)");
    stat = xiSetParamInt(camera, XI_PRM_OFFSET_X, 320);
    HandleResult(stat,"xiSetParam (XI_PRM_OFFSET_X)");
    stat = xiSetParamInt(camera, XI_PRM_OFFSET_Y, 256);
    HandleResult(stat,"xiSetParam (XI_PRM_OFFSET_Y)");

    // Setting reasonable default settings
    xiSetParamFloat(camera, XI_PRM_GAMMAY, 1.0);
    xiSetParamInt(camera, XI_PRM_EXPOSURE, 16666); //us
    xiSetParamFloat(camera, XI_PRM_GAIN, 0);

    // Configure for software trigger (for getSingleFrame())
    stat = xiSetParamInt(camera, XI_PRM_TRG_SOURCE, XI_TRG_SOFTWARE);
    HandleResult(stat,"xiSetParam (XI_PRM_TRG_SOURCE)");

    // Start aquisition
    stat = xiStartAcquisition(camera);
    HandleResult(stat,"xiStartAcquisition");
}

void CameraXIMEA::startCapture(){

    // Configure for hardware trigger
    stat = xiSetParamInt(camera, XI_PRM_TRG_SOURCE, XI_TRG_EDGE_FALLING);
    HandleResult(stat,"xiSetParam (XI_PRM_TRG_SOURCE)");

    capturing = true;

}

void CameraXIMEA::stopCapture(){

    if(!capturing){
        std::cerr << "CameraXIMEA: not capturing!" << std::endl;
        return;
    }

    // Configure for software trigger (for getSingleFrame())
    stat = xiSetParamInt(camera, XI_PRM_TRG_SOURCE, XI_TRG_SOFTWARE);
    HandleResult(stat,"xiSetParam (XI_PRM_TRG_SOURCE)");

}

CameraFrame CameraXIMEA::lockFrame(){

    // Create single image buffer
    XI_IMG image;
    image.bp = NULL;
    image.bp_size = 0;

    // getting image from camera
    stat = xiGetImage(camera, 1000, &image);
    HandleResult(stat,"xiGetImage");

    CameraFrame frame;
    frame.height = image.height;
    frame.width = image.width;
    frame.memory = (unsigned char*)image.bp;
    frame.timeStamp = image.tsUSec;
    frame.sizeBytes = image.bp_size;

    return frame;
}

void CameraXIMEA::unlockFrame(){


}

CameraFrame CameraXIMEA::getSingleFrame(){

    XI_IMG image;
    image.bp = NULL;
    image.bp_size = 0;

    // Fire software trigger
    stat = xiSetParamInt(camera, XI_PRM_TRG_SOFTWARE, 0);
    HandleResult(stat,"xiSetParam (XI_PRM_TRG_SOFTWARE)");

    stat = xiGetImage(camera, 1000, &image);
    HandleResult(stat,"xiGetImage");

    CameraFrame frame;
    frame.height = image.height;
    frame.width = image.width;
    frame.memory = (unsigned char*)image.bp;
    frame.timeStamp = image.tsUSec;
    frame.sizeBytes = image.bp_size;

    return frame;
}


size_t CameraXIMEA::getFrameSizeBytes(){
    return 0;
}

void CameraXIMEA::getFrameWidthHeight(unsigned int *width, unsigned int *height){
    int w, h;
    xiGetParamInt(camera, XI_PRM_WIDTH, &w);
    xiGetParamInt(camera, XI_PRM_HEIGHT, &h);
    *width = w;
    *height = h;
}


void CameraXIMEA::setCameraSettings(CameraSettings settings){

    // Set shutter (in us)
    xiSetParamInt(camera, XI_PRM_EXPOSURE, settings.shutter*1000);
    // Set gain (in dB)
    xiSetParamFloat(camera, XI_PRM_GAIN, settings.gain);

    std::cout << "Setting camera parameters:" << std::endl
              << "Shutter: " << settings.shutter << " ms" << std::endl
              << "Gain: " << settings.gain << " dB" << std::endl;
}

CameraSettings CameraXIMEA::getCameraSettings(){

    CameraSettings settings;

    int shutter;
    xiGetParamInt(camera, XI_PRM_EXPOSURE, &shutter);
    settings.shutter = shutter/1000.0; // from us to ms
    xiGetParamFloat(camera, XI_PRM_GAIN, &settings.gain);

    return settings;

}

CameraXIMEA::~CameraXIMEA(){

    // Stop acquisition
    stat = xiStopAcquisition(camera);
    HandleResult(stat,"xiStopAcquisition");

    // Close device
    xiCloseDevice(camera);
}