joshr120 / PD-Stepper

GNU General Public License v3.0
336 stars 29 forks source link

ESPHome examples using custom tmc2209 component. #12

Closed slimcdk closed 6 days ago

joshr120 commented 6 days ago

Think my previous problem was partially caused by the stall detection being triggered when the motor slowed down. This caused the home position to be reset.

I also found a problem with your handling of wrap around on the as5600 sensor. The encoder_diff calculation was only correct in one direction. I replaced the lambda with the following and seems to be working correctly now:

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 (went under 0)
} else if (curr < 1000 && prev > 3000) {
  id(encoder_tracking_)[1] += (4095 - prev + curr); // crossed zero counterclockwise (went over 4095)
} else {
  id(encoder_tracking_)[1] += (curr - prev);
}
id(encoder_tracking_)[0] = curr;
return id(encoder_tracking_)[1];

I also added the following to the on_boot so that home is the start position:

- delay: 0.5s
- lambda: id(sensored_home_pos) = id(encoder)->get_state();  # Set the initial encoder value to sensored_home_pos

I imagine your idea of not having this was so that it could home itself on boot using the stall guard? However this does not work for all use cases (such as when using a large gear ratio or when blinds can move past the open/close position)

slimcdk commented 6 days ago

Correct, false stalls detections can occur if the parameters hasn't been dialed in for the specific application, motor etc. The values I provided are for my setup which runs without false triggers.

Good catch! I must have been sleeply when I submitted this as it appear that I have submitted parts of my testing config and not a final one 🙈 Sorry!

I have a corrected version of the encoder logic, but yours is a little cleaner to read, so I won't bother including mine. Instead of setting the homed position on boot, it might actually be better to save the value to flash and restore it after boot. The homed position can't be trusted before an actual homing is completed.

Thanks for taking a look at it!

slimcdk commented 6 days ago

Working configs are now in examples for ESPHome, so I'll close this PR