ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Publish to Topics of different node [Solved]

asked 2016-03-30 13:47:31 -0500

PKumars gravatar image

updated 2016-03-31 07:43:33 -0500

Dear All, I have a silly question. is there a way to publish to topics of different node using c++ methods?

I mean like we are subscribing to topics of different nodes, is there a way to publish to topics of different nodes?

I want to do something like this ("/pubJoints").publish(1.4);

I hope I explained the question clearly.

Thanks . waiting for your valuable reply...

edit retag flag offensive close merge delete


What do you want to do that isn't possible with the standard C++ publisher like ?

ahendrix gravatar image ahendrix  ( 2016-03-30 21:07:29 -0500 )edit

Hm... not sure if I understand correctly. You mean you want to create a node which publishes to multiple topics with different topic names?

DavidN gravatar image DavidN  ( 2016-03-30 21:19:45 -0500 )edit

I'm sorry if I couldn't describe clearly. I wanted to publish to topic that's already waiting to take some values. I have two nodes running and I wanted to publish to topics of second node from first node. BTW I solved that problem and I'm posting it now.

PKumars gravatar image PKumars  ( 2016-03-31 04:15:51 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2016-03-31 04:19:54 -0500

PKumars gravatar image

updated 2016-03-31 13:35:23 -0500

ahendrix gravatar image

Here is a complete template where I'm using this for a 7-dof dynamixel arm for sending joint values from .cpp node..

here is a node where I'm publishing to topics of another node and those particular topics are /joint0_controller/command /joint1_controller/command /joint2_controller/command /joint3_controller/command /joint4_controller/command /joint5_controller/command /joint6_controller/command /joint7_controller/command

and msg type is std_msgs/Float64

################# .cpp code.......... ################

#include <ros/ros.h>
#include <tf/transform_listener.h>
#include <geometry_msgs/Twist.h>

// Message publication
#include "std_msgs/MultiArrayLayout.h"
#include "std_msgs/MultiArrayDimension.h"
#include "std_msgs/Float32MultiArray.h"
#include "std_msgs/Float64.h"
#include <std_msgs/Float64.h>
#include <sstream>

    using namespace std;

    // publisher declaration
    ros::Publisher shoulder_roll_joint, shoulder_pitch_joint, shoulder_yaw_joint,
                        elbow_pitch_joint, elbow_yaw_joint, wrist_pitch_joint, wrist_roll_joint, gripper_joint;

int main(int argc, char** argv)
    ros::init(argc, argv, "arm_joints");
    ros::NodeHandle nh;

    // publisher declaration

    shoulder_roll_joint = nh.advertise<std_msgs::Float64>("/joint0_controller/command", 1);
    shoulder_pitch_joint = nh.advertise<std_msgs::Float64>("/joint1_controller/command", 1);
    shoulder_yaw_joint = nh.advertise<std_msgs::Float64>("/joint2_controller/command", 1);
    elbow_pitch_joint = nh.advertise<std_msgs::Float64>("/joint3_controller/command", 1);
    elbow_yaw_joint = nh.advertise<std_msgs::Float64>("/joint4_controller/command", 1);
    wrist_pitch_joint = nh.advertise<std_msgs::Float64>("/joint5_controller/command", 1);
    wrist_roll_joint = nh.advertise<std_msgs::Float64>("/joint6_controller/command", 1);
    gripper_joint = nh.advertise<std_msgs::Float64>("/joint7_controller/command", 1);

    ros::Rate rate(50.0); // frequency of operation

    while (1)
        std_msgs::Float64 areaValue;
        std_msgs::Float64 Joint0, Joint1, Joint2, Joint3, Joint4, Joint5, Joint6, Joint7; = 0.0; = -1.2; = 0.0; = -1.2; = -1.2; = -1.2; = 0.0; = 0.0;



    return 0;


Hope it helps someone.

if anyone finds error or difficulty in understanding then please let me know.

Regards, Prasanna

edit flag offensive delete link more


I have to sign up to say thank you to you! This post really helped me! Good job! Best regards

AnNguyen gravatar image AnNguyen  ( 2019-11-09 05:50:08 -0500 )edit

answered 2016-03-31 13:40:42 -0500

ahendrix gravatar image

The "Anonymous" part of the "Anonymous Publish Subscribe" architecture that ROS is based on means that a node does not have to know if there are subscribers on a topic or not.

This means that publishing to a topic with subscribers is exactly the same as publishing to a topic that doesn't have any subscribers.

If you have a particular node that you're trying to interact with by publishing messages, you should publish them with the same type and topic name as the subscriber. Usually the topic names and types will be documented on the node's wiki page, or if the node is running you can find topic names with rostopic list and the type of a topic with rostopic info.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2016-03-30 13:47:31 -0500

Seen: 2,010 times

Last updated: Mar 31 '16