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

ros2 - Adaptaions to Eloquent #493

Closed
wants to merge 6 commits into from

Conversation

mmatthe
Copy link
Contributor

@mmatthe mmatthe commented Apr 28, 2020

During usage of the ROS2 branch of the rosbridge_suite, several problems occured in the Python Code. These were fixed such that it works for our use case. The overview of the changes:

allow an increased subscribing frequency of 1000Hz (it was 10Hz, meaning only 10 ROS2 messages were received per second)
Speed up receiving CompressedImage from the websocket by creating a shortcut implementation with some optimizations
allow both nsecs and nanosec fields for time (ROS1/2 compatiblity).
allow received int values to be interpreted as float values, when needed.

- adapt rosbridge_suite to ROS2 Eloquent
- increase spin period to 1000Hz to allow 1000 messages per second into the websocket
- increase parsing speed of sensor_msgs/CompressedImage
- add some debug messages
- allow interpreting int as float when needed
- better handling array.array and numpy arrays
- allow bytes and str websocket messages
- adapt rosbridge_suite to ROS2 Eloquent
- increase spin period to 1000Hz to allow 1000 messages per second into the websocket
- increase parsing speed of sensor_msgs/CompressedImage
- add some debug messages
- allow interpreting int as float when needed
- better handling array.array and numpy arrays
- allow bytes and str websocket messages

Contributors: @joshwapohlmann @mmatthe
@mvollrath
Copy link
Contributor

I'm happy to merge this with a review by a ROS2 user.

@flynneva
Copy link
Contributor

flynneva commented May 13, 2020

Tested and confirmed on Ubuntu 18.04 with ROS2 Eloquent.
Compiled, no issues.

compiled

NOTE: to run, I had to pip install pymongo before using rosbridge_websocket.

tested with webviz.io resulted in a service call error. I think the service that webviz was looking for just was missing.

tested on a less-complex website and it worked great.

pymongo_dep

@flynneva
Copy link
Contributor

just tested it with dashing too...works great

@mvollrath
Copy link
Contributor

Please try removing the pip-installed pymongo package and test with apt package python3-bson, because this is what will be installed for BSON library.

It looks like the webviz error would go away if the rosapi node (part of rosbridge) were running.

@flynneva
Copy link
Contributor

followed your recommendation - uninstalled the pip pymongo and installed the apt package python3-bson.

same as before, compiled and worked great.

I also tried to launch the rosapi node in another terminal, however it looks like the service /rosapi/topics_and_raw_types that webviz is looking for isnt available. it probably wasn't carried over to the ros2 version of rosapi yet.

rosapi_service_list

@mvollrath
Copy link
Contributor

Is there something new in ROS2 where having a requirements.txt is either supported or desired?


def populate_instance(msg, inst):
""" Returns an instance of the provided class, with its fields populated
according to the values in msg """
inst_type = msg_instance_type_repr(inst)
if inst_type == "sensor_msgs/CompressedImage":
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not convinced yet that hardcoding special handling for specific message types in this module in this way is a good idea.

If the data is coming in as anything but a base64 string, it shouldn't be coming anywhere near this code path. I don't see any place where base64 decoding was added, so I don't understand how this decoder could possibly work. Can you explain how this works?

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 agree that this is kind of a hacky solution to shortcut decoding of CompressedImage. At this point in the data flow, msg is a dict containing all the fields. In particular, arrays/lists have already been decoded into python/numpy arrays. From this point in the data flow on, the only remaining thing is to translate the fields from the dict into the attributes of inst (inst is already the target object of the correct type). in our debugging sessions it happened that decoding of CompressedImages was extremely slow. This is due to an known problem within the python-idl mapping. Particular, inst.data = msg["data"] would be the slow thing. Instead, it should be inst.data = array.array("B", msg["data"]) which omits the runtime check if all values are within bounds.

Since the generic decoding within populate_instance can only do inst.data = msg["data"], I have included the shortcut here.

Please let me know, if I should delete it in this fork or keep it.

Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like this could be made into a general solution by moving it into _to_inst with the rest of the decoding. Otherwise I would accept this hack in this PR if there is a comment linking to your comment explaining why it exists.

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 have created an extra file that collects shortcut decoders. Currently, it only contains a decoder for sensor_msgs/CompressedImage. I needed that file to keep the message_conversion.py clean. I have moved the call to these function into the _to_object_inst

Please see, if this solution is fine with you.

requirements.txt Outdated Show resolved Hide resolved
rosbridge_library/src/rosbridge_library/protocol.py Outdated Show resolved Hide resolved
rosbridge_library/src/rosbridge_library/protocol.py Outdated Show resolved Hide resolved
mmatthe and others added 3 commits May 14, 2020 16:17
- remove extraneous print statements
- remove unused packages
- remove whitespace
- move message conversion to str into websocket_handler
Adding a dict `shortcut_object_decoders` that contains functions
that handles decoding of specific objects quicker than the generic method.

remove requirements.txt

Co-authored-by: joshwapohlmann <joshwa.pohlmann@barkhauseninstitut.org>
Co-Authored-by: joshwapohlmann <joshwa.pohlmann@barkhauseninstitut.org>
@flynneva
Copy link
Contributor

just tested this for foxy on ubuntu 20.04 - works there too!

@maxlein
Copy link

maxlein commented May 20, 2020

I am confused on which ros2 bridge I shall use now!?
This one on ros2 branch or ros2-web-bridge?
Seems both are missing topics_and_raw_types service for usage with webviz.
Am I missing something? Are they dependent on each other somehow?

@flynneva
Copy link
Contributor

flynneva commented May 20, 2020

either one! this ones based on python and the ros2-web-bridge one is based on js. they provide similar functionality

the maintainer provides an answer to this question here RobotWebTools/ros2-web-bridge#117

@mvollrath
Copy link
Contributor

Seems both are missing topics_and_raw_types service for usage with webviz.

IIRC this service is provided by rosapi which is a separate script under rosbridge_suite. I don't know if rosapi has been ported for ROS2 yet.

# explicitely cast to array.array to overcome a costly runtime
# check within inst.data instance.
# See also https://github.com/RobotWebTools/rosbridge_suite/pull/493#discussion_r425151919
inst.data = array.array("B", msg["data"])
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it not true that if you did this for all uint8[] in message_conversion.py _to_binary_inst you wouldn't need special cases for specific message types?

Copy link
Contributor Author

@mmatthe mmatthe May 24, 2020

Choose a reason for hiding this comment

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

Well, you can, in the deeply buried code where the instance is set (it'd be in _to_object_inst and _to_list_inst), capture if a uint8[] is set and then apply the optimization. However, this is not very clear to understand and it might be too general, as sometimes you might want to have the additional error check that is done by ROS2 idl_typesupport. Therefore, I would not recommend using this optimization generally.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see how maintaining unique decoder logic for specific message types is better. Everything about rosbridge is general and should remain general. If the usual decoding pathway is too slow for CompressedImage it's probably too slow for every other message that uses uint8[]. If the additional error check can be bypassed for CompressedImage it can probably be bypassed for every other message type.

If there is an open issue about ROS2 message conversion performance somewhere else please link.

@travipross
Copy link
Contributor

Also compiled and tested on Ubuntu 18.04.3 with ROS Eloquent. Seems to work 👍

@AviKenz
Copy link

AviKenz commented Aug 6, 2020

Tested and confirmed on Ubuntu 18.04 with ROS2 Eloquent.
Compiled, no issues.

compiled

NOTE: to run, I had to pip install pymongo before using rosbridge_websocket.

tested with webviz.io resulted in a service call error. I think the service that webviz was looking for just was missing.

tested on a less-complex website and it worked great.

pymongo_dep

Hello !
i am getting similar output and i CANT publish anything from any Client library.
i already tried roslibjs and jrosbridge. same errors...

Here is the issue i opened: rctoris/jrosbridge#29

I need help pleaaaaase....

im running ros-dashing.

Thanks in advance !

@AviKenz
Copy link

AviKenz commented Aug 6, 2020

just tested it with dashing too...works great

im using dashing too. but nothing works.

my issue: rctoris/jrosbridge#29

need help !

@flynneva
Copy link
Contributor

@mmatthe if you make one more small commit on your PR branch it should trigger the new ROS2 CI for this PR. might be helpful in diagnosing if some of these fixes help solve some of the compilation issues.

@travipross
Copy link
Contributor

@mmatthe I've opened an additional PR in your fork for a simple addition to this PR regarding boolean types.

@mvollrath Is the only outstanding issue holding up the approval of this PR related to the addition of the shortcut decoders in rosbridge_library/src/rosbridge_library/internal/object_decoders.py?

If so, is there any way we can move that portion to a separate PR for further discussion? There are some other fixes in this branch that address some currently broken functionality in the base branch, and it'd be nice to see this merged.

@mvollrath
Copy link
Contributor

@mvollrath Is the only outstanding issue holding up the approval of this PR related to the addition of the shortcut decoders in rosbridge_library/src/rosbridge_library/internal/object_decoders.py?

Yes.

@travipross
Copy link
Contributor

Is there any way we can just comment/remove the two offending lines relating to checking for / returning a shortcut decoder, and potentially discuss their reintroduction in a different PR if desired?

It'd be nice to see the majority of this moved into the ros2 branch, as in its current state, many features are still broken and this contains some fixes.

I don't want to clutter things and open a new PR containing most of these same changes with this PR still open, but it seems @mmatthe might not be actively monitoring this PR right now.

@AviKenz
Copy link

AviKenz commented Sep 18, 2020

@mvollrath its seems that the change request was already proceeded. Please check object_decoders.py.
let me know if its enough or not. i will proceed the changes on my PR :)

@travipross travipross mentioned this pull request Sep 19, 2020
@travipross
Copy link
Contributor

travipross commented Sep 19, 2020

@mvollrath I've removed the offending shortcut_object_decoders in #533 as well as made a number of other small, but missing adaptations. I suggest we close this PR as @mmatthe seems to have abandoned it for now.

@travipross
Copy link
Contributor

@mvollrath It looks as though this has sat abandoned by the original author for almost a year and can be closed thanks to #533

@jihoonl jihoonl closed this Jun 14, 2021
@kenji-miyake kenji-miyake mentioned this pull request Feb 12, 2022
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

7 participants