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

Cannot subscribe to own topics (in rostest)

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

Westranger gravatar image

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

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()
        joint_state.name = ['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)
        pubA.publish(joint_state)

    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

Comments

1

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 -0600 )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 -0600 )edit

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

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

1 Answer

Sort by ยป oldest newest most voted
2

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

gvdhoorn gravatar image

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

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

Comments

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 -0600 )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 -0600 )edit

See rospy.Publisher for the API docs for that.

gvdhoorn gravatar image gvdhoorn  ( 2017-11-14 13:27:47 -0600 )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 -0600 )edit

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

Westranger gravatar image Westranger  ( 2017-11-15 02:23:11 -0600 )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 -0600 )edit

Question Tools

2 followers

Stats

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

Seen: 1,432 times

Last updated: Nov 14 '17