5 from roscompile.util import convert_to_caps_notation, convert_to_underscore_notation
7 ROS2_INCLUDE_PATTERN =
'#include <%s/%s/%s.hpp>' 10 'ROS_DEBUG':
'RCLCPP_DEBUG',
11 'ROS_INFO':
'RCLCPP_INFO',
12 'ROS_ERROR':
'RCLCPP_ERROR',
13 'ROS_WARN':
'RCLCPP_WARN' 18 return r'#include\s*[<\\"]' + s +
'[>\\"]' 22 r'( *)ros::init\(argc, argv, "([^"]*)"\);(\s+)ros::NodeHandle (\w+);':
23 '$0rclcpp::init(argc, argv);$2auto $3 = rclcpp::Node::make_shared("$1");' 26 CPP_CODE_REPLACEMENTS = {
28 'ros::Time':
'rclcpp::Time',
29 'std_msgs::Time':
'builtin_interfaces::msg::Time',
30 'std_msgs/time.h':
'builtin_interfaces/msg/time.hpp',
31 'ros::Rate':
'rclcpp::Rate',
32 'ros::Duration':
'rclcpp::Duration',
33 r'ros::ok\(\)':
'rclcpp::ok()',
34 r'( *)ros::init\(argc, argv, "([^"]*)"\);':
35 '$0rclcpp::init(argc, argv);\n$0auto node = rclcpp::Node::make_shared("$1");',
36 r'ros::spinOnce\(\);':
'rclcpp::spin_some(node);',
37 r'ros::spin\(\);':
'rclcpp::spin(node);',
38 'ros::NodeHandle':
'rclcpp::Node',
39 'ros::Publisher (.*) = (.*)advertise(.*);':
'auto $0 = $1advertise$2;',
40 'ros::Subscriber (.*) = (.*)subscribe(.*);':
'auto $0 = $1subscribe$2;',
44 'boost::shared_ptr':
'std::shared_ptr',
46 'boost::weak_ptr':
'std::weak_ptr',
48 'boost::mutex':
'std::mutex',
49 'boost::mutex::scoped_lock':
'std::unique_lock<std::mutex>',
51 'boost::unordered_map':
'std::unordered_map',
53 'boost::function':
'std::function',
57 '#include "tf2_ros/buffer.h"\n#include "tf2_ros/transform_listener.h"',
58 'tf::TransformListener':
'tf2_ros::TransformListener',
59 'tf::Stamped':
'tf2::Stamped',
60 'tf::Pose':
'tf2::Pose',
61 'tf::get':
'tf2::get',
67 for gen_type, full_list
in [(
'msg', MESSAGES), (
'srv', SERVICES)]:
68 for pkg, gen_name
in full_list:
69 gen_pattern = re.compile(pkg +
'.*' + gen_name)
70 if package.source_code.search_for_pattern(gen_pattern):
71 messages.add((pkg, gen_name, gen_type))
76 service_replacements = {}
77 generator_replacements = {}
80 value = ROS2_INCLUDE_PATTERN % (pkg, gen_type, convert_to_underscore_notation(msg))
81 generator_replacements[key] = value
83 two_colons =
'%s::%s' % (pkg, msg)
84 four_colons =
'%s::%s::%s' % (pkg, gen_type, msg)
85 generator_replacements[two_colons] = four_colons
87 generator_replacements[
'(' + msg +
')ConstPtr'] =
'$0::ConstSharedPtr' 88 generator_replacements[
'(' + msg +
')::ConstPtr'] =
'$0::ConstSharedPtr' 91 key =
r'bool ([^\(]+)\(\s*' + two_colons +
r'::Request\s+&\s+([^,]+),\s+' 92 key += two_colons +
r'::Response\s+&\s+([^\)]+)\)' 93 value =
'void $1(const std::shared_ptr<' + four_colons +
'::Request> $2, ' 94 value +=
'std::shared_ptr<' + four_colons +
'::Response> $3)' 95 service_replacements[key] = value
96 return generator_replacements, service_replacements
100 LOGGER_REPLACEMENTS = {}
101 PackageName = convert_to_caps_notation(package.name)
102 for old_logger, new_logger
in LOGGERS.items():
103 LOGGER_REPLACEMENTS[old_logger +
r'\('] = new_logger +
'(rclcpp::get_logger("' + PackageName +
'"), ' 105 LOGGER_REPLACEMENTS[old_logger +
r'_NAMED\(([^,]+),'] = new_logger +
'(rclcpp::get_logger($0),' 106 return LOGGER_REPLACEMENTS
111 package.source_code.modify_with_patterns(service_replacements)
112 package.source_code.modify_with_patterns(generator_replacements)
114 package.source_code.modify_with_patterns(FIRST_PASS)
115 package.source_code.modify_with_patterns(CPP_CODE_REPLACEMENTS)