cah-model.cpp
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 <algorithm>
5 #include "cah-model.h"
6 #include "model-views.h"
7 
8 using namespace rs2;
9 
10 // This variable is global for protecting the case when the callback will be called when the device model no longer exist.
11 // TODO: Refactor for handling multiple L515 devices support.
12 static std::atomic<rs2_calibration_status> global_calib_status;
13 
15  _dev_model(dev_model),
16  _viewer(viewer),
17  _state(model_state_type::TRIGGER_MODAL),
18  _process_started(false),
19  _process_timeout(std::chrono::seconds(30))
20 {
22 
23  // Register AC status change callback
24  device_calibration dev_cal(dev_model.dev);
26  {
27  global_calib_status = cal_status;
28  });
29 }
30 
31 
33 {
34  // This process is built from a 2 stages windows, first a yes/no window and then a process window
35  bool keep_showing = true;
36  bool yes_was_chosen = false;
37 
38  switch (_state.load())
39  {
41  {
42  // Make sure the firmware meets the minimal version for Trigger Camera Accuracy features
43  const std::string& min_fw_version("1.5.0.0");
45  bool is_depth_streaming = std::any_of(_dev_model.subdevices.begin(), _dev_model.subdevices.end(), [](const std::shared_ptr<subdevice_model>& sm) { return sm->streaming && sm->s->as<depth_sensor>(); });
46  bool is_color_streaming = std::any_of(_dev_model.subdevices.begin(), _dev_model.subdevices.end(), [](const std::shared_ptr<subdevice_model>& sm) { return sm->streaming && sm->s->as<color_sensor>(); });
47  bool auto_cah_is_working = RS2_CALIBRATION_SUCCESSFUL != global_calib_status
51 
52  std::string message_text = "Camera Accuracy Health will ensure you get the highest accuracy from your camera.\n\n"
53  "This process may take several minutes and requires special setup to get good results.\n"
54  "While it is working, the viewer will not be usable.";
55 
56  std::string disable_reason_text;
57  if (fw_upgrade_needed)
58  {
59  disable_reason_text = "Camera Accuracy Health requires a minimal FW version of " + min_fw_version +
60  "\n\nPlease update your firmware and try again. ";
61  }
62  else if (!is_depth_streaming || !is_color_streaming)
63  {
64  disable_reason_text = "Camera Accuracy Health cannot be triggered : both depth & RGB streams must be active.";
65  }
66  else if (auto_cah_is_working)
67  {
68  disable_reason_text = "Camera Accuracy Health is already in progress in the background.\n"
69  "Please try again in a few minutes. ";
70  }
71  else
72  {
73  message_text += "\n\nAre you sure you want to continue?";
74  }
75 
76  bool option_disabled = !is_depth_streaming || !is_color_streaming || auto_cah_is_working || fw_upgrade_needed;
77  if (yes_no_dialog("Camera Accuracy Health Trigger", message_text, yes_was_chosen, window, error_message, option_disabled, disable_reason_text))
78  {
79  if (yes_was_chosen)
80  {
81 
82  auto itr = std::find_if(_dev_model.subdevices.begin(), _dev_model.subdevices.end(), [](std::shared_ptr<subdevice_model> sub)
83  {
84  if (sub->s->as<depth_sensor>())
85  return true;
86  return false;
87  });
88 
89 
90  if (is_depth_streaming && is_color_streaming && itr != _dev_model.subdevices.end())
91  {
92  auto sd = *itr;
93  global_calib_status = RS2_CALIBRATION_RETRY; // To indicate in progress state
94  try
95  {
96  sd->s->set_option(RS2_OPTION_TRIGGER_CAMERA_ACCURACY_HEALTH, static_cast<float>(RS2_CAH_TRIGGER_NOW));
97  }
98  catch( std::exception const & e )
99  {
100  error_message = to_string() << "Trigger calibration failure:\n" << e.what();
101  _process_started = false;
103  return false;
104  }
105 
107  // We switch to process state without a guarantee that the process really started,
108  // Set a timeout to make sure if it is not started we will allow closing the window.
110 
111  }
112  }
113  else
114  {
115  keep_showing = false;
116  }
117  }
118  }
119  break;
120 
122  {
123  if (!_process_started)
124  {
125  // Indication of calibration process start
129  }
130 
131  bool process_finished = global_calib_status == RS2_CALIBRATION_SUCCESSFUL
135 
136  static std::map<rs2_calibration_status, std::string> status_map{
137  {RS2_CALIBRATION_TRIGGERED , "In Progress" },
138  {RS2_CALIBRATION_SPECIAL_FRAME , "In Progress" },
139  {RS2_CALIBRATION_STARTED , "In Progress" },
140  {RS2_CALIBRATION_NOT_NEEDED , "Ended" },
141  {RS2_CALIBRATION_SUCCESSFUL , "Ended Successfully" },
142  {RS2_CALIBRATION_BAD_CONDITIONS , "Invalid Conditions" },
143  {RS2_CALIBRATION_RETRY , "In Progress" },
144  {RS2_CALIBRATION_FAILED , "Ended With Failure" },
145  {RS2_CALIBRATION_SCENE_INVALID , "In Progress" },
146  {RS2_CALIBRATION_BAD_RESULT , "In Progress" } };
147 
148  rs2_calibration_status calibration_status = global_calib_status;
149 
150  // We don't know if AC really started working so we add a timeout for not blocking the viewer forever.
151  if (!_process_started)
152  {
154  {
155  process_finished = true; // on timeout display failure and allow closing the window
156  calibration_status = RS2_CALIBRATION_FAILED;
157  }
158  }
159 
160  const std::string & message = process_finished ? " " :
161  "Camera Accuracy Health is In progress, this may take a while...";
162  if (status_dialog("Camera Accuracy Health Status", message, status_map[calibration_status], process_finished, window))
163  {
164  keep_showing = false;
165  }
166  }
167  break;
168  default:
169  break;
170 
171  }
172 
173  //reset internal elements for next process
174  if (!keep_showing)
175  {
177  _process_started = false;
178  }
179  return keep_showing;
180 }
181 
183 {
184  bool keep_showing = true;
185  bool yes_was_chosen = false;
186 
187  std::string message_text("This will reset the camera settings to their factory-calibrated state.\nYou will lose any improvements made with Camera Accuracy Health.\n\n Are you sure?");
188  if (yes_no_dialog("Camera Accuracy Health Reset", message_text, yes_was_chosen, window, error_message))
189  {
190  if (yes_was_chosen)
191  {
192  auto itr = std::find_if(_dev_model.subdevices.begin(), _dev_model.subdevices.end(), [](std::shared_ptr<subdevice_model> sub)
193  {
194  if (sub->s->as<depth_sensor>())
195  return true;
196  return false;
197  });
198 
199 
200  if (itr != _dev_model.subdevices.end())
201  {
202  auto sd = *itr;
203  // Trigger CAH process
204  try
205  {
206  sd->s->set_option(RS2_OPTION_RESET_CAMERA_ACCURACY_HEALTH, 1.0f);
207  }
208  catch (std::exception const & e)
209  {
210  error_message = to_string() << "Calibration reset failure:\n" << e.what();
211  }
212  }
213  }
214  keep_showing = false;
215  }
216 
217  return keep_showing;
218 }
GLenum GLuint GLenum GLsizei const GLchar * message
bool prompt_trigger_popup(ux_window &window, std::string &error_message)
Definition: cah-model.cpp:32
device_model & _dev_model
Definition: cah-model.h:25
bool status_dialog(const std::string &title, const std::string &process_topic_text, const std::string &process_status_text, bool enable_close, ux_window &window)
void start() const
Definition: timer.h:24
Definition: cah-model.h:10
GLsizei const GLchar *const * string
e
Definition: rmse.py:177
utilities::time::timer _process_timeout
Definition: cah-model.h:30
GLdouble f
rs2_calibration_status
Definition: rs_device.h:356
cah_model(device_model &dev_model, viewer_model &viewer)
Definition: cah-model.cpp:14
const char * get_info(rs2_camera_info info) const
Definition: rs_device.hpp:79
static std::atomic< rs2_calibration_status > global_calib_status
Definition: cah-model.cpp:12
bool has_expired() const
Definition: timer.h:30
bool yes_no_dialog(const std::string &title, const std::string &message_text, bool &approved, ux_window &window, const std::string &error_message, bool disabled, const std::string &disabled_reason)
bool _process_started
Definition: cah-model.h:29
std::atomic< model_state_type > _state
Definition: cah-model.h:28
std::vector< std::shared_ptr< subdevice_model > > subdevices
Definition: model-views.h:794
bool prompt_reset_popup(ux_window &window, std::string &error_message)
Definition: cah-model.cpp:182
bool is_upgradeable(const std::string &curr, const std::string &available)
void register_calibration_change_callback(T callback)
Definition: rs_device.hpp:596
std::string to_string(T value)


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