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

[py] Make it easier to pass C++ node pointers in Python #285

Merged

Conversation

EricCousineau-TRI
Copy link
Collaborator

@EricCousineau-TRI EricCousineau-TRI commented Jun 14, 2023

I would like to not get too creative when trying to bind C++ code that has rclcpp::Node arguments. This is one approach.
Relates ros2/rclpy#291


This change is Reviewable

Copy link
Collaborator Author

@EricCousineau-TRI EricCousineau-TRI left a comment

Choose a reason for hiding this comment

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

+@sloretz Might you have time to review this?

\cc @dmcconachie-tri @calderpg-tri @IanTheEngineer

Reviewable status: 0 of 6 files reviewed, all discussions resolved (waiting on @sloretz)

Copy link
Collaborator Author

@EricCousineau-TRI EricCousineau-TRI left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 6 files reviewed, 1 unresolved discussion (waiting on @EricCousineau-TRI and @sloretz)


drake_ros/drake_ros/core/cc_pybind.cc line 143 at r1 (raw file):

  py::class_<rclcpp::Node, std::shared_ptr<rclcpp::Node>>(m, "CppNode",
                                                          py::module_local())

Working: This seems to cause a segfault in Anzu. Removing py::module_local() makes things a bit happier.

Copy link
Collaborator Author

@EricCousineau-TRI EricCousineau-TRI left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 6 files reviewed, 1 unresolved discussion (waiting on @sloretz)

a discussion (no related file):
There seems to be a new CI failure, most likely unrelated to this PR:
https://github.com/RobotLocomotion/drake-ros/actions/runs/5272233626/jobs/9534218327?pr=285#step:7:345

  CMake Error at drake_ros/CMakeLists.txt:7 (target_link_libraries):
    Error evaluating generator expression:
  
      $<TARGET_PROPERTY:fastcdr,TYPE>
  
    Target "fastcdr" not found.

@sloretz Is this something we need to work around, or is there something else going awry here?



drake_ros/drake_ros/core/cc_pybind.cc line 143 at r1 (raw file):

Previously, EricCousineau-TRI (Eric Cousineau) wrote…

Working: This seems to cause a segfault in Anzu. Removing py::module_local() makes things a bit happier.

Done. For now, making this global; can take a step back if/when it interferes with another library's bindings.

Copy link
Collaborator

@adityapande-1995 adityapande-1995 left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 6 files reviewed, 2 unresolved discussions (waiting on @EricCousineau-TRI and @sloretz)


drake_ros/drake_ros/test/core_test.py line 122 at r2 (raw file):

    assert not cpp_node_options.use_global_arguments
    node_cpp = CppNode("direct_node", node_options=cpp_node_options)
    assert node_cpp.get_name() == "direct_node"
  1. If we are planning to expose existing rclcpp nodes to python, what APIs are we planning to connect ? i.e. can we add subscribers, publishers, callbacks, etc ?
    It'd be nice to add tests for and document the features that we'll be exposing as a docstring, and also the limitations of this binding.

  2. I'd be great if we can add a test that shows that this node can actually spin and receive / send msgs, and play alongside existing "vanilla" rclpy nodes. I'm curious to see how rclpy.spin() and rclcpp::spin() interact.

Copy link
Collaborator

@adityapande-1995 adityapande-1995 left a comment

Choose a reason for hiding this comment

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

If we're planning to bind subscribers or publishers, this automatically requires a generic typecaster for data types : #257

Reviewable status: 0 of 6 files reviewed, 2 unresolved discussions (waiting on @EricCousineau-TRI and @sloretz)

Copy link
Collaborator Author

@EricCousineau-TRI EricCousineau-TRI left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 6 files reviewed, 2 unresolved discussions (waiting on @sloretz)


drake_ros/drake_ros/test/core_test.py line 122 at r2 (raw file):

Previously, adityapande-1995 (Aditya Pande) wrote…
  1. If we are planning to expose existing rclcpp nodes to python, what APIs are we planning to connect ? i.e. can we add subscribers, publishers, callbacks, etc ?
    It'd be nice to add tests for and document the features that we'll be exposing as a docstring, and also the limitations of this binding.

  2. I'd be great if we can add a test that shows that this node can actually spin and receive / send msgs, and play alongside existing "vanilla" rclpy nodes. I'm curious to see how rclpy.spin() and rclcpp::spin() interact.

Working: Per Slack DM, the intent of this is to only bind the minimum amount of the C++ Node API in Python so we don't need too much creativity in writing downstream bindings.
I'm going to hold off on binding additional facets, but I will add a quick test.

(I think functionality that consumes DrakeRos effectively shows it, but yes, it'd be nice to just test directly.)

Useful when writing Python bindings against C++ code

drake_ros: Switch to shared_ptr to eleverage enable_shared_from_this
core: Ensure we import DrakeRos; add CppNode and CppNodeOptions
Copy link
Collaborator Author

@EricCousineau-TRI EricCousineau-TRI left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 7 files reviewed, 2 unresolved discussions (waiting on @adityapande-1995 and @sloretz)


drake_ros/drake_ros/test/core_test.py line 122 at r2 (raw file):

Previously, EricCousineau-TRI (Eric Cousineau) wrote…

Working: Per Slack DM, the intent of this is to only bind the minimum amount of the C++ Node API in Python so we don't need too much creativity in writing downstream bindings.
I'm going to hold off on binding additional facets, but I will add a quick test.

(I think functionality that consumes DrakeRos effectively shows it, but yes, it'd be nice to just test directly.)

Done.

Copy link
Collaborator

@adityapande-1995 adityapande-1995 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 the linter error resolved !

Reviewable status: 0 of 7 files reviewed, 2 unresolved discussions (waiting on @sloretz)

@EricCousineau-TRI
Copy link
Collaborator Author

Done, I think. I had an odd failure of CMake-based ROS failing, but will see if CI shows the same issue.

@EricCousineau-TRI
Copy link
Collaborator Author

EricCousineau-TRI commented Jun 21, 2023

@adityapande-1995 CI seems to have failed.

Can I ask if you can reproduce this (Bazel passes, CMake fails) and, if so, what might be the problem?

# Bazel
cd drake-ros
cd drake_ros
bazel run //drake_ros:core_test
# Passes

# CMake
cd ros_ws
colcon build --packages-up-to drake_ros --symlink-install
# "Shortcut" - not sure how to run single test
source install/setup.bash
( cd build/drake_ros && ctest -V -R core_test_py; )
# Fails

Copy link
Collaborator Author

@EricCousineau-TRI EricCousineau-TRI left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 8 files reviewed, 1 unresolved discussion (waiting on @adityapande-1995 and @sloretz)

a discussion (no related file):

Previously, EricCousineau-TRI (Eric Cousineau) wrote…

There seems to be a new CI failure, most likely unrelated to this PR:
https://github.com/RobotLocomotion/drake-ros/actions/runs/5272233626/jobs/9534218327?pr=285#step:7:345

  CMake Error at drake_ros/CMakeLists.txt:7 (target_link_libraries):
    Error evaluating generator expression:
  
      $<TARGET_PROPERTY:fastcdr,TYPE>
  
    Target "fastcdr" not found.

@sloretz Is this something we need to work around, or is there something else going awry here?

Done (I think) - I did not see this error once I rebased onto latest main


Copy link
Collaborator Author

@EricCousineau-TRI EricCousineau-TRI left a comment

Choose a reason for hiding this comment

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

This appears to be an RMW implementation issue. I've addressed it with a workaround, will file an issue.

Reviewable status: 0 of 9 files reviewed, 1 unresolved discussion (waiting on @adityapande-1995 and @sloretz)

@EricCousineau-TRI
Copy link
Collaborator Author

Core bazel testing of drake_ros is done. I'm going to go ahead and skip drake_ros_examples because our caching is still broken.

@EricCousineau-TRI EricCousineau-TRI merged commit 4ca38a8 into RobotLocomotion:main Jun 27, 2023
tervay-bdai added a commit to tervay-bdai/drake-ros that referenced this pull request Jul 10, 2023
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.

3 participants