Skip to content

Commit

Permalink
ros2GH-23 Allow to write all available topics
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin-Idel-SI authored and anhosi committed Sep 5, 2018
1 parent 01947ca commit c4098b2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
18 changes: 18 additions & 0 deletions rosbag2/include/rosbag2/rosbag2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,37 @@ class Rosbag2Node;
class Rosbag2
{
public:
/**
* Records topics to a bagfile. Subscription happens at startup time, hence the topics must
* exist when "record" is called.
*
* @param file_name Name of the bagfile to write
* @param topic_names Vector of topics to subscribe to. Topics must exist at startup time. If
* the vector is empty, all topics will be subscribed.
* @param after_write_action This function will be executed after each write to the database
* where the input parameter is the topic name of the topic written Currently needed for testing.
* Might be removed later.
*/
ROSBAG2_PUBLIC
void record(
const std::string & file_name,
std::vector<std::string> topic_names,
std::function<void(std::string)> after_write_action = nullptr);

/**
* Replay all topics in a bagfile.
*
* @param file_name Name of the bagfile to replay
*/
ROSBAG2_PUBLIC
void play(const std::string & file_name);

/// Exposed for testing
ROSBAG2_PUBLIC
std::map<std::string, std::string> get_topics_with_types(
const std::vector<std::string> & topic_names, std::shared_ptr<rclcpp::Node> node);

/// Exposed for testing
ROSBAG2_PUBLIC
std::map<std::string, std::string> get_all_topics_with_types(std::shared_ptr<rclcpp::Node> node);

Expand Down
8 changes: 5 additions & 3 deletions rosbag2/src/rosbag2/demo_record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
int main(int argc, const char ** argv)
{
if (argc < 2) {
std::cerr << "\nThe names of topics to record must be given as parameter!\n";
std::cerr << "\nThe names of topics or `--all` must be given to record as parameter!\n";
return 0;
}
std::vector<std::string> topics;

for (int i = 1; i < argc; i++) {
topics.emplace_back(argv[i]);
if (strcmp(argv[1], "--all") != 0) {
for (int i = 1; i < argc; i++) {
topics.emplace_back(argv[i]);
}
}

// TODO(anhosi): allow output file to be specified by cli argument and do proper checking if
Expand Down
4 changes: 3 additions & 1 deletion rosbag2/src/rosbag2/rosbag2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ void Rosbag2::record(

auto node = std::make_shared<Rosbag2Node>("rosbag2");

auto topics_and_types = get_topics_with_types(topic_names, node);
auto topics_and_types = topic_names.empty() ?
get_all_topics_with_types(node) :
get_topics_with_types(topic_names, node);

if (topics_and_types.empty()) {
throw std::runtime_error("No topics found. Abort");
Expand Down

0 comments on commit c4098b2

Please sign in to comment.