example: check curve_step for stepper motor encoder

Closes https://github.com/espressif/esp-idf/issues/10253
This commit is contained in:
morris
2022-11-29 17:47:21 +08:00
parent 4b2ac6cdde
commit a933539dd2
2 changed files with 5 additions and 3 deletions

View File

@@ -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; bool is_accel_curve = config->start_freq_hz < config->end_freq_hz;
// prepare the curve table, in RMT symbol format // prepare the curve table, in RMT symbol format
uint32_t curve_step = 0;
if (is_accel_curve) { 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++) { 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); 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; 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; step_encoder->curve_table[i].duration1 = symbol_duration;
} }
} else { } 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++) { 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); 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; 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; 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->sample_points = config->sample_points;
step_encoder->flags.is_accel_curve = is_accel_curve; step_encoder->flags.is_accel_curve = is_accel_curve;

View File

@@ -17,7 +17,7 @@ extern "C" {
*/ */
typedef struct { typedef struct {
uint32_t resolution; // Encoder resolution, in Hz 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 start_freq_hz; // Start frequency on the curve, in Hz
uint32_t end_freq_hz; // End frequency on the curve, in Hz uint32_t end_freq_hz; // End frequency on the curve, in Hz
} stepper_motor_curve_encoder_config_t; } stepper_motor_curve_encoder_config_t;