Skip to content
This repository has been archived by the owner on Jan 23, 2025. It is now read-only.

Commit

Permalink
refactor(node): dynamically create a publisher per zone on construction
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanaelGandhi committed May 16, 2024
1 parent 7dfc1b2 commit ac43d09
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <rclcpp/rclcpp.hpp>
#include <string>
#include <vector>

#include "linux_thermal_zone_interfaces/msg/linux_thermal_zone.hpp"
#include "linux_thermal_zone_interfaces/msg/linux_thermal_zone_base_node_hk.hpp"
Expand All @@ -14,11 +15,11 @@ class LinuxThermalZoneBaseNode : public rclcpp::Node

protected:
private:
size_t thermal_pub_count_;
size_t linux_thermal_zone_pub_count_;
rclcpp::TimerBase::SharedPtr timer_1s_;
rclcpp::TimerBase::SharedPtr timer_10s_;
rclcpp::Publisher<linux_thermal_zone_interfaces::msg::LinuxThermalZone>::SharedPtr
publisher_thermal_;
std::vector<rclcpp::Publisher<linux_thermal_zone_interfaces::msg::LinuxThermalZone>::SharedPtr>
publishers_linux_thermal_zone_;
rclcpp::Publisher<linux_thermal_zone_interfaces::msg::LinuxThermalZoneBaseNodeHk>::SharedPtr
publisher_node_hk_;

Expand Down
31 changes: 19 additions & 12 deletions src/linux_thermal_zone_base_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>

using namespace std::chrono_literals;

// PUBLIC FUNCTIONS

LinuxThermalZoneBaseNode::LinuxThermalZoneBaseNode(const std::string & node_name)
: rclcpp::Node(node_name), thermal_pub_count_(0)
: rclcpp::Node(node_name), linux_thermal_zone_pub_count_(0)
{
RCLCPP_INFO_STREAM(this->get_logger(), "default constructor executed");

Expand All @@ -21,8 +20,14 @@ LinuxThermalZoneBaseNode::LinuxThermalZoneBaseNode(const std::string & node_name
this->create_wall_timer(10s, std::bind(&LinuxThermalZoneBaseNode::timer_10s_callback, this));
RCLCPP_INFO_STREAM(this->get_logger(), "timers created");

publisher_thermal_ =
this->create_publisher<linux_thermal_zone_interfaces::msg::LinuxThermalZone>("thermal", 10);
uint8_t num_thermal_zones = CountMatchingDirectories("thermal_zone");

for (uint8_t zone_index = 0; zone_index < num_thermal_zones; zone_index++) {
std::string zone_string = "thermal_zone" + std::to_string(zone_index);
publishers_linux_thermal_zone_.push_back(
this->create_publisher<linux_thermal_zone_interfaces::msg::LinuxThermalZone>(
zone_string, 10));
}
publisher_node_hk_ =
this->create_publisher<linux_thermal_zone_interfaces::msg::LinuxThermalZoneBaseNodeHk>(
"node_hk", 10);
Expand All @@ -43,18 +48,19 @@ void LinuxThermalZoneBaseNode::timer_1s_callback()
RCLCPP_DEBUG_STREAM(this->get_logger(), "timer_1s_callback executed");
std::vector<linux_thermal_zone_interfaces::msg::LinuxThermalZone> msgs = GetZoneMsgVector();

RCLCPP_INFO_STREAM(this->get_logger(), "Publishing: " << msgs.size() << " ThermalZone messages");
for (linux_thermal_zone_interfaces::msg::LinuxThermalZone message : msgs) {
publisher_thermal_->publish(message);
thermal_pub_count_++;
RCLCPP_INFO_STREAM(
this->get_logger(), "Publishing: " << msgs.size() << " LinuxThermalZone messages");
for (uint8_t index = 0; index < publishers_linux_thermal_zone_.size(); index++) {
publishers_linux_thermal_zone_.at(index)->publish(msgs.at(index));
linux_thermal_zone_pub_count_++;
}
}

void LinuxThermalZoneBaseNode::timer_10s_callback()
{
RCLCPP_DEBUG_STREAM(this->get_logger(), "timer_10s_callback executed");
linux_thermal_zone_interfaces::msg::LinuxThermalZoneBaseNodeHk message;
message.set__linux_thermal_zone_publish_count(thermal_pub_count_);
message.set__linux_thermal_zone_publish_count(linux_thermal_zone_pub_count_);
publisher_node_hk_->publish(message);
}

Expand All @@ -77,11 +83,12 @@ LinuxThermalZoneBaseNode::GetZoneMsgVector(void)
{
std::vector<linux_thermal_zone_interfaces::msg::LinuxThermalZone> msgs;
const std::string key = "thermal_zone";
uint8_t num_zones = CountMatchingDirectories(key);

for (uint8_t zone_index = 0; zone_index < num_zones; ++zone_index) {
uint8_t num_thermal_zones = CountMatchingDirectories(key);

for (uint8_t zone_index = 0; zone_index < num_thermal_zones; ++zone_index) {
msgs.push_back(GetZoneMsg(key, zone_index));
RCLCPP_INFO_STREAM(
RCLCPP_DEBUG_STREAM(
this->get_logger(), msgs.at(zone_index).temperature.header.frame_id
<< " temperature: " << msgs.at(zone_index).temperature.temperature
<< "°C");
Expand Down

0 comments on commit ac43d09

Please sign in to comment.