mirror of
https://github.com/joshr120/PD-Stepper.git
synced 2025-11-04 14:49:56 +08:00
Add option to reverse encoder input for motors wired with different pinout
This commit is contained in:
parent
cc3eaf42a6
commit
e455185c5b
@ -68,7 +68,7 @@ esphome:
|
|||||||
id: CFG3_pin
|
id: CFG3_pin
|
||||||
|
|
||||||
- delay: 0.5s
|
- delay: 0.5s
|
||||||
- lambda: id(sensored_home_pos) = id(encoder)->get_state(); # Set the home position to power on position
|
- lambda: id(sensored_home_pos) = id(encoder)->get_state(); # Set the home (open) position to power on position
|
||||||
# - button.press: home
|
# - button.press: home
|
||||||
|
|
||||||
i2c:
|
i2c:
|
||||||
@ -220,6 +220,8 @@ sensor:
|
|||||||
filters:
|
filters:
|
||||||
- delta: 2 # throttle the high polling rate to only act on value changes
|
- delta: 2 # throttle the high polling rate to only act on value changes
|
||||||
# compute absolute position from angle value
|
# compute absolute position from angle value
|
||||||
|
|
||||||
|
######## NORMAL DIRECTION ########
|
||||||
- lambda: |
|
- lambda: |
|
||||||
const uint16_t curr = x; //current encoder value 0-4095
|
const uint16_t curr = x; //current encoder value 0-4095
|
||||||
const uint16_t prev = id(encoder_tracking_)[0]; //previous encoder value 0-4095
|
const uint16_t prev = id(encoder_tracking_)[0]; //previous encoder value 0-4095
|
||||||
@ -232,6 +234,21 @@ sensor:
|
|||||||
}
|
}
|
||||||
id(encoder_tracking_)[0] = curr;
|
id(encoder_tracking_)[0] = curr;
|
||||||
return id(encoder_tracking_)[1];
|
return id(encoder_tracking_)[1];
|
||||||
|
|
||||||
|
######## REVERSE DIRECTION ######## (uncomment this instead of previous function if your motor is moving in reverse relvative to your encoder)
|
||||||
|
# - lambda: |
|
||||||
|
# const uint16_t curr = x; // current encoder value 0-4095
|
||||||
|
# const uint16_t prev = id(encoder_tracking_)[0]; // previous encoder value 0-4095
|
||||||
|
# if (curr > 3000 && prev < 1000) {
|
||||||
|
# id(encoder_tracking_)[1] += (4095 - curr + prev); // crossed zero clockwise (was decreasing, now increasing total)
|
||||||
|
# } else if (curr < 1000 && prev > 3000) {
|
||||||
|
# id(encoder_tracking_)[1] -= (4095 - prev + curr); // crossed zero counterclockwise (was increasing, now decreasing total)
|
||||||
|
# } else {
|
||||||
|
# id(encoder_tracking_)[1] -= (curr - prev);
|
||||||
|
# }
|
||||||
|
# id(encoder_tracking_)[0] = curr;
|
||||||
|
# return id(encoder_tracking_)[1];
|
||||||
|
|
||||||
- multiply: -1.0
|
- multiply: -1.0
|
||||||
- throttle: 250ms # limit the amount of new sensor states from this component
|
- throttle: 250ms # limit the amount of new sensor states from this component
|
||||||
accuracy_decimals: 0
|
accuracy_decimals: 0
|
||||||
@ -275,4 +292,4 @@ cover:
|
|||||||
position: !lambda return (1.0-id(pd_blinds)->position) * ${encoder_closed_pos} * ${stepper_encoder_ratio};
|
position: !lambda return (1.0-id(pd_blinds)->position) * ${encoder_closed_pos} * ${stepper_encoder_ratio};
|
||||||
- stepper.set_target:
|
- stepper.set_target:
|
||||||
id: motor
|
id: motor
|
||||||
target: !lambda return (1.0-pos) * ${encoder_closed_pos} * ${stepper_encoder_ratio};
|
target: !lambda return (1.0-pos) * ${encoder_closed_pos} * ${stepper_encoder_ratio};
|
||||||
Loading…
Reference in New Issue
Block a user