Skip to content

Commit b986388

Browse files
ahcordeKotochleb
andauthored
[forward iron] Add ROS namespaces to GZ topics (gazebosim#516)
Signed-off-by: Kotochleb <[email protected]> Signed-off-by: Krzysztof Wojciechowski <[email protected]> Signed-off-by: Alejandro Hernández Cordero <[email protected]> Co-authored-by: Krzysztof Wojciechowski <[email protected]>
1 parent 0367d9a commit b986388

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

ros_gz_bridge/README.md

+37-3
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Now we start the ROS listener.
8585

8686
```
8787
# Shell B:
88-
. /opt/ros/galactic/setup.bash
88+
. /opt/ros/iron/setup.bash
8989
ros2 topic echo /chatter
9090
```
9191

@@ -117,7 +117,7 @@ Now we start the ROS talker.
117117

118118
```
119119
# Shell C:
120-
. /opt/ros/galactic/setup.bash
120+
. /opt/ros/iron/setup.bash
121121
ros2 topic pub /chatter std_msgs/msg/String "data: 'Hi'" --once
122122
```
123123

@@ -155,7 +155,7 @@ Now we start the ROS GUI:
155155

156156
```
157157
# Shell C:
158-
. /opt/ros/galactic/setup.bash
158+
. /opt/ros/iron/setup.bash
159159
ros2 run rqt_image_view rqt_image_view /rgbd_camera/image
160160
```
161161

@@ -273,9 +273,43 @@ To run the bridge node with the above configuration:
273273
ros2 run ros_gz_bridge parameter_bridge --ros-args -p config_file:=$WORKSPACE/ros_gz/ros_gz_bridge/test/config/full.yaml
274274
```
275275

276+
## Example 6: Using ROS namespace with the Bridge
277+
278+
When spawning multiple robots inside the same ROS environment, it is convenient to use namespaces to avoid overlapping topic names.
279+
There are three main types of namespaces: relative, global (`/`) and private (`~/`). For more information, refer to ROS documentation.
280+
Namespaces are applied to Gazebo topic both when specified as `topic_name` as well as `gz_topic_name`.
281+
282+
By default, the Bridge will not apply ROS namespace on the Gazebo topics. To enable this feature, use parameter `expand_gz_topic_names`.
283+
Let's test our topic with namespace:
284+
285+
```bash
286+
# Shell A:
287+
. ~/bridge_ws/install/setup.bash
288+
ros2 run ros_gz_bridge parameter_bridge chatter@std_msgs/msg/[email protected] \
289+
--ros-args -p expand_gz_topic_names:=true -r __ns:=/demo
290+
```
291+
292+
Now we start the Gazebo Transport listener.
293+
294+
```bash
295+
# Shell B:
296+
ign topic -e -t /demo/chatter
297+
```
298+
299+
Now we start the ROS talker.
300+
301+
```bash
302+
# Shell C:
303+
. /opt/ros/iron/setup.bash
304+
ros2 topic pub /demo/chatter std_msgs/msg/String "data: 'Hi from inside of a namespace'" --once
305+
```
306+
307+
By changing `chatter` to `/chatter` or `~/chatter` you can obtain different results.
308+
276309
## API
277310

278311
ROS 2 Parameters:
279312

280313
* `subscription_heartbeat` - Period at which the node checks for new subscribers for lazy bridges.
281314
* `config_file` - YAML file to be loaded as the bridge configuration
315+
* `expand_gz_topic_names` - Enable or disable ROS namespace applied on GZ topics.

ros_gz_bridge/src/ros_gz_bridge.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "bridge_handle_ros_to_gz.hpp"
2121
#include "bridge_handle_gz_to_ros.hpp"
2222

23+
#include <rclcpp/expand_topic_or_service_name.hpp>
24+
2325
namespace ros_gz_bridge
2426
{
2527

@@ -30,6 +32,7 @@ RosGzBridge::RosGzBridge(const rclcpp::NodeOptions & options)
3032

3133
this->declare_parameter<int>("subscription_heartbeat", 1000);
3234
this->declare_parameter<std::string>("config_file", "");
35+
this->declare_parameter<bool>("expand_gz_topic_names", false);
3336

3437
int heartbeat;
3538
this->get_parameter("subscription_heartbeat", heartbeat);
@@ -43,14 +46,21 @@ void RosGzBridge::spin()
4346
if (handles_.empty()) {
4447
std::string config_file;
4548
this->get_parameter("config_file", config_file);
49+
bool expand_names;
50+
this->get_parameter("expand_gz_topic_names", expand_names);
51+
const std::string ros_ns = this->get_namespace();
52+
const std::string ros_node_name = this->get_name();
4653
if (!config_file.empty()) {
4754
auto entries = readFromYamlFile(config_file);
48-
for (const auto & entry : entries) {
55+
for (auto & entry : entries) {
56+
if (expand_names) {
57+
entry.gz_topic_name = rclcpp::expand_topic_or_service_name(
58+
entry.gz_topic_name, ros_node_name, ros_ns, false);
59+
}
4960
this->add_bridge(entry);
5061
}
5162
}
5263
}
53-
5464
for (auto & bridge : handles_) {
5565
bridge->Spin();
5666
}

0 commit comments

Comments
 (0)