Skip to content

Commit

Permalink
PR #3214 from acornaglia: Add ROS bag loop option
Browse files Browse the repository at this point in the history
  • Loading branch information
Nir-Az authored Nov 17, 2024
2 parents 7a9f5a3 + 37ef8a9 commit e48855f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
5 changes: 5 additions & 0 deletions realsense2_camera/examples/launch_from_rosbag/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ or
ros2 launch realsense2_camera rs_launch.py rosbag_filename:="/full/path/to/rosbag/file"
```

Additionally, the 'rosbag_loop' cmd line argument enables the looped playback of the rosbag file:
```
ros2 launch realsense2_camera rs_launch_from_rosbag.py rosbag_filename:="/full/path/to/rosbag/file" rosbag_loop:="true"
```

Check-out [sample-recordings](https://github.com/IntelRealSense/librealsense/blob/master/doc/sample-data.md) for a few recorded samples.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
{'name': 'enable_gyro', 'default': 'true', 'description': "'enable gyro stream'"},
{'name': 'enable_accel', 'default': 'true', 'description': "'enable accel stream'"},
{'name': 'rosbag_filename', 'default': [ThisLaunchFileDir(), "/rosbag/D435i_Depth_and_IMU_Stands_still.bag"], 'description': 'A realsense bagfile to run from as a device'},
{'name': 'rosbag_loop', 'default': 'false', 'description': 'enable realsense bagfile loop playback'},
]

def set_configurable_parameters(local_params):
Expand All @@ -48,7 +49,7 @@ def generate_launch_description():
params = rs_launch.configurable_parameters
return LaunchDescription(
rs_launch.declare_configurable_parameters(local_parameters) +
rs_launch.declare_configurable_parameters(params) +
rs_launch.declare_configurable_parameters(params) +
[
OpaqueFunction(function=rs_launch.launch_setup,
kwargs = {'params' : set_configurable_parameters(params)}
Expand Down
1 change: 1 addition & 0 deletions realsense2_camera/launch/rs_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
{'name': 'initial_reset', 'default': 'false', 'description': "''"},
{'name': 'accelerate_gpu_with_glsl', 'default': "false", 'description': 'enable GPU acceleration with GLSL'},
{'name': 'rosbag_filename', 'default': "''", 'description': 'A realsense bagfile to run from as a device'},
{'name': 'rosbag_loop', 'default': 'false', 'description': 'Enable loop playback when playing a bagfile'},
{'name': 'log_level', 'default': 'info', 'description': 'debug log level [DEBUG|INFO|WARN|ERROR|FATAL]'},
{'name': 'output', 'default': 'screen', 'description': 'pipe node output [screen|log]'},
{'name': 'enable_color', 'default': 'true', 'description': 'enable color stream'},
Expand Down
38 changes: 35 additions & 3 deletions realsense2_camera/src/realsense_node_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RealSenseNodeFactory::RealSenseNodeFactory(const rclcpp::NodeOptions & node_opti
}

RealSenseNodeFactory::RealSenseNodeFactory(const std::string & node_name, const std::string & ns,
const rclcpp::NodeOptions & node_options) :
const rclcpp::NodeOptions & node_options) :
Node(node_name, ns, node_options),
_logger(this->get_logger())
{
Expand Down Expand Up @@ -200,7 +200,7 @@ void RealSenseNodeFactory::getDevice(rs2::device_list list)
ROS_INFO("Resetting device...");
_device.hardware_reset();
_device = rs2::device();

}
catch(const std::exception& ex)
{
Expand Down Expand Up @@ -294,7 +294,39 @@ void RealSenseNodeFactory::init()
}
if (_device)
{
bool rosbag_loop(declare_parameter("rosbag_loop", rclcpp::ParameterValue(false)).get<rclcpp::PARAMETER_BOOL>());
startDevice();

if (rosbag_loop)
{
auto playback = _device.as<rs2::playback>(); // Object to check the playback status periodically.
bool is_playing = true; // Flag to indicate if the playback is active

while (rclcpp::ok())
{
// Check the current status only if the playback is not active
auto status = playback.current_status();
if (!is_playing && status == RS2_PLAYBACK_STATUS_STOPPED)
{
RCLCPP_INFO(this->get_logger(), "Bag file playback has completed and it is going to be replayed.");
startDevice(); // Re-start bag file execution
is_playing = true; // Set the flag to true as playback has been restarted
}
else if (status != RS2_PLAYBACK_STATUS_STOPPED)
{
// If the playback status is not stopped, it means the playback is active
is_playing = true;
}
else
{
// If the playback status is stopped, set the flag to false
is_playing = false;
}

// Add a small delay to prevent busy-waiting
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
}
}
else
Expand Down Expand Up @@ -410,7 +442,7 @@ void RealSenseNodeFactory::startDevice()
std::cerr << "Failed to start device: " << e.what() << '\n';
_device.hardware_reset();
_device = rs2::device();
}
}
}

void RealSenseNodeFactory::tryGetLogSeverity(rs2_log_severity& severity) const
Expand Down

0 comments on commit e48855f

Please sign in to comment.