@@ -56,16 +56,45 @@ Recorder::Recorder(
56
56
const rosbag2_transport::RecordOptions & record_options,
57
57
const std::string & node_name,
58
58
const rclcpp::NodeOptions & node_options)
59
+ : Recorder(
60
+ std::move (writer),
61
+ std::make_shared<KeyboardHandler>(),
62
+ storage_options,
63
+ record_options,
64
+ node_name,
65
+ node_options)
66
+ {}
67
+
68
+ Recorder::Recorder (
69
+ std::shared_ptr<rosbag2_cpp::Writer> writer,
70
+ std::shared_ptr<KeyboardHandler> keyboard_handler,
71
+ const rosbag2_storage::StorageOptions & storage_options,
72
+ const rosbag2_transport::RecordOptions & record_options,
73
+ const std::string & node_name,
74
+ const rclcpp::NodeOptions & node_options)
59
75
: rclcpp::Node(node_name, rclcpp::NodeOptions(node_options).start_parameter_event_publisher(false )),
60
76
writer_(std::move(writer)),
61
77
storage_options_(storage_options),
62
78
record_options_(record_options),
63
- stop_discovery_(record_options_.is_discovery_disabled)
79
+ stop_discovery_(record_options_.is_discovery_disabled),
80
+ paused_(record_options.start_paused),
81
+ keyboard_handler_(std::move(keyboard_handler))
64
82
{
83
+ std::string key_str = enum_key_code_to_str (Recorder::kPauseResumeToggleKey );
84
+ toggle_paused_key_callback_handle_ =
85
+ keyboard_handler_->add_key_press_callback (
86
+ [this ](KeyboardHandler::KeyCode /* key_code*/ ,
87
+ KeyboardHandler::KeyModifiers /* key_modifiers*/ ) {this ->toggle_paused ();},
88
+ Recorder::kPauseResumeToggleKey );
89
+ // show instructions
90
+ RCLCPP_INFO_STREAM (
91
+ get_logger (),
92
+ " Press " << key_str << " for pausing/resuming" );
65
93
}
66
94
67
95
Recorder::~Recorder ()
68
96
{
97
+ keyboard_handler_->delete_key_press_callback (toggle_paused_key_callback_handle_);
69
98
stop_discovery_ = true ;
70
99
if (discovery_future_.valid ()) {
71
100
discovery_future_.wait ();
@@ -113,6 +142,32 @@ const rosbag2_cpp::Writer & Recorder::get_writer_handle()
113
142
return *writer_;
114
143
}
115
144
145
+ void Recorder::pause ()
146
+ {
147
+ paused_.store (true );
148
+ RCLCPP_INFO_STREAM (get_logger (), " Pausing recording." );
149
+ }
150
+
151
+ void Recorder::resume ()
152
+ {
153
+ paused_.store (false );
154
+ RCLCPP_INFO_STREAM (get_logger (), " Resuming recording." );
155
+ }
156
+
157
+ void Recorder::toggle_paused ()
158
+ {
159
+ if (paused_.load ()) {
160
+ this ->resume ();
161
+ } else {
162
+ this ->pause ();
163
+ }
164
+ }
165
+
166
+ bool Recorder::is_paused ()
167
+ {
168
+ return paused_.load ();
169
+ }
170
+
116
171
void Recorder::topics_discovery ()
117
172
{
118
173
while (rclcpp::ok () && stop_discovery_ == false ) {
@@ -225,7 +280,9 @@ Recorder::create_subscription(
225
280
topic_type,
226
281
qos,
227
282
[this , topic_name, topic_type](std::shared_ptr<rclcpp::SerializedMessage> message) {
228
- writer_->write (message, topic_name, topic_type, rclcpp::Clock (RCL_SYSTEM_TIME).now ());
283
+ if (!paused_.load ()) {
284
+ writer_->write (message, topic_name, topic_type, rclcpp::Clock (RCL_SYSTEM_TIME).now ());
285
+ }
229
286
});
230
287
return subscription;
231
288
}
0 commit comments