ivcam-device.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2016 Intel Corporation. All Rights Reserved.
3 
4 #define _USE_MATH_DEFINES
5 #include <math.h>
6 #include <limits>
7 #include <climits>
8 #include <algorithm>
9 
10 #include "image.h"
11 #include "ivcam-private.h"
12 #include "ivcam-device.h"
13 
14 namespace rsimpl
15 {
17  {
18  return{ dims.x, dims.y, (c.Kc[0][2] * 0.5f + 0.5f) * dims.x, (c.Kc[1][2] * 0.5f + 0.5f) * dims.y, c.Kc[0][0] * 0.5f * dims.x, c.Kc[1][1] * 0.5f * dims.y,
20  }
21 
23  {
24  rs_intrinsics intrin = { dims.x, dims.y, c.Kt[0][2] * 0.5f + 0.5f, c.Kt[1][2] * 0.5f + 0.5f, c.Kt[0][0] * 0.5f, c.Kt[1][1] * 0.5f, RS_DISTORTION_NONE, {} };
25  if (dims.x * 3 == dims.y * 4) // If using a 4:3 aspect ratio, adjust intrinsics (defaults to 16:9)
26  {
27  intrin.fx *= 4.0f / 3;
28  intrin.ppx *= 4.0f / 3;
29  intrin.ppx -= 1.0f / 6;
30  }
31  intrin.fx *= dims.x;
32  intrin.fy *= dims.y;
33  intrin.ppx *= dims.x;
34  intrin.ppy *= dims.y;
35  return intrin;
36  }
37 
38  void update_supported_options(uvc::device& dev,
40  const std::vector <std::pair<rs_option, char>> options,
41  std::vector<supported_option>& supported_options)
42  {
43  for (auto p : options)
44  {
45  int min, max, step, def;
46  get_extension_control_range(dev, depth_xu, p.second, &min, &max, &step, &def);
48  so.option = p.first;
49  so.min = min;
50  so.max = max;
51  so.step = step;
52  so.def = def;
53  supported_options.push_back(so);
54  }
55  }
56 
57  iv_camera::iv_camera(std::shared_ptr<uvc::device> device, const static_device_info & info, const ivcam::camera_calib_params & calib) :
58  rs_device_base(device, info),
59  base_calibration(calib)
60  {
61  }
62 
63  void iv_camera::start_fw_logger(char fw_log_op_code, int grab_rate_in_ms, std::timed_mutex& mutex)
64  {
65  rs_device_base::start_fw_logger(fw_log_op_code, grab_rate_in_ms, mutex);
66  }
67 
69  {
71  }
72 
73  void iv_camera::on_before_start(const std::vector<subdevice_mode_selection> & /*selected_modes*/)
74  {
75  }
76 
77  rs_stream iv_camera::select_key_stream(const std::vector<rsimpl::subdevice_mode_selection> & selected_modes)
78  {
79  int fps[RS_STREAM_NATIVE_COUNT] = {}, max_fps = 0;
80  for (const auto & m : selected_modes)
81  {
82  for (const auto & output : m.get_outputs())
83  {
84  fps[output.first] = m.mode.fps;
85  max_fps = std::max(max_fps, m.mode.fps);
86  }
87  }
88 
89  // Prefer to sync on depth or infrared, but select the stream running at the fastest framerate
91  {
92  if (fps[s] == max_fps) return s;
93  }
94  return RS_STREAM_DEPTH;
95  }
96 
97  void iv_camera::set_options(const rs_option options[], size_t count, const double values[])
98  {
99  std::vector<rs_option> base_opt;
100  std::vector<double> base_opt_val;
101 
102  for (size_t i = 0; i < count; ++i)
103  {
104 
105  if (uvc::is_pu_control(options[i]))
106  {
107  // Disabling auto-setting controls, if needed
108  switch (options[i])
109  {
112  default: break;
113  }
114 
115  uvc::set_pu_control_with_retry(get_device(), 0, options[i], static_cast<int>(values[i]));
116  continue;
117  }
118 
119  switch (options[i])
120  {
121  case RS_OPTION_F200_LASER_POWER: ivcam::set_laser_power(get_device(), static_cast<uint8_t>(values[i])); break;
122  case RS_OPTION_F200_ACCURACY: ivcam::set_accuracy(get_device(), static_cast<uint8_t>(values[i])); break;
123  case RS_OPTION_F200_MOTION_RANGE: ivcam::set_motion_range(get_device(), static_cast<uint8_t>(values[i])); break;
124  case RS_OPTION_F200_FILTER_OPTION: ivcam::set_filter_option(get_device(), static_cast<uint8_t>(values[i])); break;
125  case RS_OPTION_F200_CONFIDENCE_THRESHOLD: ivcam::set_confidence_threshold(get_device(), static_cast<uint8_t>(values[i])); break;
126 
127  default:
128  base_opt.push_back(options[i]); base_opt_val.push_back(values[i]); break;
129  }
130  }
131 
132  //Set common options
133  if (!base_opt.empty())
134  rs_device_base::set_options(base_opt.data(), base_opt.size(), base_opt_val.data());
135  }
136 
137  void iv_camera::get_options(const rs_option options[], size_t count, double values[])
138  {
139  std::vector<rs_option> base_opt;
140  std::vector<size_t> base_opt_index;
141  std::vector<double> base_opt_val;
142 
143  for (size_t i = 0; i < count; ++i)
144  {
145  LOG_INFO("Reading option " << options[i]);
146 
147  if (uvc::is_pu_control(options[i]))
148  throw std::logic_error(to_string() << __FUNCTION__ << " Option " << options[i] << " must be processed by a concrete class");
149 
150  uint8_t val = 0;
151  switch (options[i])
152  {
153  case RS_OPTION_F200_LASER_POWER: ivcam::get_laser_power(get_device(), val); values[i] = val; break;
154  case RS_OPTION_F200_ACCURACY: ivcam::get_accuracy(get_device(), val); values[i] = val; break;
155  case RS_OPTION_F200_MOTION_RANGE: ivcam::get_motion_range(get_device(), val); values[i] = val; break;
156  case RS_OPTION_F200_FILTER_OPTION: ivcam::get_filter_option(get_device(), val); values[i] = val; break;
158 
159  default:
160  base_opt.push_back(options[i]); base_opt_index.push_back(i);
161  /* LOG_WARNING("Cannot get " << options[i] << " on " << get_name());
162  throw std::logic_error("Option unsupported");*/
163  break;
164  }
165  }
166  //Retrieve the common options
167  if (base_opt.size())
168  {
169  base_opt_val.resize(base_opt.size());
170  rs_device_base::get_options(base_opt.data(), base_opt.size(), base_opt_val.data());
171  }
172 
173  // Merge the local data with values obtained by base class
174  for (auto i : base_opt_index)
175  values[i] = base_opt_val[i];
176  }
177 
178  // TODO: This may need to be modified for thread safety
180  {
181  bool started;
182  int64_t total;
184  int64_t counter = 0;
185  public:
186  rolling_timestamp_reader() : started(), total() {}
187 
188  bool validate_frame(const subdevice_mode & mode, const void * frame) override
189  {
190  // Validate that at least one byte of the image is nonzero
191  for (const uint8_t * it = (const uint8_t *)frame, *end = it + mode.pf.get_image_size(mode.native_dims.x, mode.native_dims.y); it != end; ++it)
192  {
193  if (*it)
194  {
195  return true;
196  }
197  }
198 
199  // F200 and SR300 can sometimes produce empty frames shortly after starting, ignore them
200  LOG_INFO("Subdevice " << mode.subdevice << " produced empty frame");
201  return false;
202  }
203 
204  double get_frame_timestamp(const subdevice_mode & /*mode*/, const void * frame, double /*actual_fps*/) override
205  {
206  // Timestamps are encoded within the first 32 bits of the image
207  int rolling_timestamp = *reinterpret_cast<const int32_t *>(frame);
208 
209  if (!started)
210  {
211  last_timestamp = rolling_timestamp;
212  started = true;
213  }
214 
215  const int delta = rolling_timestamp - last_timestamp; // NOTE: Relies on undefined behavior: signed int wraparound
216  last_timestamp = rolling_timestamp;
217  total += delta;
218  const int timestamp = static_cast<int>(total / 100000);
219  return timestamp;
220  }
221  unsigned long long get_frame_counter(const subdevice_mode & /*mode*/, const void * /*frame*/) override
222  {
223  return ++counter;
224  }
225  };
226 
227  std::vector<std::shared_ptr<frame_timestamp_reader>> iv_camera::create_frame_timestamp_readers() const
228  {
229  auto the_reader = std::make_shared<rolling_timestamp_reader>(); // single shared timestamp reader for all subdevices
230  return { the_reader, the_reader }; // clone the reference for color and depth
231  }
232 
233 } // namespace rsimpl::f200
void get_filter_option(const uvc::device &device, uint8_t &filter_option)
iv_camera(std::shared_ptr< uvc::device > device, const static_device_info &info, const ivcam::camera_calib_params &calib)
void get_motion_range(const uvc::device &device, uint8_t &motion_range)
void get_laser_power(const uvc::device &device, uint8_t &laser_power)
virtual void get_options(const rs_option options[], size_t count, double values[]) override
Definition: device.cpp:681
const rsimpl::uvc::device & get_device() const
Definition: device.h:102
void update_supported_options(uvc::device &dev, const uvc::extension_unit depth_xu, const std::vector< std::pair< rs_option, char >> options, std::vector< supported_option > &supported_options)
const GLfloat * m
Definition: glext.h:6461
native_pixel_format pf
Definition: types.h:158
float ppy
Definition: rs.h:305
Definition: archive.h:12
rs_option
Defines general configuration controls.
Definition: rs.h:128
void set_confidence_threshold(uvc::device &device, uint8_t conf_thresh)
void start_fw_logger(char fw_log_op_code, int grab_rate_in_ms, std::timed_mutex &mutex) override
void get_confidence_threshold(const uvc::device &device, uint8_t &conf_thresh)
void set_laser_power(uvc::device &device, uint8_t laser_power)
const uint8_t RS_STREAM_NATIVE_COUNT
Definition: types.h:27
rs_intrinsics MakeDepthIntrinsics(const ivcam::camera_calib_params &c, const int2 &dims)
const GLubyte * c
Definition: glext.h:11542
GLuint counter
Definition: glext.h:5361
GLuint GLuint GLsizei count
Definition: glext.h:111
void get_extension_control_range(const device &device, const extension_unit &xu, char control, int *min, int *max, int *step, int *def)
rs_stream select_key_stream(const std::vector< rsimpl::subdevice_mode_selection > &selected_modes) override
GLuint GLuint end
Definition: glext.h:111
const uvc::extension_unit depth_xu
Definition: ivcam-private.h:14
void get_accuracy(const uvc::device &device, uint8_t &accuracy)
size_t get_image_size(int width, int height) const
Definition: types.h:146
void set_filter_option(uvc::device &device, uint8_t filter_option)
unsigned long long get_frame_counter(const subdevice_mode &, const void *) override
virtual void disable_auto_option(int subdevice, rs_option auto_opt)
Definition: device.cpp:701
void set_accuracy(uvc::device &device, uint8_t accuracy)
rs_intrinsics MakeColorIntrinsics(const ivcam::camera_calib_params &c, const int2 &dims)
void get_options(const rs_option options[], size_t count, double values[]) override
GLenum mode
Definition: glext.h:1117
float fy
Definition: rs.h:307
Video stream intrinsics.
Definition: rs.h:300
#define LOG_INFO(...)
Definition: types.h:78
void set_options(const rs_option options[], size_t count, const double values[]) override
void set_pu_control_with_retry(device &device, int subdevice, rs_option option, int value)
Definition: uvc.h:71
GLenum GLsizei GLsizei GLint * values
Definition: glext.h:1484
void on_before_start(const std::vector< subdevice_mode_selection > &selected_modes) override
GLdouble s
Definition: glext.h:231
rs_stream
Streams are different types of data provided by RealSense devices.
Definition: rs.h:33
std::vector< std::shared_ptr< frame_timestamp_reader > > create_frame_timestamp_readers() const override
rs_device * dev
float fx
Definition: rs.h:306
void stop_fw_logger() override
bool is_pu_control(rs_option option)
Definition: uvc.h:46
float ppx
Definition: rs.h:304
void set_motion_range(uvc::device &device, uint8_t motion_range)
virtual void set_options(const rs_option options[], size_t count, const double values[]) override
Definition: device.cpp:661
virtual void stop_fw_logger() override
Definition: device.cpp:311
virtual void start_fw_logger(char fw_log_op_code, int grab_rate_in_ms, std::timed_mutex &mutex) override
Definition: device.cpp:283
GLfloat GLfloat p
Definition: glext.h:11539
bool validate_frame(const subdevice_mode &mode, const void *frame) override
GLuint GLfloat * val
Definition: glext.h:1490
double get_frame_timestamp(const subdevice_mode &, const void *frame, double) override


librealsense
Author(s): Sergey Dorodnicov , Mark Horn , Reagan Lopez
autogenerated on Fri Mar 13 2020 03:16:17