Skip to content

Commit 02dcbf3

Browse files
authored
Merge pull request #21 from hoffmann-stefan/feature/bridge-log
Bridge /rosout
2 parents 52c3a7c + e1c358c commit 02dcbf3

File tree

6 files changed

+167
-4
lines changed

6 files changed

+167
-4
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ set(actionlib_msgs_DEPRECATED_QUIET TRUE)
105105
foreach(package_name ${ros2_interface_packages})
106106
find_package(${package_name} QUIET REQUIRED)
107107
message(STATUS "Found ${package_name}: ${${package_name}_VERSION} (${${package_name}_DIR})")
108-
if(NOT "${package_name}" STREQUAL "builtin_interfaces")
108+
if(NOT "${package_name}" STREQUAL "builtin_interfaces" AND
109+
NOT "${package_name}" STREQUAL "rcl_interfaces")
109110
list(APPEND generated_files "${generated_path}/${package_name}_factories.cpp")
110111
list(APPEND generated_files "${generated_path}/${package_name}_factories.hpp")
111112
foreach(interface_file ${${package_name}_IDL_FILES})

include/ros1_bridge/builtin_interfaces_factories.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
#define ROS1_BRIDGE__BUILTIN_INTERFACES_FACTORIES_HPP_
1717

1818
// include ROS 1 messages
19+
#include <rosgraph_msgs/Log.h>
1920
#include <std_msgs/Duration.h>
2021
#include <std_msgs/Time.h>
2122

2223
// include ROS 2 messages
2324
#include <builtin_interfaces/msg/duration.hpp>
2425
#include <builtin_interfaces/msg/time.hpp>
26+
#include <rcl_interfaces/msg/log.hpp>
2527

2628
#include <memory>
2729
#include <string>
@@ -74,6 +76,24 @@ Factory<
7476
const builtin_interfaces::msg::Time & ros2_msg,
7577
std_msgs::Time & ros1_msg);
7678

79+
template<>
80+
void
81+
Factory<
82+
rosgraph_msgs::Log,
83+
rcl_interfaces::msg::Log
84+
>::convert_1_to_2(
85+
const rosgraph_msgs::Log & ros1_msg,
86+
rcl_interfaces::msg::Log & ros2_msg);
87+
88+
template<>
89+
void
90+
Factory<
91+
rosgraph_msgs::Log,
92+
rcl_interfaces::msg::Log
93+
>::convert_2_to_1(
94+
const rcl_interfaces::msg::Log & ros2_msg,
95+
rosgraph_msgs::Log & ros1_msg);
96+
7797
} // namespace ros1_bridge
7898

7999
#endif // ROS1_BRIDGE__BUILTIN_INTERFACES_FACTORIES_HPP_

include/ros1_bridge/convert_builtin_interfaces.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
#include "ros1_bridge/convert_decl.hpp"
1919

2020
// include ROS 1 builtin messages
21+
#include "rosgraph_msgs/Log.h"
2122
#include "ros/duration.h"
2223
#include "ros/time.h"
2324

2425
// include ROS 2 builtin messages
2526
#include "builtin_interfaces/msg/duration.hpp"
2627
#include "builtin_interfaces/msg/time.hpp"
28+
#include "rcl_interfaces/msg/log.hpp"
2729

2830
namespace ros1_bridge
2931
{
@@ -52,6 +54,18 @@ convert_2_to_1(
5254
const builtin_interfaces::msg::Time & ros2_msg,
5355
ros::Time & ros1_type);
5456

57+
template<>
58+
void
59+
convert_1_to_2(
60+
const rosgraph_msgs::Log & ros1_msg,
61+
rcl_interfaces::msg::Log & ros2_msg);
62+
63+
template<>
64+
void
65+
convert_2_to_1(
66+
const rcl_interfaces::msg::Log & ros2_msg,
67+
rosgraph_msgs::Log & ros1_msg);
68+
5569
} // namespace ros1_bridge
5670

5771
#endif // ROS1_BRIDGE__CONVERT_BUILTIN_INTERFACES_HPP_

ros1_bridge/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ def generate_cpp(output_path, template_dir):
9898
data['ros2_package_names_msg'] + data['ros2_package_names_srv'])
9999
# skip builtin_interfaces since there is a custom implementation
100100
unique_package_names -= {'builtin_interfaces'}
101+
# skip rcl_interfaces since there is a custom implementation
102+
# Only for Log which is the only type that is useful to map.
103+
unique_package_names -= {'rcl_interfaces'}
101104
data['ros2_package_names'] = list(unique_package_names)
102105

103106
template_file = os.path.join(template_dir, 'get_factory.cpp.em')
@@ -531,9 +534,7 @@ def __init__(self, data, expected_package_name):
531534
self.ros1_package_name = data['ros1_package_name']
532535
self.ros2_package_name = data['ros2_package_name']
533536
self.foreign_mapping = bool(data.get('enable_foreign_mappings'))
534-
self.package_mapping = (
535-
len(data) == (2 + int('enable_foreign_mappings' in data))
536-
)
537+
self.package_mapping = self.ros1_package_name != self.ros2_package_name
537538
else:
538539
raise MappingException(
539540
'Ignoring a rule without a ros1_package_name and/or ros2_package_name')
@@ -1095,6 +1096,10 @@ def determine_field_mapping(ros1_msg, ros2_msg, mapping_rules, msg_idx):
10951096
# check that no fields exist in ROS 2 but not in ROS 1
10961097
# since then it might be the case that those have been renamed and should be mapped
10971098
for ros2_member in ros2_spec.structure.members:
1099+
if any(ros2_member == key[0] for key in mapping.fields_2_to_1.keys()):
1100+
# If they are explicitly mapped this should be fine...
1101+
continue
1102+
10981103
for ros1_field in ros1_spec.parsed_fields():
10991104
if ros1_field.name.lower() == ros2_member.name:
11001105
break

src/builtin_interfaces_factories.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ get_factory_builtin_interfaces(
5252
>
5353
>("std_msgs/Time", ros2_type_name);
5454
}
55+
if (
56+
(ros1_type_name == "rosgraph_msgs/Log" || ros1_type_name == "") &&
57+
ros2_type_name == "rcl_interfaces/msg/Log")
58+
{
59+
return std::make_shared<
60+
Factory<
61+
rosgraph_msgs::Log,
62+
rcl_interfaces::msg::Log
63+
>
64+
>("rosgraph_msgs/Log", ros2_type_name);
65+
}
66+
5567
return std::shared_ptr<FactoryInterface>();
5668
}
5769

@@ -104,4 +116,28 @@ Factory<
104116
ros1_bridge::convert_2_to_1(ros2_msg, ros1_msg.data);
105117
}
106118

119+
template<>
120+
void
121+
Factory<
122+
rosgraph_msgs::Log,
123+
rcl_interfaces::msg::Log
124+
>::convert_1_to_2(
125+
const rosgraph_msgs::Log & ros1_msg,
126+
rcl_interfaces::msg::Log & ros2_msg)
127+
{
128+
ros1_bridge::convert_1_to_2(ros1_msg, ros2_msg);
129+
}
130+
131+
template<>
132+
void
133+
Factory<
134+
rosgraph_msgs::Log,
135+
rcl_interfaces::msg::Log
136+
>::convert_2_to_1(
137+
const rcl_interfaces::msg::Log & ros2_msg,
138+
rosgraph_msgs::Log & ros1_msg)
139+
{
140+
ros1_bridge::convert_2_to_1(ros2_msg, ros1_msg);
141+
}
142+
107143
} // namespace ros1_bridge

src/convert_builtin_interfaces.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,91 @@ convert_2_to_1(
5858
ros1_type.nsec = ros2_msg.nanosec;
5959
}
6060

61+
62+
template<>
63+
void
64+
convert_1_to_2(
65+
const rosgraph_msgs::Log & ros1_msg,
66+
rcl_interfaces::msg::Log & ros2_msg)
67+
{
68+
ros1_bridge::convert_1_to_2(ros1_msg.header.stamp, ros2_msg.stamp);
69+
70+
// Explicitly map the values of the log level as they differ between
71+
// ROS1 and ROS2.
72+
switch (ros1_msg.level) {
73+
case rosgraph_msgs::Log::DEBUG:
74+
ros2_msg.level = rcl_interfaces::msg::Log::DEBUG;
75+
break;
76+
77+
case rosgraph_msgs::Log::INFO:
78+
ros2_msg.level = rcl_interfaces::msg::Log::INFO;
79+
break;
80+
81+
case rosgraph_msgs::Log::WARN:
82+
ros2_msg.level = rcl_interfaces::msg::Log::WARN;
83+
break;
84+
85+
case rosgraph_msgs::Log::ERROR:
86+
ros2_msg.level = rcl_interfaces::msg::Log::ERROR;
87+
break;
88+
89+
case rosgraph_msgs::Log::FATAL:
90+
ros2_msg.level = rcl_interfaces::msg::Log::FATAL;
91+
break;
92+
93+
default:
94+
ros2_msg.level = ros1_msg.level;
95+
break;
96+
}
97+
98+
ros2_msg.name = ros1_msg.name;
99+
ros2_msg.msg = ros1_msg.msg;
100+
ros2_msg.file = ros1_msg.file;
101+
ros2_msg.function = ros1_msg.function;
102+
ros2_msg.line = ros1_msg.line;
103+
}
104+
105+
template<>
106+
void
107+
convert_2_to_1(
108+
const rcl_interfaces::msg::Log & ros2_msg,
109+
rosgraph_msgs::Log & ros1_msg)
110+
{
111+
ros1_bridge::convert_2_to_1(ros2_msg.stamp, ros1_msg.header.stamp);
112+
113+
// Explicitly map the values of the log level as they differ between
114+
// ROS1 and ROS2.
115+
switch (ros2_msg.level) {
116+
case rcl_interfaces::msg::Log::DEBUG:
117+
ros1_msg.level = rosgraph_msgs::Log::DEBUG;
118+
break;
119+
120+
case rcl_interfaces::msg::Log::INFO:
121+
ros1_msg.level = rosgraph_msgs::Log::INFO;
122+
break;
123+
124+
case rcl_interfaces::msg::Log::WARN:
125+
ros1_msg.level = rosgraph_msgs::Log::WARN;
126+
break;
127+
128+
case rcl_interfaces::msg::Log::ERROR:
129+
ros1_msg.level = rosgraph_msgs::Log::ERROR;
130+
break;
131+
132+
case rcl_interfaces::msg::Log::FATAL:
133+
ros1_msg.level = rosgraph_msgs::Log::FATAL;
134+
break;
135+
136+
default:
137+
ros1_msg.level = ros1_msg.level;
138+
break;
139+
}
140+
141+
ros1_msg.name = ros2_msg.name;
142+
ros1_msg.msg = ros2_msg.msg;
143+
ros1_msg.file = ros2_msg.file;
144+
ros1_msg.function = ros2_msg.function;
145+
ros1_msg.line = ros2_msg.line;
146+
}
147+
61148
} // namespace ros1_bridge

0 commit comments

Comments
 (0)