Ask Your Question
0

Publish to Topics of different node [Solved]

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

PKumars gravatar image

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

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

Comments

What do you want to do that isn't possible with the standard C++ publisher like http://wiki.ros.org/ROS/Tutorials/Wri... ?

ahendrix gravatar imageahendrix ( 2016-03-30 21:07:29 -0600 )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 imageDavidN ( 2016-03-30 21:19:45 -0600 )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 imagePKumars ( 2016-03-31 04:15:51 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted
1

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

PKumars gravatar image

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

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;

        Joint0.data = 0.0;
        Joint1.data = -1.2; 
        Joint2.data = 0.0; 
        Joint3.data = -1.2;
        Joint4.data = -1.2; 
        Joint5.data = -1.2;
        Joint6.data = 0.0;
        Joint7.data = 0.0;

        shoulder_roll_joint.publish(Joint0);
        shoulder_pitch_joint.publish(Joint1);
        shoulder_yaw_joint.publish(Joint2);
        elbow_pitch_joint.publish(Joint3);
        elbow_yaw_joint.publish(Joint4);
        wrist_pitch_joint.publish(Joint5);
        wrist_roll_joint.publish(Joint6);
        gripper_joint.publish(Joint7);


        rate.sleep();


    }
    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

Comments

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

AnNguyen gravatar imageAnNguyen ( 2019-11-09 05:50:08 -0600 )edit
0

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

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

Stats

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

Seen: 1,205 times

Last updated: Mar 31 '16