Skip to content

Commit

Permalink
Add API samples on main branch - Rolling C++ API examples (#1068)
Browse files Browse the repository at this point in the history
* Take samples from Galactic, rename to rosbag2_exampes_cpp, update for Rolling usage

Signed-off-by: Emerson Knapp <[email protected]>
  • Loading branch information
emersonknapp authored Sep 30, 2022
1 parent c58d73c commit 6a07c16
Show file tree
Hide file tree
Showing 7 changed files with 251 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
rosbag2_compression
rosbag2_compression_zstd
rosbag2_cpp
rosbag2_examples_cpp
rosbag2_interfaces
rosbag2_py
rosbag2_storage
Expand Down Expand Up @@ -53,6 +54,7 @@ jobs:
rosbag2_compression
rosbag2_compression_zstd
rosbag2_cpp
rosbag2_examples_cpp
rosbag2_py
rosbag2_storage
rosbag2_storage_default_plugins
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
rosbag2_compression
rosbag2_compression_zstd
rosbag2_cpp
rosbag2_examples_cpp
rosbag2_interfaces
rosbag2_py
rosbag2_storage
Expand Down
53 changes: 53 additions & 0 deletions rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
cmake_minimum_required(VERSION 3.5)
project(rosbag2_examples_cpp)

# Default to C99
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()

# Default to C++17
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rosbag2_cpp REQUIRED)
find_package(example_interfaces REQUIRED)

add_executable(simple_bag_recorder src/simple_bag_recorder.cpp)
ament_target_dependencies(simple_bag_recorder rclcpp rosbag2_cpp example_interfaces)

install(TARGETS
simple_bag_recorder
DESTINATION lib/${PROJECT_NAME}
)

add_executable(data_generator_node src/data_generator_node.cpp)
ament_target_dependencies(data_generator_node rclcpp rosbag2_cpp example_interfaces)

install(TARGETS
data_generator_node
DESTINATION lib/${PROJECT_NAME}
)

add_executable(data_generator_executable src/data_generator_executable.cpp)
ament_target_dependencies(data_generator_executable rclcpp rosbag2_cpp example_interfaces)

install(TARGETS
data_generator_executable
DESTINATION lib/${PROJECT_NAME}
)

if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()
endif()

ament_package()
22 changes: 22 additions & 0 deletions rosbag2_examples/rosbag2_examples_cpp/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>rosbag2_examples_cpp</name>
<version>0.0.0</version>
<description>rosbag2 C++ API tutorials and examples</description>
<maintainer email="[email protected]">geoff</maintainer>
<license>Apache License 2.0</license>

<buildtool_depend>ament_cmake</buildtool_depend>

<depend>rclcpp</depend>
<depend>rosbag2_cpp</depend>
<depend>example_interfaces</depend>

<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2021 Open Source Robotics Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <chrono>
#include <memory>

#include "example_interfaces/msg/int32.hpp"
#include "rclcpp/rclcpp.hpp"

#include "rosbag2_cpp/writer.hpp"
#include "rosbag2_cpp/writers/sequential_writer.hpp"
#include "rosbag2_storage/serialized_bag_message.hpp"

using namespace std::chrono_literals;

int main(int, char **)
{
example_interfaces::msg::Int32 data;
data.data = 0;
std::unique_ptr<rosbag2_cpp::Writer> writer_ = std::make_unique<rosbag2_cpp::Writer>();

writer_->open("big_synthetic_bag");

writer_->create_topic(
{
"synthetic",
"example_interfaces/msg/Int32",
rmw_get_serialization_format(),
""
});

rclcpp::Clock clock;
rclcpp::Time time_stamp = clock.now();
for (int32_t ii = 0; ii < 100; ++ii) {
writer_->write(data, "synthetic", time_stamp);
++data.data;
time_stamp += rclcpp::Duration(1s);
}

return 0;
}
66 changes: 66 additions & 0 deletions rosbag2_examples/rosbag2_examples_cpp/src/data_generator_node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2021 Open Source Robotics Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <chrono>
#include <memory>

#include "example_interfaces/msg/int32.hpp"
#include "rclcpp/rclcpp.hpp"

#include "rosbag2_cpp/writer.hpp"

using namespace std::chrono_literals;

class DataGenerator : public rclcpp::Node
{
public:
DataGenerator()
: Node("data_generator")
{
data.data = 0;
writer_ = std::make_unique<rosbag2_cpp::Writer>();

writer_->open("timed_synthetic_bag");

writer_->create_topic(
{
"synthetic",
"example_interfaces/msg/Int32",
rmw_get_serialization_format(),
""
});

timer_ = create_wall_timer(1s, std::bind(&DataGenerator::timer_callback, this));
}

private:
void timer_callback()
{
writer_->write(data, "synthetic", now());

++data.data;
}

rclcpp::TimerBase::SharedPtr timer_;
std::unique_ptr<rosbag2_cpp::Writer> writer_;
example_interfaces::msg::Int32 data;
};

int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<DataGenerator>());
rclcpp::shutdown();
return 0;
}
55 changes: 55 additions & 0 deletions rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_recorder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2021 Open Source Robotics Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <memory>

#include "example_interfaces/msg/string.hpp"
#include "rclcpp/rclcpp.hpp"

#include "rosbag2_cpp/writer.hpp"

using std::placeholders::_1;

class SimpleBagRecorder : public rclcpp::Node
{
public:
SimpleBagRecorder()
: Node("simple_bag_recorder")
{
writer_ = std::make_unique<rosbag2_cpp::Writer>();

writer_->open("my_bag");

subscription_ = create_subscription<example_interfaces::msg::String>(
"chatter", 10, std::bind(&SimpleBagRecorder::topic_callback, this, _1));
}

private:
void topic_callback(std::shared_ptr<rclcpp::SerializedMessage> msg) const
{
rclcpp::Time time_stamp = this->now();
writer_->write(msg, "chatter", "example_interfaces/msg/String", time_stamp);
}

rclcpp::Subscription<example_interfaces::msg::String>::SharedPtr subscription_;
std::unique_ptr<rosbag2_cpp::Writer> writer_;
};

int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<SimpleBagRecorder>());
rclcpp::shutdown();
return 0;
}

0 comments on commit 6a07c16

Please sign in to comment.