From e1d86a4f35e6961680a2d543b537935f21024eb1 Mon Sep 17 00:00:00 2001 From: Amol Palshetkar Date: Sat, 14 Nov 2026 15:19:04 +0530 Subject: [PATCH] easycat: clean shutdown, runtime timeouts, feed PDO through SAFE-OP - Handle SIGINT/SIGTERM so main() returns and the socket destructor runs, releasing a TAP slot (otherwise a rerun hits "Socket is full"). - Add --timeout, --rt-timeout and --watchdog arguments (milliseconds, defaulting to the existing 2 / 10 / 100 ms) so a slow software bus over a real NIC can be retuned at runtime without changing the defaults. - Keep process data flowing during the SAFE-OP wait and don't insert slow per-slave state reads before the first cyclic exchange, so the SyncManager watchdog stays fed across the OP transition. --- examples/master/easycat/easycat_example.cc | 50 +++++++++++++++++++--- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/examples/master/easycat/easycat_example.cc b/examples/master/easycat/easycat_example.cc index 2d9a0426..b1caf62a 100644 --- a/examples/master/easycat/easycat_example.cc +++ b/examples/master/easycat/easycat_example.cc @@ -1,3 +1,4 @@ +#include #include #include #include @@ -11,8 +12,20 @@ using namespace kickcat; +// Clean shutdown on Ctrl-C: break the loop so main returns and the socket +// destructor runs, releasing the TAP slot (otherwise the next run sees +// "Socket is full" until the server is restarted). +namespace +{ + volatile std::sig_atomic_t g_running = 1; + void onSignal(int) { g_running = 0; } +} + int main(int argc, char* argv[]) { + std::signal(SIGINT, onSignal); + std::signal(SIGTERM, onSignal); + argparse::ArgumentParser program("easycat_example"); std::string nom_interface_name; @@ -27,6 +40,29 @@ int main(int argc, char* argv[]) .default_value(std::string{""}) .store_into(red_interface_name); + // Timeouts default to hardware values; raise them for a slow software bus + // over a real NIC (network_simulator / Windows pcap). + int read_timeout_ms = 2; + program.add_argument("--timeout") + .help("Link read timeout for enumeration/init, in milliseconds") + .default_value(2) + .scan<'i', int>() + .store_into(read_timeout_ms); + + int rt_timeout_ms = 10; + program.add_argument("--rt-timeout") + .help("Link read timeout in the cyclic loop, in milliseconds") + .default_value(10) + .scan<'i', int>() + .store_into(rt_timeout_ms); + + int watchdog_ms = 100; + program.add_argument("--watchdog") + .help("PDIO watchdog passed to Bus::init, in milliseconds (0 disables)") + .default_value(100) + .scan<'i', int>() + .store_into(watchdog_ms); + try { program.parse_args(argc, argv); @@ -58,7 +94,7 @@ int main(int argc, char* argv[]) }; std::shared_ptr link = std::make_shared(socket_nominal, socket_redundancy, report_redundancy); - link->setTimeout(2ms); + link->setTimeout(std::chrono::milliseconds(read_timeout_ms)); link->checkRedundancyNeeded(); Bus bus(link); @@ -76,7 +112,7 @@ int main(int argc, char* argv[]) try { printf("Initializing Bus...\n"); - bus.init(100ms); // to adapt to your use case + bus.init(std::chrono::milliseconds(watchdog_ms)); printf("Init done\n"); printf("Detected slaves: %zu\n", bus.slaves().size()); @@ -120,8 +156,9 @@ int main(int argc, char* argv[]) { printf("Switching to SAFE_OP...\n"); bus.requestState(State::SAFE_OP); - bus.waitForState(State::SAFE_OP, 1s); - print_current_state(); + // Feed process data through the transition: on a slow software bus the slaves' + // SyncManager watchdog expires without a frame arriving and OP is rejected. + bus.waitForState(State::SAFE_OP, 1s, cyclic_process_data); // Set valid output for all slaves to exit safe op for (auto& slave : bus.slaves()) @@ -136,6 +173,7 @@ int main(int argc, char* argv[]) printf("Switching to OPERATIONAL...\n"); bus.requestState(State::OPERATIONAL); bus.waitForState(State::OPERATIONAL, 1s, cyclic_process_data); + print_current_state(); printf("After OPERATIONAL - Slave info:\n"); for (auto& slave : bus.slaves()) @@ -156,7 +194,7 @@ int main(int argc, char* argv[]) } auto callback_error = [](DatagramState const&){ THROW_ERROR("something bad happened"); }; - link->setTimeout(10ms); // Adapt to your use case (RT loop) + link->setTimeout(std::chrono::milliseconds(rt_timeout_ms)); MailboxSequencer mailbox_sequencer(bus); printf("Running loop...\n"); @@ -164,7 +202,7 @@ int main(int argc, char* argv[]) Timer timer{1ms}; timer.start(); - while (true) + while (g_running) { timer.wait_next_tick();