|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Greenwave Monitor is a ROS 2 diagnostic tool for runtime monitoring of topic frame rates and latency. The project consists of two packages: |
| 8 | + |
| 9 | +1. **greenwave_monitor** - Core C++ monitoring node, ncurses dashboard frontend, and header-only library |
| 10 | +2. **greenwave_monitor_interfaces** - ROS service definitions (ManageTopic, SetExpectedFrequency) |
| 11 | + |
| 12 | +## Build Commands |
| 13 | + |
| 14 | +```bash |
| 15 | +# Build all packages |
| 16 | +cd <ros_workspace> |
| 17 | +colcon build --packages-up-to greenwave_monitor |
| 18 | + |
| 19 | +# Build individual packages |
| 20 | +colcon build --packages-select greenwave_monitor |
| 21 | +colcon build --packages-select greenwave_monitor_interfaces |
| 22 | +``` |
| 23 | + |
| 24 | +## Testing |
| 25 | + |
| 26 | +```bash |
| 27 | +# Run all tests for greenwave_monitor |
| 28 | +colcon test --packages-select greenwave_monitor |
| 29 | +colcon test-result --verbose |
| 30 | + |
| 31 | +# Run specific test types |
| 32 | +colcon test --packages-select greenwave_monitor --pytest-args -k test_greenwave_monitor |
| 33 | +colcon test --packages-select greenwave_monitor --pytest-args -k test_topic_monitoring_integration |
| 34 | + |
| 35 | +# C++ unit tests (gtest) are run automatically with colcon test |
| 36 | +# - test_message_diagnostics.cpp - Tests MessageDiagnostics header-only library |
| 37 | +# - test_minimal_publisher.cpp - Tests example publisher node |
| 38 | + |
| 39 | +# Python linting tests |
| 40 | +colcon test --packages-select greenwave_monitor --pytest-args -k test_flake8 |
| 41 | +colcon test --packages-select greenwave_monitor --pytest-args -k test_pep257 |
| 42 | +colcon test --packages-select greenwave_monitor --pytest-args -k test_copyright |
| 43 | +``` |
| 44 | + |
| 45 | +## Architecture |
| 46 | + |
| 47 | +### Core Components |
| 48 | + |
| 49 | +**GreenwaveMonitor Node** (`greenwave_monitor.cpp`): |
| 50 | +- Subscribes to topics using generic subscriptions (rclcpp::GenericSubscription) |
| 51 | +- Extracts timestamps from serialized messages for latency calculation |
| 52 | +- Uses MessageDiagnostics to track frame rates and latency per topic |
| 53 | +- Publishes diagnostics to `/diagnostics` topic (diagnostic_msgs/DiagnosticArray) |
| 54 | +- Exposes services at `~/manage_topic` and `~/set_expected_frequency` |
| 55 | +- Timer callback (1Hz) triggers diagnostics publishing for all monitored topics |
| 56 | + |
| 57 | +**MessageDiagnostics Library** (`message_diagnostics.hpp`): |
| 58 | +- Header-only C++ library for calculating and publishing diagnostics |
| 59 | +- Tracks frame rate using both node clock and message timestamps |
| 60 | +- Uses rolling window filtering (default 300 messages) for smoothing |
| 61 | +- Calculates jitter, dropped frames, and latency metrics |
| 62 | +- Can be integrated directly into custom nodes to avoid subscription overhead |
| 63 | +- Thread-safe with mutex protection for updateDiagnostics() and publishDiagnostics() |
| 64 | + |
| 65 | +**UI Adaptor**: |
| 66 | +- `ncurses_frontend.py` - Lightweight terminal dashboard |
| 67 | +- Subscribes to `/diagnostics` topic and calls services to manage monitoring |
| 68 | +- `ui_adaptor.py` provides GreenwaveUiAdaptor class for building custom UIs |
| 69 | + |
| 70 | +Third-party UIs (like r2s_gw, available as a separate package) can integrate by subscribing to the `/diagnostics` topic and using the ManageTopic and SetExpectedFrequency services. |
| 71 | + |
| 72 | +### Diagnostics Message Format |
| 73 | + |
| 74 | +Greenwave follows Isaac ROS NITROS diagnostic conventions with these KeyValue fields: |
| 75 | +- `frame_rate_node` - Hz calculated from node clock |
| 76 | +- `frame_rate_msg` - Hz calculated from message timestamps |
| 77 | +- `current_delay_from_realtime_ms` - Latency (or "N/A" if unavailable) |
| 78 | +- `num_jitter_outliers_node/msg` - Count of deadline misses |
| 79 | +- `max_abs_jitter_node/msg` - Maximum jitter observed (µs) |
| 80 | +- `mean_abs_jitter_node/msg` - Average jitter (µs) |
| 81 | +- `total_dropped_frames` - Total frames dropped |
| 82 | + |
| 83 | +This format allows NITROS-enabled nodes to be monitored without greenwave_monitor subscribing (when `ENABLE_GLOBAL_NITROS_DIAGNOSTICS=1` is set). |
| 84 | + |
| 85 | +### Latency Calculation |
| 86 | + |
| 87 | +Latency is computed as: `current_time - message.header.stamp` |
| 88 | + |
| 89 | +**Important constraints:** |
| 90 | +- Message type must have a `std_msgs/Header` field |
| 91 | +- Message type must be registered in `known_header_types` map in greenwave_monitor.cpp:has_header_from_type() |
| 92 | +- Header timestamp must use epoch time (not boottime) |
| 93 | + |
| 94 | +If conditions aren't met, latency shows as "N/A". Commonly supported types include sensor_msgs/Image, sensor_msgs/CameraInfo, etc. |
| 95 | + |
| 96 | +**To add support for new message types:** |
| 97 | +1. Edit `greenwave_monitor.cpp::has_header_from_type()` |
| 98 | +2. Add entry to `known_header_types` map with type name and header offset |
| 99 | +3. Example: `{"sensor_msgs/msg/Image", {0, true}}` |
| 100 | + |
| 101 | +## Service API |
| 102 | + |
| 103 | +**ManageTopic.srv** - Add/remove topics from monitoring: |
| 104 | +``` |
| 105 | +string topic_name |
| 106 | +bool add_topic # true=add, false=remove |
| 107 | +--- |
| 108 | +bool success |
| 109 | +string message |
| 110 | +``` |
| 111 | + |
| 112 | +**SetExpectedFrequency.srv** - Configure expected rate and tolerances: |
| 113 | +``` |
| 114 | +string topic_name |
| 115 | +float64 expected_hz |
| 116 | +float64 tolerance_percent # e.g., 5.0 = 5% |
| 117 | +bool clear_expected # reset to no expectation |
| 118 | +bool add_topic_if_missing # auto-add topic if not monitored |
| 119 | +--- |
| 120 | +bool success |
| 121 | +string message |
| 122 | +``` |
| 123 | + |
| 124 | +## Contributing |
| 125 | + |
| 126 | +All commits must be signed off using `git commit -s` to certify the Developer Certificate of Origin (DCO). This indicates you have rights to submit the contribution under the Apache 2.0 license. |
| 127 | + |
| 128 | +CI automatically runs on PRs. For local testing across ROS distributions, use: |
| 129 | +```bash |
| 130 | +scripts/docker-test.sh |
| 131 | +``` |
| 132 | + |
| 133 | +## Code Style |
| 134 | + |
| 135 | +- C++17 standard (enforced in CMakeLists.txt) |
| 136 | +- Python code must pass flake8, pep257, and copyright tests |
| 137 | +- Use ament_lint_auto for automatic style checking |
| 138 | +- Copyright header required: Apache 2.0 license with NVIDIA CORPORATION & AFFILIATES |
0 commit comments