This commit is contained in:
Niklas Halle 2025-04-29 14:57:54 +02:00
parent a93291f4fd
commit 1531c9adc1
6 changed files with 214 additions and 1 deletions

View file

@ -89,6 +89,9 @@ RUN apt-get install -y python3-babeltrace python3-lttng python3-pytest python3-p
# So we install it using pip
RUN python3 -m pip install --upgrade bokeh
# Install additional Python packages
RUN python3 -m pip install --upgrade numexpr
# Add user to the "tracing" group
RUN usermod -aG tracing $USERNAME

View file

@ -0,0 +1,42 @@
cmake_minimum_required(VERSION 3.5)
project(simple_topology)
# Default to C99
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()
# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
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(std_msgs REQUIRED)
add_executable(simple_topology src/simple_topology.cpp)
ament_target_dependencies(simple_topology rclcpp std_msgs)
install(TARGETS simple_topology DESTINATION lib/${PROJECT_NAME})
install(
DIRECTORY launch
DESTINATION share/${PROJECT_NAME}
)
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# uncomment the line when a copyright and license is not present in all source files
#set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# uncomment the line when this package is not in a git repo
#set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
ament_package()

View file

@ -0,0 +1,24 @@
from launch import LaunchDescription
from launch_ros.actions import Node
from tracetools_launch.action import Trace
def generate_launch_description():
return LaunchDescription([
# Start tracing automatically
Trace(
session_name='ros2_tracing_session',
events_ust=[
'rclcpp:*', 'rcl:*', 'rmw:*', 'ros2:*', 'rclcpp:*callback*', 'rclcpp:*executor*'
],
events_kernel=[
'sched_switch', 'sched_wakeup'
],
),
# Your executable as a Node
Node(
package='simple_topology',
executable='simple_topology',
output='screen',
),
])

View file

@ -0,0 +1,34 @@
<?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>simple_topology</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="niklas@niklashalle.net">dev</maintainer>
<license>TODO: License declaration</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<depend>launch</depend>
<depend>launch_ros</depend>
<depend>tracetools_trace</depend>
<depend>tracetools_launch</depend>
<depend>rclcpp</depend>
<depend>std_msgs</depend>
<test_depend>ament_copyright</test_depend>
<test_depend>ament_flake8</test_depend>
<test_depend>ament_mypy</test_depend>
<test_depend>ament_pep257</test_depend>
<test_depend>ament_xmllint</test_depend>
<test_depend>python3-pytest</test_depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<export>
<build_type>ament_cmake</build_type>
<build_type>ament_python</build_type>
</export>
</package>

View file

@ -0,0 +1,110 @@
// --- includes ---
#include <chrono>
#include <memory>
#include <string>
#include <thread>
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
using namespace std::chrono_literals;
constexpr int RUN_DURATION_SECONDS = 5;
// --- Sensor Node ---
class SensorNode : public rclcpp::Node
{
public:
SensorNode() : Node("sensor_node")
{
publisher_ = this->create_publisher<std_msgs::msg::String>("sensor_topic", 10);
timer_ = this->create_wall_timer(
100ms, [this]() { publish_message(); });
}
private:
void publish_message()
{
auto message = std_msgs::msg::String();
message.data = "Sensor reading";
RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());
publisher_->publish(message);
}
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
rclcpp::TimerBase::SharedPtr timer_;
};
// --- Processing Node ---
class ProcessingNode : public rclcpp::Node
{
public:
ProcessingNode() : Node("processing_node")
{
subscription_ = this->create_subscription<std_msgs::msg::String>(
"sensor_topic", 10,
[this](std_msgs::msg::String::SharedPtr msg) { process_message(msg); });
publisher_ = this->create_publisher<std_msgs::msg::String>("processed_topic", 10);
}
private:
void process_message(const std_msgs::msg::String::SharedPtr msg)
{
RCLCPP_INFO(this->get_logger(), "Processing: '%s'", msg->data.c_str());
// Simulate processing delay
std::this_thread::sleep_for(10ms);
auto new_msg = std_msgs::msg::String();
new_msg.data = msg->data + " -> processed";
publisher_->publish(new_msg);
}
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
};
// --- Decision Node ---
class DecisionNode : public rclcpp::Node
{
public:
DecisionNode() : Node("decision_node")
{
subscription_ = this->create_subscription<std_msgs::msg::String>(
"processed_topic", 10,
[this](std_msgs::msg::String::SharedPtr msg) { decide(msg); });
}
private:
void decide(const std_msgs::msg::String::SharedPtr msg)
{
RCLCPP_INFO(this->get_logger(), "Deciding based on: '%s'", msg->data.c_str());
// Future place for semantic detection (e.g., "if smoke detected, boost priority!")
}
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
};
// --- Main ---
int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
auto sensor_node = std::make_shared<SensorNode>();
auto processing_node = std::make_shared<ProcessingNode>();
auto decision_node = std::make_shared<DecisionNode>();
rclcpp::executors::MultiThreadedExecutor executor;
executor.add_node(sensor_node);
executor.add_node(processing_node);
executor.add_node(decision_node);
auto start_time = std::chrono::steady_clock::now();
while (rclcpp::ok() && (std::chrono::steady_clock::now() - start_time) < std::chrono::seconds(RUN_DURATION_SECONDS))
{
executor.spin_some();
std::this_thread::sleep_for(1ms);
}
rclcpp::shutdown();
return 0;
}

@ -1 +1 @@
Subproject commit e3591d1664af8159dde023f93751d4fbb7619542
Subproject commit 00a3e8c3a6b67ce0dab01ef69e6abc42bb48bf10