bumblebee2.cpp
Go to the documentation of this file.
1 #include "bumblebee2.h"
2 #include <exception>
3 #include <sensor_msgs/Image.h>
4 #include <sensor_msgs/image_encodings.h>
5 #include <sensor_msgs/distortion_models.h>
6 
7 using namespace std;
8 using namespace FlyCapture2;
9 using namespace ros;
10 
11 Bumblebee2::Bumblebee2(NodeHandle &handle, string path)
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")
14 {
15  try
16  {
17  startCamera();
18  } catch(const char* s) {
19  ROS_ERROR_STREAM("Bumblebee2 failed to initialize.");
20  ROS_ERROR_STREAM(s);
21  return;
22  }
23 
24  _left_pub = _it.advertise("/stereo/left/image_raw", 1);
25  _right_pub = _it.advertise("/stereo/right/image_raw", 1);
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);
28 }
29 
31 {
32  try
33  {
34  closeCamera();
35  } catch(const char* s) {
36  ROS_ERROR_STREAM("Bumblebee2 failed to close.");
37  ROS_ERROR_STREAM(s);
38  }
39 }
40 
42 {
43  return _cam.IsConnected();
44 }
45 
47 {
48  constexpr Mode k_fmt7Mode = MODE_3;
49  constexpr PixelFormat k_fmt7PixFmt = PIXEL_FORMAT_RAW16;
50  Error error;
51 
52  BusManager busMgr;
53  unsigned int numCameras;
54  error = busMgr.GetNumOfCameras(&numCameras);
55  if (error != PGRERROR_OK)
56  throw error.GetDescription();
57 
58  if ( numCameras < 1 )
59  throw "No camera connected.";
60 
61  PGRGuid guid;
62  error = busMgr.GetCameraFromIndex(0, &guid);
63  if (error != PGRERROR_OK)
64  throw error.GetDescription();
65 
66  // Connect to a camera
67  error = _cam.Connect(&guid);
68  if (error != PGRERROR_OK)
69  throw error.GetDescription();
70 
71  // Query for available Format 7 modes
72  Format7Info fmt7Info;
73  bool supported;
74  fmt7Info.mode = k_fmt7Mode;
75  error = _cam.GetFormat7Info( &fmt7Info, &supported );
76  if (error != PGRERROR_OK)
77  throw error.GetDescription();
78 
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;
86 
87  bool valid;
88  Format7PacketInfo fmt7PacketInfo;
89 
90  // Validate the settings to make sure that they are valid
91  error = _cam.ValidateFormat7Settings(
92  &fmt7ImageSettings,
93  &valid,
94  &fmt7PacketInfo );
95  if (error != PGRERROR_OK)
96  throw error.GetDescription();
97 
98  if ( !valid )
99  throw "Invalid Settings.";
100 
101  // Set the settings to the camera
102  error = _cam.SetFormat7Configuration(
103  &fmt7ImageSettings,
104  fmt7PacketInfo.recommendedBytesPerPacket>>1); //packet size of recommendedBytesPerPacket>>1(which is 2048) ensures proper transmission of data at 10fps, do not change
105 
106  unsigned int dis;
107  float dat;
108  _cam.GetFormat7Configuration(&fmt7ImageSettings, &dis, &dat);
109 
110  ROS_INFO_STREAM("Packet Size set to " << dis << " should be " << (fmt7PacketInfo.recommendedBytesPerPacket>>1));
111 
112  if (error != PGRERROR_OK)
113  throw error.GetDescription();
114 
115 
116  //Set Frame Rate
117  Property frmRate;
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.");
124 
125  // Start capturing images
126  error = _cam.StartCapture(&ProcessFrame, this);
127  if (error != PGRERROR_OK)
128  throw error.GetDescription();
129 }
130 
132 {
133  if(_cam.IsConnected())
134  {
135  Error error;
136  // Stop capturing images
137  error = _cam.StopCapture();
138  if (error != PGRERROR_OK)
139  throw error.GetDescription();
140 
141  // Disconnect the camera
142  error = _cam.Disconnect();
143  if (error != PGRERROR_OK)
144  throw error.GetDescription();
145  }
146 }
147 
148 void Bumblebee2::ProcessFrame(FlyCapture2::Image* rawImage, const void* callbackData)
149 {
150  //Image* fake;
151  Bumblebee2& self = *((Bumblebee2*)callbackData);
152 
153  Image savedRaw;
154  Error error;
155 
156  error = savedRaw.DeepCopy((const Image*) rawImage);
157  if (error != PGRERROR_OK)
158  throw error.GetDescription();
159 
160  // Get the raw image dimensions
161  PixelFormat pixFormat;
162  unsigned int rows, cols, stride;
163  savedRaw.GetDimensions( &rows, &cols, &stride, &pixFormat );
164 
165  // Create a converted image
166  Image convertedImage;
167 
168  // Convert the raw image
169  error = savedRaw.Convert( PIXEL_FORMAT_BGR, &convertedImage );
170  if (error != PGRERROR_OK)
171  throw error.GetDescription();
172 
173  // convertedImage is now the right image.
174 
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]);
185 
186  unsigned char* data = savedRaw.GetData();
187  for(unsigned int i = 1; i < savedRaw.GetDataSize(); i += 2)
188  swap(data[i], data[i-1]);
189 
190  error = savedRaw.Convert( PIXEL_FORMAT_BGR, &convertedImage );
191  if (error != PGRERROR_OK)
192  throw error.GetDescription();
193 
194  // convertedImage is now the left image.
195 
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]);
206 
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);
211 
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);
216 
217  self._left_pub.publish(left);
218  self._leftInfo_pub.publish(self.cameraManagerLeft.getCameraInfo());
219  self._right_pub.publish(right);
220  self._rightInfo_pub.publish(self.cameraManagerRight.getCameraInfo());
221 
222  return;
223 }
bool isOpen()
Definition: bumblebee2.cpp:41
action_pathConstPtr path
FlyCapture2::Camera _cam
Definition: bumblebee2.h:23
void closeCamera()
Definition: bumblebee2.cpp:131
ros::Publisher _rightInfo_pub
Definition: bumblebee2.h:29
Bumblebee2(ros::NodeHandle &handle, std::string path)
Definition: bumblebee2.cpp:11
camera_info_manager::CameraInfoManager cameraManagerRight
Definition: bumblebee2.h:32
image_transport::ImageTransport _it
Definition: bumblebee2.h:25
static void ProcessFrame(FlyCapture2::Image *rawImage, const void *callbackData)
Definition: bumblebee2.cpp:148
image_transport::Publisher _right_pub
Definition: bumblebee2.h:27
void startCamera()
Definition: bumblebee2.cpp:46
image_transport::Publisher _left_pub
Definition: bumblebee2.h:26
ros::Publisher _leftInfo_pub
Definition: bumblebee2.h:28
camera_info_manager::CameraInfoManager cameraManagerLeft
Definition: bumblebee2.h:31


igvc
Author(s): Matthew Barulic , Al Chaussee
autogenerated on Sun May 10 2015 16:18:45