diff --git a/Firmware/MotorControl/axis.cpp b/Firmware/MotorControl/axis.cpp index 80987c00..41a125e4 100644 --- a/Firmware/MotorControl/axis.cpp +++ b/Firmware/MotorControl/axis.cpp @@ -28,7 +28,7 @@ Axis::Axis(const AxisHardwareConfig_t& hw_config, trap_.axis_ = this; decode_step_dir_pins(); - update_watchdog_settings(); + watchdog_feed(); } static void step_cb_wrapper(void* ctx) { @@ -89,21 +89,6 @@ void Axis::decode_step_dir_pins() { dir_pin_ = get_gpio_pin_by_pin(config_.dir_gpio_pin); } -// @brief: Setup the watchdog reset value from the configuration watchdog timeout interval. -void Axis::update_watchdog_settings() { - - if(config_.watchdog_timeout <= 0.0f) { // watchdog disabled - watchdog_reset_value_ = 0; - } else if(config_.watchdog_timeout >= UINT32_MAX / (current_meas_hz+1)) { //overflow! - watchdog_reset_value_ = UINT32_MAX; - } else { - watchdog_reset_value_ = static_cast(config_.watchdog_timeout * current_meas_hz); - } - - // Do a feed to avoid instant timeout - watchdog_feed(); -} - // @brief (de)activates step/dir input void Axis::set_step_dir_active(bool active) { if (active) { @@ -159,16 +144,16 @@ bool Axis::do_updates() { // @brief Feed the watchdog to prevent watchdog timeouts. void Axis::watchdog_feed() { - watchdog_current_value_ = watchdog_reset_value_; + watchdog_current_value_ = get_watchdog_reset(); } -// @brief Check the watchdog timer for expiration. Also sets the watchdog error bit if expired. +// @brief Check the watchdog timer for expiration. Also sets the watchdog error bit if expired. bool Axis::watchdog_check() { - // reset value = 0 means watchdog disabled. - if(watchdog_reset_value_ == 0) return true; + // reset value = 0 means watchdog disabled. + if (get_watchdog_reset() == 0) return true; // explicit check here to ensure that we don't underflow back to UINT32_MAX - if(watchdog_current_value_ > 0) { + if (watchdog_current_value_ > 0) { watchdog_current_value_--; return true; } else { diff --git a/Firmware/MotorControl/axis.hpp b/Firmware/MotorControl/axis.hpp index 9e78fdab..e72f7232 100644 --- a/Firmware/MotorControl/axis.hpp +++ b/Firmware/MotorControl/axis.hpp @@ -181,6 +181,10 @@ public: bool run_closed_loop_control_loop(); bool run_idle_loop(); + constexpr uint32_t get_watchdog_reset() { + return static_cast(std::clamp(config_.watchdog_timeout, 0, UINT32_MAX / (current_meas_hz + 1)) * current_meas_hz); + } + void run_state_machine_loop(); const AxisHardwareConfig_t& hw_config_; @@ -212,7 +216,6 @@ public: LockinState_t lockin_state_ = LOCKIN_STATE_INACTIVE; // watchdog - uint32_t watchdog_reset_value_ = 0; //computed from config_.watchdog_timeout in update_watchdog_settings() uint32_t watchdog_current_value_= 0; // Communication protocol definitions @@ -232,8 +235,7 @@ public: make_protocol_property("startup_sensorless_control", &config_.startup_sensorless_control), make_protocol_property("enable_step_dir", &config_.enable_step_dir), make_protocol_property("counts_per_step", &config_.counts_per_step), - make_protocol_property("watchdog_timeout", &config_.watchdog_timeout, - [](void* ctx) { static_cast(ctx)->update_watchdog_settings(); }, this), + make_protocol_property("watchdog_timeout", &config_.watchdog_timeout), make_protocol_property("step_gpio_pin", &config_.step_gpio_pin, [](void* ctx) { static_cast(ctx)->decode_step_dir_pins(); }, this), make_protocol_property("dir_gpio_pin", &config_.dir_gpio_pin,