From 09e5abbcb7c58905fc03093a3391569a8e7d5879 Mon Sep 17 00:00:00 2001 From: Wessel Tip Date: Tue, 9 Sep 2025 11:16:14 +0200 Subject: [PATCH] feat(hw1): First iteration of pubsub random number --- src/hw1_pubsub/CMakeLists.txt | 41 +++++++++++++++++++++++ src/hw1_pubsub/package.xml | 18 ++++++++++ src/hw1_pubsub/src/1-publisher.cpp | 51 +++++++++++++++++++++++++++++ src/hw1_pubsub/src/2-subscriber.cpp | 51 +++++++++++++++++++++++++++++ 4 files changed, 161 insertions(+) create mode 100644 src/hw1_pubsub/CMakeLists.txt create mode 100644 src/hw1_pubsub/package.xml create mode 100644 src/hw1_pubsub/src/1-publisher.cpp create mode 100644 src/hw1_pubsub/src/2-subscriber.cpp diff --git a/src/hw1_pubsub/CMakeLists.txt b/src/hw1_pubsub/CMakeLists.txt new file mode 100644 index 0000000..7ea9d45 --- /dev/null +++ b/src/hw1_pubsub/CMakeLists.txt @@ -0,0 +1,41 @@ +cmake_minimum_required(VERSION 3.8) +project(hw1_pubsub) + +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) +# uncomment the following section in order to fill in +# further dependencies manually. +# find_package( REQUIRED) + +add_executable(1_pub src/1-publisher.cpp) +add_executable(2_sub src/2-subscriber.cpp) + +ament_target_dependencies(1_pub rclcpp std_msgs) +ament_target_dependencies(2_sub rclcpp std_msgs) + +install ( + TARGETS + 1_pub + 2_sub + DESTINATION lib/${PROJECT_NAME} +) + +if(BUILD_TESTING) + find_package(ament_lint_auto REQUIRED) + # the following line skips the linter which checks for copyrights + # comment the line when a copyright and license is added to all source files + set(ament_cmake_copyright_FOUND TRUE) + # the following line skips cpplint (only works in a git repo) + # comment the line when this package is in a git repo and when + # a copyright and license is added to all source files + set(ament_cmake_cpplint_FOUND TRUE) + ament_lint_auto_find_test_dependencies() +endif() + +ament_package() diff --git a/src/hw1_pubsub/package.xml b/src/hw1_pubsub/package.xml new file mode 100644 index 0000000..7f4becf --- /dev/null +++ b/src/hw1_pubsub/package.xml @@ -0,0 +1,18 @@ + + + + hw1_pubsub + 0.0.0 + TODO: Package description + wessel + TODO: License declaration + + ament_cmake + + ament_lint_auto + ament_lint_common + + + ament_cmake + + diff --git a/src/hw1_pubsub/src/1-publisher.cpp b/src/hw1_pubsub/src/1-publisher.cpp new file mode 100644 index 0000000..0e7f8ac --- /dev/null +++ b/src/hw1_pubsub/src/1-publisher.cpp @@ -0,0 +1,51 @@ +/* hw1_pubsub/1-publisher.cpp + * Node that publishes a random integer every 3 seconds. + * + * Reviewed by: + * Changelog: + * [04-09-2025] Wessel T: Implement template + * [09-09-2025] Wessel T: (BASE) Work out assignment + */ + +#include + +#include "rclcpp/rclcpp.hpp" +#include "std_msgs/msg/int32.hpp" // For random integer publisher + +class NodeHw1RandomPublisher : public rclcpp::Node { + +public: + NodeHw1RandomPublisher() + : Node("node_hw1_random_publisher") + { + publisher_random_number_ = + this->create_publisher("random_number", 10); + + timer_random_number_ = this->create_wall_timer( + std::chrono::seconds(3), + std::bind(&NodeHw1RandomPublisher::timer_random_number_function, this) + ); + } + + void timer_random_number_function() { + std_msgs::msg::Int32 msg; + msg.data = std::rand() % 101; + publisher_random_number_->publish(msg); + + RCLCPP_INFO(this->get_logger(), "Published random number: %d", msg.data); + } + +private: + rclcpp::Publisher::SharedPtr publisher_random_number_; + rclcpp::TimerBase::SharedPtr timer_random_number_; +}; + +int main(int argc,char *argv[]) { + rclcpp::init(argc,argv); + + auto node = std::make_shared(); + rclcpp::spin(node); + rclcpp::shutdown(); + + return 0; +} diff --git a/src/hw1_pubsub/src/2-subscriber.cpp b/src/hw1_pubsub/src/2-subscriber.cpp new file mode 100644 index 0000000..7ec5133 --- /dev/null +++ b/src/hw1_pubsub/src/2-subscriber.cpp @@ -0,0 +1,51 @@ + +/* hw1_pubsub/2-subscriber.cpp + * Node that subscribes and expect a random integer. Adds 1000 to it. + * + * Reviewed by: + * Changelog: + * [04-09-2025] Wessel T: Implement template + * [09-09-2025] Wessel T: (BASE) Work out assignment + */ + +#include + +#include "rclcpp/rclcpp.hpp" +#include "std_msgs/msg/int32.hpp" // For random integer publisher + +using namespace std::placeholders; + +class NodeHw1RandomSubscriber : public rclcpp::Node { + +public: + NodeHw1RandomSubscriber() + : Node("node_hw1_random_subscriber") + { + subscriber_random_number_ = + this->create_subscription( + "random_number", 10, + std::bind(&NodeHw1RandomSubscriber::callback_random_number, this, _1) + ); + } + + void callback_random_number(const std_msgs::msg::Int32::SharedPtr msg) { + int result = msg->data + 1000; + + RCLCPP_INFO(this->get_logger(), + "Received: %d, after adding 1000: %d", msg->data, result + ); + } + +private: + rclcpp::Subscription::SharedPtr subscriber_random_number_; +}; + +int main(int argc,char *argv[]) { + rclcpp::init(argc,argv); + + auto node = std::make_shared(); + rclcpp::spin(node); + rclcpp::shutdown(); + + return 0; +}