Publishing unbound array in ROS2 topic
Hello,
I have a custom msg with one element uint8[] a
.
In my publisher (example from the tutorial tutorial_interfaces
, I am trying to populate that unbounded array but I am getting an error. Here is the code:
#include <chrono>
#include <memory>
#include "rclcpp/rclcpp.hpp"
#include "tutorial_interfaces/msg/num.hpp" // CHANGE
using namespace std::chrono_literals;
class MinimalPublisher : public rclcpp::Node
{
public:
MinimalPublisher()
: Node("minimal_publisher"), count_(0)
{
publisher_ = this->create_publisher<tutorial_interfaces::msg::Num>("topic", 10); // CHANGE
timer_ = this->create_wall_timer(
500ms, std::bind(&MinimalPublisher::timer_callback, this));
}
private:
void timer_callback()
{
auto message = tutorial_interfaces::msg::Num(); // CHANGE
//message.a = 170;//this->count_++; // CHANGE
//message.b = 171;this->count_++; // CHANGE
message.a.push_back(170);
message.a.push_back(171);
//RCLCPP_INFO(this->get_logger(), "Publishing: '%d'", message.a); // CHANGE
publisher_->publish(message);
}
rclcpp::TimerBase::SharedPtr timer_;
rclcpp::Publisher<tutorial_interfaces::msg::Num>::SharedPtr publisher_; // CHANGE
size_t count_;
};
int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<MinimalPublisher>());
rclcpp::shutdown();
return 0;
}
And the error I get is:
root@4f18f7e74ccb:/home/wd_dev# colcon build --packages-select tutorial_interfaces
Starting >>> tutorial_interfaces
[0.331s] WARNING:colcon.colcon_core.prefix_path.colcon:The path '/home/ws_dev/install' in the environment variable COLCON_PREFIX_PATH doesn't exist
[0.331s] WARNING:colcon.colcon_ros.prefix_path.ament:The path '/home/ws_dev/install/tutorial_interfaces' in the environment variable AMENT_PREFIX_PATH doesn't exist
[0.331s] WARNING:colcon.colcon_ros.prefix_path.ament:The path '/home/ws_dev/install/cpp_pubsub' in the environment variable AMENT_PREFIX_PATH doesn't exist
[0.331s] WARNING:colcon.colcon_ros.prefix_path.catkin:The path '/home/ws_dev/install/tutorial_interfaces' in the environment variable CMAKE_PREFIX_PATH doesn't exist
[0.331s] WARNING:colcon.colcon_ros.prefix_path.catkin:The path '/home/ws_dev/install/cpp_pubsub' in the environment variable CMAKE_PREFIX_PATH doesn't exist
--- stderr: tutorial_interfaces
/home/wd_dev/src/tutorial_interfaces/src/publisher.cpp: In member function ‘void MinimalPublisher::timer_callback()’:
/home/wd_dev/src/tutorial_interfaces/src/publisher.cpp:26:15: error: request for member ‘push_back’ in ‘message.tutorial_interfaces::msg::Num_<std::allocator<void> >::a’, which is of non-class type ‘tutorial_interfaces::msg::Num_<std::allocator<void> >::_a_type’ {aka ‘unsigned char’}
26 | message.a.push_back(170);
| ^~~~~~~~~
/home/wd_dev/src/tutorial_interfaces/src/publisher.cpp:27:15: error: request for member ‘push_back’ in ‘message.tutorial_interfaces::msg::Num_<std::allocator<void> >::a’, which is of non-class type ‘tutorial_interfaces::msg::Num_<std::allocator<void> >::_a_type’ {aka ‘unsigned char’}
27 | message.a.push_back(171);
| ^~~~~~~~~
make[2]: *** [CMakeFiles/talker.dir/build.make:63: CMakeFiles/talker.dir/src/publisher.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:123: CMakeFiles/talker.dir/all] Error 2
make: *** [Makefile:141: all] Error 2
---
Failed <<< tutorial_interfaces [3.14s, exited with code 2]
Summary: 0 packages finished [3.35s]
1 package failed: tutorial_interfaces
1 package had stderr output: tutorial_interfaces
That same line of code used to work with ROS1. Has anything changed? Thanks for the help.
Can you please post your CMakeLists.txt and package.xml as well