Simple topic subscriber callback function not working (C++)

asked 2017-07-13 09:46:08 -0500

rosquestion gravatar image

updated 2017-07-14 12:33:38 -0500

allenh1 gravatar image

Hey all, I'm trying to get a simple subscriber in roscpp working. I have my node subscribe to a topic with a callback called commandCB. The callback simply prints out that it was called. When I run the code and rostopic info the topic, it says that the node is subscribing to the topic correctly. However, when I publish to the topic, the callback function does not do anything. I rostopic echo the topic and see the message getting published so why wouldn't my callback function simply print out the string? Below is the main part of my code, thank you so much!

void commandCB(const maxon_slider::maxon_command::ConstPtr& msg)

int main(int argc, char **argv)
    ros::init(argc, argv, "eric_slider_server");
    ros::NodeHandle nh("~");
        ROS_WARN("unable to load required parameter %s!",nh.resolveName("nodeId").c_str());
        return -1;

    std::stringstream sstm;
    std::string res;
    sstm << "/eric_a2z_hand_" << nodeId << "/command";
    res = sstm.str();
    ros::Subscriber command_sub = nh.subscribe(res,10,commandCB);

    ROS_INFO("slider topics started with nodeId=%d",nodeId);

    ros::Rate loop_rate(10.0);
    while (ros::ok())

Python Code:

import rospy import maxon_slider.msg import maxon_command
pub = rospy.Publisher('/eric_a2z_hand_2/command',maxon_command, queue_size=10)
msg = maxon_command()
edit retag flag offensive close merge delete



Please include your code directly into this question. Copy-paste it in, then select it all and press the Preformatted Text button (the one with 101010 on it). Thanks.

gvdhoorn gravatar image gvdhoorn  ( 2017-07-13 10:30:17 -0500 )edit

Embedded the code now!

rosquestion gravatar image rosquestion  ( 2017-07-13 10:50:47 -0500 )edit

Is this node running on the same PC that you're publishing the topic, or is there networking involved?

Ed Venator gravatar image Ed Venator  ( 2017-07-13 21:19:30 -0500 )edit

The node is running on the same PC. I just have a basic python script that publishes to that same topic in a different terminal.

rosquestion gravatar image rosquestion  ( 2017-07-14 07:55:35 -0500 )edit

Could we see the python script?

allenh1 gravatar image allenh1  ( 2017-07-14 11:54:59 -0500 )edit

And the nodeID in the subscriber code is definitely 2 so I am publishing to the same topic I am subscribing. I know this is true because if I rostopic info that topic, it says there is a subscriber, which is the one I made. So it knows there's a subscriber but doesn't pass the msg to the callback.

rosquestion gravatar image rosquestion  ( 2017-07-14 12:31:22 -0500 )edit

IS it possible that the callback is somehow dropping the one message? Shouldn't I be able to send just one message and that one message get received by the callback?

rosquestion gravatar image rosquestion  ( 2017-07-14 12:32:53 -0500 )edit

Is that the full python script?

allenh1 gravatar image allenh1  ( 2017-07-14 12:35:32 -0500 )edit