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

Commit

Permalink
feat(node): split data acquisition into its own thread
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanaelGandhi committed May 17, 2024
1 parent 65a35b2 commit 779be91
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#pragma once

#include <atomic>
#include <mutex>
#include <rclcpp/rclcpp.hpp>
#include <string>
#include <thread>
#include <vector>

#include "linux_thermal_zone_interfaces/msg/linux_thermal_zone.hpp"
Expand All @@ -15,7 +18,10 @@ class LinuxThermalZoneBaseNode : public rclcpp::Node

protected:
private:
size_t linux_thermal_zone_pub_count_;
std::thread data_acquisition_thread_;
std::mutex linux_thermal_zone_msgs_mutex_;
std::vector<linux_thermal_zone_interfaces::msg::LinuxThermalZone> linux_thermal_zone_msgs_;
std::atomic<size_t> linux_thermal_zone_pub_count_;
rclcpp::TimerBase::SharedPtr timer_1s_;
rclcpp::TimerBase::SharedPtr timer_10s_;
std::vector<rclcpp::Publisher<linux_thermal_zone_interfaces::msg::LinuxThermalZone>::SharedPtr>
Expand All @@ -25,6 +31,7 @@ class LinuxThermalZoneBaseNode : public rclcpp::Node

void timer_1s_callback(void);
void timer_10s_callback(void);
void data_acquisition_thread(void);
std::vector<linux_thermal_zone_interfaces::msg::LinuxThermalZone> GetZoneMsgVector(void);
linux_thermal_zone_interfaces::msg::LinuxThermalZone GetZoneMsg(
std::string key, uint8_t zone_index);
Expand Down
31 changes: 28 additions & 3 deletions src/linux_thermal_zone_base_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ LinuxThermalZoneBaseNode::LinuxThermalZoneBaseNode(const std::string & node_name
{
RCLCPP_INFO_STREAM(this->get_logger(), "default constructor executed");

// threads
data_acquisition_thread_ =
std::thread(std::bind(&LinuxThermalZoneBaseNode::data_acquisition_thread, this));

// timers
timer_1s_ =
this->create_wall_timer(1s, std::bind(&LinuxThermalZoneBaseNode::timer_1s_callback, this));
timer_10s_ =
Expand All @@ -22,6 +27,7 @@ LinuxThermalZoneBaseNode::LinuxThermalZoneBaseNode(const std::string & node_name

uint8_t num_thermal_zones = CountMatchingDirectories("thermal_zone");

// publishers
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(
Expand All @@ -37,21 +43,40 @@ LinuxThermalZoneBaseNode::LinuxThermalZoneBaseNode(const std::string & node_name
LinuxThermalZoneBaseNode::~LinuxThermalZoneBaseNode()
{
RCLCPP_INFO_STREAM(this->get_logger(), "destructor executed");

data_acquisition_thread_.join();
}

// PROTECTED FUNCTIONS

// PRIVATE FUNCTIONS

void LinuxThermalZoneBaseNode::data_acquisition_thread(void)
{
while (rclcpp::ok()) {
RCLCPP_INFO_STREAM(
this->get_logger(),
"data_acquisition_thread executed on thread id: " << std::this_thread::get_id());
auto msgs = GetZoneMsgVector();
std::unique_lock<std::mutex> lock(linux_thermal_zone_msgs_mutex_);
linux_thermal_zone_msgs_ = msgs;
lock.unlock(); // unlock the mutex explicitly
std::this_thread::sleep_for(2s);
}
}

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() << " LinuxThermalZone messages");
this->get_logger(),
"Publishing: " << linux_thermal_zone_msgs_.size() << " LinuxThermalZone messages");

const std::lock_guard<std::mutex> lock(
linux_thermal_zone_msgs_mutex_); // lock until end of scope
for (uint8_t index = 0; index < publishers_linux_thermal_zone_.size(); index++) {
publishers_linux_thermal_zone_.at(index)->publish(msgs.at(index));
publishers_linux_thermal_zone_.at(index)->publish(linux_thermal_zone_msgs_.at(index));
linux_thermal_zone_pub_count_++;
}
}
Expand Down

0 comments on commit 779be91

Please sign in to comment.