|
| 1 | +# Simple Backward Compatibility |
| 2 | + |
| 3 | + |
| 4 | +This package is meant to be used to demonstrate how to create messages with optional fields and how to exploit them for achieving backward compatibility. |
| 5 | + |
| 6 | +Long story short, usually when you add a new field to a message interface and then you try to send these updated messages to a subscriber which has been compiled against an old version of the message library (i.e. without the new field) a memory error will arise. |
| 7 | + |
| 8 | + |
| 9 | +ROS2 introduces optional fields which are dynamically allocated by the subscriber. |
| 10 | + |
| 11 | + |
| 12 | +## How to use it |
| 13 | + |
| 14 | +Let's start by creating a workspace and compile two simple publisher and subscriber nodes. |
| 15 | + |
| 16 | +``` |
| 17 | +mkdir -p ws_v1/src |
| 18 | +cd ws_v1/src |
| 19 | +ln -s <PATH_TO_THIS_REPO>/simple_backward_compatible . |
| 20 | +cd .. |
| 21 | +colcon build |
| 22 | +``` |
| 23 | + |
| 24 | +The nodes created in this workspace will communicate using [this message interface](interfaces_v1/msg/Optional_fields.msg) which we will call v1. |
| 25 | + |
| 26 | +You can immediately run the subscriber node. |
| 27 | + |
| 28 | +``` |
| 29 | +source install/local_setup.sh |
| 30 | +ros2 run simple_backward_compatible subscriber_main |
| 31 | +``` |
| 32 | + |
| 33 | +Now, while the subscriber is still running, open a new terminal and create a second workspace. |
| 34 | + |
| 35 | +``` |
| 36 | +mkdir -p ws_v2/src |
| 37 | +cd ws_v2/src |
| 38 | +ln -s <PATH_TO_THIS_REPO>/simple_backward_compatible . |
| 39 | +cd .. |
| 40 | +``` |
| 41 | + |
| 42 | +However this time, before compiling, let's make a quick change. |
| 43 | +Open with a text editor `CMakeLists.txt` and uncomment line #5 (the one saying `#set(BUILD_V2 true)`). |
| 44 | +Then build. |
| 45 | + |
| 46 | +``` |
| 47 | +colcon build |
| 48 | +``` |
| 49 | + |
| 50 | +You have just told the compiler to use a different version of the message interface, which you can find [here](interfaces_v2/msg/Optional_fields.msg) and we will call it v2. |
| 51 | + |
| 52 | +As you can see, the new messages have some additional fields characterized by variable length arrays. |
| 53 | +You can note from the [publisher code](src/publisher_main.cpp) that the fields of the updated message are being properly filled with values. |
| 54 | + |
| 55 | +Now run a publisher node from this second workspace. |
| 56 | + |
| 57 | +``` |
| 58 | +source install/local_setup.sh |
| 59 | +ros2 run simple_backward_compatible publisher_main |
| 60 | +``` |
| 61 | + |
| 62 | +You will note that the two nodes are communicating properly, even if compiled against two different versions of the message interfaces. |
| 63 | + |
| 64 | +**NOTE**: if you try to run a v2 subscriber and a v1 publisher you will still get a memory error. |
0 commit comments