Skip to content

Commit

Permalink
PR #7895 from Matan: serializable_device preset bug fix & unit-test
Browse files Browse the repository at this point in the history
  • Loading branch information
maloel committed Dec 21, 2020
2 parents dcd020c + dfcfcea commit 82ca18e
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 2 deletions.
38 changes: 37 additions & 1 deletion src/l500/l500-serializable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "l500-serializable.h"
#include "../../../third-party/json.hpp"
#include <set>

namespace librealsense
{
Expand Down Expand Up @@ -34,11 +35,46 @@ namespace librealsense
{
json j = json::parse(json_content);

// Set of options that should not be set in the loop
std::set< rs2_option > options_to_ignore{ RS2_OPTION_SENSOR_MODE };

// We have to set the sensor mode (resolution) first
auto & sensor_mode = _depth_sensor.get_option( RS2_OPTION_SENSOR_MODE );
auto found_sensor_mode = j.find( get_string( RS2_OPTION_SENSOR_MODE ) );
if( found_sensor_mode != j.end() )
{
float sensor_mode_val = found_sensor_mode.value();
sensor_mode.set( sensor_mode_val );
}

// If a non custom preset is used, we should ignore all the settings that are automatically
// set by the preset
auto found_iterator = j.find( get_string( RS2_OPTION_VISUAL_PRESET ) );
if( found_iterator != j.end() )
{
auto found_preset = rs2_l500_visual_preset( int( found_iterator.value() ));
if( found_preset != RS2_L500_VISUAL_PRESET_CUSTOM)
{
options_to_ignore.insert( RS2_OPTION_POST_PROCESSING_SHARPENING );
options_to_ignore.insert( RS2_OPTION_PRE_PROCESSING_SHARPENING );
options_to_ignore.insert( RS2_OPTION_NOISE_FILTERING );
options_to_ignore.insert( RS2_OPTION_AVALANCHE_PHOTO_DIODE );
options_to_ignore.insert( RS2_OPTION_CONFIDENCE_THRESHOLD );
options_to_ignore.insert( RS2_OPTION_LASER_POWER );
options_to_ignore.insert( RS2_OPTION_MIN_DISTANCE );
options_to_ignore.insert( RS2_OPTION_INVALIDATION_BYPASS );
options_to_ignore.insert( RS2_OPTION_DIGITAL_GAIN );
}
}

auto opts = _depth_sensor.get_supported_options();
for (auto o: opts)
{
auto& opt = _depth_sensor.get_option(o);
if (opt.is_read_only()) continue;
if (opt.is_read_only())
continue;
if (options_to_ignore.find(o) != options_to_ignore.end())
continue;

auto key = get_string(o);
auto it = j.find(key);
Expand Down
52 changes: 52 additions & 0 deletions unit-tests/func/serializable-device/test-l500-json-load.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import pyrealsense2 as rs, test

devices = test.find_devices_by_product_line_or_exit(rs.product_line.L500)
device = devices[0]
depth_sensor = device.first_depth_sensor()
sd = rs.serializable_device(device)

visual_preset_number = depth_sensor.get_option(rs.option.visual_preset)
visual_preset_name = rs.l500_visual_preset(int(visual_preset_number))

#############################################################################################
# The default preset is scheduled to be removed, once that happens this test is irrelevant and should be removed
if visual_preset_name == rs.l500_visual_preset.default:
test.start("Trying to load default settings from json. Should fail")
try:
serialized_json = sd.serialize_json()
sd.load_json(serialized_json)
except RuntimeError as e:
test.check_exception(e, RuntimeError, "The Default preset signifies that the controls have not been changed"
" since initialization, the API does not support changing back to this"
" state, Please choose one of the other presets")
else:
test.unexpected_exception()
test.finish()

#############################################################################################
test.start("Trying to load non default presets")
presets = [rs.l500_visual_preset.custom,
rs.l500_visual_preset.no_ambient_light,
rs.l500_visual_preset.low_ambient_light,
rs.l500_visual_preset.max_range,
rs.l500_visual_preset.short_range]

for preset in presets:
try:
depth_sensor.set_option(rs.option.visual_preset, int(preset))
serialized_json = sd.serialize_json()
# Changing the preset to make sure the load function changes it back
if preset == rs.l500_visual_preset.custom:
depth_sensor.set_option(rs.option.visual_preset, int(rs.l500_visual_preset.short_range))
else:
depth_sensor.set_option(rs.option.visual_preset, int(rs.l500_visual_preset.custom))
sd.load_json(serialized_json)
visual_preset_number = depth_sensor.get_option(rs.option.visual_preset)
visual_preset_name = rs.l500_visual_preset(int(visual_preset_number))
test.check_equal(visual_preset_name, preset)
except:
test.unexpected_exception()
test.finish()

#############################################################################################
test.print_results_and_exit()
2 changes: 1 addition & 1 deletion unit-tests/py/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def check_equal(result, expected, abort_if_failed = False):
global n_assertions
n_assertions += 1
if result != expected:
print("Result was:" + result + "\nBut we expected: " + expected)
print("Result was:", result ,"\nBut we expected: ", expected)
check_failed()
print_stack()
if abort_if_failed:
Expand Down
1 change: 1 addition & 0 deletions wrappers/python/c_files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ void init_c_files(py::module &m) {
py_rs2_option.value("ambient_light", RS2_OPTION_AMBIENT_LIGHT);
py_rs2_option.value( "lld_temperature", RS2_OPTION_LLD_TEMPERATURE );

BIND_ENUM(m, rs2_l500_visual_preset, RS2_L500_VISUAL_PRESET_COUNT, "For L500 devices: provides optimized settings (presets) for specific types of usage.")
BIND_ENUM(m, rs2_playback_status, RS2_PLAYBACK_STATUS_COUNT, "") // No docstring in C++
BIND_ENUM(m, rs2_calibration_type, RS2_CALIBRATION_TYPE_COUNT, "Calibration type for use in device_calibration")
BIND_ENUM_CUSTOM(m, rs2_calibration_status, RS2_CALIBRATION_STATUS_FIRST, RS2_CALIBRATION_STATUS_LAST, "Calibration callback status for use in device_calibration.trigger_device_calibration")
Expand Down
8 changes: 8 additions & 0 deletions wrappers/python/pyrs_advanced_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Copyright(c) 2017 Intel Corporation. All Rights Reserved. */

#include "python.hpp"
#include "../include/librealsense2/rs_advanced_mode.hpp"
#include "../include/librealsense2/hpp/rs_serializable_device.hpp"

void init_advanced_mode(py::module &m) {
/** RS400 Advanced Mode commands **/
Expand Down Expand Up @@ -253,3 +254,10 @@ void init_advanced_mode(py::module &m) {
.def("serialize_json", &rs400::advanced_mode::serialize_json)
.def("load_json", &rs400::advanced_mode::load_json, "json_content"_a);
}

void init_serializable_device(py::module& m) {
py::class_<rs2::serializable_device> serializable_device(m, "serializable_device");
serializable_device.def(py::init<rs2::device>(), "device"_a)
.def("serialize_json", &rs2::serializable_device::serialize_json)
.def("load_json", &rs2::serializable_device::load_json, "json_content"_a);
}
1 change: 1 addition & 0 deletions wrappers/python/python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ PYBIND11_MODULE(NAME, m) {
init_internal(m); // must be run after init_frame()
init_export(m);
init_advanced_mode(m);
init_serializable_device(m);
init_util(m);

/** rs_export.hpp **/
Expand Down
1 change: 1 addition & 0 deletions wrappers/python/python.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ void init_pipeline(py::module &m);
void init_internal(py::module &m);
void init_export(py::module &m);
void init_advanced_mode(py::module &m);
void init_serializable_device(py::module& m);
void init_util(py::module &m);

#endif // LIBREALSENSE_PYTHON_HPP

0 comments on commit 82ca18e

Please sign in to comment.