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

Node publishes to a topic but subscriber does not react

asked 2018-10-22 08:12:47 -0600

kump gravatar image

updated 2018-10-22 09:10:43 -0600

I have a robot with a gazebo_ros_control plugin. So there is a robot/joint_drive_wheel/command topic to which you publish commands for velocity.

I have also written an rqt plugin that publishes to the topic and ros node that publishes to the topic.

When I use terminal command rostopic pub robot/joint_right_wheel_controller/command std_msgs/Float64 "data: 5.0", the robot reacts.

When I use the rqt plugin, the robot reacts.

When I use the ros node, however, the robot reacts only to commands published in a callback function of a subscriber to different topic. When I want to command the velocity in after creating the publisher, the command does get to the topic (I watch with rostopic echo), but the robot doesn't react to the command.

This is my node:

import rospy
import rospkg
from std_msgs.msg import Bool
from std_msgs.msg import Float64

import time

def callback(data):

def _robot_stop():

def _robot_backup():

def _robot_turn90right():

def _robot_turn90left():

def _robot_forward():

def mynode():


    publisher_dict['right'] = rospy.Publisher('/robot/joint_right_wheel_controller/command', Float64, queue_size=1)
    publisher_dict['left'] = rospy.Publisher('/robot/joint_left_wheel_controller/command', Float64, queue_size=1)

    rospy.Subscriber("/robot/another_topic", Bool, callback)



publisher_dict = { } 

if __name__ == '__main__':
    except rospy.ROSInterruptException:

Once I run the node (when the simulation is running), I would expect the robot to move forward, because of the command _robot_forward() right before the rospy.spin(). And the command does appears at the topic when I listen to it via terminal. But the robot does not move. I'm confused. Why might that be?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2018-10-22 12:45:33 -0600

These connections take a little bit to be set up. I would try 2 things 1) set up publisher, then sleep for half a second or something, then subscribe and run 2) set up publisher, set up subscriber, sleep for half a second, then run

Assuming either of those makes it work, it should prompt you down the right road.

edit flag offensive delete link more


Thank you. I tried to put sleep before the robot command before, but I guess it slept for too little or it really makes difference that it sleeps in between commands setting the publisher and subscriber. This advice solved the issue.

kump gravatar image kump  ( 2018-10-23 02:57:29 -0600 )edit

Though I still wonder. How comes the message is being published to the topic (seen through rostopic echo /robot/joint_controller/command) but the robot doesn't react to it, if the controller manager is already running? What am I missing?

kump gravatar image kump  ( 2018-10-23 03:35:12 -0600 )edit

It takes a little bit for the ROS master to register the subs and have all the connections drawn. This is a major reason to create all your sub/pubs in a constructor before use so that they're all initialized.

stevemacenski gravatar image stevemacenski  ( 2018-10-23 13:37:21 -0600 )edit

Question Tools

1 follower


Asked: 2018-10-22 08:12:47 -0600

Seen: 696 times

Last updated: Oct 22 '18