new loadbalancer

This commit is contained in:
2025-06-08 14:42:16 +01:00
parent a57477d95b
commit 619682052a
2 changed files with 13 additions and 9 deletions

View File

@@ -7,6 +7,7 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "input_filter.h"
#include <string.h> // 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
}