modes.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*********************************************************************
4 * Software License Agreement (BSD License)
5 *
6 * Copyright (c) 2010 Jack O'Quin
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
19 * * Neither the name of the author nor other contributors may be
20 * used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 *********************************************************************/
36 
37 #include <ros/ros.h>
38 #include "modes.h"
39 
47 // static data and functions:
50 namespace Modes
51 {
52  // driver parameter names, corresponding to DC1394 video modes
53  static const std::string video_mode_names_[DC1394_VIDEO_MODE_NUM] =
54  {
55  "160x120_yuv444",
56  "320x240_yuv422",
57  "640x480_yuv411",
58  "640x480_yuv422",
59  "640x480_rgb8",
60  "640x480_mono8",
61  "640x480_mono16",
62  "800x600_yuv422",
63  "800x600_rgb8",
64  "800x600_mono8",
65  "1024x768_yuv422",
66  "1024x768_rgb8",
67  "1024x768_mono8",
68  "800x600_mono16",
69  "1024x768_mono16",
70  "1280x960_yuv422",
71  "1280x960_rgb8",
72  "1280x960_mono8",
73  "1600x1200_yuv422",
74  "1600x1200_rgb8",
75  "1600x1200_mono8",
76  "1280x960_mono16",
77  "1600x1200_mono16",
78  "exif",
79  "format7_mode0",
80  "format7_mode1",
81  "format7_mode2",
82  "format7_mode3",
83  "format7_mode4",
84  "format7_mode5",
85  "format7_mode6",
86  "format7_mode7"
87  };
88 
94  inline const std::string videoModeName(dc1394video_mode_t mode)
95  {
96  if (mode >= DC1394_VIDEO_MODE_MIN
97  && mode <= DC1394_VIDEO_MODE_MAX)
98  return video_mode_names_[mode - DC1394_VIDEO_MODE_MIN];
99  else
100  return "";
101  }
102 
104  static const std::string color_coding_names_[DC1394_COLOR_CODING_NUM] =
105  {
106  "mono8",
107  "yuv411",
108  "yuv422",
109  "yuv444",
110  "rgb8",
111  "mono16",
112  "rgb16",
113  "mono16s",
114  "rgb16s",
115  "raw8",
116  "raw16",
117  };
118 
124  inline const std::string colorCodingName(dc1394color_coding_t mode)
125  {
126  if (mode >= DC1394_COLOR_CODING_MIN
127  && mode <= DC1394_COLOR_CODING_MAX)
128  return color_coding_names_[mode - DC1394_COLOR_CODING_MIN];
129  else
130  return "";
131  }
132 
134  // public functions:
136 
148  dc1394color_coding_t getColorCoding(dc1394camera_t *camera,
149  dc1394video_mode_t video_mode,
150  std::string &color_coding)
151  {
152  for (int ccode = DC1394_COLOR_CODING_MIN;
153  ccode <= DC1394_COLOR_CODING_MAX;
154  ++ccode)
155  {
156  if (color_coding_names_[ccode-DC1394_COLOR_CODING_MIN] == color_coding)
157  {
158  // found the requested mode
159  dc1394color_codings_t ccs;
160  dc1394error_t err =
161  dc1394_format7_get_color_codings(camera, video_mode, &ccs);
162  if (err != DC1394_SUCCESS)
163  {
164  ROS_FATAL("unable to get supported color codings");
165  // TODO raise exception
166  return (dc1394color_coding_t) 0;
167  }
168 
169  // see if requested mode is available
170  for (uint32_t i = 0; i < ccs.num; ++i)
171  {
172  if (ccs.codings[i] == ccode)
173  return (dc1394color_coding_t) ccode; // yes: success
174  }
175 
176  // requested mode not available, revert to current mode of camera
177  ROS_ERROR_STREAM("Color coding " << color_coding
178  << " not supported by this camera");
179  dc1394color_coding_t current_mode;
180  err = dc1394_format7_get_color_coding(camera, video_mode,
181  &current_mode);
182  if (err != DC1394_SUCCESS)
183  {
184  ROS_FATAL("unable to get current color coding");
185  // TODO raise exception
186  return (dc1394color_coding_t) 0;
187  }
188 
189  // TODO list available modes
190 
191  // change color_coding parameter to show current mode of camera
192  color_coding = colorCodingName(current_mode);
193  return current_mode;
194  }
195  }
196 
197  // Requested color coding does not match any known string, set to
198  // "mono8" and update parameter.
199  ROS_FATAL_STREAM("Unknown color_coding: " << color_coding);
200  color_coding = colorCodingName(DC1394_COLOR_CODING_MONO8);
201  return (dc1394color_coding_t) DC1394_COLOR_CODING_MONO8;
202  }
203 
216  dc1394framerate_t getFrameRate(dc1394camera_t *camera,
217  dc1394video_mode_t video_mode,
218  double &frame_rate)
219  {
220  // list frame rates supported for this video mode
221  dc1394framerates_t avail_rates;
222  dc1394error_t err =
223  dc1394_video_get_supported_framerates(camera, video_mode, &avail_rates);
224  if (err != DC1394_SUCCESS)
225  {
226  ROS_FATAL("getFrameRate() cannot be used for Format7 modes");
227  return (dc1394framerate_t) DC1394_FRAMERATE_NUM; // failure
228  }
229 
230  int result = DC1394_FRAMERATE_240;
231  double rate = 240.0;
232 
233  // round frame rate down to next-lower defined value
234  while (result >= DC1394_FRAMERATE_MIN)
235  {
236  for (uint32_t i = 0; i < avail_rates.num; ++i)
237  {
238  if (avail_rates.framerates[i] == result
239  && rate <= frame_rate)
240  {
241  // update configured rate to match selected value
242  frame_rate = rate;
243  return (dc1394framerate_t) result;
244  }
245  }
246 
247  // continue with next-lower possible value
248  --result;
249  rate = rate / 2.0;
250  }
251 
252  // no valid frame rate discovered
253  ROS_ERROR("requested frame_rate (%.3f) not available", frame_rate);
254  return (dc1394framerate_t) DC1394_FRAMERATE_NUM; // failure
255  }
256 
265  dc1394video_mode_t getVideoMode(dc1394camera_t *camera,
266  std::string &video_mode)
267  {
268  for (int vm = DC1394_VIDEO_MODE_MIN;
269  vm <= DC1394_VIDEO_MODE_MAX;
270  ++vm)
271  {
272  if (video_mode_names_[vm-DC1394_VIDEO_MODE_MIN] == video_mode)
273  {
274  // found the requested mode
275  dc1394video_modes_t vmodes;
276  dc1394error_t err =
277  dc1394_video_get_supported_modes(camera, &vmodes);
278  if (err != DC1394_SUCCESS)
279  {
280  ROS_FATAL("unable to get supported video modes");
281  // TODO raise exception
282  return (dc1394video_mode_t) 0;
283  }
284 
285  // see if requested mode is available
286  for (uint32_t i = 0; i < vmodes.num; ++i)
287  {
288  if (vmodes.modes[i] == vm)
289  return (dc1394video_mode_t) vm; // yes: success
290  }
291 
292  // requested mode not available, revert to current mode of camera
293  ROS_ERROR_STREAM("Video mode " << video_mode
294  << " not supported by this camera");
295  dc1394video_mode_t current_mode;
296  err = dc1394_video_get_mode(camera, &current_mode);
297  if (err != DC1394_SUCCESS)
298  {
299  ROS_FATAL("unable to get current video mode");
300  // TODO raise exception
301  return (dc1394video_mode_t) 0;
302  }
303 
304  // TODO list available modes
305 
306  // change video_mode parameter to show current mode of camera
307  video_mode = videoModeName(current_mode);
308  return current_mode;
309  }
310  }
311 
312  // request video mode does not match any known string
313  ROS_FATAL_STREAM("Unknown video_mode:" << video_mode);
314  ROS_BREAK();
315  // TODO raise exception
316  //CAM_EXCEPT(camera1394::Exception, "Unsupported video_mode");
317  return (dc1394video_mode_t) 0;
318  }
319 
320 
332  bool setFrameRate(dc1394camera_t *camera,
333  dc1394video_mode_t video_mode,
334  double &frame_rate)
335  {
336  dc1394framerate_t rate = getFrameRate(camera, video_mode, frame_rate);
337  if (DC1394_FRAMERATE_NUM == rate)
338  {
339  ROS_WARN("No valid frame rate");
340  return false; // failure
341  }
342  if (DC1394_SUCCESS != dc1394_video_set_framerate(camera, rate))
343  {
344  ROS_WARN("Failed to set frame rate");
345  return false; // failure
346  }
347  return true;
348  }
349 
360  bool setIsoSpeed(dc1394camera_t *camera, int &iso_speed)
361  {
362  // Enable IEEE1394b mode if the camera and bus support it
363  bool bmode = camera->bmode_capable;
364  if (bmode
365  && (DC1394_SUCCESS !=
366  dc1394_video_set_operation_mode(camera,
367  DC1394_OPERATION_MODE_1394B)))
368  {
369  bmode = false;
370  ROS_WARN("failed to set IEEE1394b mode");
371  }
372 
373  // start with highest speed supported
374  dc1394speed_t request = DC1394_ISO_SPEED_3200;
375  int rate = 3200;
376  if (!bmode)
377  {
378  // not IEEE1394b capable: so 400Mb/s is the limit
379  request = DC1394_ISO_SPEED_400;
380  rate = 400;
381  }
382 
383  // round requested speed down to next-lower defined value
384  while (rate > iso_speed)
385  {
386  if (request <= DC1394_ISO_SPEED_MIN)
387  {
388  // get current ISO speed of the device
389  dc1394speed_t curSpeed;
390  if (DC1394_SUCCESS == dc1394_video_get_iso_speed(camera, &curSpeed)
391  && curSpeed <= DC1394_ISO_SPEED_MAX)
392  {
393  // Translate curSpeed back to an int for the parameter
394  // update, works as long as any new higher speeds keep
395  // doubling.
396  request = curSpeed;
397  rate = 100 << (curSpeed - DC1394_ISO_SPEED_MIN);
398  }
399  else
400  {
401  ROS_WARN("Unable to get ISO speed; assuming 400Mb/s");
402  rate = 400;
403  request = DC1394_ISO_SPEED_400;
404  }
405  break;
406  }
407 
408  // continue with next-lower possible value
409  request = (dc1394speed_t) ((int) request - 1);
410  rate = rate / 2;
411  }
412 
413  // update configured rate to match selected value
414  iso_speed = rate;
415 
416  // set the requested speed
417  if (DC1394_SUCCESS != dc1394_video_set_iso_speed(camera, request))
418  {
419  ROS_WARN("Failed to set iso speed");
420  return false;
421  }
422 
423  return true;
424  }
425 
426 } // namespace Modes
#define ROS_FATAL(...)
libdc1394 enumerated modes interface
const std::string videoModeName(dc1394video_mode_t mode)
Definition: modes.cpp:94
const std::string colorCodingName(dc1394color_coding_t mode)
Definition: modes.cpp:124
Definition: modes.cpp:50
#define ROS_WARN(...)
bool setIsoSpeed(dc1394camera_t *camera, int &iso_speed)
Definition: modes.cpp:360
dc1394color_coding_t getColorCoding(dc1394camera_t *camera, dc1394video_mode_t video_mode, std::string &color_coding)
Definition: modes.cpp:148
static const std::string video_mode_names_[DC1394_VIDEO_MODE_NUM]
Definition: modes.cpp:53
#define ROS_FATAL_STREAM(args)
bool setFrameRate(dc1394camera_t *camera, dc1394video_mode_t video_mode, double &frame_rate)
Definition: modes.cpp:332
#define ROS_ERROR_STREAM(args)
dc1394framerate_t getFrameRate(dc1394camera_t *camera, dc1394video_mode_t video_mode, double &frame_rate)
Definition: modes.cpp:216
static const std::string color_coding_names_[DC1394_COLOR_CODING_NUM]
driver parameter names, corresponding to DC1394 color codings
Definition: modes.cpp:104
#define ROS_BREAK()
#define ROS_ERROR(...)
dc1394video_mode_t getVideoMode(dc1394camera_t *camera, std::string &video_mode)
Definition: modes.cpp:265


camera1394
Author(s): Jack O'Quin, Ken Tossell, Patrick Beeson, Nate Koenig, Andrew Howard, Damien Douxchamps, Dan Dennedy
autogenerated on Mon Jun 10 2019 12:52:31