]> www.infradead.org Git - users/dwmw2/esp32-pool.git/commitdiff
Fix up "production" motor direction and GPIO for reed switch
authorDavid Woodhouse <dwmw@amazon.co.uk>
Sun, 9 Mar 2025 10:26:28 +0000 (10:26 +0000)
committerDavid Woodhouse <dwmw@amazon.co.uk>
Sun, 9 Mar 2025 10:26:28 +0000 (10:26 +0000)
blind.yaml
landing-blind-3.yaml

index c9ae422f0cb45c1f8173dc95e3272c1d9bab57de..b8285b46ea5d9376d6b0960b5fdfc2e12e5ad057 100644 (file)
@@ -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);
index 3ad0cab32f7a1c64b0eb5326d9b1be8699dc0242..4755082cb87332765c484c9ecbb7b4e67cae879e 100644 (file)
@@ -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"