Skip to content

Commit e9a1502

Browse files
committed
update readme with ros2 instructions
1 parent 259dd85 commit e9a1502

File tree

1 file changed

+112
-43
lines changed

1 file changed

+112
-43
lines changed

README.md

Lines changed: 112 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<a href="https://github.com/ika-rwth-aachen/mqtt_client"><img src="https://img.shields.io/github/stars/ika-rwth-aachen/mqtt_client?style=social"/></a>
99
</p>
1010

11-
The *mqtt_client* package provides a ROS nodelet that enables connected ROS-based devices or robots to exchange ROS messages via an MQTT broker using the [MQTT](http://mqtt.org) protocol. This works generically for arbitrary ROS message types. The *mqtt_client* can also exchange primitive messages with MQTT clients running on devices not based on ROS.
11+
The *mqtt_client* package provides a ROS nodelet or ROS 2 node that enables connected ROS-based devices or robots to exchange ROS messages via an MQTT broker using the [MQTT](http://mqtt.org) protocol. This works generically for arbitrary ROS message types. The *mqtt_client* can also exchange primitive messages with MQTT clients running on devices not based on ROS.
1212

1313
- [Installation](#installation)
1414
- [Usage](#usage)
@@ -26,7 +26,11 @@ The *mqtt_client* package provides a ROS nodelet that enables connected ROS-base
2626
The *mqtt_client* package is released as an official ROS Noetic package and can easily be installed via a package manager.
2727

2828
```bash
29+
# ROS 1
2930
sudo apt install ros-noetic-mqtt-client
31+
32+
# ROS 2
33+
sudo apt install ros-humble-mqtt-client
3034
```
3135

3236
If you would like to install *mqtt_client* from source, simply clone this repository into your ROS workspace. All dependencies that are listed in the ROS [`package.xml`](package.xml) can be installed using [*rosdep*](http://wiki.ros.org/rosdep).
@@ -56,38 +60,34 @@ docker run --rm --network host --name mosquitto eclipse-mosquitto
5660

5761
#### Demo Configuration
5862

59-
The *mqtt_client* is best configured with a ROS parameter *yaml* file. The configuration shown below (also see [`params.yaml`](launch/params.yaml)) allows an exchange of messages as follows:
63+
The *mqtt_client* is best configured with a ROS parameter *yaml* file. The configuration shown below (also see [`params.yaml`](mqtt_client/config/params.yaml) / [`params.ros2.yaml`](mqtt_client/config/params.ros2.yaml)) allows an exchange of messages as follows:
6064

6165
- ROS messages received locally on ROS topic `/ping/ros` are sent to the broker on MQTT topic `pingpong/ros`;
6266
- MQTT messages received from the broker on MQTT topic `pingpong/ros` are published locally on ROS topic `/pong/ros`;
63-
- primitive ROS messages received locally on ROS topic `/ping/primitive` are sent as primitive (string) messages to the broker on MQTT topic `pingpong/primitive`;
64-
- MQTT messages received from the broker on MQTT topic `pingpong/primitive` are published locally as primitive ROS messages on ROS topic `/pong/primitive`.
6567

6668
```yaml
6769
broker:
6870
host: localhost
6971
port: 1883
7072
bridge:
7173
ros2mqtt:
72-
- ros_topic: /ping/ros
73-
mqtt_topic: pingpong/ros
74-
- ros_topic: /ping/primitive
75-
mqtt_topic: pingpong/primitive
76-
primitive: true
74+
- ros_topic: /ping
75+
mqtt_topic: pingpong
7776
mqtt2ros:
78-
- mqtt_topic: pingpong/ros
79-
ros_topic: /pong/ros
80-
- mqtt_topic: pingpong/primitive
81-
ros_topic: /pong/primitive
82-
primitive: true
77+
- mqtt_topic: pingpong
78+
ros_topic: /pong
8379
```
8480
8581
#### Demo Client Launch
8682
8783
After building your ROS workspace, launch the *mqtt_client* nodelet with the pre-configured demo parameters using *roslaunch*, which should yield the following output.
8884
8985
```bash
86+
# ROS 1
9087
roslaunch mqtt_client standalone.launch
88+
89+
# ROS 2
90+
ros2 launch mqtt_client standalone.launch.ros2.xml
9191
```
9292

9393
```txt
@@ -98,9 +98,7 @@ roslaunch mqtt_client standalone.launch
9898
[ WARN] [1665575657.360576344]: Parameter 'client/keep_alive_interval' not set, defaulting to '60.000000'
9999
[ WARN] [1665575657.360847295]: Parameter 'client/max_inflight' not set, defaulting to '65535'
100100
[ INFO] [1665575657.361281461]: Bridging ROS topic '/ping/ros' to MQTT topic 'pingpong/ros'
101-
[ INFO] [1665575657.361303380]: Bridging primitive ROS topic '/ping/primitive' to MQTT topic 'pingpong/primitive'
102101
[ INFO] [1665575657.361352809]: Bridging MQTT topic 'pingpong/ros' to ROS topic '/pong/ros'
103-
[ INFO] [1665575657.361370558]: Bridging MQTT topic 'pingpong/primitive' to primitive ROS topic '/pong/primitive'
104102
[ INFO] [1665575657.362153083]: Connecting to broker at 'tcp://localhost:1883' ...
105103
[ INFO] [1665575657.462622065]: Connected to broker at 'tcp://localhost:1883'
106104
```
@@ -110,55 +108,83 @@ Note that the *mqtt_client* successfully connected to the broker and also echoed
110108
In order to test the communication among *mqtt_clients*, publish any ROS message on ROS topic `/ping/ros` and wait for a response on ROS topic `/pong/ros`.
111109

112110
```bash
113-
# 1st terminal: listen for ROS messages on /pong/ros
114-
rostopic echo /pong/ros
111+
# 1st terminal: publish ROS message to /ping
112+
113+
# ROS 1
114+
rostopic pub -r 1 /ping/ros std_msgs/String "Hello MQTT"
115+
116+
# ROS 2
117+
ros2 topic pub /ping/ros std_msgs/msg/String "{data: \"Hello MQTT\"}"
115118
```
116119

117120
```bash
118-
# 2nd terminal: publish ROS message to /ping/ros
119-
rostopic pub -r 1 /ping/ros std_msgs/String "Hello MQTT!"
121+
# 2nd terminal: listen for ROS messages on /pong
122+
123+
# ROS 1
124+
rostopic echo /pong/ros
125+
126+
# ROS 2
127+
ros2 topic echo /pong/ros
120128
```
121129

122-
In order to test the communication between *mqtt_client* and other MQTT clients, publish a primitive ROS message on ROS topic `/ping/primitive`, directly publish a primitive MQTT message on MQTT topic `pingpong/primitive` and wait for responses on ROS topic `/pong/primitive`.
130+
In order to test the communication between *mqtt_client* and other MQTT clients, publish a primitive ROS message on ROS topic `/ping/primitive`, directly publish a primitive MQTT message on MQTT topic `pingpong/primitive` and wait for responses on ROS topic `/pong/primitive`. Note that you need to restart the ROS 2 *mqtt_client* with a different config file.
123131

124132
```bash
125-
# 3rd terminal: listen for primitive ROS messages on /pong/primitive
126-
rostopic echo /pong/primitive
133+
# ROS 2
134+
# mqtt_client$
135+
ros2 launch mqtt_client standalone.launch.ros2.xml params_file:=$(ros2 pkg prefix mqtt_client)/share/mqtt_client/config/params.ros2.primitive.yaml
127136
```
128137

129138
```bash
130-
# 4th terminal: publish primitive ROS message to /ping/primitive
139+
# 3rd terminal: publish primitive ROS message to /ping/primitive
140+
141+
# ROS1
131142
rostopic pub -r 1 /ping/primitive std_msgs/Int32 42
143+
144+
# ROS2
145+
ros2 topic pub /ping/primitive std_msgs/msg/Int32 "{data: 42}"
132146
```
133147

134148
```bash
135-
# 5th terminal: publish primitive MQTT message to pingpong/primitive
149+
# 4th terminal: listen for primitive ROS messages on /pong/primitive
150+
151+
# ROS 1
152+
rostopic echo /pong/primitive
153+
154+
# ROS2
155+
ros2 topic echo /pong/primitive
156+
```
157+
158+
```bash
159+
# 5th terminal: publish primitive MQTT message to pingpong/primitive directly using mosquitto_pub
136160
docker run --rm --network host eclipse-mosquitto mosquitto_pub -h localhost -t "pingpong/primitive" --repeat 20 --repeat-delay 1 -m 69
137161
```
138162

139-
If everything works as expected, the second terminal should print a message at 1Hz, while the third terminal should print two different messages at 1Hz.
163+
If everything works as expected, the second terminal should print a message at 1Hz, while the fourth terminal should print two different messages at 1Hz.
140164

141165
### Launch
142166

143167
You can start the *mqtt_client* nodelet in a standalone nodelet manager with:
144168

145169
```bash
170+
# ROS 1
146171
roslaunch mqtt_client standalone.launch
172+
173+
# ROS 2
174+
ros2 launch mqtt_client standalone.launch.ros2.xml
147175
```
148176

149-
This will automatically load the provided demo [params.yaml](launch/params.yaml) to the ROS parameter server. If you wish to load your custom configuration file, simply pass `params_file`.
177+
This will automatically load the provided demo [`params.yaml`](mqtt_client/config/params.yaml) / [`params.ros2.yaml`](mqtt_client/config/params.ros2.yaml). If you wish to load your custom configuration file, simply pass `params_file`.
150178

151179
```bash
180+
# ROS 1
152181
roslaunch mqtt_client standalone.launch params_file:="</PATH/TO/PARAMS.YAML>"
153-
```
154182

155-
You can also disable parameter loading altogether by passing `load_params:=false`. In this case, make sure to populate the ROS parameter server accordingly with other means.
156-
157-
```bash
158-
roslaunch mqtt_client standalone.launch load_params:=false
183+
# ROS 2
184+
ros2 launch mqtt_client standalone.launch.ros2.xml params_file:="</PATH/TO/PARAMS.YAML>"
159185
```
160186

161-
In order to exploit the benefits of *mqtt_client* being a nodelet, load the nodelet to your own nodelet manager shared with other nodelets.
187+
In order to exploit the benefits of *mqtt_client* being a ROS 1 nodelet, load the nodelet to your own nodelet manager shared with other nodelets.
162188

163189
### Configuration
164190

@@ -201,9 +227,11 @@ client:
201227
202228
#### Bridge Parameters
203229
230+
> :warning: The ROS 2 version of *mqtt_client* is currently only capable of running one ROS-to-MQTT and one MQTT-to-ROS transmission at a time. The parameters `brige/ros2mqtt` and `bridge/mqtt2ros` therefore are not lists of dictionaries, but nested dictionaries without list index instead, see [`params.ros2.yaml`](mqtt_client/config/params.ros2.yaml). If you need to bridge multiple topics, you can run multiple instances of *mqtt_client* simultaneously.
231+
204232
```yaml
205233
bridge:
206-
ros2mqtt: # array specifying which ROS topics to map to which MQTT topics
234+
ros2mqtt: # array specifying which ROS topics to map to which MQTT topics (not an array in ROS 2 version)
207235
- ros_topic: # ROS topic whose messages are transformed to MQTT messages
208236
mqtt_topic: # MQTT topic on which the corresponding ROS messages are sent to the broker
209237
primitive: # [false] whether to publish as primitive message
@@ -214,7 +242,7 @@ bridge:
214242
mqtt:
215243
qos: # [0] MQTT QoS value
216244
retained: # [false] whether to retain MQTT message
217-
mqtt2ros: # array specifying which MQTT topics to map to which ROS topics
245+
mqtt2ros: # array specifying which MQTT topics to map to which ROS topics (not an array in ROS 2 version)
218246
- mqtt_topic: # MQTT topic on which messages are received from the broker
219247
ros_topic: # ROS topic on which corresponding MQTT messages are published
220248
primitive: # [false] whether to publish as primitive message (if coming from non-ROS MQTT client)
@@ -243,48 +271,89 @@ In order to inject the current timestamp into outgoing MQTT messages, the parame
243271
These latencies can be printed easily with *rostopic echo*
244272

245273
```bash
274+
# ROS 1
246275
rostopic echo --clear /<mqtt_client_name>/latencies/<mqtt2ros/ros_topic>/data
276+
277+
# ROS 2
278+
ros2 topic echo /<mqtt_client_name>/latencies/<mqtt2ros/ros_topic>/data
247279
```
248280

249281
or plotted with [rqt_plot](http://wiki.ros.org/rqt_plot):
250282

251283
```bash
252-
rqt_plot /<mqtt_client_name>/latencies/<mqtt2ros/ros_topic>/data
284+
# ROS 1
285+
rosrun rqt_plot rqt_plot /<mqtt_client_name>/latencies/<mqtt2ros/ros_topic>/data
286+
287+
# ROS 2
288+
ros2 run rqt_plot rqt_plot /<mqtt_client_name>/latencies/<mqtt2ros/ros_topic>/data
253289
```
254290

255291
## Package Summary
256292

257293
This short package summary documents the package in line with the [ROS Wiki Style Guide](http://wiki.ros.org/StyleGuide).
258294

259-
### Nodelets
295+
### ROS 1
260296

261-
#### `mqtt_client/MqttClient`
297+
#### Nodelets
298+
299+
##### `mqtt_client/MqttClient`
262300

263301
Enables connected ROS-based devices or robots to exchange ROS messages via an MQTT broker using the [MQTT](http://mqtt.org) protocol.
264302

265-
##### Subscribed Topics
303+
###### Subscribed Topics
266304

267305
- `<bridge/ros2mqtt[*]/ros_topic>` ([`topic_tools/ShapeShifter`](http://wiki.ros.org/topic_tools))
268306
ROS topic whose messages are transformed to MQTT messages and sent to the MQTT broker. May have arbitrary ROS message type.
269307

270-
##### Published Topics
308+
###### Published Topics
271309

272310
- `<bridge/mqtt2ros[*]/ros_topic>` ([`topic_tools/ShapeShifter`](http://wiki.ros.org/topic_tools))
273311
ROS topic on which MQTT messages received from the MQTT broker are published. May have arbitrary ROS message type.
274312
- `~/latencies/<bridge/mqtt2ros[*]/ros_topic>` ([`std_msgs/Float64`](https://docs.ros.org/en/api/std_msgs/html/msg/Float64.html))
275313
Latencies measured on the message transfer to `<bridge/mqtt2ros[*]/ros_topic>` are published here, if the received messages have a timestamp injected (see [Latency Computation](#latency-computation)).
276314

277-
##### Services
315+
###### Services
316+
317+
- `is_connected` ([`mqtt_client/srv/IsConnected`](mqtt_client_interfaces/srv/IsConnected.srv))
318+
Returns whether the client is connected to the MQTT broker.
319+
320+
###### Parameters
321+
322+
See [Configuration](#configuration).
323+
324+
### ROS 2
325+
326+
#### Nodes
278327

279-
- `is_connected` ([`mqtt_client/IsConnected`](srv/IsConnected.srv))
328+
##### `mqtt_client/mqtt_client`
329+
330+
Enables connected ROS-based devices or robots to exchange ROS messages via an MQTT broker using the [MQTT](http://mqtt.org) protocol.
331+
332+
###### Subscribed Topics
333+
334+
- `<bridge/ros2mqtt/ros_topic>` ([`rclcpp::SerializedMessage`](https://docs.ros.org/en/ros2_packages/rolling/api/rclcpp/generated/classrclcpp_1_1GenericSubscription.html))
335+
ROS topic whose messages are transformed to MQTT messages and sent to the MQTT broker. May have arbitrary ROS message type.
336+
337+
###### Published Topics
338+
339+
- `<bridge/mqtt2ros/ros_topic>` ([`rclcpp::SerializedMessage`](https://docs.ros.org/en/ros2_packages/rolling/api/rclcpp/generated/classrclcpp_1_1GenericPublisher.html))
340+
ROS topic on which MQTT messages received from the MQTT broker are published. May have arbitrary ROS message type.
341+
- `~/latencies/<bridge/mqtt2ros/ros_topic>` ([`std_msgs/Float64`](https://docs.ros.org/en/api/std_msgs/html/msg/Float64.html))
342+
Latencies measured on the message transfer to `<bridge/mqtt2ros/ros_topic>` are published here, if the received messages have a timestamp injected (see [Latency Computation](#latency-computation)).
343+
344+
###### Services
345+
346+
- `is_connected` ([`mqtt_client/srv/IsConnected`](mqtt_client_interfaces/srv/IsConnected.srv))
280347
Returns whether the client is connected to the MQTT broker.
281348

282-
##### Parameters
349+
###### Parameters
283350

284351
See [Configuration](#configuration).
285352

286353
## How It Works
287354

355+
### ROS 1
356+
288357
The *mqtt_client* is able to bridge ROS messages of arbitrary message type to an MQTT broker. To this end, it needs to employ generic ROS subscribers and publishers, which only take shape at runtime.
289358

290359
These generic ROS subscribers and publishers are realized through [topic_tools::ShapeShifter](http://docs.ros.org/diamondback/api/topic_tools/html/classtopic__tools_1_1ShapeShifter.html). For each pair of `ros_topic` and `mqtt_topic` specified under `bridge/ros2mqtt/`, a ROS subscriber is setup with the following callback signature:

0 commit comments

Comments
 (0)