-
Notifications
You must be signed in to change notification settings - Fork 18
Writing ROS Version Independent Code
Motivation
Overview
Examples
Getting Help
Limitations
Additional Resources
- Multiple groups use this repository over different operating systems (Linux, Windows) and ROS versions (ROS1, ROS2).
- We currently maintain both ROS1 and ROS2 branches to maximize compatibility for all users.
- It is desired to share as much common code as possible between ROS1 and ROS2 branches such that syncing changes between branches can be done easily.
- We have moved all ROS version specific code to their own "wrapper" or "proxy" classes.
- These "wrapper" or "proxy" classes are internally ROS version specific (different in ROS1 and ROS2 branches) but need only be defined once and have external interfaces that are ROS version independent.
- All node or algorithm code which previously directly referenced ROS version specific headers should now instead reference these proxy classes with ROS version independent interfaces.
- In this way, all algorithm and node code will not directly depend on a specific version of ROS and should be directly portable between ROS1 and ROS2 branches.
Before ROS Independence Changes
Node and algorithm code directly reference ROS version specific interfaces, making them ROS version dependent. Porting between ROS1 and ROS2 branches is difficult.
After ROS Independence Changes
Node and algorithm code reference "wrapper" or "proxy" classes which have ROS version independent interfaces, thus making them ROS independent.
Summary: If your code directly references any ROS specific headers then it is not ROS version independent. Reference the "wrapper" or "proxy" headers instead.
- Below are some examples of how ROS1 or ROS2 version specific code is changes to the ROS version independent format.
- There are also numerous examples in the current repository of ROS version independent code which can be used as a reference.
❌ ROS 1 Version (Don't Use)
#include "geometry_msgs/Twist.h"
void function(geometry_msgs::Twist msg){
...
}❌ ROS 2 Version (Don't Use)
#include "geometry_msgs/msg/twist.hpp"
void function(geometry_msgs::msg::Twist msg){
...
}✔️ ROS Version Independent Syntax
Reference avt_341/node/ros_types.h and all message types have avt_341::msg namespace
#include "avt_341/node/ros_types.h"
void function(avt_341::msg::Twist msg){
...
}❌ ROS 1 Version (Don't Use)
#include "ros/ros.h"
#include "std_msgs/Int32.h"
void onSubscriptionMsg(const std_msgs::Int32::ConstPtr & msg){
...
}
int main(int argc, char** argv) {
ros::init(argc, argv, "node_name");
ros::NodeHandle n;
auto sub = n.subscribe("topic1", 1, onSubscriptionMsg);
auto pub = n.advertise<std_msgs::Int32>("topic2", 1);
ros::Rate rate(10.0f);
…
while (ros::ok()) {
pub->publish(msg);
rate.sleep();
ros::spinOnce();
}
}
❌ ROS 2 Version (Don't Use)
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/int32.hpp"
void onSubscriptionMsg(const std_msgs::Int32::SharedPtr msg){
...
}
int main(int argc, char** argv) {
rclcpp::init(argc, argv);
auto n = rclcpp::Node::make_shared("node_name");
auto s = n->create_subscription<std_msgs::msg::Int32>("topic1", 1, onSubscriptionMsg);
auto p = n->create_publisher<std_msgs::msg::Int32>("topic2", 1);
rclcpp::Rate rate(10.0f);
…
while (rclcpp::ok()) {
p->publish(msg);
rate.sleep();
rclcpp::spin_some(n)
}
}
✔️ ROS Version Independent Syntax
Use ROS version independent interfaces from node proxy in avt_341/node/node_proxy.h
#include "avt_341/node/ros_types.h"
#include "avt_341/node/node_proxy.h"
void onSubscriptionMsg(avt_341::msg::Int32Ptr msg){
...
}
int main(int argc, char** argv) {
auto n = avt_341::node::init_node(argc, argv, "node_name");
auto s = n->create_subscription<avt_341::msg::Int32>("topic2", 1, onSubscriptionMsg);
auto p = n->create_publisher<avt_341::msg::Int32>("topic2", 1);
avt_341::node::Rate rate(10.0f);
…
while (avt_341::node::ok()) {
p->publish(msg);
rate.sleep();
n->spin_some();
}
}
If you find that commits to the repository cannot be incorporated with the current ROS independent interfaces, please file a new issue.
Additional ROS1 and ROS2 specific node features such as nodelets or classes which inherit from rclcpp::Node are not currently supported when writing ROS version independent code in this repository. ROS version independent interfaces only currently support the legacy, inline way of creating nodes in ROS2.