forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rs-enumerate-devices.cpp
505 lines (447 loc) · 19.7 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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// 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 <cmath>
#include <limits>
#include <thread>
#include "tclap/CmdLine.h"
using namespace std;
using namespace TCLAP;
using namespace rs2;
std::vector<std::string> tokenize_floats(string input, char separator){
std::vector<std::string> tokens;
stringstream ss(input);
string token;
while (std::getline(ss, token, separator)) {
tokens.push_back(token);
}
return tokens;
}
void print(const rs2_extrinsics& extrinsics)
{
stringstream ss;
ss << " Rotation Matrix:\n";
// Align displayed data along decimal point
for (auto i = 0 ; i < 3 ; ++i)
{
for (auto j = 0 ; j < 3 ; ++j)
{
std::ostringstream oss;
oss << extrinsics.rotation[j*3 +i];
auto tokens = tokenize_floats(oss.str().c_str(),'.');
ss << right << setw(4) << tokens[0];
if (tokens.size()>1)
ss << "." << left <<setw(12) << tokens[1];
}
ss << endl;
}
ss << "\n Translation Vector: ";
for (auto i = 0u ; 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: \t";
for (auto i = 0u ; i < sizeof(intrinsics.bias_variances)/sizeof(intrinsics.bias_variances[0]) ; ++i)
ss << setprecision(15) << std::fixed << intrinsics.bias_variances[i] << " ";
ss << "\nNoise Variances: \t";
for (auto i = 0u ; i < sizeof(intrinsics.noise_variances)/sizeof(intrinsics.noise_variances[0]) ; ++i)
ss << setprecision(15) << std::fixed << intrinsics.noise_variances[i] << " ";
ss << "\nSensitivity : " << std::endl;
for (auto i = 0u ; i < sizeof(intrinsics.data)/sizeof(intrinsics.data[0]) ; ++i)
{
for (auto j = 0u ; j < sizeof(intrinsics.data[0])/sizeof(intrinsics.data[0][0]) ; ++j)
ss << std::right << std::setw(13) << setprecision(6) << 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 = 0u ; 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 &&
(fabs(lhs.ppx - rhs.ppx) < std::numeric_limits<float>::min()) &&
(fabs(lhs.ppy - rhs.ppy) < std::numeric_limits<float>::min()) &&
(fabs(lhs.fx - rhs.fx) < std::numeric_limits<float>::min()) &&
(fabs(lhs.fy - rhs.fy) < std::numeric_limits<float>::min()) &&
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 tool", ' ', RS2_API_VERSION_STR);
SwitchArg compact_view_arg("s", "short", "Provide short summary of the devices");
SwitchArg show_options_arg("o", "option", "Show all supported options per subdevice");
SwitchArg show_calibration_data_arg("c", "calib_data", "Show extrinsic and intrinsic of all subdevices");
SwitchArg show_defaults("d", "defaults", "Show default streams configuration");
ValueArg<string> show_playback_device_arg("p", "playback_device", "Inspect and enumerate playback device (from file)",
false, "", "Playback device - ROSBag record full path");
cmd.add(compact_view_arg);
cmd.add(show_options_arg);
cmd.add(show_calibration_data_arg);
cmd.add(show_defaults);
cmd.add(show_playback_device_arg);
cmd.parse(argc, argv);
log_to_console(RS2_LOG_SEVERITY_ERROR);
bool compact_view = compact_view_arg.getValue();
bool show_options = show_options_arg.getValue();
bool show_calibration_data = show_calibration_data_arg.getValue();
bool show_modes = !compact_view;
auto playback_dev_file = show_playback_device_arg.getValue();
// Obtain a list of devices currently present on the system
context ctx;
rs2::device d;
if (!playback_dev_file.empty())
d = ctx.load_device(playback_dev_file.data());
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)
{
cout << left << setw(30) << "Device Name"
<< setw(20) << "Serial Number"
<< setw(20) << "Firmware Version"
<< endl;
for (auto i = 0u; 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 || show_calibration_data)
cout << "\n\nNote: \"-s\" option is not compatible with the other flags specified,"
<< " all the additional options are skipped" << endl;
show_options = show_calibration_data = false;
}
log_to_console(RS2_LOG_SEVERITY_FATAL);
for (auto i = 0u; i < device_count; ++i)
{
auto dev = devices[i];
// Show which options are supported by this device
cout << "Device info: \n";
if (auto pb_dev = dev.as<playback>())
cout << "Playback Device (" << pb_dev.file_name() << ")" << std::endl;
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_defaults.getValue())
{
if (dev.supports(RS2_CAMERA_INFO_SERIAL_NUMBER))
{
cout << "Default streams:" << endl;
config cfg;
cfg.enable_device(dev.get_info(RS2_CAMERA_INFO_SERIAL_NUMBER));
pipeline p;
auto profile = cfg.resolve(p);
for (auto&& sp : profile.get_streams())
{
cout << " " << sp.stream_name() << " as " << sp.format() << " at " << sp.fps() << " Hz";
if (auto vp = sp.as<video_stream_profile>())
{
cout << "; Resolution: " << vp.width() << "x" << vp.height();
}
cout << endl;
}
}
else
{
cout << "Cannot list default streams since the device does not provide a serial number!" << endl;
}
cout << endl;
}
if (show_options)
{
for (auto&& sensor : dev.query_sensors())
{
cout << "Options for " << sensor.get_info(RS2_CAMERA_INFO_NAME) << endl;
cout << setw(35) << " 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))
{
try
{
auto range = sensor.get_option_range(opt);
cout << " " << left << setw(30) << opt << " : "
<< setw(5) << range.min << "... " << setw(12) << range.max
<< setw(6) << range.step << setw(10) << range.def << "\n";
}
catch (const error & e)
{
cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << endl;
}
}
}
cout << endl;
}
}
if (show_modes)
{
for (auto&& sensor : dev.query_sensors())
{
cout << "Stream Profiles supported by " << sensor.get_info(RS2_CAMERA_INFO_NAME) << endl;
cout << " Supported modes:\n" << setw(16) << " stream" << setw(16)
<< " resolution" << setw(10) << " 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() << setw(6) << "Hz\t" << profile.format() << endl;
}
else
{
cout << " " << profile.stream_name() << "\t N/A\t\t@ " << profile.fps()
<< setw(6) << "Hz\t" << profile.format() << endl;
}
}
cout << endl;
}
}
// Print Intrinsics
if (show_calibration_data)
{
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
}
}
}
else
{
if (pose_stream_profile pose = profile.as<pose_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;
}
}
else
{
cout << " Unhandled profile encountered: " << profile.stream_name() << "/" << profile.format() << std::endl;
}
}
}
}
}
if (intrinsics_map.size())
{
cout << "Intrinsic Parameters:\n" << 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 << "\" / " << stream_res.width << "x"
<< stream_res.height << " / " << formats << endl;
if (intrinsics.second == rs2_intrinsics{})
{
cout << "Intrinsic NOT available!\n\n";
}
else
{
print(intrinsics.second);
}
}
}
}
if (motion_intrinsics_map.size())
{
cout << "Motion Intrinsic Parameters:\n" << 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);
}
}
}
}
if (streams.size())
{
// Print Extrinsics
cout << "\nExtrinsic Parameters:" << 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";
}
}
}
}
}
}
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;
}