presets-common.h
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2020 Intel Corporation. All Rights Reserved.
3 
4 #include "../../test.h"
5 #include <concurrency.h>
6 #include <types.h>
7 #include "l500/l500-private.h"
8 #include "l500/l500-options.h"
9 #include "../send-hw-monitor-command.h"
10 
11 using namespace rs2;
12 
14 
15 typedef std::pair< rs2_l500_visual_preset, rs2_sensor_mode > preset_mode_pair;
16 
17 typedef std::map< preset_mode_pair,
18  std::map< rs2_option, float > >
20 
21 typedef std::map< std::pair< rs2_digital_gain, rs2_sensor_mode >, std::map< rs2_option, float > >
23 
24 // These are all the options that are changed when changing a preset
34 
35 std::map< rs2_option, librealsense::l500_control > option_to_code = {
45 
47 {
50 };
51 
52 const std::map< rs2_l500_visual_preset, std::pair< rs2_digital_gain, presets_useful_laser_values > >
58 
59 // except from RS2_L500_VISUAL_PRESET_AUTOMATIC and RS2_L500_VISUAL_PRESET_CUSTOM
61  std::function< void(preset_mode_pair) > action )
62 {
64  preset++ )
65  {
66  for( int sensor_mode = RS2_SENSOR_MODE_VGA; sensor_mode < RS2_SENSOR_MODE_QVGA;
67  sensor_mode++ )
68  {
69  action({ rs2_l500_visual_preset(preset), rs2_sensor_mode(sensor_mode) });
70  }
71  }
72 }
73 
74 // except from RS2_L500_VISUAL_PRESET_AUTOMATIC and RS2_L500_VISUAL_PRESET_CUSTOM
76  std::function< void( rs2_digital_gain, rs2_sensor_mode ) > action )
77 {
78  for( int gain = RS2_DIGITAL_GAIN_HIGH; gain <= RS2_DIGITAL_GAIN_LOW; gain++ )
79  {
80  for( int sensor_mode = RS2_SENSOR_MODE_VGA; sensor_mode < RS2_SENSOR_MODE_COUNT;
81  sensor_mode++ )
82  {
83  action( rs2_digital_gain( gain ), rs2_sensor_mode( sensor_mode ) );
84  }
85  }
86 }
87 // Can be called at a point on test we want to be sure we are on preset and not on custom
89 {
93 }
94 
96 {
98 
102 
103  return res;
104 }
105 
106 std::map< rs2_option, float > get_defaults_from_fw( rs2::device & dev )
107 {
108  std::map< rs2_option, float > option_to_defaults;
109 
110  auto dp = dev.as< rs2::debug_protocol >();
111  REQUIRE( dp );
112 
113  auto ds = dev.first< rs2::depth_sensor >();
114 
115  REQUIRE( ds.supports( RS2_OPTION_SENSOR_MODE ) );
116 
118  REQUIRE_NOTHROW( mode = rs2_sensor_mode( (int)ds.get_option( RS2_OPTION_SENSOR_MODE ) ) );
119 
120  for( auto op : option_to_code )
121  {
122 
124  op.second,
126  (int)mode,
127  0 };
128 
129  auto res = send_command_and_check( dp, command, 1 );
130 
131  REQUIRE( ds.supports( RS2_OPTION_DIGITAL_GAIN ) );
132  float digital_gain_val;
133 
134  REQUIRE_NOTHROW( digital_gain_val = ds.get_option( RS2_OPTION_DIGITAL_GAIN ) );
135 
136  auto val = *reinterpret_cast< int32_t * >( (void *)res.data() );
137 
138  option_to_defaults[op.first] = float( val );
139  }
140 
141  return option_to_defaults;
142 }
143 
144 std::map< rs2_option, float > build_preset_to_expected_defaults_map( rs2::device & dev,
145  rs2::depth_sensor & depth_sens,
146  preset_mode_pair preset_mode )
147 {
148  auto preset_to_gain_and_laser = preset_to_gain_and_laser_map;
149 
151  (float)preset_to_gain_and_laser[preset_mode.first].first );
152  depth_sens.set_option( RS2_OPTION_SENSOR_MODE, (float)preset_mode.second );
153 
154  return get_defaults_from_fw( dev );
155 }
156 
158  rs2::depth_sensor & depth_sens )
159 {
160  preset_values_map preset_to_expected_defaults_values;
161 
162  auto preset_to_gain_and_laser = preset_to_gain_and_laser_map;
163 
165  preset_to_expected_defaults_values[preset_mode]
166  = build_preset_to_expected_defaults_map( dev, depth_sens, preset_mode );
167  } );
168 
169  return preset_to_expected_defaults_values;
170 }
171 
173  rs2::depth_sensor & depth_sens,
174  preset_mode_pair preset_mode,
175  std::map< rs2_option, float > & expected_values,
176  std::map< rs2_option, float > & expected_defs )
177 {
178 
179  auto preset_to_gain_and_laser = preset_to_gain_and_laser_map;
180 
181  auto preset = preset_mode.first;
182  auto mode = preset_mode.second;
183 
184  depth_sens.set_option( RS2_OPTION_DIGITAL_GAIN, preset_to_gain_and_laser[preset].first );
185  depth_sens.set_option( RS2_OPTION_SENSOR_MODE, mode );
186 
187  expected_defs = get_defaults_from_fw( dev );
188 
189  for( auto dependent_option : preset_dependent_options )
190  {
191  expected_values[dependent_option] = depth_sens.get_option_range( dependent_option ).def;
192  }
193 
194  expected_values[RS2_OPTION_DIGITAL_GAIN] = (float)preset_to_gain_and_laser[preset].first;
195  auto laser_range = depth_sens.get_option_range( RS2_OPTION_LASER_POWER );
196 
197 
198  expected_values[RS2_OPTION_LASER_POWER]
199  = preset_to_gain_and_laser[preset].second == default_laser ? laser_range.def
200  : laser_range.max;
201 }
202 
204  rs2::depth_sensor & depth_sens,
205  preset_values_map & expected_values,
206  preset_values_map & expected_defs )
207 {
208 
211  depth_sens,
212  preset_mode,
213  expected_values[preset_mode],
214  expected_defs[preset_mode] );
215  } );
216 }
217 
218 std::map< rs2_option, float > get_defaults_from_lrs( rs2::depth_sensor & depth_sens )
219 {
220  std::map< rs2_option, float > option_to_defaults;
221 
222  for( auto op : option_to_code )
223  {
224  REQUIRE( depth_sens.supports( op.first ) );
226  REQUIRE_NOTHROW( range = depth_sens.get_option_range( op.first ) );
227  option_to_defaults[op.first] = range.def;
228  }
229  return option_to_defaults;
230 }
231 
233  rs2::depth_sensor & depth_sens )
234 {
235  gain_values_map gain_to_expected_defaults_values;
236 
237  auto preset_to_gain_and_laser = preset_to_gain_and_laser_map;
238 
240  depth_sens.set_option( RS2_OPTION_DIGITAL_GAIN, (float)gain );
241  depth_sens.set_option( RS2_OPTION_SENSOR_MODE, (float)mode );
242 
243  gain_to_expected_defaults_values[{ gain, mode }] = get_defaults_from_fw( dev );
244  } );
245 
246  return gain_to_expected_defaults_values;
247 }
248 
250 {
251  auto dp = dev.as< rs2::debug_protocol >();
252 
253  for( auto op : option_to_code )
254  {
256  librealsense::l500_control( op.second ),
257  -1,
258  0,
259  0 };
260  send_command_and_check( dp, set_command );
261  }
262 }
263 
264 std::map< rs2_option, float > get_currents_from_lrs( rs2::depth_sensor & depth_sens )
265 {
266  std::map< rs2_option, float > option_to_currents;
267 
268  for( auto op : option_to_code )
269  {
270  REQUIRE( depth_sens.supports( op.first ) );
271  float curr;
272  REQUIRE_NOTHROW( curr = depth_sens.get_option( op.first ) );
273  option_to_currents[op.first] = curr;
274  }
275  return option_to_currents;
276 }
277 
278 void compare( const std::map< rs2_option, float > & first,
279  const std::map< rs2_option, float > & second )
280 {
281  CAPTURE( first.size() );
282  CAPTURE( second.size() );
283  REQUIRE( first.size() == second.size() );
284 
285  // an order map - should be sorted
286  for( auto it_first = first.begin(), it_second = second.begin(); it_first != first.end();
287  it_first++, it_second++ )
288  {
289  REQUIRE( it_first->first == it_second->first );
290  REQUIRE( it_first->second == it_second->second );
291  }
292 }
293 
295  preset_values_map & preset_to_expected_values )
296 {
298 
299  std::ofstream csv( "presets.csv" );
300 
302  auto expected_values
303  = preset_to_expected_values[preset_mode];
304 
305  csv << "-----" << preset_mode.first << " " << preset_mode.second << "-----"
306  << "\n";
307 
308  for( auto i : expected_values )
309  {
310  csv << i.first << "," << i.second << "\n";
311  }
312  } );
313 
314  csv.close();
315 }
316 
317 void set_option_values( const rs2::sensor & sens,
318  const std::map< rs2_option, float > & option_values )
319 {
320  for( auto option_value : option_values )
321  {
322  REQUIRE_NOTHROW( sens.set_option( option_value.first, option_value.second ) );
323  float value;
324  REQUIRE_NOTHROW( value = sens.get_option( option_value.first ) );
325  REQUIRE( value == option_value.second );
326  }
327 }
328 
330  const std::map< rs2_option, float > & expected_values )
331 {
332  for( auto & i : expected_values )
333  {
334  CAPTURE( i.first );
335  CHECK( sens.get_option( i.first ) == i.second );
336  }
337 }
338 
340  const std::map< rs2_option, float > & expected_defaults )
341 {
342  for( auto & i : expected_defaults )
343  {
344  CAPTURE( i.first );
345  CHECK( sens.get_option_range( i.first ).def == i.second );
346  }
347 }
348 
349 void compare_to_actual( const rs2::sensor & sens,
350  const std::map< rs2_option, float > & expected_values,
351  const std::map< rs2_option, float > & expected_defaults )
352 {
353  compare_expected_currents_to_actual( sens, expected_values );
354  compare_expected_defaults_to_actual( sens, expected_defaults );
355 }
356 
357 void check_preset_values( const rs2::sensor & sens,
358  rs2_l500_visual_preset preset,
360  const std::map< rs2_option, float > & expected_values,
361  const std::map< rs2_option, float > & expected_defaults )
362 {
363 
364  if( preset == RS2_L500_VISUAL_PRESET_CUSTOM )
365  return;
366 
367  CAPTURE( preset, mode );
368  compare_to_actual( sens, expected_values, expected_defaults );
369 }
370 
371 void check_presets_values( std::vector< rs2_l500_visual_preset > presets_to_check,
372  const rs2::sensor & sens,
373  preset_values_map & preset_to_expected_values,
374  preset_values_map & preset_to_expected_defaults,
375  std::function< void( preset_mode_pair ) > do_before_check = nullptr,
376  std::function< void( preset_mode_pair ) > do_after_check = nullptr )
377 {
379 
380  for (auto& preset : presets_to_check)
381  {
382  for (int sensor_mode = RS2_SENSOR_MODE_VGA; sensor_mode < RS2_SENSOR_MODE_QVGA;
383  sensor_mode++)
384  {
385  preset_mode_pair preset_mode = { preset, (rs2_sensor_mode)sensor_mode };
386  if (do_before_check)
387  do_before_check(preset_mode);
388 
389  check_preset_values(sens,
390  preset_mode.first,
391  preset_mode.second,
392  preset_to_expected_values[preset_mode],
393  preset_to_expected_defaults[preset_mode]);
394 
395  if (do_after_check)
396  do_after_check(preset_mode);
397  }
398  }
399 }
400 
403 {
404  auto depth = find_profile( sens, RS2_STREAM_DEPTH, mode );
405  auto ir = find_profile( sens, RS2_STREAM_INFRARED, mode );
407 
408  REQUIRE_NOTHROW( sens.open( { depth, ir, confidence } ) );
409 
410  // Wait for the first frame to arrive: until this time, the FW is in an "undefined"
411  // streaming state, and we may get weird behavior.
412  frame_queue q;
413  REQUIRE_NOTHROW(sens.start(q));
414  q.wait_for_frame();
415 }
416 
417 void stop_depth( const rs2::sensor & sens )
418 {
419  sens.stop();
420  sens.close();
421 }
422 
423 void set_mode_preset( const rs2::sensor & sens, preset_mode_pair preset_mode)
424 {
425  REQUIRE_NOTHROW( sens.set_option( RS2_OPTION_SENSOR_MODE, (float)preset_mode.second) );
426  REQUIRE_NOTHROW( sens.set_option( RS2_OPTION_VISUAL_PRESET, (float)preset_mode.first) );
427  CHECK( sens.get_option( RS2_OPTION_VISUAL_PRESET ) == (float)preset_mode.first);
428 }
429 
431  std::vector<rs2_l500_visual_preset> presets_to_check,
432  const rs2::sensor & sens,
433  preset_values_map & preset_to_expected_values,
434  preset_values_map & preset_to_expected_defaults,
435  std::function< void( preset_mode_pair ) > do_before_stream_start = nullptr,
436  std::function< void( preset_mode_pair ) > do_after_stream_start = nullptr )
437 {
439 
441  presets_to_check,
442  sens,
443  preset_to_expected_values,
444  preset_to_expected_defaults,
445  [&](preset_mode_pair preset_mode) {
446  if( do_before_stream_start )
447  do_before_stream_start(preset_mode);
448 
449  start_depth_ir_confidence( sens, preset_mode.second);
450 
451  if( do_after_stream_start )
452  do_after_stream_start(preset_mode);
453  },
454  [&](preset_mode_pair) { stop_depth( sens ); } );
455 }
456 
458 {
459  auto curr_preset
461  CAPTURE( curr_preset );
462 
463  if( curr_preset != preset )
464  {
465  auto preset_to_gain_and_laser = preset_to_gain_and_laser_map;
466  option_range laser_range;
467  REQUIRE_NOTHROW( laser_range = depth_sens.get_option_range( RS2_OPTION_LASER_POWER ) );
468  CHECK( laser_range.def == laser_range.max );
469  CHECK( preset_to_gain_and_laser[preset].first
470  == preset_to_gain_and_laser[curr_preset].first );
471  }
472 }
473 
474 void build_new_device_and_do( std::function< void( rs2::depth_sensor & depth_sens ) > action )
475 {
477  auto dev = devices[0];
478 
479  auto depth_sens = dev.first< rs2::depth_sensor >();
480 
481  action( depth_sens );
482 }
void check_presets_values(std::vector< rs2_l500_visual_preset > presets_to_check, const rs2::sensor &sens, preset_values_map &preset_to_expected_values, preset_values_map &preset_to_expected_defaults, std::function< void(preset_mode_pair) > do_before_check=nullptr, std::function< void(preset_mode_pair) > do_after_check=nullptr)
std::map< rs2_option, float > build_preset_to_expected_defaults_map(rs2::device &dev, rs2::depth_sensor &depth_sens, preset_mode_pair preset_mode)
void compare_expected_currents_to_actual(const rs2::sensor &sens, const std::map< rs2_option, float > &expected_values)
const librealsense::firmware_version MIN_GET_DEFAULT_FW_VERSION("1.5.4.0")
void compare_to_actual(const rs2::sensor &sens, const std::map< rs2_option, float > &expected_values, const std::map< rs2_option, float > &expected_defaults)
void check_preset_is_equal_to(rs2::depth_sensor &depth_sens, rs2_l500_visual_preset preset)
std::pair< rs2_l500_visual_preset, rs2_sensor_mode > preset_mode_pair
std::map< rs2_option, float > get_defaults_from_lrs(rs2::depth_sensor &depth_sens)
std::map< preset_mode_pair, std::map< rs2_option, float > > preset_values_map
GLfloat value
void check_presets_values_while_streaming(std::vector< rs2_l500_visual_preset > presets_to_check, const rs2::sensor &sens, preset_values_map &preset_to_expected_values, preset_values_map &preset_to_expected_defaults, std::function< void(preset_mode_pair) > do_before_stream_start=nullptr, std::function< void(preset_mode_pair) > do_after_stream_start=nullptr)
std::vector< uint8_t > send_command_and_check(rs2::debug_protocol dp, hw_monitor_command command, uint32_t expected_size_return=0)
void compare_expected_defaults_to_actual(const rs2::sensor &sens, const std::map< rs2_option, float > &expected_defaults)
GLint GLint GLsizei GLsizei GLsizei depth
Definition: cah-model.h:10
void set_option_values(const rs2::sensor &sens, const std::map< rs2_option, float > &option_values)
GLuint GLfloat * val
presets_useful_laser_values
GLenum mode
std::map< rs2_option, float > get_currents_from_lrs(rs2::depth_sensor &depth_sens)
void check_preset_values(const rs2::sensor &sens, rs2_l500_visual_preset preset, rs2_sensor_mode mode, const std::map< rs2_option, float > &expected_values, const std::map< rs2_option, float > &expected_defaults)
const std::map< rs2_l500_visual_preset, std::pair< rs2_digital_gain, presets_useful_laser_values > > preset_to_gain_and_laser_map
const std::vector< rs2_option > preset_dependent_options
gain_values_map build_gain_to_expected_defaults_map(rs2::device &dev, rs2::depth_sensor &depth_sens)
#define CAPTURE(...)
Definition: catch.hpp:17432
REQUIRE(n_callbacks==1)
std::map< std::pair< rs2_digital_gain, rs2_sensor_mode >, std::map< rs2_option, float > > gain_values_map
rs2::device_list find_devices_by_product_line_or_exit(int product)
Definition: func-common.h:24
devices
Definition: test-fg.py:9
void print_presets_to_csv(rs2::depth_sensor &depth_sens, preset_values_map &preset_to_expected_values)
void build_new_device_and_do(std::function< void(rs2::depth_sensor &depth_sens) > action)
GLint first
#define RS2_PRODUCT_LINE_L500
Definition: rs_context.h:96
void compare(const std::map< rs2_option, float > &first, const std::map< rs2_option, float > &second)
bool supports(rs2_camera_info info) const
Definition: rs_sensor.hpp:125
action
Definition: enums.py:62
void reset_hw_controls(rs2::device &dev)
rs2_sensor_mode
For setting the camera_mode option.
Definition: rs_option.h:165
void stop_depth(const rs2::sensor &sens)
GLdouble GLdouble GLdouble q
std::map< rs2_option, librealsense::l500_control > option_to_code
void open(const stream_profile &profile) const
Definition: rs_sensor.hpp:111
rs2_digital_gain
digital gain for RS2_OPTION_DIGITAL_GAIN option.
Definition: rs_option.h:183
T first() const
Definition: rs_device.hpp:52
GLsizei range
void for_each_gain_mode_combination(std::function< void(rs2_digital_gain, rs2_sensor_mode) > action)
REQUIRE_NOTHROW(rs2_log(RS2_LOG_SEVERITY_INFO,"Log message using rs2_log()", nullptr))
void build_preset_to_expected_values_map(rs2::device &dev, rs2::depth_sensor &depth_sens, preset_mode_pair preset_mode, std::map< rs2_option, float > &expected_values, std::map< rs2_option, float > &expected_defs)
void reset_camera_preset_mode_to_defaults(const rs2::sensor &sens)
int i
GLuint res
Definition: glext.h:8856
void build_presets_to_expected_values_defs_map(rs2::device &dev, rs2::depth_sensor &depth_sens, preset_values_map &expected_values, preset_values_map &expected_defs)
void close() const
Definition: rs_sensor.hpp:173
option_range get_option_range(rs2_option option) const
Definition: rs_options.hpp:84
signed int int32_t
Definition: stdint.h:77
void for_each_preset_mode_combination(std::function< void(preset_mode_pair) > action)
void set_mode_preset(const rs2::sensor &sens, preset_mode_pair preset_mode)
preset_values_map build_presets_to_expected_defaults_map(rs2::device &dev, rs2::depth_sensor &depth_sens)
stream_profile find_profile(rs2::depth_sensor depth_sens, rs2_stream stream, rs2_sensor_mode mode)
Definition: func-common.h:187
void set_option(rs2_option option, float value) const
Definition: rs_options.hpp:99
float get_option(rs2_option option) const
Definition: rs_options.hpp:72
GeneratorWrapper< T > map(Func &&function, GeneratorWrapper< U > &&generator)
Definition: catch.hpp:4271
#define CHECK(condition)
void start(T callback) const
Definition: rs_sensor.hpp:185
preset_mode_pair get_camera_preset_mode_defaults(const rs2::sensor &sens)
void start_depth_ir_confidence(const rs2::sensor &sens, rs2_sensor_mode mode=RS2_SENSOR_MODE_VGA)
void stop() const
Definition: rs_sensor.hpp:195
T as() const
Definition: rs_device.hpp:129
std::map< rs2_option, float > get_defaults_from_fw(rs2::device &dev)
rs2_l500_visual_preset
For L500 devices: provides optimized settings (presets) for specific types of usage.
Definition: rs_option.h:151


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:39