From 2ea3dc746771a2875a938941267b160754620d8e Mon Sep 17 00:00:00 2001 From: lgv Date: Thu, 26 Nov 2026 17:50:55 +0800 Subject: [PATCH] fix(ethercat): skip expired cycle deadlines and report timing --- .../include/ethercat_motor_bus_runtime.h | 4 +- .../src/ethercat_motor_bus_runtime.cpp | 69 +++++++++++++++++-- 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/cmvr-es/devices/motor/bus_runtime/ethercat/include/ethercat_motor_bus_runtime.h b/cmvr-es/devices/motor/bus_runtime/ethercat/include/ethercat_motor_bus_runtime.h index 2b4a57ed..467ae0f2 100644 --- a/cmvr-es/devices/motor/bus_runtime/ethercat/include/ethercat_motor_bus_runtime.h +++ b/cmvr-es/devices/motor/bus_runtime/ethercat/include/ethercat_motor_bus_runtime.h @@ -158,11 +158,13 @@ private: unsigned int al_state{0}; }; + struct CycleTiming; + bool configureSlave_(SlaveRuntime& slave); bool configureDc_(); bool waitSlavesOperational_(); void cyclicLoop_(); - void monitorBusHealth_(); + void monitorBusHealth_(const CycleTiming& timing); void readFeedbackLocked_(); void writeCommandsLocked_(); void releaseMaster_(); diff --git a/cmvr-es/devices/motor/bus_runtime/ethercat/src/ethercat_motor_bus_runtime.cpp b/cmvr-es/devices/motor/bus_runtime/ethercat/src/ethercat_motor_bus_runtime.cpp index d678d2c0..b27bc454 100644 --- a/cmvr-es/devices/motor/bus_runtime/ethercat/src/ethercat_motor_bus_runtime.cpp +++ b/cmvr-es/devices/motor/bus_runtime/ethercat/src/ethercat_motor_bus_runtime.cpp @@ -15,6 +15,38 @@ namespace cmvr::device { +namespace { + +using SteadyClock = std::chrono::steady_clock; + +constexpr std::uint64_t skipExpiredCycleDeadlines(SteadyClock::time_point finished_at, + SteadyClock::duration period, + SteadyClock::time_point& next_cycle) +{ + if (next_cycle > finished_at) { + return 0; + } + const auto missed = 1 + (finished_at - next_cycle) / period; + // Give the last PDO send a full period before processing the next cycle. + next_cycle = finished_at + period; + return static_cast(missed); +} + +auto toMicroseconds(SteadyClock::duration duration) +{ + return std::chrono::duration_cast(duration).count(); +} + +} // namespace + +struct EthercatMotorBusRuntime::CycleTiming { + SteadyClock::duration last_send_interval{}; + SteadyClock::duration min_send_interval{SteadyClock::duration::max()}; + SteadyClock::duration max_send_interval{}; + SteadyClock::duration max_wake_lateness{}; + std::uint64_t missed_deadlines{0}; +}; + EthercatMotorBusRuntime::~EthercatMotorBusRuntime() { stop(); @@ -538,11 +570,17 @@ void EthercatMotorBusRuntime::cyclicLoop_() : std::chrono::milliseconds(0); std::uint32_t dc_sync_counter = 0; bool dc_monitor_queued = false; - auto cycle_time = std::chrono::steady_clock::now(); + auto cycle_time = SteadyClock::now(); auto next_dc_monitor_time = cycle_time + dc_monitor_period; const auto bus_monitor_period = std::chrono::milliseconds(100); auto next_bus_monitor_time = cycle_time; + constexpr auto timing_log_period = std::chrono::seconds(1); + auto next_timing_log = cycle_time + timing_log_period; + SteadyClock::time_point last_send_at{}; + CycleTiming timing; while (running_.load()) { + const auto wake_lateness = SteadyClock::now() - cycle_time; + timing.max_wake_lateness = std::max(timing.max_wake_lateness, wake_lateness); if (dc_enabled) { ecrt_master_application_time(master_, timePointNs_(cycle_time)); } @@ -590,21 +628,40 @@ void EthercatMotorBusRuntime::cyclicLoop_() } while (cycle_time >= next_dc_monitor_time); } } + const auto send_at = SteadyClock::now(); ecrt_master_send(master_); + if (last_send_at != SteadyClock::time_point{}) { + timing.last_send_interval = send_at - last_send_at; + timing.min_send_interval = std::min(timing.min_send_interval, timing.last_send_interval); + timing.max_send_interval = std::max(timing.max_send_interval, timing.last_send_interval); + } + last_send_at = send_at; if (health_monitor_enabled_.load() && cycle_time >= next_bus_monitor_time) { - monitorBusHealth_(); + monitorBusHealth_(timing); do { next_bus_monitor_time += bus_monitor_period; } while (cycle_time >= next_bus_monitor_time); } + if (send_at >= next_timing_log && timing.min_send_interval != SteadyClock::duration::max()) { + CMVR_LOG(DEBUG) << "[EthercatMotorBusRuntime] cyclic timing: " << id_ + << ", period_us=" << period.count() + << ", min_send_interval_us=" << toMicroseconds(timing.min_send_interval) + << ", max_send_interval_us=" << toMicroseconds(timing.max_send_interval) + << ", max_wake_lateness_us=" << toMicroseconds(timing.max_wake_lateness) + << ", missed_deadlines=" << timing.missed_deadlines; + timing = {}; + next_timing_log = send_at + timing_log_period; + } + cycle_time += period; + timing.missed_deadlines += skipExpiredCycleDeadlines(SteadyClock::now(), period, cycle_time); std::this_thread::sleep_until(cycle_time); } } -void EthercatMotorBusRuntime::monitorBusHealth_() +void EthercatMotorBusRuntime::monitorBusHealth_(const CycleTiming& timing) { if (!master_ || !domain_) { healthy_.store(false); @@ -685,7 +742,11 @@ void EthercatMotorBusRuntime::monitorBusHealth_() << ", master_al_states=" << current.master_al_states << ", domain_result=" << current.domain_result << ", wc_state=" << current.wc_state - << ", working_counter=" << current.working_counter; + << ", working_counter=" << current.working_counter + << ", last_send_interval_us=" << toMicroseconds(timing.last_send_interval) + << ", max_send_interval_us=" << toMicroseconds(timing.max_send_interval) + << ", max_wake_lateness_us=" << toMicroseconds(timing.max_wake_lateness) + << ", missed_deadlines=" << timing.missed_deadlines; } else if (changed && last_bus_health_.initialized && current.healthy) { CMVR_LOG(INFO) << "[EthercatMotorBusRuntime] bus communication recovered: " << id_