0
$\begingroup$

Rosanswers logo

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?


Originally posted by kump on ROS Answers with karma: 308 on 2018-10-22

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by stevemacenski with karma: 8272 on 2018-10-22

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by kump on 2018-10-23:
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.

Comment by kump on 2018-10-23:
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?

Comment by stevemacenski on 2018-10-23:
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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.