forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrs-enumerate-devices.cpp
436 lines (380 loc) · 16.5 KB
/
rs-enumerate-devices.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2015 Intel Corporation. All Rights Reserved.
#include <librealsense2/rs.hpp>
#include <iostream>
#include <iomanip>
#include <map>
#include <set>
#include <cstring>
#include "tclap/CmdLine.h"
using namespace std;
using namespace TCLAP;
using namespace rs2;
void print(const rs2_extrinsics& extrinsics)
{
stringstream ss;
ss << "Rotation Matrix:\n";
for (auto i = 0 ; i < 3 ; ++i)
{
for (auto j = 0 ; j < 3 ; ++j)
{
ss << left << setw(15) << setprecision(5) << extrinsics.rotation[j*3 +i];
}
ss << endl;
}
ss << "\nTranslation Vector: ";
for (auto i = 0 ; i < sizeof(extrinsics.translation)/sizeof(extrinsics.translation[0]) ; ++i)
ss << setprecision(15) << extrinsics.translation[i] << " ";
cout << ss.str() << endl << endl;
}
void print(const rs2_motion_device_intrinsic& intrinsics)
{
stringstream ss;
ss << "Bias Variances: ";
for (auto i = 0 ; i < sizeof(intrinsics.bias_variances)/sizeof(intrinsics.bias_variances[0]) ; ++i)
ss << setprecision(15) << intrinsics.bias_variances[i] << " ";
ss << "\nNoise Variances: ";
for (auto i = 0 ; i < sizeof(intrinsics.noise_variances)/sizeof(intrinsics.noise_variances[0]) ; ++i)
ss << setprecision(15) << intrinsics.noise_variances[i] << " ";
ss << "\nData: " << std::endl;
for (auto i = 0 ; i < sizeof(intrinsics.data)/sizeof(intrinsics.data[0]) ; ++i)
{
for (auto j = 0 ; j < sizeof(intrinsics.data[0])/sizeof(intrinsics.data[0][0]) ; ++j)
ss << std::setw(13) << setprecision(10) << intrinsics.data[i][j] << " ";
ss << "\n";
}
cout << ss.str() << endl << endl;
}
void print(const rs2_intrinsics& intrinsics)
{
stringstream ss;
ss << left << setw(14) << "Width: " << "\t" << intrinsics.width << endl <<
left << setw(14) << "Height: " << "\t" << intrinsics.height << endl <<
left << setw(14) << "PPX: " << "\t" << setprecision(15) << intrinsics.ppx << endl <<
left << setw(14) << "PPY: " << "\t" << setprecision(15) << intrinsics.ppy << endl <<
left << setw(14) << "Fx: " << "\t" << setprecision(15) << intrinsics.fx << endl <<
left << setw(14) << "Fy: " << "\t" << setprecision(15) << intrinsics.fy << endl <<
left << setw(14) << "Distortion: " << "\t" << rs2_distortion_to_string(intrinsics.model) << endl <<
left << setw(14) << "Coeffs: ";
for (auto i = 0 ; i < sizeof(intrinsics.coeffs)/sizeof(intrinsics.coeffs[0]) ; ++i)
ss << "\t" << setprecision(15) << intrinsics.coeffs[i] << " ";
cout << ss.str() << endl << endl;
}
bool safe_get_intrinsics(const video_stream_profile& profile, rs2_intrinsics& intrinsics)
{
bool ret = false;
try
{
intrinsics = profile.get_intrinsics();
ret = true;
}
catch(...)
{}
return ret;
}
bool safe_get_motion_intrinsics(const motion_stream_profile& profile, rs2_motion_device_intrinsic& intrinsics)
{
bool ret = false;
try
{
intrinsics = profile.get_motion_intrinsics();
ret = true;
}
catch(...)
{}
return ret;
}
struct stream_and_resolution{
rs2_stream stream;
int stream_index;
int width;
int height;
string stream_name;
bool operator <(const stream_and_resolution& obj) const
{
return (std::make_tuple(stream, stream_index, width, height) < std::make_tuple(obj.stream, obj.stream_index, obj.width, obj.height));
}
};
struct stream_and_index{
rs2_stream stream;
int stream_index;
bool operator <(const stream_and_index& obj) const
{
return (std::make_tuple(stream, stream_index) < std::make_tuple(obj.stream, obj.stream_index));
}
};
bool operator ==(const rs2_intrinsics& lhs,
const rs2_intrinsics& rhs)
{
return lhs.width == rhs.width &&
lhs.height == rhs.height &&
lhs.ppx == rhs.ppx &&
lhs.ppy == rhs.ppy &&
lhs.fx == rhs.fx &&
lhs.fy == rhs.fy &&
lhs.model == rhs.model &&
!std::memcmp(lhs.coeffs, rhs.coeffs, sizeof(rhs.coeffs));
}
bool operator ==(const rs2_motion_device_intrinsic& lhs,
const rs2_motion_device_intrinsic& rhs)
{
return !std::memcmp(&lhs, &rhs, sizeof(rs2_motion_device_intrinsic));
}
string get_str_formats(const set<rs2_format>& formats)
{
stringstream ss;
for (auto format = formats.begin(); format != formats.end(); ++format)
{
ss << *format << ((format != formats.end()) && (next(format) == formats.end())?"":"/");
}
return ss.str();
}
int main(int argc, char** argv) try
{
CmdLine cmd("librealsense rs-enumerate-devices example tool", ' ', RS2_API_VERSION_STR);
SwitchArg compact_view_arg("s", "short", "Provide short summary of the devices");
SwitchArg show_options("o", "option", "Show all supported options per subdevice");
SwitchArg show_modes("m", "modes", "Show all supported stream modes per subdevice");
SwitchArg show_calibration_data("c", "calib_data", "Show extrinsic and intrinsic of all subdevices");
cmd.add(compact_view_arg);
cmd.add(show_options);
cmd.add(show_modes);
cmd.add(show_calibration_data);
cmd.parse(argc, argv);
log_to_console(RS2_LOG_SEVERITY_ERROR);
// Obtain a list of devices currently present on the system
context ctx;
auto devices = ctx.query_devices();
size_t device_count = devices.size();
if (!device_count)
{
cout <<"No device detected. Is it plugged in?\n";
return EXIT_SUCCESS;
}
if (compact_view_arg.getValue())
{
cout << left << setw(30) << "Device Name"
<< setw(20) << "Serial Number"
<< setw(20) << "Firmware Version"
<< endl;
for (auto i = 0; i < device_count; ++i)
{
auto dev = devices[i];
cout << left << setw(30) << dev.get_info(RS2_CAMERA_INFO_NAME)
<< setw(20) << dev.get_info(RS2_CAMERA_INFO_SERIAL_NUMBER)
<< setw(20) << dev.get_info(RS2_CAMERA_INFO_FIRMWARE_VERSION)
<< endl;
}
if (show_options.getValue() || show_modes.getValue())
cout << "\n\nNote: \"-s\" option is not compatible with the other flags specified,"
<< " all the additional options are skipped" << endl;
return EXIT_SUCCESS;
}
for (auto i = 0; i < device_count; ++i)
{
auto dev = devices[i];
// Show which options are supported by this device
cout << " Device info: \n";
for (auto j = 0; j < RS2_CAMERA_INFO_COUNT; ++j)
{
auto param = static_cast<rs2_camera_info>(j);
if (dev.supports(param))
cout << " " << left << setw(30) << rs2_camera_info_to_string(rs2_camera_info(param))
<< ": \t" << dev.get_info(param) << endl;
}
cout << endl;
if (show_options.getValue())
{
for (auto&& sensor : dev.query_sensors())
{
cout << "Options for " << sensor.get_info(RS2_CAMERA_INFO_NAME) << endl;
cout << setw(55) << " Supported options:" << setw(10) << "min" << setw(10)
<< " max" << setw(6) << " step" << setw(10) << " default" << endl;
for (auto j = 0; j < RS2_OPTION_COUNT; ++j)
{
auto opt = static_cast<rs2_option>(j);
if (sensor.supports(opt))
{
auto range = sensor.get_option_range(opt);
cout << " " << left << setw(50) << opt << " : "
<< setw(5) << range.min << "... " << setw(12) << range.max
<< setw(6) << range.step << setw(10) << range.def << "\n";
}
}
cout << endl;
}
}
if (show_modes.getValue())
{
for (auto&& sensor : dev.query_sensors())
{
cout << "Stream Profiles supported by " << sensor.get_info(RS2_CAMERA_INFO_NAME) << endl;
cout << setw(55) << " Supported modes:" << setw(10) << "stream" << setw(10)
<< " resolution" << setw(6) << " fps" << setw(10) << " format" << endl;
// Show which streams are supported by this device
for (auto&& profile : sensor.get_stream_profiles())
{
if (auto video = profile.as<video_stream_profile>())
{
cout << " " << profile.stream_name() << "\t " << video.width() << "x"
<< video.height() << "\t@ " << profile.fps() << "Hz\t" << profile.format() << endl;
}
else
{
cout << " " << profile.stream_name() << "\t@ " << profile.fps() << "Hz\t" << profile.format() << endl;
}
}
cout << endl;
}
}
for (auto&& sensor : dev.query_sensors())
{
cout << "Stream Profiles supported by " << sensor.get_info(RS2_CAMERA_INFO_NAME) << endl;
cout << setw(55) << " Supported modes:" << setw(10) << "stream" << setw(10)
<< " resolution" << setw(6) << " fps" << setw(10) << " format" << endl;
// Show which streams are supported by this device
for (auto&& profile : sensor.get_stream_profiles())
{
if (auto video = profile.as<video_stream_profile>())
{
cout << " " << profile.stream_name() << "\t " << video.width() << "x"
<< video.height() << "\t@ " << profile.fps() << "Hz\t" << profile.format() << endl;
}
else
{
cout << " " << profile.stream_name() << "\t@ " << profile.fps() << "Hz\t" << profile.format() << endl;
}
}
cout << endl;
}
// Print Intrinsics
if (show_calibration_data.getValue())
{
std::map<stream_and_index, stream_profile> streams;
std::map<stream_and_resolution, std::vector<std::pair<std::set<rs2_format>, rs2_intrinsics>>> intrinsics_map;
std::map<stream_and_resolution, std::vector<std::pair<std::set<rs2_format>, rs2_motion_device_intrinsic>>> motion_intrinsics_map;
for (auto&& sensor : dev.query_sensors())
{
// Intrinsics
for (auto&& profile : sensor.get_stream_profiles())
{
if (auto video = profile.as<video_stream_profile>())
{
if (streams.find(stream_and_index{profile.stream_type(), profile.stream_index()}) == streams.end())
{
streams[stream_and_index{profile.stream_type(), profile.stream_index()}] = profile;
}
rs2_intrinsics intrinsics{};
stream_and_resolution stream_res{profile.stream_type(), profile.stream_index(), video.width(), video.height(), profile.stream_name()};
if (safe_get_intrinsics(video, intrinsics))
{
auto it = std::find_if((intrinsics_map[stream_res]).begin(), (intrinsics_map[stream_res]).end(), [&](const std::pair<std::set<rs2_format>, rs2_intrinsics>& kvp) {
return intrinsics == kvp.second;
});
if (it == (intrinsics_map[stream_res]).end())
{
(intrinsics_map[stream_res]).push_back({ {profile.format()}, intrinsics });
}
else
{
it->first.insert(profile.format()); // If the intrinsics are equals, add the profile format to format set
}
}
}
else
{
if (motion_stream_profile motion = profile.as<motion_stream_profile>())
{
if (streams.find(stream_and_index{profile.stream_type(), profile.stream_index()}) == streams.end())
{
streams[stream_and_index{profile.stream_type(), profile.stream_index()}] = profile;
}
rs2_motion_device_intrinsic motion_intrinsics{};
stream_and_resolution stream_res{profile.stream_type(), profile.stream_index(), motion.stream_type(), motion.stream_index(), profile.stream_name()};
if (safe_get_motion_intrinsics(motion, motion_intrinsics))
{
auto it = std::find_if((motion_intrinsics_map[stream_res]).begin(), (motion_intrinsics_map[stream_res]).end(),
[&](const std::pair<std::set<rs2_format>, rs2_motion_device_intrinsic>& kvp)
{
return motion_intrinsics == kvp.second;
});
if (it == (motion_intrinsics_map[stream_res]).end())
{
(motion_intrinsics_map[stream_res]).push_back({ {profile.format()}, motion_intrinsics });
}
else
{
it->first.insert(profile.format()); // If the intrinsics are equals, add the profile format to format set
}
}
}
}
}
}
cout << "Provided Intrinsic:" << endl;
for (auto& kvp : intrinsics_map)
{
auto stream_res = kvp.first;
for (auto& intrinsics : kvp.second)
{
auto formats = get_str_formats(intrinsics.first);
cout << "Intrinsic of \"" << stream_res.stream_name << "\"\t " << stream_res.width << "x"
<< stream_res.height << "\t " << formats << endl;
if (intrinsics.second == rs2_intrinsics{})
{
cout << "Intrinsic NOT available!\n\n";
}
else
{
print(intrinsics.second);
}
}
}
cout << "Provided Motion Intrinsic:" << endl;
for (auto& kvp : motion_intrinsics_map)
{
auto stream_res = kvp.first;
for (auto& intrinsics : kvp.second)
{
auto formats = get_str_formats(intrinsics.first);
cout << "Motion Intrinsic of \"" << stream_res.stream_name << "\"\t " << formats << endl;
if (intrinsics.second == rs2_motion_device_intrinsic{})
{
cout << "Intrinsic NOT available!\n\n";
}
else
{
print(intrinsics.second);
}
}
}
// Print Extrinsics
cout << "\nProvided Extrinsic:" << endl;
rs2_extrinsics extrinsics{};
for (auto kvp1 = streams.begin(); kvp1 != streams.end(); ++kvp1)
{
for (auto kvp2 = streams.begin(); kvp2 != streams.end(); ++kvp2)
{
cout << "Extrinsic from \"" << kvp1->second.stream_name() << "\"\t " <<
"To" << "\t \"" << kvp2->second.stream_name() << "\" :\n";
try
{
extrinsics = kvp1->second.get_extrinsics_to(kvp2->second);
print(extrinsics);
}
catch (...)
{
cout << "N/A\n";
}
}
}
}
}
cout << endl;
return EXIT_SUCCESS;
}
catch (const error & e)
{
cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << endl;
return EXIT_FAILURE;
}