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

Add ROS2 bridge support for C++ nodes #425

Merged
merged 33 commits into from
Feb 28, 2024
Merged

Add ROS2 bridge support for C++ nodes #425

merged 33 commits into from
Feb 28, 2024

Conversation

phil-opp
Copy link
Collaborator

@phil-opp phil-opp commented Feb 14, 2024

Build and Usage Instructions

Usage Example:

    auto dora_node = init_dora_node();
    auto merged_events = dora_events_into_combined(std::move(dora_node.events));

    auto qos = qos_default();
    qos.durability = Ros2Durability::Volatile;
    qos.liveliness = Ros2Liveliness::Automatic;
    qos.reliable = true;
    qos.max_blocking_time = 0.1;

    auto ros2_context = init_ros2_context();
    auto node = ros2_context->new_node("/ros2_demo", "turtle_teleop");
    auto vel_topic = node->create_topic_geometry_msgs_Twist("/turtle1", "cmd_vel", qos);
    auto vel_publisher = node->create_publisher(vel_topic, qos);
    auto pose_topic = node->create_topic_turtlesim_Pose("/turtle1", "pose", qos);
    auto pose_subscription = node->create_subscription(pose_topic, qos, merged_events);

    auto event = merged_events.next();

    if (event.is_dora())
    {
        auto dora_event = downcast_dora(std::move(event)); 
        ...
        geometry_msgs::Twist twist = {
                    .linear = {.x = 1, .y = 0, .z = 0},
                    .angular = {.x = 0, .y = 0, .z = 1}
        };
        vel_publisher->publish(twist);
    }
    else if (pose_subscription->matches(event))
    {
        auto pose = pose_subscription->downcast(std::move(event));
        std::cout << "Received pose x:" << pose.x << ", y:" << pose.y << std::endl;
    }

Also part of this PR:

  • Update Rust to 1.76
  • Small fix for C operator API because Option<String> is not FFI-safe.

The `cxx_build` crate does not support proc macro output (see dtolnay/cxx#808), so we have to generate a standard Rust source file instead.
…ging

Stream merging based on the cxx crate requires the bridge and the api to share an `ExternalEvents` type, which is only possible if the two bridge modules are defined in the same crate.
Use the subscriber type for downcasting to ensure that the correct type is used. Also, store an unique ID per subscriber to differentiate subscriptions of same type after merging.
@phil-opp phil-opp force-pushed the c++-ros2-bridge branch 2 times, most recently from 02b6141 to 18044f6 Compare February 14, 2024 14:42
@phil-opp phil-opp marked this pull request as ready for review February 14, 2024 14:51
@phil-opp
Copy link
Collaborator Author

I'm still working on fixing the build on the Windows CI runner, which uses a non-standard target dir. Otherwise, this should be ready for an initial round of testing. I tried a simple example with the turtlesim and both publish and subscribe seem to work.

@phil-opp
Copy link
Collaborator Author

I'm still working on fixing the build on the Windows CI runner, which uses a non-standard target dir.

This should be fixed with 3b1777c.

@phil-opp
Copy link
Collaborator Author

For example, the generated struct for the Twist message looks like this:

struct Twist final {
  ::geometry_msgs::Vector3 linear;
  ::geometry_msgs::Vector3 angular;

  bool operator==(Twist const &) const noexcept;
  bool operator!=(Twist const &) const noexcept;
  using IsRelocatable = ::std::true_type;
};

@phil-opp
Copy link
Collaborator Author

I added some usage instructions in 70fb49b. I think this PR is ready for another round of review.

There are still multiple things that we want to improve, but I think it's better to do this in follow-up PRs.

Copy link
Collaborator

@haixuanTao haixuanTao left a comment

Choose a reason for hiding this comment

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

Looks great! Thanks!

apis/c++/node/README.md Outdated Show resolved Hide resolved
@haixuanTao haixuanTao merged commit edb2a28 into main Feb 28, 2024
17 checks passed
@haixuanTao haixuanTao deleted the c++-ros2-bridge branch February 28, 2024 08:54
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.

2 participants