Reputation: 15
I would like to create a stand alone ros2 Python node that, when ran, is equivelent to the command
ros2 topic pub --once other_topic message
How do I go about doing this?
I do not want to create a new publisher/subscriber/topic, I am simply trying to publish a message to an existing topic from a given node.
Upvotes: 0
Views: 1229
Reputation: 64
A node that has the functionality that you describe is a publisher node. Check out the official tutorials by ROS2. You do not need to create a whole new message type or a topic to start publishing messages. You can publish messages over already existing topics. you just need to specify the topic name and type appropriately when instantiating your publisher object.
Firstly, in your node declare your publisher
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
Then you must instantiate it according to the type of the topic
publisher_ = this->create_publisher<std_msgs::msg::String>("topic", 10);
Then according to some programmatic condition, you can publish the message over an already existing topic
publisher_->publish(message);
This example assumes that your topic is of type string included in the std_msgs package. The above code is in C++ but the implementation strategy is the same in python. See here for a python example
Upvotes: 0