From: David Woodhouse Date: Sun, 9 Mar 2025 10:26:28 +0000 (+0000) Subject: Fix up "production" motor direction and GPIO for reed switch X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=6244a75f4e8e00871bf17276ee7cda526e86cf46;p=users%2Fdwmw2%2Fesp32-pool.git Fix up "production" motor direction and GPIO for reed switch --- diff --git a/blind.yaml b/blind.yaml index c9ae422..b8285b4 100644 --- a/blind.yaml +++ b/blind.yaml @@ -190,6 +190,11 @@ globals: restore_value: no initial_value: "false" + - id: need_rehome_ + type: bool + restore_value: no + initial_value: "true" + stepper: - platform: tmc2209 id: motor @@ -210,7 +215,8 @@ stepper: args: [ "id(encoder)->get_state()", "id(motor)->current_direction", "cover_operation_to_str(id(pd_blinds)->current_operation)" ] - lambda: | - if (id(motor).current_direction < 0) { + if (id(motor).current_direction == -(${motor_direction})) { + id(need_rehome_) = false; if (id(pd_blinds)->current_operation == COVER_OPERATION_IDLE) { id(sensored_home_pos) = id(encoder)->get_state(); } else { @@ -237,7 +243,7 @@ button: on_press: - lambda: | id(motor).set_max_speed(${max_speed}/2); - id(motor).set_target(id(motor)->current_position - 2.5 * ${encoder_closed_pos}); + id(motor).set_target(id(motor)->current_position - 2.5 * ${encoder_closed_pos} * ${motor_direction}); - platform: template name: Stop @@ -296,7 +302,7 @@ binary_sensor: format: "Blind rehome" - lambda: | id(motor).set_max_speed(${max_speed}/2); - id(motor).set_target(id(motor)->current_position - 2.5 * ${encoder_closed_pos}); + id(motor).set_target(id(motor)->current_position - 2.5 * ${encoder_closed_pos} * ${motor_direction}); - platform: gpio name: Button 3 @@ -315,10 +321,10 @@ binary_sensor: name: Reed Switch id: reedsw pin: - number: 14 + number: 7 mode: input: true - pullup: true + pullup: false inverted: true filters: - delayed_on_off: 10ms @@ -330,9 +336,15 @@ binary_sensor: "cover_operation_to_str(id(pd_blinds)->current_operation)" ] - lambda: | // If we hit the reed switch while travelling upwards, stop the motor and reset the home position. - if (id(motor).current_direction < 0) { + if (id(motor).current_direction == -(${motor_direction})) { id(motor).stop(); - id(sensored_home_pos) = id(encoder)->get_state(); + id(need_rehome_) = false; + if (id(pd_blinds)->current_operation == COVER_OPERATION_IDLE) { + id(sensored_home_pos) = id(encoder)->get_state(); + } else { + // Reset home once the tension is released and the encoder is idle + id(home_resetting_) = true; + } } on_release: - logger.log: @@ -450,19 +462,23 @@ cover: - stepper.stop: motor open_action: - lambda: |- + if (id(need_rehome_)) { + id(home).press(); + } else { int32_t wantpos = id(sensored_home_pos); int32_t posdelta = wantpos - id(encoder)->get_state(); - int32_t stepdelta = ((posdelta * 200 * ${microsteps} * ${encoder_direction}) + 2048) / 4096; + int32_t stepdelta = ((posdelta * 200 * ${microsteps} * ${motor_direction}) + 2048) / 4096; ESP_LOGI("cover", "From %f to open (%f to %d) is delta %d, %d steps. Step %d -> %d", id(pd_blinds)->position, id(encoder)->get_state(), wantpos, posdelta, stepdelta, id(motor)->current_position, id(motor)->current_position + stepdelta); id(home_resetting_) = false; id(motor).set_target(id(motor)->current_position + stepdelta); + } close_action: - lambda: |- int32_t wantpos = id(sensored_home_pos) + ${encoder_closed_pos}; int32_t posdelta = wantpos - id(encoder)->get_state(); - int32_t stepdelta = ((posdelta * 200 * ${microsteps} * ${encoder_direction}) + 2048) / 4096; + int32_t stepdelta = ((posdelta * 200 * ${microsteps} * ${motor_direction}) + 2048) / 4096; ESP_LOGI("cover", "From %f to closed (%f to %d) is delta %d, %d steps. Step %d -> %d", id(pd_blinds)->position, id(encoder)->get_state(), wantpos, posdelta, stepdelta, id(motor)->current_position, id(motor)->current_position + stepdelta); @@ -472,7 +488,7 @@ cover: - lambda: |- int32_t wantpos = id(sensored_home_pos) + (1.0 - pos) * ${encoder_closed_pos}; int32_t posdelta = wantpos - id(encoder)->get_state(); - int32_t stepdelta = ((posdelta * 200 * ${microsteps} * ${encoder_direction}) + 2048) / 4096; + int32_t stepdelta = ((posdelta * 200 * ${microsteps} * ${motor_direction}) + 2048) / 4096; ESP_LOGI("cover", "From %f to %f (%f to %d) is delta %d, %d steps. Step %d -> %d", id(pd_blinds)->position, pos, id(encoder)->get_state(), wantpos, posdelta, stepdelta, id(motor)->current_position, id(motor)->current_position + stepdelta); diff --git a/landing-blind-3.yaml b/landing-blind-3.yaml index 3ad0cab..4755082 100644 --- a/landing-blind-3.yaml +++ b/landing-blind-3.yaml @@ -2,7 +2,7 @@ substitutions: name: "landing-blind-3" encoder_closed_pos: "230000" # full blinds length (in encoder counts) !! CHANGE TO SUIT YOUR SETUP !! - encoder_direction: "1" + motor_direction: "1" microsteps: "2" run_current: "100m" max_speed: "800"