diff --git a/components/loadbalancer/src/loadbalancer.c b/components/loadbalancer/src/loadbalancer.c index 029db76..006366a 100755 --- a/components/loadbalancer/src/loadbalancer.c +++ b/components/loadbalancer/src/loadbalancer.c @@ -7,6 +7,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "input_filter.h" +#include // Para memcpy static const char *TAG = "loadbalancer"; @@ -16,7 +17,7 @@ static float max_grid_current = 32.0f; // Amperes #define MIN_EVSE_CURRENT 6.0f -// Filtros +// Filtros exponenciais para suavizar leituras static input_filter_t grid_filter; static input_filter_t evse_filter; @@ -46,18 +47,22 @@ void loadbalancer_init(void) { ESP_LOGI(TAG, "Initializing load balancer"); - // Inicializa filtros input_filter_init(&grid_filter, 0.3f); input_filter_init(&evse_filter, 0.3f); - // Registra eventos - if (esp_event_handler_register(GRIDMETER_EVENT, GRIDMETER_EVENT_UPDATE, grid_event_handler, NULL) != ESP_OK) { + if (esp_event_handler_register(GRIDMETER_EVENT, GRIDMETER_EVENT_UPDATE, + grid_event_handler, NULL) != ESP_OK) { ESP_LOGE(TAG, "Failed to register gridmeter event handler"); } - if (esp_event_handler_register(EVSEMETER_EVENT, EVSEMETER_EVENT_UPDATE, evse_event_handler, NULL) != ESP_OK) { + if (esp_event_handler_register(EVSEMETER_EVENT, EVSEMETER_EVENT_UPDATE, + evse_event_handler, NULL) != ESP_OK) { ESP_LOGE(TAG, "Failed to register evsemeter event handler"); } + + if (xTaskCreate(loadbalancer_task, "loadbalancer", 4096, NULL, 5, NULL) != pdPASS) { + ESP_LOGE(TAG, "Failed to create loadbalancer task"); + } } void loadbalancer_task(void *param) @@ -66,7 +71,7 @@ void loadbalancer_task(void *param) { float available = max_grid_current - grid_current + evse_current; - // Aplica restrições de segurança + // Restrição de corrente mínima e máxima if (available < MIN_EVSE_CURRENT) { available = 0.0f; } else if (available > max_grid_current) { @@ -82,7 +87,7 @@ void loadbalancer_task(void *param) void setMaxGridCurrent(int value) { - max_grid_current = value / 10.0f; // valor em décimos de ampere + max_grid_current = value / 10.0f; } void setLiveGridCurrent(int value) @@ -93,5 +98,5 @@ void setLiveGridCurrent(int value) void setLiveVolt(int value) { - (void)value; // não usado ainda + (void)value; // reservado para uso futuro } diff --git a/main/main.c b/main/main.c index b4f7940..6a4099e 100755 --- a/main/main.c +++ b/main/main.c @@ -231,7 +231,6 @@ static void init_modules(void) auth_set_event_queue(auth_queue); evse_manager_start(auth_queue); loadbalancer_init(); - xTaskCreate(loadbalancer_task, "loadbalancer", 4096, NULL, 5, NULL); // Outros módulos (descomente conforme necessário)