How to Publish Multiple Message Types from a Single Custom ROS Node in C++

Creating Custom ROS Nodes in C++

When working with the Robot Operating System (ROS), it’s common to need custom nodes that can perform specific tasks. One such task might be publishing messages of different types, depending on certain conditions or inputs to your node. In this article, we’ll walk through how you can create a ROS node written in C++ that is capable of publishing messages of multiple types.

Setting Up Your ROS Project

Before starting with the actual coding, make sure you have a basic understanding of ROS and are able to set up a ROS project. This includes installing ROS on your system, creating a workspace, and initializing it. If you’re new to ROS, you might want to start by looking into ROS tutorials that cover these basics.

Writing Your C++ Node

Let’s begin with the code for our custom node. We’ll name this node “multi_pub” and have it publish messages of two types: std_msgs/String and geometry_msgs/PoseStamped. This will be a simple example to demonstrate how you can achieve this using ROS.

// multi_pub.cpp
#include <ros/ros.h>
#include <std_msgs/String.h>
#include <geometry_msgs/PoseStamped.h>
class MultiPub {
public:
    MultiPub() : pub1(nh.advertise<std_msgs::String>("string_message", 10)), 
                 pub2(nh.advertise<geometry_msgs::PoseStamped>("pose_message", 10)) {}
    
    void publish() {
        // Publish a String message
        std_msgs::String msg;
        msg.data = "This is a string message";
        pub1.publish(msg);
        
        // Publish a PoseStamped message
        geometry_msgs::PoseStamped msg2;
        msg2.header.frame_id = "world";
        msg2.pose.position.x = 1.0;
        msg2.pose.position.y = 2.0;
        msg2.pose.orientation.w = 1.0;
        pub2.publish(msg2);
        
        ROS_INFO("Published String and PoseStamped messages");
    }
private:
    ros::NodeHandle nh;
    ros::Publisher pub1, pub2;
};
int main(int argc, char** argv) {
    ros::init(argc, argv, "multi_pub");
    ros::start();
    
    MultiPub multi_pub_instance;
    while(ros::ok()) {
        multi_pub_instance.publish();
        ros::Duration(10).sleep();  // Sleep for 10 seconds
    }
    
    return 0;
}

Compiling and Running Your Node

To compile the node, you’ll need to use the catkin_make command in your ROS workspace. After compilation, you can run your node using rosrun.

$ catkin_make -DCATKIN_WHITELIST_PACKAGES=*
# ... Compilation output ...
$ rosrun multi_pub multi_pub_node

This will start publishing messages of both types every 10 seconds.

Conclusion

In this article, we demonstrated how to create a custom ROS node in C++ that publishes messages of multiple types. This can be useful in scenarios where you need to communicate different information depending on the context or input conditions within your ROS project. Remember to adapt this code example to fit your specific needs and experiment with publishing different message types as per your requirements.