Skip to content

Commit

Permalink
ros2GH-27 Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
botteroa-si authored and anhosi committed Dec 6, 2018
1 parent 6cc6171 commit 4def41e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
6 changes: 4 additions & 2 deletions ros2bag/ros2bag/verb/record.py
Expand Up @@ -48,10 +48,12 @@ def add_arguments(self, parser, cli_name): # noqa: D102
' rmw currently in use')
parser.add_argument(
'--no-discovery', action='store_true',
help='disables topic auto discovery during recording')
help='disables topic auto discovery during recording: only topics present at '
'startup will be recorded')
parser.add_argument(
'-p', '--polling-interval', default=100,
help='time in ms to wait between querying available topics for recording'
help='time in ms to wait between querying available topics for recording. It has no '
'effect if --no-discovery is enabled.'
)

def create_bag_directory(self, uri):
Expand Down
49 changes: 25 additions & 24 deletions rosbag2_transport/src/rosbag2_transport/recorder.cpp
Expand Up @@ -39,7 +39,7 @@ void Recorder::record(const RecordOptions & record_options)
throw std::runtime_error("No serialization format specified!");
}
serialization_format_ = record_options.rmw_serialization_format;
ROSBAG2_TRANSPORT_LOG_INFO("Setup complete. Listening for topics...");
ROSBAG2_TRANSPORT_LOG_INFO("Listening for topics...");
subscribe_topics(record_options.topics);
std::future<void> discovery_future;
if (!record_options.is_discovery_disabled) {
Expand All @@ -55,29 +55,6 @@ void Recorder::record(const RecordOptions & record_options)
subscriptions_.clear();
}

std::shared_ptr<GenericSubscription>
Recorder::create_subscription(
const std::string & topic_name, const std::string & topic_type)
{
auto subscription = node_->create_generic_subscription(
topic_name,
topic_type,
[this, topic_name](std::shared_ptr<rmw_serialized_message_t> message) {
auto bag_message = std::make_shared<rosbag2::SerializedBagMessage>();
bag_message->serialized_data = message;
bag_message->topic_name = topic_name;
rcutils_time_point_value_t time_stamp;
int error = rcutils_system_time_now(&time_stamp);
if (error != RCUTILS_RET_OK) {
ROSBAG2_TRANSPORT_LOG_ERROR_STREAM(
"Error getting current time. Error:" << rcutils_get_error_string().str);
}
bag_message->time_stamp = time_stamp;

writer_->write(bag_message);
});
return subscription;
}

std::future<void> Recorder::launch_topics_discovery(
std::chrono::milliseconds topic_polling_interval,
Expand Down Expand Up @@ -140,6 +117,30 @@ void Recorder::subscribe_topic(const rosbag2::TopicMetadata & topic)
}
}

std::shared_ptr<GenericSubscription>
Recorder::create_subscription(
const std::string & topic_name, const std::string & topic_type)
{
auto subscription = node_->create_generic_subscription(
topic_name,
topic_type,
[this, topic_name](std::shared_ptr<rmw_serialized_message_t> message) {
auto bag_message = std::make_shared<rosbag2::SerializedBagMessage>();
bag_message->serialized_data = message;
bag_message->topic_name = topic_name;
rcutils_time_point_value_t time_stamp;
int error = rcutils_system_time_now(&time_stamp);
if (error != RCUTILS_RET_OK) {
ROSBAG2_TRANSPORT_LOG_ERROR_STREAM(
"Error getting current time. Error:" << rcutils_get_error_string().str);
}
bag_message->time_stamp = time_stamp;

writer_->write(bag_message);
});
return subscription;
}

void Recorder::record_messages() const
{
spin(node_);
Expand Down
4 changes: 2 additions & 2 deletions rosbag2_transport/src/rosbag2_transport/recorder.hpp
Expand Up @@ -46,12 +46,12 @@ class Recorder
void record(const RecordOptions & record_options);

private:
std::shared_ptr<GenericSubscription> create_subscription(
const std::string & topic_name, const std::string & topic_type);
std::future<void> launch_topics_discovery(
std::chrono::milliseconds topic_polling_interval,
const std::vector<std::string> & topics_to_record = {});
bool subscribe_topics(const std::vector<std::string> & topics_to_record);
std::shared_ptr<GenericSubscription> create_subscription(
const std::string & topic_name, const std::string & topic_type);
void subscribe_all_missing_topics(
const std::unordered_map<std::string, std::string> & all_topics_and_types);
void subscribe_topic(const rosbag2::TopicMetadata & topic);
Expand Down

0 comments on commit 4def41e

Please sign in to comment.