Ask Your Question
0

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):
    _robot_stop()
    _robot_backup()
    _robot_turn90right()
    _robot_forward()


def _robot_stop():
    publisher_dict['right'].publish(0.0)
    publisher_dict['left'].publish(0.0)

def _robot_backup():
    publisher_dict['right'].publish(-5.0)
    publisher_dict['left'].publish(-5.0)
    time.sleep(1)
    _robot_stop()

def _robot_turn90right():
    publisher_dict['right'].publish(-5.0)
    publisher_dict['left'].publish(5.0)
    time.sleep(1)
    _robot_stop()  

def _robot_turn90left():
    publisher_dict['right'].publish(5.0)
    publisher_dict['left'].publish(-5.0)
    time.sleep(1)
    _robot_stop()  

def _robot_forward():
    publisher_dict['right'].publish(5.0)
    publisher_dict['left'].publish(5.0)

def mynode():

    rospy.init_node('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)

    _robot_forward()

    rospy.spin()


publisher_dict = { } 

if __name__ == '__main__':
    try:
        mynode()
    except rospy.ROSInterruptException:
        pass

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
1

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

Comments

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 imagekump ( 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 imagekump ( 2018-10-23 03:35:12 -0600 )edit
1

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 imagestevemacenski ( 2018-10-23 13:37:21 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

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

Seen: 147 times

Last updated: Oct 22 '18