Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions esphome/components/stepper/stepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace stepper {

static const char *const TAG = "stepper";

void Stepper::calculate_speed_(time_t now = micros()) {
void Stepper::calculate_speed_(uint32_t now = micros()) {
// delta t since last calculation in seconds
float dt = (now - this->last_calculation_) * 1e-6f;
this->last_calculation_ = now;
Expand All @@ -29,7 +29,7 @@ void Stepper::calculate_speed_(time_t now = micros()) {
}
this->current_speed_ = clamp(this->current_speed_, 0.0f, this->max_speed_);
}
Direction Stepper::should_step_(time_t now = micros()) {
Direction Stepper::should_step_(uint32_t now = micros()) {
this->calculate_speed_(now);
if (this->current_speed_ == 0.0f) {
this->current_direction = Direction::STANDSTILL;
Expand Down
12 changes: 8 additions & 4 deletions esphome/components/stepper/stepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,20 @@ class Stepper {
Direction current_direction{Direction::STANDSTILL};

protected:
void calculate_speed_(time_t now);
Direction should_step_(time_t now);
void calculate_speed_(uint32_t now);
Direction should_step_(uint32_t now);
int32_t should_step_();

float acceleration_{1e6f};
float deceleration_{1e6f};
float current_speed_{0.0f};
float max_speed_{1e6f};
time_t last_calculation_{0};
time_t last_step_{0};
// uint32_t (not time_t) so timing deltas of micros() are wrap-safe: micros()
// overflows every ~71.6 min, and unsigned subtraction wraps correctly whereas
// a signed/wider time_t delta goes negative across the wrap. Mirrors upstream
// esphome/components/stepper.
uint32_t last_calculation_{0};
uint32_t last_step_{0};
};

template<typename... Ts> class SetTargetAction : public Action<Ts...> {
Expand Down
6 changes: 4 additions & 2 deletions esphome/components/tmc2209/stepper/tmc2209_stepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void TMC2209Stepper::loop() {
TMC2209Component::loop();

// Compute speed and direction
const time_t now = micros();
const uint32_t now = micros();
this->calculate_speed_(now);
const int32_t to_target = (this->target_position - this->current_position);
this->current_direction = (to_target != 0 ? (Direction) (to_target / abs(to_target)) : Direction::STANDSTILL);
Expand All @@ -64,7 +64,9 @@ void TMC2209Stepper::loop() {
}

if (this->control_method_ == ControlMethod::PULSES_CONTROL) {
time_t dt = now - this->last_step_;
// uint32_t so the delta is wrap-safe across the ~71.6 min micros() overflow
// (a signed/wider time_t delta went negative on wrap, freezing STEP output).
uint32_t dt = now - this->last_step_;
if (dt >= (1 / (float) vactual_) * 1e6f) {
if (this->direction_ != this->current_direction) {
this->dir_pin_->digital_write(this->current_direction == Direction::BACKWARD);
Expand Down