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

Hydro: Unable to subscribe to topic : installation or coding ? [closed]

asked 2013-08-03 20:38:23 -0500

rnunziata gravatar image

updated 2014-01-28 17:17:32 -0500

ngrennan gravatar image

I have simplified the problem I am having. The callback below is never called. rosnode infor robot_jntcmd_to_jntst on my node name says there are no subscriptions. Yet there are no erros in the logs.

The publisher works fine and rostopic list the topic as well as echo topic.

include <string>
include <ros/ros.h>
include <std_msgs/String.h>



void stateCallback(const std_msgs::String msg)
{
  ROS_INFO("Here in callback");
}

int main(int argc, char **argv)
{ 

  ros::init(argc, argv, "robot_jntcmd_to_jntst");

  ros::NodeHandle n;

  ros::Publisher chatter_pub = n.advertise<std_msgs::String>("/rrbot/joint1_position_controller/state", 1000);

  n.subscribe("/rrbot/joint1_position_controller/state", 10, stateCallback);

    std_msgs::String msg;

    std::stringstream ss;

    ss << "hello world ";

    msg.data = ss.str();

  while(true) {

    chatter_pub.publish(msg);

    ros::spinOnce();

  }

  ROS_INFO("Ended ");

  return 0;
}
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by rnunziata
close date 2013-08-16 11:22:10

2 Answers

Sort by ยป oldest newest most voted
0

answered 2013-08-04 13:24:12 -0500

rnunziata gravatar image

Adding right hand side of assignment worked now the callback is called. Why is this needed? Is there a scope issue?

edit flag offensive delete link more
0

answered 2013-08-04 09:20:17 -0500

updated 2013-08-04 13:58:04 -0500

First check this on your callback arguments (see the ConstPtr&)

void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
   ROS_INFO("I heard: [%s]", msg->data.c_str());
}

Then, if that doesn't work, use ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback); instead of just invoke the method 'subscribe'

This is from subscriber/publisher tutorial:

/**
   * The subscribe() call is how you tell ROS that you want to receive messages
   * on a given topic.  This invokes a call to the ROS
   * master node, which keeps a registry of who is publishing and who
   * is subscribing.  Messages are passed to a callback function, here
   * called chatterCallback.  subscribe() returns a Subscriber object that you
   * must hold on to until you want to unsubscribe.  When all copies of the Subscriber
   * object go out of scope, this callback will automatically be unsubscribed from
   * this topic.
   *
   * The second parameter to the subscribe() function is the size of the message
   * queue.  If messages are arriving faster than they are being processed, this
   * is the number of messages that will be buffered up before beginning to throw
   * away the oldest ones.
   */
edit flag offensive delete link more

Comments

Interesting it say subscription none. yet there are no errors? How can I get this to format correctly when I cut and past from terminal. viki:~$ rosnode info robot_jntcmd_to_jntst -------------------------------------------------------------------------------- Node [/robot_jntcmd_to_jntst] Publications: * /xxx [std_msgs/String] * /rosout [rosgraph_msgs/Log] Subscriptions: None Services: * /robot_jntcmd_to_jntst/set_logger_level * /robot_jntcmd_to_jntst/get_loggers contacting node http://ubuntu:49532/ ... Pid: 13462 Connections: * topic: /rosout * to: /rosout * direction: outbound * transport: TCPROS

rnunziata gravatar image rnunziata  ( 2013-08-04 09:24:32 -0500 )edit

Add this to your answer below a subtitle **EDIT**. Select it and click on code format option

gustavo.velascoh gravatar image gustavo.velascoh  ( 2013-08-04 12:39:01 -0500 )edit

Node [/robot_jntcmd_to_jntst] Publications: * /xxx [std_msgs/String] * /rosout [rosgraph_msgs/Log] Subscriptions: None Services: * /robot_jntcmd_to_jntst/set_logger_level * /robot_jntcmd_to_jntst/get_loggers contacting node http://ubuntu:56554/ ... Pid: 18238 Connections: * topic: /rosout * to: /rosout * direction: outbound * transport: TCPROS

rnunziata gravatar image rnunziata  ( 2013-08-04 12:51:06 -0500 )edit

Question Tools

Stats

Asked: 2013-08-03 20:38:23 -0500

Seen: 404 times

Last updated: Aug 04 '13