Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement times for ros2 topic pub #491

Merged
merged 7 commits into from
Apr 27, 2020
Merged

Implement times for ros2 topic pub #491

merged 7 commits into from
Apr 27, 2020

Conversation

clalancette
Copy link
Contributor

@clalancette clalancette commented Apr 17, 2020

Signed-off-by: Chris Lalancette clalancette@openrobotics.org

While doing some testing, it turned out to be useful to tell ros2 topic pub exactly how many times to publish a message. This is basically a generic version of the -1 flag that already exists, though that flag is left for backwards-compatibility.

I should point out that this is not the same as doing for i in seq 1 10 ; do ros2 topic pub ... ; done. The difference is that with the bash loop, a new node and publisher is created every time. This can be problematic for discovery, since it can happen that we bring up the node, publish, and destroy the node before connections are properly made. Doing it inside of ros2 topic pub itself neatly avoids this issue.

The one potentially flaky part of this is the test, which expects exactly 5 messages to be published. Depending on the platform and load, that may not be a reasonable assumption. Thoughts on how to improve that, while still testing that we are publishing a fixed number of times, are appreciated.

@mjcarroll
Copy link
Member

The one potentially flaky part of this is the test, which expects exactly 5 messages to be published. Depending on the platform and load, that may not be a reasonable assumption. Thoughts on how to improve that, while still testing that we are publishing a fixed number of times, are appreciated.

Would it be sufficient to check the console output like:

publishing #1: std_msgs.msg.String(data='hello')
publishing #2: std_msgs.msg.String(data='hello')
publishing #3: std_msgs.msg.String(data='hello')
publishing #4: std_msgs.msg.String(data='hello')
publishing #5: std_msgs.msg.String(data='hello')

While that doesn't guarantee that the messages have been published, it would be a bit more robust.

@clalancette
Copy link
Contributor Author

While that doesn't guarantee that the messages have been published, it would be a bit more robust.

Good idea! I'll go with that instead. Thanks.

@clalancette
Copy link
Contributor Author

clalancette commented Apr 17, 2020

CI:

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

Copy link
Member

@mjcarroll mjcarroll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with Happy CI.

@@ -115,6 +120,11 @@ def publisher(
if not isinstance(values_dictionary, dict):
return 'The passed value needs to be a dictionary in YAML format'

if once:
if times > 0:
return 'Cannot pass both -1 and -t <times>'
Copy link
Member

@dirk-thomas dirk-thomas Apr 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check should happen at parse_args time as part of a custom type function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up moving this check into main, alongside the other check for args.rate <= 0.

There is actually further work that could be done here to check for errors earlier. For instance, we could look up the message type/module and fill out the message all within main, before passing it down to the publish function. That would do less setup/teardown work in the case of an error. But I'll leave that for a future cleanup.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would still suggest to move both checks into custom type functions to determine valid input at argument parsing time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, this isn't possible. You can define a custom type callable in argparse, but that callable does not have access to the overall args structure. Even if it did, there is no way of knowing that you parsed all of the arguments by the time you got there. You can really only do this particular check at the end.

That being said, I did implement a custom type check for both rate and times that ensures that that they are positive float and non-negative integer, respectively.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was referring to both individual checks which have been moved into type functions 👍

If both arguments can't be passed at the same time the arguments should be added to an exclusive group.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call about the function names; fixed now. Also, I didn't know about argparse's mutually exclusive groups, thanks for that. I've now changed it to use them. I'll run another CI here, and this is ready for another review.

ros2topic/ros2topic/verb/pub.py Outdated Show resolved Hide resolved
@clalancette
Copy link
Contributor Author

clalancette commented Apr 20, 2020

New CI:

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
@clalancette
Copy link
Contributor Author

CI:

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

@clalancette
Copy link
Contributor Author

The macOS failure is a flake in topic_hz test, unrelated to this PR. So I think this is ready to go now.

@clalancette clalancette merged commit 718d30d into master Apr 27, 2020
@delete-merged-branch delete-merged-branch bot deleted the times branch April 27, 2020 17:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants