From 41db350f5c2022916b81cdc8b473b262f1fdcc46 Mon Sep 17 00:00:00 2001 From: Artem Sheremet Date: Fri, 22 May 2026 22:29:42 +0000 Subject: [PATCH] More robust partial door closing mechanism --- components/ratgdo/ratgdo.cpp | 38 ++++++++++++++++++++++++++---------- components/ratgdo/ratgdo.h | 2 ++ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/components/ratgdo/ratgdo.cpp b/components/ratgdo/ratgdo.cpp index f79f234..2826eda 100644 --- a/components/ratgdo/ratgdo.cpp +++ b/components/ratgdo/ratgdo.cpp @@ -162,12 +162,18 @@ void RATGDOComponent::received(const DoorState door_state) } this->cancel_position_sync_callbacks(); this->cancel_timeout(TIMEOUT_DOOR_QUERY_STATE); + this->target_position_ = DOOR_POSITION_UNKNOWN; + this->target_direction_ = DoorAction::UNKNOWN; } else if (door_state == DoorState::OPEN) { this->door_position = 1.0; this->cancel_position_sync_callbacks(); + this->target_position_ = DOOR_POSITION_UNKNOWN; + this->target_direction_ = DoorAction::UNKNOWN; } else if (door_state == DoorState::CLOSED) { this->door_position = 0.0; this->cancel_position_sync_callbacks(); + this->target_position_ = DOOR_POSITION_UNKNOWN; + this->target_direction_ = DoorAction::UNKNOWN; } if (door_state == DoorState::CLOSED && door_state != prev_door_state) { @@ -250,8 +256,26 @@ void RATGDOComponent::door_position_update() return; } auto position = this->door_start_position + (now - this->door_start_moving) / (1000 * duration); + position = clamp(position, 0.0f, 1.0f); ESP_LOG2(TAG, "[%d] Position update: %f", now, position); - this->door_position = clamp(position, 0.0f, 1.0f); + this->door_position = position; + + // Check if we reached our move-to-position target + if (this->target_position_ != DOOR_POSITION_UNKNOWN && this->target_direction_ != DoorAction::UNKNOWN) { + bool reached = false; + if (this->target_direction_ == DoorAction::OPEN && position >= this->target_position_) { + reached = true; + } else if (this->target_direction_ == DoorAction::CLOSE && position <= this->target_position_) { + reached = true; + } + + if (reached) { + ESP_LOGD(TAG, "Reached target position %.2f, stopping door", this->target_position_); + this->door_stop(); + this->target_position_ = DOOR_POSITION_UNKNOWN; + this->target_direction_ = DoorAction::UNKNOWN; + } + } } void RATGDOComponent::set_opening_duration(float duration) @@ -451,16 +475,10 @@ void RATGDOComponent::door_move_to_position(float position) ESP_LOGD(TAG, "Moving to position %.2f (target duration %.1fs)", position, operation_time / 1000.0); - // Wait for the door to actually start moving before starting the stop timer. - // This makes the timing independent of any smart fallback delays. - this->on_door_state([this, operation_time](DoorState s) { - if (s == DoorState::OPENING || s == DoorState::CLOSING) { - this->set_timeout(TIMEOUT_MOVE_TO_POSITION, operation_time, - [this] { this->door_action(DoorAction::STOP); }); - } - }); + this->target_position_ = position; + this->target_direction_ = (delta > 0 ? DoorAction::OPEN : DoorAction::CLOSE); - this->smart_door_action(delta > 0 ? DoorAction::OPEN : DoorAction::CLOSE); + this->smart_door_action(this->target_direction_); } void RATGDOComponent::cancel_position_sync_callbacks() diff --git a/components/ratgdo/ratgdo.h b/components/ratgdo/ratgdo.h index 44c691c..7968c31 100644 --- a/components/ratgdo/ratgdo.h +++ b/components/ratgdo/ratgdo.h @@ -71,6 +71,8 @@ public: float door_start_position { DOOR_POSITION_UNKNOWN }; float door_move_delta { DOOR_DELTA_UNKNOWN }; uint16_t position_sync_remaining_ { 0 }; + float target_position_ { DOOR_POSITION_UNKNOWN }; + DoorAction target_direction_ { DoorAction::UNKNOWN }; single_observable light_state { LightState::UNKNOWN }; single_observable lock_state { LockState::UNKNOWN };