diff --git a/examples/peripherals/rmt/stepper_motor/main/stepper_motor_encoder.c b/examples/peripherals/rmt/stepper_motor/main/stepper_motor_encoder.c index b125b59a31..ea2e58cb8e 100644 --- a/examples/peripherals/rmt/stepper_motor/main/stepper_motor_encoder.c +++ b/examples/peripherals/rmt/stepper_motor/main/stepper_motor_encoder.c @@ -76,8 +76,9 @@ esp_err_t rmt_new_stepper_motor_curve_encoder(const stepper_motor_curve_encoder_ bool is_accel_curve = config->start_freq_hz < config->end_freq_hz; // prepare the curve table, in RMT symbol format + uint32_t curve_step = 0; if (is_accel_curve) { - uint32_t curve_step = (config->end_freq_hz - config->start_freq_hz) / (config->sample_points - 1); + curve_step = (config->end_freq_hz - config->start_freq_hz) / (config->sample_points - 1); for (uint32_t i = 0; i < config->sample_points; i++) { smooth_freq = convert_to_smooth_freq(config->start_freq_hz, config->end_freq_hz, config->start_freq_hz + curve_step * i); symbol_duration = config->resolution / smooth_freq / 2; @@ -87,7 +88,7 @@ esp_err_t rmt_new_stepper_motor_curve_encoder(const stepper_motor_curve_encoder_ step_encoder->curve_table[i].duration1 = symbol_duration; } } else { - uint32_t curve_step = (config->start_freq_hz - config->end_freq_hz) / (config->sample_points - 1); + curve_step = (config->start_freq_hz - config->end_freq_hz) / (config->sample_points - 1); for (uint32_t i = 0; i < config->sample_points; i++) { smooth_freq = convert_to_smooth_freq(config->end_freq_hz, config->start_freq_hz, config->end_freq_hz + curve_step * i); symbol_duration = config->resolution / smooth_freq / 2; @@ -97,6 +98,7 @@ esp_err_t rmt_new_stepper_motor_curve_encoder(const stepper_motor_curve_encoder_ step_encoder->curve_table[config->sample_points - i - 1].duration1 = symbol_duration; } } + ESP_GOTO_ON_FALSE(curve_step > 0, ESP_ERR_INVALID_ARG, err, TAG, "|end_freq_hz - start_freq_hz| can't be smaller than sample_points"); step_encoder->sample_points = config->sample_points; step_encoder->flags.is_accel_curve = is_accel_curve; diff --git a/examples/peripherals/rmt/stepper_motor/main/stepper_motor_encoder.h b/examples/peripherals/rmt/stepper_motor/main/stepper_motor_encoder.h index 915d6b5e6f..4dae7930c1 100644 --- a/examples/peripherals/rmt/stepper_motor/main/stepper_motor_encoder.h +++ b/examples/peripherals/rmt/stepper_motor/main/stepper_motor_encoder.h @@ -17,7 +17,7 @@ extern "C" { */ typedef struct { uint32_t resolution; // Encoder resolution, in Hz - uint32_t sample_points; // Sample points used for deceleration phase + uint32_t sample_points; // Sample points used for deceleration phase. Note: |end_freq_hz - start_freq_hz| >= sample_points uint32_t start_freq_hz; // Start frequency on the curve, in Hz uint32_t end_freq_hz; // End frequency on the curve, in Hz } stepper_motor_curve_encoder_config_t;