From 8d0da42deb3c344607f9cfdcc1cb28fb4b31639d Mon Sep 17 00:00:00 2001
From: David Woodhouse <dwmw@amazon.co.uk>
Date: Wed, 12 Feb 2025 22:22:38 +0000
Subject: [PATCH] Go slower when rehoming

---
 blind.yaml           | 15 ++++++++-------
 landing-blind-3.yaml |  4 ++--
 2 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/blind.yaml b/blind.yaml
index 8e9cb15..23abaca 100644
--- a/blind.yaml
+++ b/blind.yaml
@@ -160,7 +160,7 @@ globals:
 stepper:
   - platform: tmc2209
     id: motor
-    max_speed: ${max_speed}
+    max_speed: ${max_speed} steps/s
     acceleration: 100 steps/s^2
     deceleration: 200 steps/s^2
     rsense: 100 mOhm
@@ -202,9 +202,9 @@ button:
     name: Home
     id: home
     on_press:
-      - stepper.set_target:
-          id: motor
-          target: !lambda "return id(motor)->current_position - 2.5 * ${encoder_closed_pos};"
+      - lambda: |
+          id(motor).set_max_speed(${max_speed}/2);
+          id(motor).set_target(id(motor)->current_position - 2.5 * ${encoder_closed_pos});
 
   - platform: template
     name: Stop
@@ -260,9 +260,9 @@ binary_sensor:
           - logger.log:
               level: INFO
               format: "Blind rehome"
-          - stepper.set_target:
-              id: motor
-              target: !lambda "return id(motor)->current_position - 2.5 * ${encoder_closed_pos};"
+          - lambda: |
+              id(motor).set_max_speed(${max_speed}/2);
+              id(motor).set_target(id(motor)->current_position - 2.5 * ${encoder_closed_pos});
 
  - platform: gpio
    name: Button 3
@@ -338,6 +338,7 @@ sensor:
              } else {
                ESP_LOGI("cover", "idle at %d", id(encoder_tracking_)[1]);
              }
+             id(motor)->set_max_speed(${max_speed});
              id(pd_blinds)->current_operation = COVER_OPERATION_IDLE;
              id(pd_blinds)->publish_state(false);
           }
diff --git a/landing-blind-3.yaml b/landing-blind-3.yaml
index ea0ab93..b70ea14 100644
--- a/landing-blind-3.yaml
+++ b/landing-blind-3.yaml
@@ -4,8 +4,8 @@ substitutions:
   encoder_closed_pos: "230000" # full blinds length (in encoder counts) !! CHANGE TO SUIT YOUR SETUP !!
   microsteps: "2"
   run_current: "100m"
-  max_speed: "800 steps/s"
-  stallguard_threshold: "55"
+  max_speed: "800"
+  stallguard_threshold: "60"
 
 packages:
   blind: !include blind.yaml
-- 
2.49.0