3 #include <sensor_msgs/Image.h>
4 #include <sensor_msgs/image_encodings.h>
5 #include <sensor_msgs/distortion_models.h>
8 using namespace FlyCapture2;
12 : _it(handle), cameraManagerLeft(handle,
"bumblebee2/left", path +
"src/igvc-software/sandbox/bumblebee2/camera_calib_left.yml"),
13 cameraManagerRight(handle,
"bumblebee2/right", path +
"src/igvc-software/sandbox/bumblebee2/camera_calib_right.yml")
18 }
catch(
const char* s) {
19 ROS_ERROR_STREAM(
"Bumblebee2 failed to initialize.");
26 _leftInfo_pub = handle.advertise<sensor_msgs::CameraInfo>(
"/stereo/left/camera_info", 1);
27 _rightInfo_pub = handle.advertise<sensor_msgs::CameraInfo>(
"/stereo/right/camera_info", 1);
35 }
catch(
const char* s) {
36 ROS_ERROR_STREAM(
"Bumblebee2 failed to close.");
43 return _cam.IsConnected();
48 constexpr Mode k_fmt7Mode = MODE_3;
49 constexpr PixelFormat k_fmt7PixFmt = PIXEL_FORMAT_RAW16;
53 unsigned int numCameras;
54 error = busMgr.GetNumOfCameras(&numCameras);
55 if (error != PGRERROR_OK)
56 throw error.GetDescription();
59 throw "No camera connected.";
62 error = busMgr.GetCameraFromIndex(0, &guid);
63 if (error != PGRERROR_OK)
64 throw error.GetDescription();
67 error =
_cam.Connect(&guid);
68 if (error != PGRERROR_OK)
69 throw error.GetDescription();
74 fmt7Info.mode = k_fmt7Mode;
75 error =
_cam.GetFormat7Info( &fmt7Info, &supported );
76 if (error != PGRERROR_OK)
77 throw error.GetDescription();
79 Format7ImageSettings fmt7ImageSettings;
80 fmt7ImageSettings.mode = k_fmt7Mode;
81 fmt7ImageSettings.offsetX = 0;
82 fmt7ImageSettings.offsetY = 0;
83 fmt7ImageSettings.width = fmt7Info.maxWidth;
84 fmt7ImageSettings.height = fmt7Info.maxHeight;
85 fmt7ImageSettings.pixelFormat = k_fmt7PixFmt;
88 Format7PacketInfo fmt7PacketInfo;
91 error =
_cam.ValidateFormat7Settings(
95 if (error != PGRERROR_OK)
96 throw error.GetDescription();
99 throw "Invalid Settings.";
102 error =
_cam.SetFormat7Configuration(
104 fmt7PacketInfo.recommendedBytesPerPacket>>1);
108 _cam.GetFormat7Configuration(&fmt7ImageSettings, &dis, &dat);
110 ROS_INFO_STREAM(
"Packet Size set to " << dis <<
" should be " << (fmt7PacketInfo.recommendedBytesPerPacket>>1));
112 if (error != PGRERROR_OK)
113 throw error.GetDescription();
118 frmRate.type = FRAME_RATE;
119 frmRate.absValue = 20;
120 error =
_cam.SetProperty(&frmRate);
121 if (error != PGRERROR_OK)
122 throw error.GetDescription();
123 ROS_INFO_STREAM(
"Frame rate is " << frmRate.absValue <<
" fps.");
127 if (error != PGRERROR_OK)
128 throw error.GetDescription();
133 if(
_cam.IsConnected())
137 error =
_cam.StopCapture();
138 if (error != PGRERROR_OK)
139 throw error.GetDescription();
142 error =
_cam.Disconnect();
143 if (error != PGRERROR_OK)
144 throw error.GetDescription();
156 error = savedRaw.DeepCopy((
const Image*) rawImage);
157 if (error != PGRERROR_OK)
158 throw error.GetDescription();
161 PixelFormat pixFormat;
162 unsigned int rows, cols, stride;
163 savedRaw.GetDimensions( &rows, &cols, &stride, &pixFormat );
166 Image convertedImage;
169 error = savedRaw.Convert( PIXEL_FORMAT_BGR, &convertedImage );
170 if (error != PGRERROR_OK)
171 throw error.GetDescription();
175 sensor_msgs::Image right;
176 right.header.stamp = ros::Time::now();
177 right.header.frame_id =
"/camera_right";
178 right.height = convertedImage.GetRows();
179 right.width = convertedImage.GetCols();
180 right.encoding = sensor_msgs::image_encodings::BGR8;
181 right.step = convertedImage.GetStride();
182 right.data.reserve(convertedImage.GetDataSize());
183 for(
int i = 0; i < convertedImage.GetDataSize(); i++)
184 right.data.push_back(convertedImage.GetData()[i]);
186 unsigned char* data = savedRaw.GetData();
187 for(
unsigned int i = 1; i < savedRaw.GetDataSize(); i += 2)
188 swap(data[i], data[i-1]);
190 error = savedRaw.Convert( PIXEL_FORMAT_BGR, &convertedImage );
191 if (error != PGRERROR_OK)
192 throw error.GetDescription();
196 sensor_msgs::Image left;
197 left.header.stamp = right.header.stamp;
198 left.header.frame_id =
"/camera_left";
199 left.height = convertedImage.GetRows();
200 left.width = convertedImage.GetCols();
201 left.encoding = sensor_msgs::image_encodings::BGR8;
202 left.step = convertedImage.GetStride();
203 left.data.reserve(convertedImage.GetDataSize());
204 for(
int i = 0; i < convertedImage.GetDataSize(); i++)
205 left.data.push_back(convertedImage.GetData()[i]);
207 sensor_msgs::CameraInfo cl =
self.cameraManagerLeft.getCameraInfo();
208 cl.header.frame_id =
"/camera_left";
209 cl.header.stamp = right.header.stamp;
210 self.cameraManagerLeft.setCameraInfo(cl);
212 sensor_msgs::CameraInfo cr =
self.cameraManagerRight.getCameraInfo();
213 cr.header.frame_id =
"/camera_right";
214 cr.header.stamp = right.header.stamp;
215 self.cameraManagerRight.setCameraInfo(cr);
217 self._left_pub.publish(left);
219 self._right_pub.publish(right);
ros::Publisher _rightInfo_pub
Bumblebee2(ros::NodeHandle &handle, std::string path)
camera_info_manager::CameraInfoManager cameraManagerRight
image_transport::ImageTransport _it
static void ProcessFrame(FlyCapture2::Image *rawImage, const void *callbackData)
image_transport::Publisher _right_pub
image_transport::Publisher _left_pub
ros::Publisher _leftInfo_pub
camera_info_manager::CameraInfoManager cameraManagerLeft