From 9d91819447a76651ccf32038ee443634bbe322b4 Mon Sep 17 00:00:00 2001 From: Wessel Tip Date: Thu, 11 Sep 2025 09:38:42 +0200 Subject: [PATCH] chore(les1): Clean up work, add node template --- src/les_pkg/src/les1/clock.cpp | 30 ++-------- src/les_pkg/src/les1/publisher.cpp | 88 ++++++++++++----------------- src/les_pkg/src/les1/subscriber.cpp | 34 +++-------- src/les_pkg/src/node_template.cpp | 31 ++++++++++ 4 files changed, 79 insertions(+), 104 deletions(-) create mode 100644 src/les_pkg/src/node_template.cpp diff --git a/src/les_pkg/src/les1/clock.cpp b/src/les_pkg/src/les1/clock.cpp index 7729fc9..d1a0ba1 100644 --- a/src/les_pkg/src/les1/clock.cpp +++ b/src/les_pkg/src/les1/clock.cpp @@ -1,22 +1,4 @@ -// Wessel T (https://wessel.gg/) -// -// "Build your own tower. -// A kingdom freed from malice. -// Create a world of bounty, peace and beauty." -// ⠀⣠⣶⣶⣤⣁ -// ⢰⣷⡟⠻⣏⠻⣧⣀⠐⠈⠀⡈⠠⠀⠂⠀⢁⠈⠀⠄⠀⠁⣠⣴⣤⡈ -// ⠀⣿⢿⡀⠘⢦⡈⢻⣦⠐⠀⠀⠄⠀⠁⠈⠀⡀⠠⠀⣠⡿⣿⣯⣽⡇ -// ⠀⢻⣿⠛⢦⣄⣹⠦⣌⣳⡀⠀⣠⠈⠀⢾⠀⠀⢀⣼⢯⠞⢡⣿⣿⠁ -// ⠄⠘⣿⣷⣤⡀⠙⣆⠈⠻⣿⡄⠘⣇⠀⣾⠀⣴⠿⢲⣋⣤⣿⡿⠃⠀ -// ⠀⡀⠹⣿⣦⡉⠛⠚⣆⠀⠈⠻⣆⢻⢠⣇⡾⠃⢠⣟⣠⣾⡞⠃⠀ -// ⠂⡀⠄⠹⣿⣏⠛⠒⠾⠷⣄⠀⠙⣞⣿⠋⣀⣴⣋⣽⡿⠋ -// ⠂⠠⠀⠀⣨⣿⢿⣶⣒⠲⢮⣿⣶⣼⣧⣾⣭⣿⠟⠉ -// ⠐⠀⠁⣰⣿⠓⠒⣛⣻⠟⠛⣩⣿⣯⠙⡯⣿⡆ -// ⠐⠀⠄⠸⣿⡟⢉⡽⢛⣿⡿⠉⠀⢸⣧⡷⣾⡇ -// ⠀⢂⠀⠄⠹⢿⣿⣴⣯⠏⠀⠀⠀⣼⢸⣽⣷⠇ -// ⠠⠀⠂⢀⠀⢀⠈⠉⠀⠀⠀⠂⡀⠹⠿⠛⠁⠀⠀ - -/* les1.cpp +/* les1/clock.cpp * Assignment done in the first lesson explaining * the basics of ROS2 * @@ -26,18 +8,16 @@ * [07-09-2025] Wessel T: * - Rename {les1.cpp -> les1/clock.cpp} * - Update node names to be in line with other files + * [11-09-2025] Wessel T: Remove unused imports */ #include #include "rclcpp/rclcpp.hpp" -#include -#include -class NodeLes1Clock : public rclcpp::Node -{ +class NodeLes1Clock : public rclcpp::Node { public: - NodeLes1Clock() - : Node("node_les1_clock") + NodeLes1Clock() + : Node("node_les1_clock") { timer_ = this->create_wall_timer( std::chrono::seconds(1), diff --git a/src/les_pkg/src/les1/publisher.cpp b/src/les_pkg/src/les1/publisher.cpp index e1e40cd..447c9b6 100644 --- a/src/les_pkg/src/les1/publisher.cpp +++ b/src/les_pkg/src/les1/publisher.cpp @@ -1,22 +1,4 @@ -// Wessel T (https://wessel.gg/) -// -// "Build your own tower. -// A kingdom freed from malice. -// Create a world of bounty, peace and beauty." -// ⠀⣠⣶⣶⣤⣁ -// ⢰⣷⡟⠻⣏⠻⣧⣀⠐⠈⠀⡈⠠⠀⠂⠀⢁⠈⠀⠄⠀⠁⣠⣴⣤⡈ -// ⠀⣿⢿⡀⠘⢦⡈⢻⣦⠐⠀⠀⠄⠀⠁⠈⠀⡀⠠⠀⣠⡿⣿⣯⣽⡇ -// ⠀⢻⣿⠛⢦⣄⣹⠦⣌⣳⡀⠀⣠⠈⠀⢾⠀⠀⢀⣼⢯⠞⢡⣿⣿⠁ -// ⠄⠘⣿⣷⣤⡀⠙⣆⠈⠻⣿⡄⠘⣇⠀⣾⠀⣴⠿⢲⣋⣤⣿⡿⠃⠀ -// ⠀⡀⠹⣿⣦⡉⠛⠚⣆⠀⠈⠻⣆⢻⢠⣇⡾⠃⢠⣟⣠⣾⡞⠃⠀ -// ⠂⡀⠄⠹⣿⣏⠛⠒⠾⠷⣄⠀⠙⣞⣿⠋⣀⣴⣋⣽⡿⠋ -// ⠂⠠⠀⠀⣨⣿⢿⣶⣒⠲⢮⣿⣶⣼⣧⣾⣭⣿⠟⠉ -// ⠐⠀⠁⣰⣿⠓⠒⣛⣻⠟⠛⣩⣿⣯⠙⡯⣿⡆ -// ⠐⠀⠄⠸⣿⡟⢉⡽⢛⣿⡿⠉⠀⢸⣧⡷⣾⡇ -// ⠀⢂⠀⠄⠹⢿⣿⣴⣯⠏⠀⠀⠀⣼⢸⣽⣷⠇ -// ⠠⠀⠂⢀⠀⢀⠈⠉⠀⠀⠀⠂⡀⠹⠿⠛⠁⠀⠀ - -/* les1.cpp +/* les1/publisher.cpp * Assignment done in the first lesson explaining * the basics of ROS2 * @@ -24,66 +6,66 @@ * Changelog: * [04-09-2025] Wessel T: * - Implement template + * [11-09-2025] Wessel T: Remove unused imports */ #include #include "rclcpp/rclcpp.hpp" -#include -#include #include "geometry_msgs/msg/point.hpp" // Point for x, y, z coordinates #include "les_interface/msg/hardware_status.hpp" // Custom HW status message class NodeLes1Publisher : public rclcpp::Node { - public: -NodeLes1Publisher() - : Node("node_les1_publisher") -{ - publisher_location_ = + NodeLes1Publisher() + : Node("node_les1_publisher") + { + publisher_location_ = this->create_publisher("location", 10); publisher_hw_status_ = this->create_publisher("hardware_status", 10); timer_location_ = this->create_wall_timer( - std::chrono::seconds(2), - std::bind(&NodeLes1Publisher::timer_location_function, this) + std::chrono::seconds(2), + std::bind(&NodeLes1Publisher::timer_location_function, this) ); timer_hw_status_ = this->create_wall_timer( - std::chrono::seconds(2), - std::bind(&NodeLes1Publisher::timer_hw_status_function, this) + std::chrono::seconds(2), + std::bind(&NodeLes1Publisher::timer_hw_status_function, this) ); -} + } -void timer_location_function() { - geometry_msgs::msg::Point coordinate; - coordinate.x = static_cast(std::rand()) / RAND_MAX * 100.0; - coordinate.y = static_cast(std::rand()) / RAND_MAX * 100.0; - coordinate.z = static_cast(std::rand()) / RAND_MAX * 100.0; + void timer_location_function() { + geometry_msgs::msg::Point coordinate; + coordinate.x = static_cast(std::rand()) / RAND_MAX * 100.0; + coordinate.y = static_cast(std::rand()) / RAND_MAX * 100.0; + coordinate.z = static_cast(std::rand()) / RAND_MAX * 100.0; - publisher_location_->publish(coordinate); - RCLCPP_INFO(this->get_logger(), - "Published x=%.2f, y=%.2f, z=%.2f", coordinate.x, coordinate.y, coordinate.z); -} + publisher_location_->publish(coordinate); + RCLCPP_INFO(this->get_logger(), + "Published x=%.2f, y=%.2f, z=%.2f", coordinate.x, coordinate.y, coordinate.z + ); + } -void timer_hw_status_function() { - les_interface::msg::HardwareStatus hw_msg; - hw_msg.version = 1; - hw_msg.temperature = static_cast(std::rand()) / RAND_MAX * 80.0; - hw_msg.are_motors_ready = (std::rand() % 2 == 0); - hw_msg.debug_message = "Status OK"; + void timer_hw_status_function() { + les_interface::msg::HardwareStatus hw_msg; + hw_msg.version = 1; + hw_msg.temperature = static_cast(std::rand()) / RAND_MAX * 80.0; + hw_msg.are_motors_ready = (std::rand() % 2 == 0); + hw_msg.debug_message = "Status OK"; - publisher_hw_status_->publish(hw_msg); - RCLCPP_INFO(this->get_logger(), - "Published HW status: version=%ld, temp=%.2f, motors_ready=%s, msg=%s", - hw_msg.version, hw_msg.temperature, - hw_msg.are_motors_ready ? "true" : "false", - hw_msg.debug_message.c_str()); -} + publisher_hw_status_->publish(hw_msg); + RCLCPP_INFO(this->get_logger(), + "Published HW status: version=%ld, temp=%.2f, motors_ready=%s, msg=%s", + hw_msg.version, hw_msg.temperature, + hw_msg.are_motors_ready ? "true" : "false", + hw_msg.debug_message.c_str() + ); + } private: rclcpp::Publisher::SharedPtr publisher_location_; diff --git a/src/les_pkg/src/les1/subscriber.cpp b/src/les_pkg/src/les1/subscriber.cpp index e2a2906..30217fa 100644 --- a/src/les_pkg/src/les1/subscriber.cpp +++ b/src/les_pkg/src/les1/subscriber.cpp @@ -1,22 +1,4 @@ -// Wessel T (https://wessel.gg/) -// -// "Build your own tower. -// A kingdom freed from malice. -// Create a world of bounty, peace and beauty." -// ⠀⣠⣶⣶⣤⣁ -// ⢰⣷⡟⠻⣏⠻⣧⣀⠐⠈⠀⡈⠠⠀⠂⠀⢁⠈⠀⠄⠀⠁⣠⣴⣤⡈ -// ⠀⣿⢿⡀⠘⢦⡈⢻⣦⠐⠀⠀⠄⠀⠁⠈⠀⡀⠠⠀⣠⡿⣿⣯⣽⡇ -// ⠀⢻⣿⠛⢦⣄⣹⠦⣌⣳⡀⠀⣠⠈⠀⢾⠀⠀⢀⣼⢯⠞⢡⣿⣿⠁ -// ⠄⠘⣿⣷⣤⡀⠙⣆⠈⠻⣿⡄⠘⣇⠀⣾⠀⣴⠿⢲⣋⣤⣿⡿⠃⠀ -// ⠀⡀⠹⣿⣦⡉⠛⠚⣆⠀⠈⠻⣆⢻⢠⣇⡾⠃⢠⣟⣠⣾⡞⠃⠀ -// ⠂⡀⠄⠹⣿⣏⠛⠒⠾⠷⣄⠀⠙⣞⣿⠋⣀⣴⣋⣽⡿⠋ -// ⠂⠠⠀⠀⣨⣿⢿⣶⣒⠲⢮⣿⣶⣼⣧⣾⣭⣿⠟⠉ -// ⠐⠀⠁⣰⣿⠓⠒⣛⣻⠟⠛⣩⣿⣯⠙⡯⣿⡆ -// ⠐⠀⠄⠸⣿⡟⢉⡽⢛⣿⡿⠉⠀⢸⣧⡷⣾⡇ -// ⠀⢂⠀⠄⠹⢿⣿⣴⣯⠏⠀⠀⠀⣼⢸⣽⣷⠇ -// ⠠⠀⠂⢀⠀⢀⠈⠉⠀⠀⠀⠂⡀⠹⠿⠛⠁⠀⠀ - -/* les1.cpp +/* les1/subscriber.cpp * Assignment done in the first lesson explaining * the basics of ROS2 * @@ -38,8 +20,8 @@ using namespace std::placeholders; class NodeLes1Subscriber : public rclcpp::Node { public: -NodeLes1Subscriber() - : Node("node_les1_subscriber") + NodeLes1Subscriber() + : Node("node_les1_subscriber") { subscriber_location_ = this->create_subscription( @@ -52,21 +34,21 @@ NodeLes1Subscriber() "hardware_status", 10, std::bind(&NodeLes1Subscriber::sub_callback_hw_status, this, _1) ); -} + } -void sub_callback_location(const geometry_msgs::msg::Point::SharedPtr msg) { + void sub_callback_location(const geometry_msgs::msg::Point::SharedPtr msg) { RCLCPP_INFO(this->get_logger(), "Received: x=%.2f, y=%.2f, z=%.2f", msg->x, msg->y, msg->z ); -} + } -void sub_callback_hw_status(const les_interface::msg::HardwareStatus::SharedPtr msg) { + void sub_callback_hw_status(const les_interface::msg::HardwareStatus::SharedPtr msg) { RCLCPP_INFO(this->get_logger(), "Received HW status: version=%ld, temp=%.2f, motors_ready=%s, msg=%s", msg->version, msg->temperature, msg->are_motors_ready ? "true" : "false", msg->debug_message.c_str()); -} + } private: rclcpp::Subscription::SharedPtr subscriber_location_; diff --git a/src/les_pkg/src/node_template.cpp b/src/les_pkg/src/node_template.cpp new file mode 100644 index 0000000..b2777d2 --- /dev/null +++ b/src/les_pkg/src/node_template.cpp @@ -0,0 +1,31 @@ +/* node_template.cpp + * Basic node template for ROS2 + * + * Reviewed by: + * Changelog: + * [04-09-2025] Wessel T: Implement template + */ + +#include +#include "rclcpp/rclcpp.hpp" + +class NodeTemplate : public rclcpp::Node { +public: + NodeTemplate() + : Node("node_template") + { + } + +private: +}; + +int main(int argc,char *argv[]) { + rclcpp::init(argc,argv); + + auto node = std::make_shared(); + + rclcpp::spin(node); + rclcpp::shutdown(); + + return 0; +}