Simple topic subscriber callback function not working (C++)
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)
{
ROS_INFO("HEARD!");
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "eric_slider_server");
ros::NodeHandle nh("~");
if(!nh.getParam("nodeId",nodeId)){
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())
{
ros::spinOnce();
loop_rate.sleep();
}
}
Python Code:
import rospy import maxon_slider.msg import maxon_command
pub = rospy.Publisher('/eric_a2z_hand_2/command',maxon_command, queue_size=10)
rospy.init_node('talker',anonymous=True)
msg = maxon_command()
pub.publish(msg)
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.Embedded the code now!
Is this node running on the same PC that you're publishing the topic, or is there networking involved?
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.
Could we see the python script?
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.
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?
Is that the full python script?