The user wants a design for a "reproducible hardware-in-the-loop (HIL) CI system for robotics."
This is a complex systems engineering task involving:
- Virtualization Strategy: How to mix real hardware with simulated/virtual components.
- Telemetry Capture: How to get data out of the robot/system for analysis.
- Failure Triage: How to figure out why something failed automatically.
- Scheduling: How to manage limited hardware resources in a queue.
Deconstruct the Request:
- Target: Robotics CI (Continuous Integration).
- Type: Hardware-in-the-Loop (HIL). This means real motors, sensors, or compute boards are involved, not just pure software simulation (SIL).
- Key Attributes:
- Reproducible: The same code commit should produce the same result (within physical tolerances).
- Scalable (implied by Scheduling): Multiple developers, limited hardware.
Section 1: Virtualization Strategy (The "Hybrid" Architecture)
- Challenge: Real hardware is slow, dangerous, and flaky. Virtualization needs to isolate the non-deterministic parts or interface them cleanly.
- Approach: Containerization (Docker) for software, clear HAL (Hardware Abstraction Layer), and "Mock" modes for sensors/actuators when hardware isn't strictly needed, but for HIL, we need the real stuff.
- Key Concept: The "Robot Container." The CI runner sits on the host (or a cloud VM) and talks to a physical rig via a gateway.
- Reproducibility trick: Determinism in hardware is impossible due to thermal/noise factors. We need "Approximate Determinism" via fixed seeds for RNGs in logic, and strict time synchronization (PTP/NTP).
- Diagram/Flow:
- Code -> CI Trigger -> Build Docker Image -> Deploy to Edge Device (Robot) -> Execute Test.
Section 2: Telemetry Capture (The "Black Box")
- Challenge: Robots generate massive data (LiDAR, Video, IMU). We can't save everything forever.
- Approach: ROS 2 is the standard. Use
rosbag (MCAP format is modern).
- Strategy:
- Lightweight: Structured logs (JSON/Protobuf) + Metrics (Prometheus) always on.
- Heavyweight: Sensor data (bags) only on failure or sampled.
- Synchronization: Foxglove is a great tool to mention for visualization.
Section 3: Failure Triage (The "AI" or "Rules")
- Challenge: Did the robot fail because the code is bad, the battery died, or the Wi-Fi dropped?
- Approach: Hierarchical check.
- Level 1: Infrastructure. Is the rig online?
- Level 2: Safety/Estops. Did we hit the E-stop or a virtual fence?
- Level 3: Functional. Did the test assertion pass (e.g.,
distance_traveled > 5m)?
- Level 4: Regression/Flakiness. Compare against "Golden Master" results.
- Tools: Allure reporting, custom Python scripts parsing logs.
Section 4: Scheduling (The "Traffic Control")
- Challenge: Hardware is scarce. If two devs push code to
main, they can't both use the one arm.
- Approach: Resource locks.
- Tools: GitLab CI (tags/runner locks), Jenkins (lockable resources plugin), or a dedicated "Test Orchestrator" (like ROS 2 Test Farm or TMT/Testify).
- Queueing:
- Fast (Unit/Regression) -> High Priority.
- Slow (Full System) -> Nightly/Weekly.
- "Lazy" Queuing -> If the rig is free, run the PR.
Synthesizing the Solution Structure:
- High-Level Architecture Diagram (Mental): Developer -> VCS -> CI Orchestrator -> Physical Test Rig -> Artifact Storage.
- Core Components:
- Virtualization: Docker + ROS 2 + Hardware Interface layers.
- Telemetry: Foxglove + MCAP + Prometheus.
- Triage: Automated classification (Hardware vs. Software vs. Flaky).
- Scheduling: Lockable resources + Prioritization.
Refining the Content - Specific Technologies:
- ROS 2: Standard for robotics. Needs to be the backbone.
- Docker/Kubernetes: K3s or MicroK8s at the edge is popular for managing agents.
- Bag format: MCAP is better than SQlite3 for CI (streaming).
- Visualizer: Foxglove Studio (industry standard now).
- Hardware Management: Nutty (network UPS tools) for power cycling, network switches with PoE for resetting rigs remotely.
Drafting the Response:
- Introduction: Define the goals (Speed vs. Reality vs. Reproducibility).
- 1. Virtualization Strategy: "The Skinny Edge." Don't virtualize the sensors; virtualize the control plane. Use containers for isolation. Mention the "Hardware Interface" pattern.
- 2. Telemetry: "Tiered Logging." Don't dump video on success. Sync timestamps!
- 3. Failure Triage: "The blame game." Is it the code, the rig, or the test? Use specific metrics (variance analysis).
- 4. Scheduling: "Resource Mutex." Exclusive access.
- 5. Architecture Diagram (Text-based).
Self-Correction during drafting:
- Initial thought: Just run a simulation.
- Correction: User asked for Hardware-in-the-Loop. Must emphasize physical rig management (power cycling, physical reset).
- Reproducibility issue: Real hardware is non-deterministic. I need to address "Flakiness" mitigation (retry logic, averaging results) rather than promising perfect determinism.
- Triage: Needs to be automated. A human shouldn't look at a log to see if the motor stalled. Check current draw limits.
Final Polish: Use professional terminology (HAL, OTA, Determinism, Soft Real-time). Structure with clear headings and bullet points. Add a "Tech Stack" summary.
Structure Check:
- Virtualization Strategy: Hardware Abstraction, Containerization.
- Telemetry Capture: ROS 2 Bags, Metrics, Time Sync.
- Failure Triage: Classification, Auto-Retry, Visual Diffing.
- Scheduling: Resource Locking, Priority Queueing.
Let's write it out.
Designing a Hardware-in-the-Loop (HIL) CI system for robotics requires bridging the gap between deterministic software testing and the stochastic nature of physical hardware. The goal is to automate the validation of firmware/control stacks against physical actuators and sensors, ensuring reproducibility despite environmental noise.
Below is a comprehensive design for a reproducible HIL CI architecture.
1. High-Level Architecture
The system follows a "Railcar" model: The CI Orchestrator (Cloud/VPS) acts as the dispatcher, while the physical lab acts as the track. The lab contains a "Test Rig Controller" (a robust x86/ARM PC) connected to the Device Under Test (DUT) via industrial buses (EtherCAT, CAN-FD, RS485).
- CI Orchestrator: GitHub Actions / GitLab CI / Jenkins.
- Test Rig Controller: Runs the agent, HIL interface drivers, and data ingestion.
- DUT: The robotic subsystem (motor controller, full robot, sensor array).
- Stimulus/Load: Dynamometers, mock sensor injectors, or linear actuators to physically load the DUT.
2. Virtualization Strategy: "Containerized Logic, Bare-Metal IO"
Reproducibility in robotics HIL requires isolation of the software stack from the hardware drivers.
The Strategy: Hardware Abstraction Layer (HAL) + Docker
You cannot easily virtualize GPIO or USB latency. Therefore, we virtualize the logic and keep the drivers on the bare metal OS, bridging them via standardized interfaces.
- The "Universal HAL": Define a hardware interface standard (e.g.,
sensor_msgs in ROS 2, or a custom Protobuf/gRPC definition). The Test Rig Controller runs a lightweight "HAL Service" (native, root privileges) that talks to hardware.
- The "Logic Container": The control software runs inside a Docker container. It does not know it is in a test rig; it simply sends commands to the HAL Service via a high-speed local network bridge (UDP/Shared Memory).
- Mock vs. Real Modes:
- Mock Mode: The Logic Container runs in the cloud. The HAL Service is replaced by a Physics Simulator (e.g., Gazebo/PyBullet) running inside the container.
- HIL Mode: The Logic Container runs on the Rig Controller. The HAL Service talks to physical drivers.
- Reproducibility Factor: The Logic Container is identical in both modes; only the endpoint configuration changes.
Reset State Mechanism
To ensure reproducibility, the hardware must return to a known state before every test.
- Hard Reset: The Rig Controller controls a network-connected Power Distribution Unit (PDU) or relay bank to power cycle the DUT.
- Mechanical Reset: For kinematic tests, the rig must command the DUT to a "Home" position before the test logic starts.
3. Telemetry Capture: The "Black Box" Recorder
In HIL, the logs are often more valuable than the pass/fail result because they explain why a physical interaction failed.
Time Synchronization
- PTP (Precision Time Protocol): Use
linuxptp to synchronize the Rig Controller and the DUT (if it runs Linux) to sub-microsecond accuracy.
- Clock Domain Bridging: If the DUT is a microcontroller (STM32/ESP32) using a different clock, the Rig Controller must inject timestamps into the serial/CAN stream to align data.
Data Strategy: The MCAP Format
Use MCAP (Modular Container for Associated Protocols) as the storage format. It is optimized for robotics (rosbag v2 alternative).
- Channels: Record raw bus data (CAN frames, Serial bytes) and decoded high-level data (Twist messages, Joint States).
- Video: Use a cheap HDMI capture stick to record the HMI or diagnostic screen into the MCAP file as a compressed stream.
Storage & Retention
- Hot Storage: High-speed NVMe on the Rig Controller for active recording.
- Cold Storage: Post-test, upload MCAP files to S3/R2.
- Smart Pruning: If the test passes, downsample the telemetry to 1Hz and delete video. If the test fails, keep 100% resolution.
4. Failure Triage: Automated Forensics
Hardware fails differently than software. A software regression looks like a logic error; a hardware failure looks like noise, drift, or timeout. The system must distinguish these to avoid false negatives.
The Triage Pipeline
-
Infrastructure Health Check:
- Did the DUT lose power unexpectedly? (Check PDU logs).
- Did the Rig Controller lose network? (Ping heartbeat logs).
- Action: If infrastructure failed, mark CI job as "Unstable/Infrastructure Error" and auto-retry.
-
Safety/Estop Monitoring:
- Did the test trigger an over-current or collision event?
- Action: Capture the last 5 seconds of high-frequency IMU/Current data. Flag as "Safety Violation".
-
Deterministic Replay (The "Golden Master"):
- Record the input commands generated by the CI test during the first successful run (the "Golden Run").
- In subsequent runs, feed the exact same command timestamps to the DUT.
- Compare the DUT's response (position/velocity) against the Golden Run using a dynamic time warping algorithm or tolerance bands (e.g., ±5% deviation allowed).
- Action: If deviation exceeds bounds, flag as "Performance Regression" (mechanical wear or control tune issue).
Reporting
Use Foxglove or PlotJuggler integrated into the CI review interface. Generate a static HTML report with graphs of the failure point so developers can debug without downloading 2GB of logs.
5. Scheduling: Resource Concurrency
Hardware is a finite resource. You cannot spin up 50 physical robots in parallel like Docker containers.
Architecture: Resource-Locked Runners
Do not use generic cloud runners. Use Self-Hosted Runners located in the lab.
Tag-Based Locking
- Rig Registration: Register each physical rig as a runner with specific tags (e.g.,
hil, differential-drive, lidar-vlp16).
- Mutex Queue: The CI system must implement a "Concurrency Group" based on the physical rig ID.
- GitLab CI Example:
resource_group: rig_01_lock
- This ensures that if Commit A and Commit B are pushed rapidly, Commit B waits for Commit A to release the physical robot.
Priority Scheduling
Implement a priority queue in your CI orchestrator:
- Critical (Main Branch): Immediate execution. Can preempt/abort long-running nightly tests if necessary (via safe stop signals).
- Nightly (Regression): Long-duration endurance tests.
- PR Verification: Fast "Smoke Tests" (e.g., boot, check sensors, move 10cm, stop).
Summary of Tech Stack
| Component | Technology Recommendation |
|---|
| CI Orchestrator | GitLab CI (best for self-hosted resource locking) or Jenkins. |
| Container Runtime | Docker (with --privileged mode or specific device mapping). |
| Middleware | ROS 2 (DDS) or Zenoh (for lighter weight). |
| Data Format | MCAP (Foxglove). |
| Time Sync | PTP (IEEE 1588) or Chrony. |
| Power/Reset | Wi-Fi Smart Plugs (TP-Link Kasa) or industrial Modbus Relays. |
| Visualization | Foxglove Studio (Web-based). |
Reproducibility Checklist (The "Golden Rule")
To ensure a test is truly reproducible, the system must enforce this sequence before every test:
- Power Cycle DUT: Wait for boot sequence completion.
- Thermal Check: Verify ambient temp and DUT temp are within operating range (scrape via CAN/Sysfs).
- Zeroing: Command actuators to hardstops or optical markers to reset odometry.
- Run Test.
- Power Down.