ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Published topic name is different than specified by `advertise`

asked 2021-12-16 05:48:37 -0500

MMductance gravatar image

Hello everyone I am writing a simple package which contains two executable nodes. One of the two nodes acgen_node_test should publish a message to the topic acgen/des_currents_reg and the other node acgen_node should receive said message.

Now in my sender node acgen_node_test I advertise using

currents_pub = nh.advertise<acgen::DesCurrentsReg>("acgen/des_currents_reg", 256);

but for some reason, it seems like these messages get published under the topic /acgen_node/acgen/des_currents_reg instead of just acgen/des_currents_reg (at least that's what rostopic list shows). I try subscribing in my receiver node using:

des_currents_res_subs=nh.subscribe("acgen/des_currents_reg",64,msg_des_currents_reg_cb);

I'm not sure how I can resolve this issue?

Full Code of sender node acgen_node_test:

#include <ros/ros.h>
#include <ros/console.h>
#include "acgen/DesCurrentsReg.h"
#import <math.h>

ros::Publisher currents_pub;
const double delta_T=20e-3;     //publishing time interval
const double i_amp=1.0;         //amplitude of desirec currents [A]
const double omega=2*M_PI*5;    //frequency of reference waveform

void acgen_timer_cb(const ros::TimerEvent&){
    double current=i_amp*sin(omega*delta_T);

    std::vector<double> currents={1.0,0,0,0,0,0};

    currents_pub.publish(currents);
}

//main
int main(int argc,char** argv){
    ros::init(argc, argv, "acgen_node_test",ros::init_options::NoSigintHandler);

    ROS_INFO("staring up acgen test node");

    ros::NodeHandle nh("~");

    currents_pub = nh.advertise<acgen::DesCurrentsReg>("acgen/des_currents_reg", 256);

    //main timer shows callback function send the driver messages
    ros::Timer timer = nh.createTimer(ros::Duration(delta_T), acgen_timer_cb);

    ROS_INFO("Start spinning");

    ros::spin();
}

Main Routine of receiver node acgen_node:

int main(int argc,char** argv){
    ros::init(argc, argv, "acgen_node",ros::init_options::NoSigintHandler);

    ROS_INFO("startup");

    signal(SIGINT, mySigintHandler);

    ros::NodeHandle nh("~");

    //init tcp connection
    bool comm_est_succeeded=init_comm();
    if(!comm_est_succeeded){
        if(errno==ETIMEDOUT)
            ROS_ERROR("Could not establish TCP connection to ControlCard: Timeout");
        else
        ROS_ERROR("Could not establish TCP connection to ControlCard: %s",strerror(errno));
        ros::shutdown();
        return EXIT_FAILURE;
    }
    ROS_INFO("Established Connection");

    //main timer showse callback function send the driver messages
    ros::Timer timer = nh.createTimer(ros::Duration(20e-3), acgen_timer_cb);
    ROS_INFO("Sending data packets at an interval of %f ms",send_interval*1e3);

    //register services
    srv_enable_acgen=nh.advertiseService("acgen/enable",srv_enable_acgen_cb);
    srv_stop_acgen=nh.advertiseService("acgen/stop",srv_stop_acgen_cb);
    srv_run_regular=nh.advertiseService("acgen/run_regular",srv_run_regular_cb);
    srv_run_resonant=nh.advertiseService("acgen/run_resonant",srv_run_resonant_cb);

    //register subscribers
    des_currents_res_subs=nh.subscribe("acgen/des_currents_reg",64,msg_des_currents_reg_cb);
    des_duties_subs=nh.subscribe("acgen/des_duties",64,msg_des_duties_reg_cb);

    ROS_INFO("Start spinning");

    ros::spin();
}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-12-16 05:53:50 -0500

MMductance gravatar image

Ok I just figured this out, I needed to add a backslash in front of the topic e.g. /acgen/des_currents_reg instead of acgen/des_currents_reg.

edit flag offensive delete link more

Comments

No, this is not a good solution. The ros convention is to use a simple service name, in particular one without any slash e.g. "run_resonant". By default, the service is created as a child of the name of the ros node that creates it.

So if the name of your ros node is "acgen", then this service is created as "acgen/run_resonant". The benefit of doing it this way is that if the parent ros node is renamed by the launch file, the service name will automatically stay consistent.

Mike Scheutzow gravatar image Mike Scheutzow  ( 2021-12-16 09:40:12 -0500 )edit

The issue is actually that the OP creates ros::NodeHandle nh("~"), which is a private node handle.

gvdhoorn gravatar image gvdhoorn  ( 2021-12-16 14:16:25 -0500 )edit

@gvdhoorn Yes, it's good you highlight that - I made the assumption it was intentional. However, that aspect is orthogonal to my point about not using slash in the name when advertising a topic or a service.

Mike Scheutzow gravatar image Mike Scheutzow  ( 2021-12-17 07:05:02 -0500 )edit

Question Tools

Stats

Asked: 2021-12-16 05:48:37 -0500

Seen: 49 times

Last updated: Dec 16 '21