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
00033
00034
00035
00036
00037 #include <ros/ros.h>
00038 #include "modes.h"
00039
00047
00048
00050 namespace Modes
00051 {
00052
00053 static const std::string video_mode_names_[DC1394_VIDEO_MODE_NUM] =
00054 {
00055 "160x120_yuv444",
00056 "320x240_yuv422",
00057 "640x480_yuv411",
00058 "640x480_yuv422",
00059 "640x480_rgb8",
00060 "640x480_mono8",
00061 "640x480_mono16",
00062 "800x600_yuv422",
00063 "800x600_rgb8",
00064 "800x600_mono8",
00065 "1024x768_yuv422",
00066 "1024x768_rgb8",
00067 "1024x768_mono8",
00068 "800x600_mono16",
00069 "1024x768_mono16",
00070 "1280x960_yuv422",
00071 "1280x960_rgb8",
00072 "1280x960_mono8",
00073 "1600x1200_yuv422",
00074 "1600x1200_rgb8",
00075 "1600x1200_mono8",
00076 "1280x960_mono16",
00077 "1600x1200_mono16",
00078 "exif",
00079 "format7_mode0",
00080 "format7_mode1",
00081 "format7_mode2",
00082 "format7_mode3",
00083 "format7_mode4",
00084 "format7_mode5",
00085 "format7_mode6",
00086 "format7_mode7"
00087 };
00088
00094 inline const std::string videoModeName(dc1394video_mode_t mode)
00095 {
00096 if (mode >= DC1394_VIDEO_MODE_MIN
00097 && mode <= DC1394_VIDEO_MODE_MAX)
00098 return video_mode_names_[mode - DC1394_VIDEO_MODE_MIN];
00099 else
00100 return "";
00101 }
00102
00104 static const std::string color_coding_names_[DC1394_COLOR_CODING_NUM] =
00105 {
00106 "mono8",
00107 "yuv411",
00108 "yuv422",
00109 "yuv444",
00110 "rgb8",
00111 "mono16",
00112 "rgb16",
00113 "mono16s",
00114 "rgb16s",
00115 "raw8",
00116 "raw16",
00117 };
00118
00124 inline const std::string colorCodingName(dc1394color_coding_t mode)
00125 {
00126 if (mode >= DC1394_COLOR_CODING_MIN
00127 && mode <= DC1394_COLOR_CODING_MAX)
00128 return color_coding_names_[mode - DC1394_COLOR_CODING_MIN];
00129 else
00130 return "";
00131 }
00132
00134
00136
00148 dc1394color_coding_t getColorCoding(dc1394camera_t *camera,
00149 dc1394video_mode_t video_mode,
00150 std::string &color_coding)
00151 {
00152 for (int ccode = DC1394_COLOR_CODING_MIN;
00153 ccode <= DC1394_COLOR_CODING_MAX;
00154 ++ccode)
00155 {
00156 if (color_coding_names_[ccode-DC1394_COLOR_CODING_MIN] == color_coding)
00157 {
00158
00159 dc1394color_codings_t ccs;
00160 dc1394error_t err =
00161 dc1394_format7_get_color_codings(camera, video_mode, &ccs);
00162 if (err != DC1394_SUCCESS)
00163 {
00164 ROS_FATAL("unable to get supported color codings");
00165
00166 return (dc1394color_coding_t) 0;
00167 }
00168
00169
00170 for (uint32_t i = 0; i < ccs.num; ++i)
00171 {
00172 if (ccs.codings[i] == ccode)
00173 return (dc1394color_coding_t) ccode;
00174 }
00175
00176
00177 ROS_ERROR_STREAM("Color coding " << color_coding
00178 << " not supported by this camera");
00179 dc1394color_coding_t current_mode;
00180 err = dc1394_format7_get_color_coding(camera, video_mode,
00181 ¤t_mode);
00182 if (err != DC1394_SUCCESS)
00183 {
00184 ROS_FATAL("unable to get current color coding");
00185
00186 return (dc1394color_coding_t) 0;
00187 }
00188
00189
00190
00191
00192 color_coding = colorCodingName(current_mode);
00193 return current_mode;
00194 }
00195 }
00196
00197
00198
00199 ROS_FATAL_STREAM("Unknown color_coding: " << color_coding);
00200 color_coding = colorCodingName(DC1394_COLOR_CODING_MONO8);
00201 return (dc1394color_coding_t) DC1394_COLOR_CODING_MONO8;
00202 }
00203
00216 dc1394framerate_t getFrameRate(dc1394camera_t *camera,
00217 dc1394video_mode_t video_mode,
00218 double &frame_rate)
00219 {
00220
00221 dc1394framerates_t avail_rates;
00222 dc1394error_t err =
00223 dc1394_video_get_supported_framerates(camera, video_mode, &avail_rates);
00224 if (err != DC1394_SUCCESS)
00225 {
00226 ROS_FATAL("getFrameRate() cannot be used for Format7 modes");
00227 return (dc1394framerate_t) DC1394_FRAMERATE_NUM;
00228 }
00229
00230 int result = DC1394_FRAMERATE_240;
00231 double rate = 240.0;
00232
00233
00234 while (result >= DC1394_FRAMERATE_MIN)
00235 {
00236 for (uint32_t i = 0; i < avail_rates.num; ++i)
00237 {
00238 if (avail_rates.framerates[i] == result
00239 && rate <= frame_rate)
00240 {
00241
00242 frame_rate = rate;
00243 return (dc1394framerate_t) result;
00244 }
00245 }
00246
00247
00248 --result;
00249 rate = rate / 2.0;
00250 }
00251
00252
00253 ROS_ERROR("requested frame_rate (%.3f) not available", frame_rate);
00254 return (dc1394framerate_t) DC1394_FRAMERATE_NUM;
00255 }
00256
00265 dc1394video_mode_t getVideoMode(dc1394camera_t *camera,
00266 std::string &video_mode)
00267 {
00268 for (int vm = DC1394_VIDEO_MODE_MIN;
00269 vm <= DC1394_VIDEO_MODE_MAX;
00270 ++vm)
00271 {
00272 if (video_mode_names_[vm-DC1394_VIDEO_MODE_MIN] == video_mode)
00273 {
00274
00275 dc1394video_modes_t vmodes;
00276 dc1394error_t err =
00277 dc1394_video_get_supported_modes(camera, &vmodes);
00278 if (err != DC1394_SUCCESS)
00279 {
00280 ROS_FATAL("unable to get supported video modes");
00281
00282 return (dc1394video_mode_t) 0;
00283 }
00284
00285
00286 for (uint32_t i = 0; i < vmodes.num; ++i)
00287 {
00288 if (vmodes.modes[i] == vm)
00289 return (dc1394video_mode_t) vm;
00290 }
00291
00292
00293 ROS_ERROR_STREAM("Video mode " << video_mode
00294 << " not supported by this camera");
00295 dc1394video_mode_t current_mode;
00296 err = dc1394_video_get_mode(camera, ¤t_mode);
00297 if (err != DC1394_SUCCESS)
00298 {
00299 ROS_FATAL("unable to get current video mode");
00300
00301 return (dc1394video_mode_t) 0;
00302 }
00303
00304
00305
00306
00307 video_mode = videoModeName(current_mode);
00308 return current_mode;
00309 }
00310 }
00311
00312
00313 ROS_FATAL_STREAM("Unknown video_mode:" << video_mode);
00314 ROS_BREAK();
00315
00316
00317 return (dc1394video_mode_t) 0;
00318 }
00319
00320
00332 bool setFrameRate(dc1394camera_t *camera,
00333 dc1394video_mode_t video_mode,
00334 double &frame_rate)
00335 {
00336 dc1394framerate_t rate = getFrameRate(camera, video_mode, frame_rate);
00337 if (DC1394_FRAMERATE_NUM == rate)
00338 {
00339 ROS_WARN("No valid frame rate");
00340 return false;
00341 }
00342 if (DC1394_SUCCESS != dc1394_video_set_framerate(camera, rate))
00343 {
00344 ROS_WARN("Failed to set frame rate");
00345 return false;
00346 }
00347 return true;
00348 }
00349
00360 bool setIsoSpeed(dc1394camera_t *camera, int &iso_speed)
00361 {
00362
00363 bool bmode = camera->bmode_capable;
00364 if (bmode
00365 && (DC1394_SUCCESS !=
00366 dc1394_video_set_operation_mode(camera,
00367 DC1394_OPERATION_MODE_1394B)))
00368 {
00369 bmode = false;
00370 ROS_WARN("failed to set IEEE1394b mode");
00371 }
00372
00373
00374 dc1394speed_t request = DC1394_ISO_SPEED_3200;
00375 int rate = 3200;
00376 if (!bmode)
00377 {
00378
00379 request = DC1394_ISO_SPEED_400;
00380 rate = 400;
00381 }
00382
00383
00384 while (rate > iso_speed)
00385 {
00386 if (request <= DC1394_ISO_SPEED_MIN)
00387 {
00388
00389 dc1394speed_t curSpeed;
00390 if (DC1394_SUCCESS == dc1394_video_get_iso_speed(camera, &curSpeed)
00391 && curSpeed <= DC1394_ISO_SPEED_MAX)
00392 {
00393
00394
00395
00396 request = curSpeed;
00397 rate = 100 << (curSpeed - DC1394_ISO_SPEED_MIN);
00398 }
00399 else
00400 {
00401 ROS_WARN("Unable to get ISO speed; assuming 400Mb/s");
00402 rate = 400;
00403 request = DC1394_ISO_SPEED_400;
00404 }
00405 break;
00406 }
00407
00408
00409 request = (dc1394speed_t) ((int) request - 1);
00410 rate = rate / 2;
00411 }
00412
00413
00414 iso_speed = rate;
00415
00416
00417 if (DC1394_SUCCESS != dc1394_video_set_iso_speed(camera, request))
00418 {
00419 ROS_WARN("Failed to set iso speed");
00420 return false;
00421 }
00422
00423 return true;
00424 }
00425
00426 }