Watchdog Timeout Sizing
Watchdog Timeout Sizing
animation
actuators
Try Leave Safety factor at 1.5, then reduce Worst-case loop time (ms).
Observe The opening recommended timeout is 495 ms. Reducing Worst-case loop time (ms) lowers it.
Explain Worst-case loop time (ms) multiplied by Safety factor produces the timeout. The range control snaps the requested 325 ms to 330 ms, which gives 495 ms at a factor of 1.5.
Technical boundaries. The sizing rule assumes the entered worst-case duration is representative. The output is rounded to whole milliseconds and does not automatically check the separate actuator safety constraint described below.
Watchdog Timeout Sizing
Scenario: An IoT-controlled automated warehouse uses a robotic arm to pick items from shelves. The control loop performs these steps every cycle:
- Read load sensor (5 ms)
- Calculate pick trajectory (20 ms)
- Send motor commands via CAN bus (15 ms)
- Wait for arm position feedback (30 ms)
- Verify grip force sensor (5 ms)
- Log to SD card (10 ms)
Total expected loop time: 85 ms
Question: What watchdog timer (WDT) timeout should you configure to detect control loop hangs without triggering false positives?
Step 1: Account for worst-case execution variance
Real-world timing varies due to: - CAN bus arbitration delays (priority collisions) - SD card write delays (wear leveling, flash erase) - Interrupt handling (network stack, sensor polling)
Measured worst-case loop times: - Typical: 85 ms - 95th percentile: 120 ms - 99th percentile: 180 ms - Worst-case loop (75 ms of other work plus a 250 ms SD card write): 325 ms
Step 2: Add safety margin
Watchdog timeout = Worst-case time × Safety factor
Using 1.5× safety factor: 325 ms × 1.5 = 487.5 ms
Step 3: Validate against actuator safety constraints
If the control loop hangs, what is the maximum safe time before forcing a system reset?
- Robot arm moving at 0.5 m/s
- Collision hazard if control lost for >300 ms (150mm uncontrolled travel)
487.5 ms candidate watchdog timeout exceeds the 300 ms safe-stop limit by 187.5 ms!
Step 4: Redesign control architecture
The 250 ms SD card write is the bottleneck creating the safety conflict. Solution: Decouple logging from critical loop
// BEFORE: Unsafe monolithic loop
void control_loop() {
read_sensors(); // 5ms
calculate_path(); // 20ms
send_motor_commands(); // 15ms
wait_for_feedback(); // 30ms
verify_grip(); // 5ms
log_to_sd(); // 10-250ms <-- PROBLEM!
feed_watchdog();
}
// AFTER: Two-tier architecture
void critical_control_loop() { // Runs every 100ms
read_sensors(); // 5ms
calculate_path(); // 20ms
send_motor_commands(); // 15ms
wait_for_feedback(); // 30ms
verify_grip(); // 5ms
queue_log_event(); // <1ms (just adds to queue)
feed_watchdog(); // WDT timeout = 150ms (100ms × 1.5)
}
void background_logger() { // Runs in lower-priority task
if (log_queue_not_empty()) {
write_to_sd_card(); // 10-250ms (doesn't block control)
}
}Final watchdog configuration:
esp_task_wdt_config_t cfg = { .timeout_ms = 150, .idle_core_mask = 0, .trigger_panic = true };
esp_err_t wdt_result = esp_task_wdt_init(&cfg); // ESP-IDF 5.x; call after the scheduler starts
if (wdt_result == ESP_ERR_INVALID_STATE) {
wdt_result = esp_task_wdt_reconfigure(&cfg); // Already initialized
}
if (wdt_result != ESP_OK) {
// Handle configuration failure; do not claim the watchdog is active.
return;
}
wdt_result = esp_task_wdt_add(NULL); // Add current task to WDT
if (wdt_result != ESP_OK) {
// Handle subscription failure before relying on the watchdog.
return;
}
while (1) {
critical_control_loop(); // Must complete in <150ms
esp_task_wdt_reset(); // Feed watchdog
}Verification: The ideal timer-only travel is 150 ms × 0.5 m/s = 75 mm (within the 150 mm target). This is not a guaranteed stopping distance: measure reset and output latency, and use an independent motion stop.
Key lesson: Watchdog timeout is a safety-critical parameter. Always: 1. Measure real worst-case timing, not theoretical 2. Account for I/O delays (SD, network, sensors) 3. Ensure timeout is shorter than actuator safety constraint 4. Decouple slow non-critical operations from control loop