Skip to content

Commit

Permalink
prep for demo
Browse files Browse the repository at this point in the history
Signed-off-by: William Woodall <[email protected]>
  • Loading branch information
wjwwood committed Aug 25, 2022
1 parent d658d34 commit bd06ae4
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 0 deletions.
10 changes: 10 additions & 0 deletions space_ros_memory_allocation_demo/launch/talker_listener.launch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<launch>
<arg name="use_memory_tools" default="false" />
<set_env
name="LD_PRELOAD"
value="/opt/ros/rolling/lib/libmemory_tools_interpose.so"
if="$(var use_memory_tools)"
/>
<node pkg="space_ros_memory_allocation_demo" exec="talker_main" output="screen" />
<node pkg="space_ros_memory_allocation_demo" exec="listener_main" output="screen" />
</launch>
67 changes: 67 additions & 0 deletions space_ros_memory_allocation_demo/src/listener.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2022 Open Source Robotics Foundation, Inc.
//
// 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.

#ifndef SPACE_ROS_MEMORY_ALLOCATION_DEMO__LISTENER_HPP_
#define SPACE_ROS_MEMORY_ALLOCATION_DEMO__LISTENER_HPP_

#include "rclcpp/rclcpp.hpp"

#include "std_msgs/msg/string.hpp"

#include "space_ros_memory_allocation_demo/visibility_control.h"
#include "space_ros_memory_allocation_demo/memory_allocator.hpp"

#include "expected_push_pop.hpp"

namespace space_ros_memory_allocation_demo
{

// TODO(wjwwood): Cannot use this until we fix https://github.com/ros2/rosidl/issues/566
// using String = std_msgs::msg::String_<MemoryAllocator>;
using String = std_msgs::msg::String;

// Create a Listener class that subclasses the generic rclcpp::Node base class.
// The main function below will instantiate the class as a ROS node.
class Listener : public rclcpp::Node
{
public:
SPACE_ROS_MEMORY_ALLOCATION_DEMO_PUBLIC
explicit Listener(
const rclcpp::NodeOptions & options,
std::pmr::memory_resource * memory_resource = std::pmr::get_default_resource())
: Node("listener", options)
{
RCUTILS_UNUSED(memory_resource);
// Create a callback function for when messages are received.
// Variations of this function also exist using, for example UniquePtr for zero-copy transport.
auto callback =
[this](std_msgs::msg::String::ConstSharedPtr msg) -> void
{
RCLCPP_INFO(this->get_logger(), "I heard: [%s]", msg->data.c_str());
this->get_node_base_interface()->get_context()->shutdown("done");
};
// Create a subscription to the topic which can be matched with one or more compatible ROS
// publishers.
// Note that not all publishers on the same topic with the same type will be compatible:
// they must have compatible Quality of Service policies.
sub_ = create_subscription<std_msgs::msg::String>("chatter", 10, callback);
}

private:
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr sub_;
};

} // namespace space_ros_memory_allocation_demo

#endif // SPACE_ROS_MEMORY_ALLOCATION_DEMO__LISTENER_HPP_
20 changes: 20 additions & 0 deletions space_ros_memory_allocation_demo/src/listener_library.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2022 Open Source Robotics Foundation, Inc.
//
// 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 "rclcpp/rclcpp.hpp"
#include "rclcpp_components/register_node_macro.hpp"

#include "listener.hpp"

RCLCPP_COMPONENTS_REGISTER_NODE(space_ros_memory_allocation_demo::Listener)
62 changes: 62 additions & 0 deletions space_ros_memory_allocation_demo/src/listener_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2022 Open Source Robotics Foundation, Inc.
//
// 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 "osrf_testing_tools_cpp/memory_tools/memory_tools.hpp"
#include "rclcpp/rclcpp.hpp"

#include "common_main.hpp"
#include "expected_push_pop.hpp"
#include "listener.hpp"

int
actual_main(int argc, char const * argv[], space_ros_memory_allocation_demo::MemoryAllocator & ma)
{
using std::pmr::polymorphic_allocator;
// Create a C-style allocator that wraps the pmr C++ allocator.
polymorphic_allocator<void> pa(&ma);
rcl_allocator_t rcl_allocator =
rclcpp::allocator::get_rcl_allocator<void, polymorphic_allocator<void>>(pa);

using rclcpp::Context;
rclcpp::InitOptions io(rcl_allocator);
auto context = std::allocate_shared<Context, polymorphic_allocator<Context>>(&ma);
context->init(argc, argv, io);

rclcpp::NodeOptions no(rcl_allocator);
no.context(context);
using space_ros_memory_allocation_demo::Listener;
auto listener =
std::allocate_shared<Listener, std::pmr::polymorphic_allocator<Listener>>(&ma, no, &ma);

// TODO(wjwwood): use ma in executor memory_strategy
rclcpp::ExecutorOptions eo;
eo.context = context;
rclcpp::executors::SingleThreadedExecutor executor(eo);

executor.add_node(listener);
EXPECT_NO_MEMORY_OPERATIONS(
{
executor.spin();
});

return 0;
}

int
main(int argc, char const * argv[])
{
return common_main(argc, argv, actual_main);
}

0 comments on commit bd06ae4

Please sign in to comment.