diff --git a/main/maxxfan-controller.c b/main/maxxfan-controller.c index d2c94f1..1287df8 100755 --- a/main/maxxfan-controller.c +++ b/main/maxxfan-controller.c @@ -3,11 +3,13 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" +#include "freertos/timers.h" #include "esp_system.h" #include "esp_wifi.h" #include "esp_event.h" #include "esp_log.h" #include "esp_http_server.h" +#include "esp_task_wdt.h" #include "nvs_flash.h" #include "driver/gpio.h" #include "driver/ledc.h" @@ -31,6 +33,14 @@ #define PWM_R_CHANNEL LEDC_CHANNEL_0 #define PWM_L_CHANNEL LEDC_CHANNEL_1 +// Motor ramping configuration +#define RAMP_STEP_MS 50 // 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 + +// Watchdog configuration +#define WATCHDOG_TIMEOUT_S 10 // Watchdog timeout in seconds + static const char* TAG = "HTTP_MOTOR"; // WiFi event group @@ -47,13 +57,29 @@ typedef enum { MOTOR_INTAKE } motor_mode_t; -static motor_mode_t current_mode = MOTOR_OFF; -static int current_speed = 0; +typedef struct { + motor_mode_t mode; + int target_speed; + int current_speed; + bool ramping; + TimerHandle_t ramp_timer; +} motor_state_t; + +static motor_state_t motor_state = { + .mode = MOTOR_OFF, + .target_speed = 0, + .current_speed = 0, + .ramping = false, + .ramp_timer = NULL +}; // HTTP server handle static httpd_handle_t server = NULL; -// HTML web page for control +// Task handles for watchdog +static TaskHandle_t main_task_handle = NULL; + +// HTML web page for control (same as before) static const char* html_page = "" "" @@ -77,6 +103,7 @@ static const char* html_page = " .speed-slider { width: 100%; height: 40px; }" " .status { background: #e3f2fd; padding: 15px; border-radius: 5px; margin: 20px 0; }" " .status h4 { margin: 0 0 10px 0; color: #1976d2; }" +" .ramping { background: #fff3e0; border-left: 4px solid #ff9800; padding: 10px; margin: 10px 0; }" " " "" "
" @@ -87,6 +114,10 @@ static const char* html_page = "Mode: OFF
" "Speed: 0%
" +"Target: 0%
" +"