rm_control
Loading...
Searching...
No Matches
DebugDataPublisher.h
Go to the documentation of this file.
1//
2// Created by wk on 2026/3/2.
3//
4#pragma once
5#include <ros/ros.h>
6#include <rm_msgs/DebugData.h>
7#include <unordered_map>
8#include <string>
9
14{
15public:
22 explicit DebugDataPublisher(ros::NodeHandle& nh, const std::string& topic = "debug_data", int queue_size = 5)
23 {
24 debug_data_pub_ = nh.advertise<rm_msgs::DebugData>(topic, queue_size);
25 }
26
32 void add(const std::string& name, double value)
33 {
34 auto it = index_map_.find(name);
35 if (it != index_map_.end())
36 {
37 // Update existing entry
38 debug_data_.stamp[it->second] = ros::Time::now();
39 debug_data_.value[it->second] = value;
40 }
41 else
42 {
43 // Insert new entry
44 index_map_[name] = debug_data_.name.size();
45 debug_data_.stamp.push_back(ros::Time::now());
46 debug_data_.name.push_back(name);
47 debug_data_.value.push_back(value);
48 }
49 }
50
57 void add(const std::string& name, double value, const ros::Time& stamp)
58 {
59 auto it = index_map_.find(name);
60 if (it != index_map_.end())
61 {
62 debug_data_.stamp[it->second] = stamp;
63 debug_data_.value[it->second] = value;
64 }
65 else
66 {
67 index_map_[name] = debug_data_.name.size();
68 debug_data_.stamp.push_back(stamp);
69 debug_data_.name.push_back(name);
70 debug_data_.value.push_back(value);
71 }
72 }
73
77 void publish()
78 {
79 if (!debug_data_.name.empty())
80 {
81 debug_data_pub_.publish(debug_data_);
82 }
83 clear();
84 }
85
90 {
91 if (!debug_data_.name.empty())
92 {
93 debug_data_pub_.publish(debug_data_);
94 }
95 }
96
100 void clear()
101 {
102 debug_data_.stamp.clear();
103 debug_data_.name.clear();
104 debug_data_.value.clear();
105 index_map_.clear();
106 }
107
108private:
109 ros::Publisher debug_data_pub_;
110 rm_msgs::DebugData debug_data_;
111 std::unordered_map<std::string, size_t> index_map_;
112};
Debug data publisher. Supports adding/updating named variables and batch publishing.
Definition DebugDataPublisher.h:14
DebugDataPublisher(ros::NodeHandle &nh, const std::string &topic="debug_data", int queue_size=5)
Construct and initialize the publisher.
Definition DebugDataPublisher.h:22
void add(const std::string &name, double value)
Add or update a debug variable, stamped at current time.
Definition DebugDataPublisher.h:32
void clear()
Clear all stored debug data.
Definition DebugDataPublisher.h:100
void publishAndKeep()
Publish accumulated debug data without clearing. Useful for persistent variables.
Definition DebugDataPublisher.h:89
void publish()
Publish accumulated debug data and clear.
Definition DebugDataPublisher.h:77
void add(const std::string &name, double value, const ros::Time &stamp)
Add or update a debug variable with a custom timestamp.
Definition DebugDataPublisher.h:57