From cbdc9adf4e2078a258009693fa12ec6b1aee5ff5 Mon Sep 17 00:00:00 2001 From: Markus Bader Date: Thu, 11 Dec 2025 15:10:21 +0100 Subject: [PATCH] example fixed and helpler classes for msgs added --- CMakeLists.txt | 26 +++++++++++++++ example.cpp | 41 +++++++++--------------- include/plotjuggler_msgs/data_point.hpp | 25 +++++++++++++++ include/plotjuggler_msgs/data_points.hpp | 26 +++++++++++++++ include/plotjuggler_msgs/dictionary.hpp | 25 +++++++++++++++ package.xml | 1 + 6 files changed, 118 insertions(+), 26 deletions(-) create mode 100644 include/plotjuggler_msgs/data_point.hpp create mode 100644 include/plotjuggler_msgs/data_points.hpp create mode 100644 include/plotjuggler_msgs/dictionary.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index fb8282e..770a469 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,6 +35,7 @@ elseif(ament_cmake_FOUND) find_package(ament_cmake REQUIRED) find_package(rosidl_default_generators REQUIRED) find_package(std_msgs REQUIRED) + find_package(rclcpp REQUIRED) rosidl_generate_interfaces(${PROJECT_NAME} msg/DataPoint.msg @@ -46,6 +47,31 @@ elseif(ament_cmake_FOUND) ADD_LINTER_TESTS ) + + add_executable(example example.cpp) + + target_include_directories(example PRIVATE + $ + $) + + rosidl_get_typesupport_target(cpp_typesupport_target + ${PROJECT_NAME} "rosidl_typesupport_cpp") + target_link_libraries(example "${cpp_typesupport_target}") + + ament_target_dependencies(example rclcpp std_msgs) + + + install(DIRECTORY include/ + DESTINATION include/${PROJECT_NAME} + ) + + ament_export_include_directories( + include + ) + install(TARGETS + example + DESTINATION lib/${PROJECT_NAME}) + ament_export_dependencies(rosidl_default_runtime) ament_export_dependencies(std_msgs) ament_package() diff --git a/example.cpp b/example.cpp index 85af06d..8bffe57 100644 --- a/example.cpp +++ b/example.cpp @@ -3,40 +3,30 @@ #include #include #include -#include "pj_msgs/msg/dictionary.hpp" -#include "pj_msgs/msg/data_points.hpp" +#include "plotjuggler_msgs/dictionary.hpp" +#include "plotjuggler_msgs/data_point.hpp" +#include "plotjuggler_msgs/data_points.hpp" using namespace std::chrono_literals; -pj_msgs::msg::DataPoint CreateDataPoint(uint16_t name_index, double time, double value) -{ - pj_msgs::msg::DataPoint point; - point.stamp = time; - point.name_index = name_index; - point.value = value; - return point; -} - int main(int argc, char * argv[]) { rclcpp::init(argc, argv); auto node = rclcpp::Node::make_shared("pj_test"); - auto publisher_dict = node->create_publisher("pj_msg_dictionary", + auto publisher_dict = node->create_publisher("pj_msg_dictionary", rclcpp::QoS(10).transient_local()); - auto publisher = node->create_publisher("pj_msg_data", 10); + auto publisher = node->create_publisher("pj_msg_data", 10); std::random_device rd; const uint32_t UUID = rd(); // just a random number - pj_msgs::msg::Dictionary dictionary; - - dictionary.dictionary_uuid = UUID; - dictionary.names.push_back("sensor_a"); // index 0 - dictionary.names.push_back("sensor_b"); // index 1 - dictionary.names.push_back("sensor_c"); // index 2 + plotjuggler_msgs::Dictionary dictionary(UUID); + dictionary.push_back("sensor_a"); // index 0 + dictionary.push_back("sensor_b"); // index 1 + dictionary.push_back("sensor_c"); // index 2 - publisher_dict->publish(dictionary); + publisher_dict->publish(dictionary.msg()); rclcpp::WallRate loop_rate(50ms); @@ -44,13 +34,12 @@ int main(int argc, char * argv[]) while (rclcpp::ok()) { t += 0.1; - pj_msgs::msg::DataPoints msg; - msg.dictionary_uuid = UUID; - msg.samples.push_back( CreateDataPoint(0, t, std::sin(t))); - msg.samples.push_back( CreateDataPoint(1, t, std::cos(t))); - msg.samples.push_back( CreateDataPoint(2, t, 2*std::cos(t))); + plotjuggler_msgs::DataPoints points(UUID); + points.samples.push_back( plotjuggler_msgs::DataPoint(0, t, std::sin(t)).msg()); + points.samples.push_back( plotjuggler_msgs::DataPoint(1, t, std::cos(t)).msg()); + points.samples.push_back( plotjuggler_msgs::DataPoint(2, t, 2*std::cos(t)).msg()); - publisher->publish(msg); + publisher->publish(points.msg()); rclcpp::spin_some(node); loop_rate.sleep(); } diff --git a/include/plotjuggler_msgs/data_point.hpp b/include/plotjuggler_msgs/data_point.hpp new file mode 100644 index 0000000..bb2d265 --- /dev/null +++ b/include/plotjuggler_msgs/data_point.hpp @@ -0,0 +1,25 @@ +#ifndef PLOTJUGGLER_MSGS__DATA_POINT_HPP_ +#define PLOTJUGGLER_MSGS__DATA_POINT_HPP_ + +#include + +namespace plotjuggler_msgs +{ +struct DataPoint : public plotjuggler_msgs::msg::DataPoint +{ + DataPoint(); + DataPoint(uint16_t name_index, double time, double value) + { + this->stamp = time; + this->name_index = name_index; + this->value = value; + } + const plotjuggler_msgs::msg::DataPoint& msg() const { + return static_cast(*this); + } + plotjuggler_msgs::msg::DataPoint& msg() { + return static_cast(*this); + } +}; +} // namespace plotjuggler_msgs +#endif // PLOTJUGGLER_MSGS__DATA_POINT_HPP_ \ No newline at end of file diff --git a/include/plotjuggler_msgs/data_points.hpp b/include/plotjuggler_msgs/data_points.hpp new file mode 100644 index 0000000..7026a76 --- /dev/null +++ b/include/plotjuggler_msgs/data_points.hpp @@ -0,0 +1,26 @@ +#ifndef PLOTJUGGLER_MSGS__DATA_POINTS_HPP_ +#define PLOTJUGGLER_MSGS__DATA_POINTS_HPP_ + +#include + +namespace plotjuggler_msgs +{ +struct DataPoints : public plotjuggler_msgs::msg::DataPoints +{ + DataPoints(); + DataPoints(uint32_t dictionary_uuid){ + this->dictionary_uuid = dictionary_uuid; + } + + void push_back(const plotjuggler_msgs::msg::DataPoint& data_point){ + this->samples.push_back(data_point); + } + const plotjuggler_msgs::msg::DataPoints& msg() const { + return static_cast(*this); + } + plotjuggler_msgs::msg::DataPoints& msg() { + return static_cast< plotjuggler_msgs::msg::DataPoints&>(*this); + } +}; +} // namespace plotjuggler_msgs +#endif // PLOTJUGGLER_MSGS__DATA_POINTS_HPP_ \ No newline at end of file diff --git a/include/plotjuggler_msgs/dictionary.hpp b/include/plotjuggler_msgs/dictionary.hpp new file mode 100644 index 0000000..2f3d1d1 --- /dev/null +++ b/include/plotjuggler_msgs/dictionary.hpp @@ -0,0 +1,25 @@ +#ifndef PLOTJUGGLER_MSGS__DICTIONARY_HPP_ +#define PLOTJUGGLER_MSGS__DICTIONARY_HPP_ + +#include + +namespace plotjuggler_msgs +{ +struct Dictionary : public plotjuggler_msgs::msg::Dictionary +{ + Dictionary(); + Dictionary(uint32_t dictionary_uuid){ + this->dictionary_uuid = dictionary_uuid; + } + void push_back(const std::string& name){ + this->names.push_back(name); + } + const plotjuggler_msgs::msg::Dictionary& msg() const { + return static_cast(*this); + } + plotjuggler_msgs::msg::Dictionary& msg() { + return static_cast(*this); + } +}; +} // namespace plotjuggler_msgs +#endif // PLOTJUGGLER_MSGS__DICTIONARY_HPP_ \ No newline at end of file diff --git a/package.xml b/package.xml index 8395405..b91bbdc 100644 --- a/package.xml +++ b/package.xml @@ -10,6 +10,7 @@ catkin ament_cmake rosidl_default_generators + rclcpp message_generation message_runtime