- name
- ros2-interfaces
- description
- Author and debug ROS 2 .msg/.srv/.action interfaces — static arrays Type[N], unbounded sequences, IDL/DDS mapping, codegen to C++/Python, and tooling limits (ros2 topic pub/echo, rqt_plot, Simulink). Use when editing message files, interface packages, covariance/fixed arrays, rosidl generation, or when the user mentions .msg, static arrays, IDL, or custom interfaces.
# ROS 2 Interfaces
## Static-size arrays (verbatim rules of thumb)
In ROS 2, a static size array is defined in message files (`.msg`) using the syntax `Type[N]`, where `N` is a fixed, non-zero integer representing the exact number of elements. When generated into C++, these are mapped to `std::array<T, N>`, and their default value is an array of `N` zero-initialized elements.
Key characteristics and limitations include:
- **IDL Mapping:** Static arrays map to `array` in the Interface Definition Language (IDL) and `T[N]` in DDS types.
- **Visualization Limits:** Tools like `rqt_plot` may have bugs or limitations when plotting fixed-size array members (e.g., covariance in `NavSatFix`), sometimes requiring workarounds or specific indexing.
- **CLI Constraints:** The `ros2 topic pub` command may fail or truncate arrays if the total number of elements exceeds 32 (specifically noted in older Dashing/Crystal versions for `geometry_msgs` covariance arrays), whereas `ros2 topic echo` truncates output at 128 elements by default for usability.
- **Simulation Integration:** In Simulink, variable-length arrays are converted to fixed-length arrays with a default maximum of 128 for primitive types; this limit must be manually adjusted if the static array size or expected dynamic data exceeds this bound.
## Field syntax cheat sheet
| `.msg` syntax | Meaning | C++ | Python |
|---------------|---------|-----|--------|
| `float64 x` | scalar | `double` | `float` |
| `float64[9]` | **static** array length 9 | `std::array<double, 9>` | `array('d')` / list len 9 |
| `float64[]` | **unbounded** sequence | `std::vector<double>` | `list` |
| `float64[<=N]` | bounded sequence (IDL) | vector with max | list (runtime check) |
| `string name` | UTF-8 string | `std::string` | `str` |
| `string<=N name` | bounded string | string | str |
Examples:
```
# Static (PoseWithCovariance-style)
float64[36] covariance
# Dynamic
geometry_msgs/Point[] points
# Bounded (prefer when wire size must be capped)
uint8[<=1024] payload
```
## Interface package checklist
1. Separate package (or clear `rosidl_generate_interfaces` target) for msgs/srvs/actions.
2. `package.xml`: `rosidl_default_generators` (build), `rosidl_default_runtime` (exec), `member_of_group` `rosidl_interface_packages`.
3. List dependencies of nested types in `rosidl_generate_interfaces(... DEPENDENCIES ...)`.
4. After rebuild: `source install/setup.bash` before Python/C++ consumers see new types.
5. Prefer **static** arrays only when length is part of the contract (covariance 36, IMU 9). Prefer **unbounded/bounded sequences** for variable payloads.
## Common pitfalls
- Changing `Type[N]` ↔ `Type[]` is an **ABI/API break** — bump interface version / coordinate all publishers.
- Nested messages with large static arrays blow up DDS sample size; watch MTU / Cyclone buffer sizing.
- `ros2 interface show <pkg>/msg/<Name>` to confirm what was generated.
- Do not hand-edit generated `*__struct.hpp` / Python stubs — change `.msg` and regenerate.
## Related
- QoS / large topics: skill `ros2-qos-dds`
- Build after interface changes: skill `ros2-workspace-ops`
- Pub/echo truncation: skill `ros2-cli-debug`
Auf GitHub ansehen