00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "flir.h"
00033
00035 #define FLIR_EXCEPT(except, msg) \
00036 { \
00037 char buf[100]; \
00038 snprintf(buf, 100, "[FLIR::%s]: " msg, __FUNCTION__); \
00039 throw except(buf); \
00040 }
00041
00043 #define FLIR_EXCEPT_ARGS(except, msg, ...) \
00044 { \
00045 char buf[100]; \
00046 snprintf(buf, 100, "[FLIR::%s]: " msg, __FUNCTION__, __VA_ARGS__); \
00047 throw except(buf); \
00048 }
00049
00050 unicap::FLIR::FLIR () : device_id_(1), color_space_(0), video_format_(0), debug_(false)
00051 {
00052 }
00053
00054 unicap::FLIR::~FLIR ()
00055 {
00056 }
00057
00059
00060 int
00061 unicap::FLIR::open ()
00062 {
00063
00064 if (!SUCCESS (unicap_enumerate_devices (NULL, &device_, device_id_)))
00065 FLIR_EXCEPT_ARGS(unicap::Exception, "Could not get info for device %i!", device_id_);
00066
00067 if ( !SUCCESS (unicap_open (&handle_, &device_)) )
00068 {
00069 FLIR_EXCEPT_ARGS(unicap::Exception, "Failed to open camera port %s!", device_.identifier);
00070 return (-1);
00071 }
00072
00073
00074 unicap_void_format (&format_spec_);
00075
00076
00077
00078 if (debug_)
00079 {
00080 fprintf (stderr, "[CompositeNode::FLIR] Available color spaces:");
00081 for ( int i = 0; SUCCESS (unicap_enumerate_formats (handle_, &format_spec_, &format_, i)); i++)
00082 fprintf (stderr, " %d(%s) ", i, format_.identifier);
00083 fprintf (stderr, "\n");
00084 }
00085
00086 if (!SUCCESS (unicap_enumerate_formats (handle_, &format_spec_, &format_, color_space_) ) )
00087 {
00088 FLIR_EXCEPT_ARGS(unicap::Exception, "Failed to set color space to %d!", color_space_);
00089 return (-1);
00090 }
00091
00092
00093 if (format_.size_count)
00094 {
00095 if (debug_)
00096 {
00097 fprintf (stderr, "[CompositeNode::FLIR] Available video formats:");
00098 for (int i = 0; i < format_.size_count; i++)
00099 fprintf (stderr, " %d(%dx%d) ", i, format_.sizes[i].width, format_.sizes[i].height);
00100 fprintf (stderr, "\n");
00101 }
00102 format_.size.width = format_.sizes[video_format_].width;
00103 format_.size.height = format_.sizes[video_format_].height;
00104 }
00105
00106 if (!SUCCESS (unicap_set_format (handle_, &format_) ) )
00107 {
00108 FLIR_EXCEPT_ARGS(unicap::Exception, "Failed to set video format to %d!", video_format_);
00109 return (-1);
00110 }
00111
00112
00113 if (!SUCCESS (unicap_start_capture (handle_) ) )
00114 {
00115 FLIR_EXCEPT_ARGS(unicap::Exception, "Failed to capture on device: %s!", device_.identifier);
00116 return (-1);
00117 }
00118
00119
00120 memset (&buffer_, 0, sizeof (unicap_data_buffer_t));
00121
00122
00123 buffer_.data = (unsigned char*)(malloc (format_.size.width * format_.size.height * format_.bpp / 8));
00124 buffer_.buffer_size = format_.size.width * format_.size.height * format_.bpp / 8;
00125
00126 return (0);
00127 }
00128
00130
00131 int
00132 unicap::FLIR::close ()
00133 {
00134
00135 if ( !SUCCESS (unicap_stop_capture (handle_) ) )
00136 FLIR_EXCEPT_ARGS(unicap::Exception, "Failed to stop capture on device: %s!", device_.identifier);
00137
00138 free (buffer_.data);
00139
00140
00141 if ( !SUCCESS (unicap_close (handle_) ) )
00142 {
00143 FLIR_EXCEPT_ARGS(unicap::Exception, "Failed to close device: %s!", device_.identifier);
00144 return (-1);
00145 }
00146
00147 return (0);
00148 }
00149
00151
00152 void
00153
00154 unicap::FLIR::readData (sensor_msgs::Image &image)
00155 {
00156
00157
00158 if (!SUCCESS (unicap_queue_buffer (handle_, &buffer_) ) )
00159 return;
00160
00161
00162 if (!SUCCESS (unicap_wait_buffer (handle_, &returned_buffer_) ) ) {}
00163
00164
00165
00166 image.width = buffer_.format.size.width;
00167 image.height = buffer_.format.size.height;
00168 image.encoding = "raw";
00169
00170
00171
00172 memcpy (&(image.data[0]), buffer_.data, buffer_.buffer_size);
00173
00174 return;
00175 }