From 27802237f8a7cbd108050910bed915bf1eb28a53 Mon Sep 17 00:00:00 2001 From: Stephen Minakian Date: Wed, 9 Jul 2025 13:10:54 -0600 Subject: [PATCH] 10 sec cooldown when switch direction --- main/maxxfan-controller.c | 247 +++++++++++++++++++++++++++++--------- 1 file changed, 189 insertions(+), 58 deletions(-) diff --git a/main/maxxfan-controller.c b/main/maxxfan-controller.c index f6732f0..54a0bf5 100755 --- a/main/maxxfan-controller.c +++ b/main/maxxfan-controller.c @@ -37,6 +37,7 @@ #define RAMP_STEP_MS 150 // Time between ramp steps (milliseconds) #define RAMP_STEP_SIZE 5 // PWM duty change per step (0-255) #define MIN_MOTOR_SPEED 10 // Minimum speed to overcome motor inertia +#define DIRECTION_CHANGE_COOLDOWN_MS 10000 // 10 seconds cooldown for direction changes // Watchdog configuration #define WATCHDOG_TIMEOUT_S 10 // Watchdog timeout in seconds @@ -57,20 +58,38 @@ typedef enum { MOTOR_INTAKE } motor_mode_t; +typedef enum { + MOTOR_STATE_IDLE, // Motor is off or running normally + MOTOR_STATE_RAMPING, // Motor is ramping up/down + MOTOR_STATE_STOPPING, // Motor is stopping for direction change + MOTOR_STATE_COOLDOWN, // Motor is in cooldown period + MOTOR_STATE_RESTARTING // Motor is restarting after cooldown +} motor_state_enum_t; + typedef struct { motor_mode_t mode; + motor_mode_t pending_mode; // Mode to switch to after cooldown int target_speed; + int pending_speed; // Speed to set after cooldown int current_speed; + motor_state_enum_t state; bool ramping; TimerHandle_t ramp_timer; + TimerHandle_t cooldown_timer; + uint32_t cooldown_remaining_ms; // For status reporting } motor_state_t; static motor_state_t motor_state = { .mode = MOTOR_OFF, + .pending_mode = MOTOR_OFF, .target_speed = 0, + .pending_speed = 0, .current_speed = 0, + .state = MOTOR_STATE_IDLE, .ramping = false, - .ramp_timer = NULL + .ramp_timer = NULL, + .cooldown_timer = NULL, + .cooldown_remaining_ms = 0 }; // HTTP server handle @@ -86,10 +105,13 @@ static const char* html_page = "h1{color:#333;text-align:center;margin:0 0 20px}button{padding:12px 20px;margin:5px;border:none;border-radius:4px;cursor:pointer;font-size:14px}" ".off{background:#f44336;color:white}.exhaust{background:#ff9800;color:white}.intake{background:#4CAF50;color:white}" ".status{background:#e3f2fd;padding:15px;border-radius:4px;margin:15px 0}.ramping{background:#fff3e0;padding:8px;margin:8px 0;display:none}" +".cooldown{background:#ffebee;padding:8px;margin:8px 0;color:#c62828;display:none}" ".error{background:#ffebee;padding:8px;margin:8px 0;color:#c62828;display:none}.slider{width:100%;height:30px;margin:10px 0}" "

Maxxfan Controller

" "

Status

Mode: OFF

Speed: 0%

" -"

Target: 0%

Ramping...
" +"

Target: 0%

State: IDLE

" +"
Ramping...
" +"
Direction change cooldown: 0s
" "
Error
Connecting...
" "

Fan Control

" "" @@ -116,7 +138,11 @@ static const char* html_page = "function updateStatus(data){document.getElementById('mode').textContent=data.mode.toUpperCase();" "document.getElementById('speed').textContent=data.current_speed;" "document.getElementById('target').textContent=data.target_speed;" -"document.getElementById('rampStatus').style.display=data.ramping?'block':'none'}" +"document.getElementById('state').textContent=data.state.toUpperCase();" +"document.getElementById('rampStatus').style.display=data.ramping?'block':'none';" +"let cooldownDiv=document.getElementById('cooldownStatus');" +"if(data.cooldown_remaining>0){cooldownDiv.style.display='block';" +"document.getElementById('cooldownTime').textContent=Math.ceil(data.cooldown_remaining/1000);}else{cooldownDiv.style.display='none';}}" "function getStatus(){fetch('/status').then(r=>{if(!r.ok)throw new Error('HTTP '+r.status);return r.json()})" ".then(d=>{updateStatus(d);hideError()}).catch(e=>{errorCount++;if(errorCount>=3)showError('Connection lost')})}" "function startUpdates(){if(updateInterval)clearInterval(updateInterval);updateInterval=setInterval(getStatus,1000)}" @@ -124,7 +150,9 @@ static const char* html_page = // Forward declarations static void motor_ramp_timer_callback(TimerHandle_t xTimer); +static void motor_cooldown_timer_callback(TimerHandle_t xTimer); static void apply_motor_pwm(int speed_percent); +static void start_motor_operation(motor_mode_t mode, int speed_percent); // Initialize watchdog timer void init_watchdog(void) { @@ -270,7 +298,7 @@ static void apply_motor_pwm(int speed_percent) { // Motor ramp timer callback static void motor_ramp_timer_callback(TimerHandle_t xTimer) { - if (!motor_state.ramping) { + if (motor_state.state != MOTOR_STATE_RAMPING) { return; } @@ -280,6 +308,7 @@ static void motor_ramp_timer_callback(TimerHandle_t xTimer) { // Close enough to target, finish ramping motor_state.current_speed = motor_state.target_speed; motor_state.ramping = false; + motor_state.state = MOTOR_STATE_IDLE; // Stop the timer xTimerStop(motor_state.ramp_timer, 0); @@ -293,14 +322,70 @@ static void motor_ramp_timer_callback(TimerHandle_t xTimer) { motor_state.current_speed -= RAMP_STEP_SIZE; } - ESP_LOGD(TAG, "Ramping: %d%% -> %d%% (target: %d%%)", - motor_state.current_speed - (speed_diff > 0 ? RAMP_STEP_SIZE : -RAMP_STEP_SIZE), - motor_state.current_speed, motor_state.target_speed); + ESP_LOGD(TAG, "Ramping: %d%% (target: %d%%)", motor_state.current_speed, motor_state.target_speed); } apply_motor_pwm(motor_state.current_speed); } +// Motor cooldown timer callback +static void motor_cooldown_timer_callback(TimerHandle_t xTimer) { + ESP_LOGI(TAG, "Cooldown complete - Starting motor in %s mode at %d%%", + motor_state.pending_mode == MOTOR_EXHAUST ? "EXHAUST" : "INTAKE", + motor_state.pending_speed); + + // Reset cooldown tracking + motor_state.cooldown_remaining_ms = 0; + + // Start the motor in the pending mode + start_motor_operation(motor_state.pending_mode, motor_state.pending_speed); +} + +// Update cooldown remaining time (called periodically) +static void update_cooldown_time(void) { + if (motor_state.state == MOTOR_STATE_COOLDOWN && motor_state.cooldown_remaining_ms > 0) { + if (motor_state.cooldown_remaining_ms >= 1000) { + motor_state.cooldown_remaining_ms -= 1000; + } else { + motor_state.cooldown_remaining_ms = 0; + } + } +} + +// Start motor operation (internal function) +static void start_motor_operation(motor_mode_t mode, int speed_percent) { + motor_state.mode = mode; + motor_state.target_speed = speed_percent; + motor_state.state = MOTOR_STATE_RAMPING; + motor_state.ramping = true; + + if (mode == MOTOR_OFF || speed_percent == 0) { + // Immediate stop + motor_state.current_speed = 0; + motor_state.target_speed = 0; + motor_state.state = MOTOR_STATE_IDLE; + motor_state.ramping = false; + apply_motor_pwm(0); + ESP_LOGI(TAG, "Motor stopped immediately"); + } else { + // Start from minimum speed if currently off + if (motor_state.current_speed == 0) { + int start_speed = (speed_percent < MIN_MOTOR_SPEED) ? speed_percent : MIN_MOTOR_SPEED; + motor_state.current_speed = start_speed; + apply_motor_pwm(start_speed); + ESP_LOGI(TAG, "Motor starting at %d%%, ramping to %d%%", start_speed, speed_percent); + } + + // Start ramping if needed + if (motor_state.current_speed != motor_state.target_speed) { + xTimerStart(motor_state.ramp_timer, 0); + } else { + motor_state.state = MOTOR_STATE_IDLE; + motor_state.ramping = false; + } + } +} + // Initialize motor ramping system void init_motor_ramping(void) { motor_state.ramp_timer = xTimerCreate( @@ -311,10 +396,18 @@ void init_motor_ramping(void) { motor_ramp_timer_callback // Callback function ); - if (motor_state.ramp_timer == NULL) { - ESP_LOGE(TAG, "Failed to create motor ramp timer"); + motor_state.cooldown_timer = xTimerCreate( + "MotorCooldownTimer", // Timer name + pdMS_TO_TICKS(DIRECTION_CHANGE_COOLDOWN_MS), // Timer period + pdFALSE, // One-shot + (void*)0, // Timer ID + motor_cooldown_timer_callback // Callback function + ); + + if (motor_state.ramp_timer == NULL || motor_state.cooldown_timer == NULL) { + ESP_LOGE(TAG, "Failed to create motor timers"); } else { - ESP_LOGI(TAG, "Motor ramping system initialized"); + ESP_LOGI(TAG, "Motor control system initialized with direction change safety"); } } @@ -323,59 +416,72 @@ void set_motor_speed(motor_mode_t mode, int speed_percent) if (speed_percent < 0) speed_percent = 0; if (speed_percent > 100) speed_percent = 100; - // Stop any current ramping - if (motor_state.ramping) { - xTimerStop(motor_state.ramp_timer, 0); - motor_state.ramping = false; + ESP_LOGI(TAG, "Motor command: %s - Speed: %d%% (Current mode: %s, Current speed: %d%%, State: %d)", + mode == MOTOR_OFF ? "OFF" : (mode == MOTOR_EXHAUST ? "EXHAUST" : "INTAKE"), + speed_percent, + motor_state.mode == MOTOR_OFF ? "OFF" : (motor_state.mode == MOTOR_EXHAUST ? "EXHAUST" : "INTAKE"), + motor_state.current_speed, + motor_state.state); + + // If we're in cooldown, update the pending command + if (motor_state.state == MOTOR_STATE_COOLDOWN) { + motor_state.pending_mode = mode; + motor_state.pending_speed = speed_percent; + ESP_LOGI(TAG, "Motor in cooldown - command queued for execution"); + return; } - motor_mode_t previous_mode = motor_state.mode; - motor_state.mode = mode; - motor_state.target_speed = speed_percent; + // Check if this is a direction change that requires cooldown + bool requires_cooldown = false; + if (motor_state.current_speed > 0 && motor_state.mode != MOTOR_OFF) { + if ((motor_state.mode == MOTOR_EXHAUST && mode == MOTOR_INTAKE) || + (motor_state.mode == MOTOR_INTAKE && mode == MOTOR_EXHAUST)) { + requires_cooldown = true; + } + } - ESP_LOGI(TAG, "Motor command: %s - Target: %d%% (Current: %d%%)", - mode == MOTOR_OFF ? "OFF" : (mode == MOTOR_EXHAUST ? "EXHAUST" : "INTAKE"), - speed_percent, motor_state.current_speed); - - // Handle different scenarios - if (mode == MOTOR_OFF || speed_percent == 0) { - // Immediate stop - motor_state.current_speed = 0; - motor_state.target_speed = 0; - apply_motor_pwm(0); - ESP_LOGI(TAG, "Motor stopped immediately"); + if (requires_cooldown) { + ESP_LOGI(TAG, "Direction change detected - initiating safety cooldown sequence"); - } else if (previous_mode == MOTOR_OFF || motor_state.current_speed == 0) { - // Starting from stop - apply minimum speed first, then ramp - int start_speed = (speed_percent < MIN_MOTOR_SPEED) ? speed_percent : MIN_MOTOR_SPEED; - motor_state.current_speed = start_speed; - apply_motor_pwm(start_speed); - - if (speed_percent > start_speed) { - // Start ramping to target - motor_state.ramping = true; - xTimerStart(motor_state.ramp_timer, 0); - ESP_LOGI(TAG, "Motor starting at %d%%, ramping to %d%%", start_speed, speed_percent); - } else { - ESP_LOGI(TAG, "Motor started at %d%% (no ramping needed)", start_speed); + // Stop any current ramping + if (motor_state.ramping) { + xTimerStop(motor_state.ramp_timer, 0); + motor_state.ramping = false; } - } else if (previous_mode != mode) { - // Direction change - ramp down to minimum, change direction, then ramp up - motor_state.target_speed = MIN_MOTOR_SPEED; - motor_state.ramping = true; - xTimerStart(motor_state.ramp_timer, 0); - ESP_LOGI(TAG, "Direction change - ramping down first"); + // Stop the motor immediately + motor_state.mode = MOTOR_OFF; + motor_state.current_speed = 0; + motor_state.target_speed = 0; + motor_state.state = MOTOR_STATE_COOLDOWN; + motor_state.cooldown_remaining_ms = DIRECTION_CHANGE_COOLDOWN_MS; + apply_motor_pwm(0); - // Note: In a real implementation, you might want to implement a state machine - // to handle the direction change sequence properly + // Store the pending command + motor_state.pending_mode = mode; + motor_state.pending_speed = speed_percent; + // Start cooldown timer + xTimerStart(motor_state.cooldown_timer, 0); + + ESP_LOGI(TAG, "Motor stopped for direction change - %d second cooldown started", + DIRECTION_CHANGE_COOLDOWN_MS / 1000); } else { - // Same mode, just speed change - ramp to new speed - motor_state.ramping = true; - xTimerStart(motor_state.ramp_timer, 0); - ESP_LOGI(TAG, "Speed change - ramping from %d%% to %d%%", - motor_state.current_speed, speed_percent); + // No direction change required, proceed normally + + // Stop any current ramping + if (motor_state.ramping) { + xTimerStop(motor_state.ramp_timer, 0); + motor_state.ramping = false; + } + + // Stop cooldown timer if running + if (motor_state.state == MOTOR_STATE_COOLDOWN) { + xTimerStop(motor_state.cooldown_timer, 0); + motor_state.cooldown_remaining_ms = 0; + } + + start_motor_operation(mode, speed_percent); } } @@ -399,9 +505,12 @@ static esp_err_t root_get_handler(httpd_req_t *req) // HTTP handler for fan status (GET /status) static esp_err_t status_get_handler(httpd_req_t *req) { - ESP_LOGI(TAG, "Status request - Mode: %d, Current: %d%%, Target: %d%%, Ramping: %s", + // Update cooldown time before reporting + update_cooldown_time(); + + ESP_LOGI(TAG, "Status request - Mode: %d, Current: %d%%, Target: %d%%, State: %d, Ramping: %s", motor_state.mode, motor_state.current_speed, motor_state.target_speed, - motor_state.ramping ? "YES" : "NO"); + motor_state.state, motor_state.ramping ? "YES" : "NO"); set_cors_headers(req); httpd_resp_set_type(req, "application/json"); @@ -412,10 +521,31 @@ static esp_err_t status_get_handler(httpd_req_t *req) if (motor_state.mode == MOTOR_EXHAUST) mode_str = "exhaust"; else if (motor_state.mode == MOTOR_INTAKE) mode_str = "intake"; + const char* state_str = "idle"; + switch (motor_state.state) { + case MOTOR_STATE_RAMPING: state_str = "ramping"; break; + case MOTOR_STATE_STOPPING: state_str = "stopping"; break; + case MOTOR_STATE_COOLDOWN: state_str = "cooldown"; break; + case MOTOR_STATE_RESTARTING: state_str = "restarting"; break; + default: state_str = "idle"; break; + } + cJSON_AddStringToObject(json, "mode", mode_str); cJSON_AddNumberToObject(json, "current_speed", motor_state.current_speed); cJSON_AddNumberToObject(json, "target_speed", motor_state.target_speed); + cJSON_AddStringToObject(json, "state", state_str); cJSON_AddBoolToObject(json, "ramping", motor_state.ramping); + cJSON_AddNumberToObject(json, "cooldown_remaining", motor_state.cooldown_remaining_ms); + + // Add pending command info if in cooldown + if (motor_state.state == MOTOR_STATE_COOLDOWN) { + const char* pending_mode_str = "off"; + if (motor_state.pending_mode == MOTOR_EXHAUST) pending_mode_str = "exhaust"; + else if (motor_state.pending_mode == MOTOR_INTAKE) pending_mode_str = "intake"; + + cJSON_AddStringToObject(json, "pending_mode", pending_mode_str); + cJSON_AddNumberToObject(json, "pending_speed", motor_state.pending_speed); + } char *json_string = cJSON_Print(json); if (json_string) { @@ -638,7 +768,7 @@ void main_task(void *pvParameters) { void app_main(void) { - ESP_LOGI(TAG, "Starting Maxxfan HTTP Controller with improvements!"); + ESP_LOGI(TAG, "Starting Maxxfan HTTP Controller with Direction Change Safety!"); // Initialize NVS esp_err_t ret = nvs_flash_init(); @@ -665,7 +795,8 @@ void app_main(void) start_webserver(); ESP_LOGI(TAG, "=== Enhanced Maxxfan Controller Ready! ==="); - ESP_LOGI(TAG, "Features: Motor Ramping, Optimized Performance, Real-time Updates"); + ESP_LOGI(TAG, "Features: Motor Ramping, Direction Change Safety, Real-time Updates"); + ESP_LOGI(TAG, "Safety: 10-second cooldown for direction changes"); ESP_LOGI(TAG, "Open your browser and go to: http://[ESP32_IP_ADDRESS]"); ESP_LOGI(TAG, "Check the monitor output above for your IP address");