Motor control implemented

This commit is contained in:
2025-07-19 22:55:58 -06:00
parent fef8da2de2
commit 5a4c91fbd3
4 changed files with 316 additions and 17 deletions

View File

@ -182,7 +182,10 @@ esp_err_t motor_control_init(void)
// Create safety timers
for (int i = 0; i < MOTOR_PUMP_MAX - 1; i++) {
s_motors[i].safety_timer = xTimerCreate("motor_safety",
char timer_name[32];
snprintf(timer_name, sizeof(timer_name), "motor_safety_%d", i + 1);
s_motors[i].safety_timer = xTimerCreate(timer_name,
pdMS_TO_TICKS(1000),
pdFALSE,
(void*)(i + 1),
@ -193,7 +196,8 @@ esp_err_t motor_control_init(void)
goto error;
}
s_motors[i].soft_start_timer = xTimerCreate("motor_soft_start",
snprintf(timer_name, sizeof(timer_name), "motor_soft_%d", i + 1);
s_motors[i].soft_start_timer = xTimerCreate(timer_name,
pdMS_TO_TICKS(50),
pdTRUE,
(void*)(i + 1),
@ -627,13 +631,22 @@ esp_err_t motor_test_run(motor_id_t id, uint32_t duration_ms)
static void motor_safety_timer_callback(TimerHandle_t xTimer)
{
motor_id_t id = (motor_id_t)(intptr_t)pvTimerGetTimerID(xTimer);
motor_t *motor = &s_motors[id - 1];
ESP_LOGW(TAG, "Safety timer expired for motor %d", id);
motor_stop(id);
if (s_error_callback) {
s_error_callback(id, "Maximum runtime exceeded");
}
// Do minimal work in timer callback to avoid stack overflow
// Just stop the PWM and update state
ledc_set_duty(LEDC_LOW_SPEED_MODE, motor->pwm_channel, 0);
ledc_update_duty(LEDC_LOW_SPEED_MODE, motor->pwm_channel);
// Update basic state
motor->speed_percent = 0;
motor->state = MOTOR_STATE_STOPPED;
// Stats update can be deferred or done in main context
// For now, just record the stop time
motor->last_stop_time = get_time_ms();
}
static void motor_soft_start_timer_callback(TimerHandle_t xTimer)
@ -661,7 +674,10 @@ static void motor_soft_start_timer_callback(TimerHandle_t xTimer)
}
uint8_t duty = (motor->speed_percent * MOTOR_PWM_MAX_DUTY) / 100;
motor_update_pwm(id, duty);
// Update PWM directly without taking mutex (atomic operation)
ledc_set_duty(LEDC_LOW_SPEED_MODE, motor->pwm_channel, duty);
ledc_update_duty(LEDC_LOW_SPEED_MODE, motor->pwm_channel);
ESP_LOGD(TAG, "Soft start motor %d: %d%% (target: %d%%)",
id, motor->speed_percent, motor->target_speed);