rs-sensor-control.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2017 Intel Corporation. All Rights Reserved.
3 
4 #include <iostream>
5 #include <iomanip>
6 #include <map>
7 #include <utility>
8 #include <vector>
9 #include <librealsense2/rs.hpp>
10 #include <algorithm>
11 #include "../example.hpp"
12 #include "api_how_to.h"
13 #include "helper.h"
14 
15 // Forward declaration of the create_sensor_actions which returns a collection of functions
16 // and their description that operate on a device or sensor
17 using sensor_action = std::pair<std::function<void(rs2::device, rs2::sensor)>, std::string>;
18 std::vector<sensor_action> create_sensor_actions();
19 
20 int main(int argc, char * argv[]) try
21 {
22  //In this example we will go through a flow of selecting a device, then selecting one of its sensors,
23  // and finally run some operations on that sensor
24 
25  //We will use the "how_to" class to make the code clear, expressive and encapsulate common actions inside a function
26 
27  std::vector<sensor_action> sensor_actions = create_sensor_actions();
28 
29  bool choose_a_device = true;
30  while (choose_a_device)
31  {
33  //First thing, let's choose a device:
35 
36  //Print the device's information
38 
39  bool choose_a_sensor = true;
40  while (choose_a_sensor)
41  {
43  //Next, choose one of the device's sensors
45 
46  bool control_sensor = true;
47  while (control_sensor)
48  {
50  std::cout << "What would you like to do with the sensor?\n" << std::endl;
51  int i = 0;
52  for (auto&& action : sensor_actions)
53  {
54  std::cout << i++ << " : " << action.second << std::endl;
55  }
56  uint32_t selected_action_index = helper::get_user_selection("Select an action: ");
57  if (selected_action_index >= sensor_actions.size())
58  {
59  throw std::out_of_range("Selected action index is out of range");
60  }
61 
62  auto selected_action = sensor_actions[selected_action_index].first;
63 
64  bool repeat = true;
65  while (repeat)
66  {
68  selected_action(dev, sensor);
69  repeat = prompt_yes_no(sensor_actions[selected_action_index].second + " again?");
70  }
71  control_sensor = prompt_yes_no("Choose another action for this sensor?");
72  }
73 
74  choose_a_sensor = prompt_yes_no("Choose another sensor?");
75  }
76 
77  choose_a_device = prompt_yes_no("Choose another device?");
78  }
79 
80  return EXIT_SUCCESS;
81 }
82 catch (const rs2::error & e)
83 {
84  std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
85  return EXIT_FAILURE;
86 }
87 catch (const std::exception & e)
88 {
89  std::cerr << e.what() << std::endl;
90  return EXIT_FAILURE;
91 }
92 
94 {
95  // The rs2::sensor allows you to control its properties such as Exposure, Brightness etc.
96  rs2_option sensor_option = how_to::get_sensor_option(sensor);
97  how_to::change_sensor_option(sensor, sensor_option);
98 }
100 {
101  // The rs2::sensor allows you to control its streams
102  // We will first choose a single stream profile from the available profiles of the sensor
103  rs2::stream_profile selected_profile = how_to::choose_a_streaming_profile(sensor);
104 
105  // Next, we will display the stream in a window
106  how_to::start_streaming_a_profile(sensor, selected_profile);
107 }
109 {
110  // Each stream has its own intrinsic information, first choose a stream, then display its intrinsics
111  rs2::stream_profile selected_profile = how_to::choose_a_streaming_profile(sensor);
112  how_to::get_field_of_view(selected_profile);
113 }
115 {
116  // A rs2::device can have its sensors and streams calibrated and provide the extrinsics between 2 streams.
117  std::cout << "Please choose a sensor and then a stream that will be used as the origin of extrinsic transformation:\n" << std::endl;
118  rs2::sensor from_sensor = how_to::get_a_sensor_from_a_device(device);
120 
121  std::cout << "Please choose a sensor and then a stream that will be used as the target of extrinsic transformation::\n" << std::endl;
124 
125  how_to::get_extrinsics(from, to);
126 }
127 std::vector<sensor_action> create_sensor_actions()
128 {
129  //This function creates several functions ("sensor_action") that takes a device and a sensor,
130  // and perform some specific action
131  return std::vector<sensor_action> {
132  std::make_pair(&control_sensor_options, "Control sensor's options"),
133  std::make_pair(&display_live_stream, "Control sensor's streams"),
134  std::make_pair(&show_stream_intrinsics, "Show stream intrinsics"),
135  std::make_pair(&show_extrinsics_between_streams, "Display extrinsics")
136  };
137 }
bool prompt_yes_no(const std::string &prompt_msg)
Definition: helper.h:14
rs2_option
Defines general configuration controls. These can generally be mapped to camera UVC controls...
Definition: rs_option.h:22
static const textual_icon repeat
Definition: model-views.h:242
void print_separator()
Definition: helper.h:35
void control_sensor_options(rs2::device device, rs2::sensor sensor)
GLsizei const GLchar *const * string
void show_stream_intrinsics(rs2::device device, rs2::sensor sensor)
e
Definition: rmse.py:177
static void start_streaming_a_profile(const rs2::sensor &sensor, const rs2::stream_profile &stream_profile)
Definition: api_how_to.h:442
int main(int argc, char *argv[])
const std::string & get_failed_args() const
Definition: rs_types.hpp:117
static rs2::sensor get_a_sensor_from_a_device(const rs2::device &dev)
Definition: api_how_to.h:118
std::ostream & cout()
static void print_device_information(const rs2::device &dev)
Definition: api_how_to.h:71
unsigned int uint32_t
Definition: stdint.h:80
static rs2::stream_profile choose_a_streaming_profile(const rs2::sensor &sensor)
Definition: api_how_to.h:349
std::pair< std::function< void(rs2::device, rs2::sensor)>, std::string > sensor_action
void display_live_stream(rs2::device device, rs2::sensor sensor)
action
Definition: enums.py:62
static void change_sensor_option(const rs2::sensor &sensor, rs2_option option_type)
Definition: api_how_to.h:296
uint32_t get_user_selection(const std::string &prompt_message)
Definition: helper.h:26
static void get_extrinsics(const rs2::stream_profile &from_stream, const rs2::stream_profile &to_stream)
Definition: api_how_to.h:277
static void get_field_of_view(const rs2::stream_profile &stream)
Definition: api_how_to.h:209
void show_extrinsics_between_streams(rs2::device device, rs2::sensor sensor)
static rs2_option get_sensor_option(const rs2::sensor &sensor)
Definition: api_how_to.h:147
std::vector< sensor_action > create_sensor_actions()
std::ostream & cerr()
int i
static rs2::device get_a_realsense_device()
Definition: api_how_to.h:22
auto device
Definition: pyrs_net.cpp:17
const std::string & get_failed_function() const
Definition: rs_types.hpp:112


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