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

Cannot subscribe to own topics (in rostest)

asked 2017-11-14 10:53:49 -0500

Westranger gravatar image

updated 2017-11-14 13:02:42 -0500

I'm still writing integration test and I'm stuct at asimple exmaple where I want my testcase to publish a message and also subscrib to the same topic

class AdvancedTestSuite(unittest.TestCase):
    def test_joint_states(self):
        rospy.init_node('test_vehicle_controller', anonymous=True)

        joint_state = JointState() = ['lift_actor', 'wheel_main_rotation', 'wheel_main_steer']
        joint_state.velocity = (0.02069770811665661, 0.0014280281623658617, 1.1028179833702783e-05)
        joint_state.position = (0.9990335843981955, -0.12971652493225605, -0.0031193465124985664)
        joint_state.effort = (37.26698108917237, 0.0, 0.0)

        rospy.Subscriber('/my_ns/joint_states', JointState, self.callback_state)
        pubA = rospy.Publisher('/my_ns/joint_states', JointState, queue_size=10)

    def callback_state(self, state):
        rospy.loginfo("received " + repr(state))

if __name__ == '__main__':
    import rostest
    rostest.rosrun(PKG, 'test_joint_states', AdvancedTestSuite)

but nothing happens the callback is called. I can not even debug what wrong. Any Ideas ?

edit retag flag offensive close merge delete



And just a note: rostest is not subscribing to anything. rostest is just the test driver here in this case. Your code is subscribing.

Suggestion: change the question title to reflect this better.

gvdhoorn gravatar image gvdhoorn  ( 2017-11-14 11:06:32 -0500 )edit

Thank you for your comment. This shows again that I dont fully understand internal ROS concepts (yet).

Westranger gravatar image Westranger  ( 2017-11-14 13:07:11 -0500 )edit

No problem. We're all posting here to learn.

gvdhoorn gravatar image gvdhoorn  ( 2017-11-14 13:07:49 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2017-11-14 11:04:16 -0500

gvdhoorn gravatar image

updated 2017-11-14 11:21:28 -0500

It takes some time for subscriptions to be setup (this is a networked middleware, not a local function call). There is probably on the order of a few milliseconds between the creation if your publisher and your subscriber, you then publish(..) and your program exits immediately. Try to add a time.sleep(..) at appropriate places to see whether that is the problem.

edit flag offensive delete link more


I already added a time.sleep(...) without a whole loop after the publish(...) without anything luck. Do I also need to wait before the publish ? It would also bei nice to somehow debug the situation. But rostopic for example complaints thats the ros master is not reachable

Westranger gravatar image Westranger  ( 2017-11-14 13:22:26 -0500 )edit

Do I also need to wait before the publish ?

that is probably the most important place to sleep.

If you're not a fan of doing this time-based, and would rather do it state-based (I would), then you could use pubA.get_num_connections() to wait for a connection before publishing.

gvdhoorn gravatar image gvdhoorn  ( 2017-11-14 13:25:28 -0500 )edit

See rospy.Publisher for the API docs for that.

gvdhoorn gravatar image gvdhoorn  ( 2017-11-14 13:27:47 -0500 )edit

It would also bei nice to somehow debug the situation.

well this is Python, and your test is basically a Python script, so pdb could perhaps help here.

But as there is a network involved, and most things are asynchronous, debugging could prove difficult.

gvdhoorn gravatar image gvdhoorn  ( 2017-11-14 13:36:57 -0500 )edit

@gvdhoorn I solved it with a time.sleep(...) before the publish, thanks

Westranger gravatar image Westranger  ( 2017-11-15 02:23:11 -0500 )edit

Suggestion: use a state-based approach. If for whatever reason the publisher and / or subscriber need more time to initialise (perhaps due to resource contention with other processes on the machine running your tests), you run the risk of the test failing for no good reason.

gvdhoorn gravatar image gvdhoorn  ( 2017-11-15 03:12:48 -0500 )edit

Question Tools



Asked: 2017-11-14 10:53:49 -0500

Seen: 1,403 times

Last updated: Nov 14 '17