Skip to content

Commit 7db1453

Browse files
authored
Added realsense2 playback support (bag) (#1636)
1 parent 93ca4ed commit 7db1453

3 files changed

Lines changed: 190 additions & 147 deletions

File tree

corelib/include/rtabmap/core/camera/CameraRealSense2.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ class RTABMAP_CORE_EXPORT CameraRealSense2 :
146146
Transform dualExtrinsics_;
147147
std::string jsonConfig_;
148148
bool closing_;
149+
bool playback_;
149150

150151
static Transform realsense2PoseRotation_;
151152
static Transform realsense2PoseRotationInv_;

corelib/src/camera/CameraRealSense2.cpp

Lines changed: 95 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030
#include <rtabmap/utilite/UThreadC.h>
3131
#include <rtabmap/utilite/UConversion.h>
3232
#include <rtabmap/utilite/UStl.h>
33+
#include <rtabmap/utilite/UDirectory.h>
3334
#include <opencv2/imgproc/types_c.h>
3435

3536
#ifdef RTABMAP_REALSENSE2
@@ -78,7 +79,8 @@ CameraRealSense2::CameraRealSense2(
7879
cameraDepthFps_(30),
7980
globalTimeSync_(true),
8081
dualMode_(false),
81-
closing_(false)
82+
closing_(false),
83+
playback_(false)
8284
#endif
8385
{
8486
UDEBUG("");
@@ -130,6 +132,7 @@ void CameraRealSense2::close()
130132
}
131133

132134
closing_ = false;
135+
playback_ = false;
133136
}
134137

135138
void CameraRealSense2::imu_callback(rs2::frame frame)
@@ -492,64 +495,76 @@ bool CameraRealSense2::init(const std::string & calibrationFolder, const std::st
492495
clockSyncWarningShown_ = false;
493496
imuGlobalSyncWarningShown_ = false;
494497

495-
rs2::device_list list = ctx_.query_devices();
496-
if (0 == list.size())
498+
if(uStrContains(deviceId_, ".bag"))
497499
{
498-
UERROR("No RealSense2 devices were found!");
499-
return false;
500+
// playback (bag recorded by realsense-viewer)
501+
dev_.resize(1);
502+
dev_[0] = ctx_.load_device(uReplaceChar(deviceId_, '~', UDirectory::homeDir()));
503+
playback_ = true;
504+
UINFO("Device ID is a bag (\"%s\"), using playback mode", deviceId_.c_str());
500505
}
501-
502-
bool found=false;
503-
try
506+
else
504507
{
505-
for (rs2::device dev : list)
508+
rs2::device_list list = ctx_.query_devices();
509+
if (0 == list.size())
506510
{
507-
auto sn = dev.get_info(RS2_CAMERA_INFO_SERIAL_NUMBER);
508-
auto pid_str = dev.get_info(RS2_CAMERA_INFO_PRODUCT_ID);
509-
auto name = dev.get_info(RS2_CAMERA_INFO_NAME);
511+
UERROR("No RealSense2 devices were found!");
512+
return false;
513+
}
510514

511-
uint16_t pid;
512-
std::stringstream ss;
513-
ss << std::hex << pid_str;
514-
ss >> pid;
515-
UINFO("Device \"%s\" with serial number %s was found with product ID=%d.", name, sn, (int)pid);
516-
if(dualMode_ && pid == 0x0B37)
517-
{
518-
// Dual setup: device[0] = D400, device[1] = T265
519-
// T265
520-
dev_.resize(2);
521-
dev_[1] = dev;
522-
}
523-
else if (!found && (deviceId_.empty() || deviceId_ == sn || uStrContains(name, uToUpperCase(deviceId_))))
515+
bool found=false;
516+
try
517+
{
518+
for (rs2::device dev : list)
524519
{
525-
if(dev_.empty())
520+
auto sn = dev.get_info(RS2_CAMERA_INFO_SERIAL_NUMBER);
521+
auto pid_str = dev.get_info(RS2_CAMERA_INFO_PRODUCT_ID);
522+
auto name = dev.get_info(RS2_CAMERA_INFO_NAME);
523+
524+
uint16_t pid;
525+
std::stringstream ss;
526+
ss << std::hex << pid_str;
527+
ss >> pid;
528+
UINFO("Device \"%s\" with serial number %s was found with product ID=%d.", name, sn, (int)pid);
529+
if(dualMode_ && pid == 0x0B37)
526530
{
527-
dev_.resize(1);
531+
// Dual setup: device[0] = D400, device[1] = T265
532+
// T265
533+
dev_.resize(2);
534+
dev_[1] = dev;
535+
}
536+
else if (!found && (deviceId_.empty() || deviceId_ == sn || uStrContains(name, uToUpperCase(deviceId_))))
537+
{
538+
if(dev_.empty())
539+
{
540+
dev_.resize(1);
541+
}
542+
dev_[0] = dev;
543+
found=true;
528544
}
529-
dev_[0] = dev;
530-
found=true;
531545
}
532546
}
533-
}
534-
catch(const rs2::error & error)
535-
{
536-
UWARN("%s. Is the camera already used with another app?", error.what());
537-
}
538-
539-
if (!found)
540-
{
541-
if(dualMode_ && dev_.size()==2)
547+
catch(const rs2::error & error)
542548
{
543-
UERROR("Dual setup is enabled, but a D400 camera is not detected!");
544-
dev_.clear();
549+
UWARN("%s. Is the camera already used with another app?", error.what());
545550
}
546-
else
551+
552+
if (!found)
547553
{
548-
UERROR("The requested device \"%s\" is NOT found!", deviceId_.c_str());
554+
if(dualMode_ && dev_.size()==2)
555+
{
556+
UERROR("Dual setup is enabled, but a D400 camera is not detected!");
557+
dev_.clear();
558+
}
559+
else
560+
{
561+
UERROR("The requested device \"%s\" is NOT found!", deviceId_.c_str());
562+
}
563+
return false;
549564
}
550-
return false;
551565
}
552-
else if(dualMode_ && dev_.size()!=2)
566+
567+
if(dualMode_ && dev_.size()!=2)
553568
{
554569
UERROR("Dual setup is enabled, but a T265 camera is not detected!");
555570
dev_.clear();
@@ -634,7 +649,14 @@ bool CameraRealSense2::init(const std::string & calibrationFolder, const std::st
634649
sensors[1] = elem;
635650
if(sensors[1].supports(rs2_option::RS2_OPTION_EMITTER_ENABLED))
636651
{
637-
sensors[1].set_option(rs2_option::RS2_OPTION_EMITTER_ENABLED, emitterEnabled_);
652+
if(!sensors[1].is_option_read_only(rs2_option::RS2_OPTION_EMITTER_ENABLED))
653+
{
654+
sensors[1].set_option(rs2_option::RS2_OPTION_EMITTER_ENABLED, emitterEnabled_);
655+
}
656+
else if(!emitterEnabled_)
657+
{
658+
UWARN("rs2_option::RS2_OPTION_EMITTER_ENABLED option is read-only, cannot disable IR emitter.");
659+
}
638660
}
639661
}
640662
else if ("Coded-Light Depth Sensor" == module_name)
@@ -732,7 +754,8 @@ bool CameraRealSense2::init(const std::string & calibrationFolder, const std::st
732754
video_profile.width() == cameraWidth_ &&
733755
video_profile.height() == cameraHeight_ &&
734756
video_profile.fps() == cameraFps_) ||
735-
(strcmp(sensors[i].get_info(RS2_CAMERA_INFO_NAME), "L500 Depth Sensor")==0 &&
757+
((strcmp(sensors[i].get_info(RS2_CAMERA_INFO_NAME), "L500 Depth Sensor")==0 ||
758+
(playback_ && strcmp(sensors[i].get_info(RS2_CAMERA_INFO_NAME), "Stereo Module")==0)) &&
736759
video_profile.width() == cameraDepthWidth_ &&
737760
video_profile.height() == cameraDepthHeight_ &&
738761
video_profile.fps() == cameraDepthFps_))
@@ -741,7 +764,7 @@ bool CameraRealSense2::init(const std::string & calibrationFolder, const std::st
741764

742765
// rgb or ir left
743766
if((!ir_ && video_profile.format() == RS2_FORMAT_RGB8 && video_profile.stream_type() == RS2_STREAM_COLOR) ||
744-
(ir_ && video_profile.format() == RS2_FORMAT_Y8 && (video_profile.stream_index() == 1 || isL500)))
767+
(ir_ && video_profile.format() == RS2_FORMAT_Y8 && (video_profile.stream_index() == 1 || isL500)))
745768
{
746769
if(!profilesPerSensor[i].empty())
747770
{
@@ -880,8 +903,17 @@ bool CameraRealSense2::init(const std::string & calibrationFolder, const std::st
880903
}
881904
if (!added)
882905
{
883-
UERROR("Given stream configuration is not supported by the device! "
884-
"Stream Index: %d, Width: %d, Height: %d, FPS: %d", i, cameraWidth_, cameraHeight_, cameraFps_);
906+
if(strcmp(sensors[i].get_info(RS2_CAMERA_INFO_NAME), "L500 Depth Sensor")==0 ||
907+
(playback_ && strcmp(sensors[i].get_info(RS2_CAMERA_INFO_NAME), "Stereo Module")==0))
908+
{
909+
UERROR("Given stream configuration is not supported by the device! "
910+
"Stream Index: %d, Width: %d, Height: %d, FPS: %d", i, cameraDepthWidth_, cameraDepthHeight_, cameraDepthFps_);
911+
}
912+
else
913+
{
914+
UERROR("Given stream configuration is not supported by the device! "
915+
"Stream Index: %d, Width: %d, Height: %d, FPS: %d", i, cameraWidth_, cameraHeight_, cameraFps_);
916+
}
885917
UERROR("Available configurations:");
886918
for (auto& profile : profiles)
887919
{
@@ -1087,9 +1119,16 @@ bool CameraRealSense2::init(const std::string & calibrationFolder, const std::st
10871119
}
10881120
if(globalTimeSync_ && sensors[i].supports(rs2_option::RS2_OPTION_GLOBAL_TIME_ENABLED))
10891121
{
1090-
float value = sensors[i].get_option(rs2_option::RS2_OPTION_GLOBAL_TIME_ENABLED);
1091-
UINFO("Set RS2_OPTION_GLOBAL_TIME_ENABLED=1 (was %f) for sensor %d", value, (int)i);
1092-
sensors[i].set_option(rs2_option::RS2_OPTION_GLOBAL_TIME_ENABLED, 1);
1122+
float value = sensors[i].get_option(rs2_option::RS2_OPTION_GLOBAL_TIME_ENABLED);
1123+
UINFO("Set RS2_OPTION_GLOBAL_TIME_ENABLED=1 (was %f) for sensor %d", value, (int)i);
1124+
if(!sensors[i].is_option_read_only(rs2_option::RS2_OPTION_GLOBAL_TIME_ENABLED))
1125+
{
1126+
sensors[i].set_option(rs2_option::RS2_OPTION_GLOBAL_TIME_ENABLED, 1);
1127+
}
1128+
else if(value != 1)
1129+
{
1130+
UWARN("rs2_option::RS2_OPTION_GLOBAL_TIME_ENABLED option is read-only, cannot enable it.");
1131+
}
10931132
}
10941133
sensors[i].open(profilesPerSensor[i]);
10951134
if(sensors[i].is<rs2::depth_sensor>())
@@ -1538,7 +1577,10 @@ SensorData CameraRealSense2::captureImage(SensorCaptureInfo * info)
15381577
}
15391578
catch(const std::exception& ex)
15401579
{
1541-
UERROR("An error has occurred during frame callback: %s", ex.what());
1580+
if(!playback_)
1581+
{
1582+
UERROR("An error has occurred during frame callback: %s", ex.what());
1583+
}
15421584
}
15431585
#else
15441586
UERROR("CameraRealSense2: RTAB-Map is not built with RealSense2 support!");

0 commit comments

Comments
 (0)