diff --git a/components/ade7758/CMakeLists.txt b/components/ade7758/CMakeLists.txt old mode 100644 new mode 100755 diff --git a/components/ade7758/include/ade7758.h b/components/ade7758/include/ade7758.h old mode 100644 new mode 100755 diff --git a/components/ade7758/include/meter.h b/components/ade7758/include/meter.h old mode 100644 new mode 100755 diff --git a/components/ade7758/src/ade7758.c b/components/ade7758/src/ade7758.c old mode 100644 new mode 100755 diff --git a/components/ade7758/src/energy_meter.c b/components/ade7758/src/energy_meter.c old mode 100644 new mode 100755 diff --git a/components/ade7758/src/meter.c b/components/ade7758/src/meter.c old mode 100644 new mode 100755 diff --git a/components/currentshaper/CMakeLists.txt b/components/currentshaper/CMakeLists.txt deleted file mode 100755 index d6cdddd..0000000 --- a/components/currentshaper/CMakeLists.txt +++ /dev/null @@ -1,9 +0,0 @@ -set(srcs - "src/input_filter.c" - "src/currentshaper.c" - ) - -idf_component_register(SRCS "${srcs}" - INCLUDE_DIRS "include" - PRIV_REQUIRES nvs_flash - REQUIRES config evse) diff --git a/components/currentshaper/include/currentshaper.h b/components/currentshaper/include/currentshaper.h deleted file mode 100755 index f1192dc..0000000 --- a/components/currentshaper/include/currentshaper.h +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef CURRENTSHAPER_H_ -#define CURRENTSHAPER_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @brief Inicia a tarefa do shaper de corrente dinâmica. - */ -void currentshaper_start(void); - -/** - * @brief Encerra a tarefa do shaper de corrente dinâmica. - */ -void currentshaper_stop(void); - -/** - * @brief Executa a lógica de modelagem da corrente com base nos dados atuais. - */ -void shapeCurrent(void); - -/** - * @brief Define a corrente máxima disponível da rede elétrica (A*10). - * - * @param max_grid_current Corrente máxima da rede (em A*10) - */ -void setMaxGridCurrent(int max_grid_current); - -/** - * @brief Define a corrente medida em tempo real disponível na rede (A*10). - * - * @param live_grid_current Corrente de rede ao vivo (em A*10) - */ -void setLiveGridCurrent(int live_grid_current); - -/** - * @brief Define a tensão medida em tempo real da rede (V). - * - * @param live_volt Tensão da rede (em volts) - */ -void setLiveVolt(int live_volt); - -#ifdef __cplusplus -} -#endif - -#endif /* CURRENTSHAPER_H_ */ diff --git a/components/currentshaper/include/input_filter.h b/components/currentshaper/include/input_filter.h deleted file mode 100755 index c2d41a5..0000000 --- a/components/currentshaper/include/input_filter.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef _INPUT_FILTER_H -#define _INPUT_FILTER_H - -#include // Necessário para uint32_t -#include // Para uso de PRIu32 se necessário em logs - -// Constante mínima de tempo (tau), em milissegundos -#ifndef INPUT_FILTER_MIN_TAU -#define INPUT_FILTER_MIN_TAU 10 // ms -#endif - -/** - * @brief Calcula o fator de suavização com base em delta e tau. - * - * @param delta Tempo desde a última atualização (ms) - * @param tau Constante de tempo do filtro (ms) - * @return double Fator de suavização (0.0 a 1.0) - */ -double getFactor(uint32_t delta, uint32_t tau); - -/** - * @brief Aplica filtro exponencial a uma entrada. - * - * @param input Valor atual medido - * @param filtered Valor suavizado anterior - * @param tau Constante de tempo (ms) - * @return double Novo valor suavizado - */ -double filter(double input, double filtered, uint32_t tau); - -#endif // _INPUT_FILTER_H diff --git a/components/currentshaper/src/currentshaper.c b/components/currentshaper/src/currentshaper.c deleted file mode 100755 index f768d90..0000000 --- a/components/currentshaper/src/currentshaper.c +++ /dev/null @@ -1,136 +0,0 @@ -// currentshaper.c -#include "esp_log.h" -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include -#include "evse_api.h" -#include "input_filter.h" - -static const char *TAG = "currentshaper"; - -#define EVSE_SHAPER_HYSTERESIS 50 // A*10 -#define CHARGING_CURRENT_MIN 60 // A*10 - -static TaskHandle_t currentshaper_task = NULL; - -static uint32_t smoothing_time_ms = 1000; -static uint32_t min_pause_time_ms = 300000; -static uint32_t max_data_interval_ms = 120000; - -static int max_grid_current = 300; // A*10 -static int live_grid_current = 0; // A*10 -static int live_voltage = 230; -static double smoothed_current = 0; - -static uint16_t max_current = 0; -static TickType_t timer = 0; -static TickType_t pause_timer = 0; - -static bool updated = false; -static bool changed = false; - -static portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED; - -void shapeCurrent() -{ - taskENTER_CRITICAL(&mux); - updated = true; - smoothed_current = filter(live_grid_current, smoothed_current, smoothing_time_ms); - int diff = max_grid_current - (int)smoothed_current; - int charge_current = evse_get_charging_current(); - int new_current = diff + charge_current; - - new_current = MIN(new_current, evse_get_max_charging_current() * 10); - new_current = MAX(new_current, CHARGING_CURRENT_MIN); - - max_current = new_current; - if (charge_current != max_current) - { - evse_set_charging_current(max_current); - changed = true; - } - taskEXIT_CRITICAL(&mux); -} - -void setMaxGridCurrent(int value) -{ - taskENTER_CRITICAL(&mux); - max_grid_current = value; - taskEXIT_CRITICAL(&mux); - ESP_LOGI(TAG, "Max grid current: %d A*10", value); -} - -void setLiveGridCurrent(int value) -{ - taskENTER_CRITICAL(&mux); - live_grid_current = value; - taskEXIT_CRITICAL(&mux); - ESP_LOGI(TAG, "Live grid current: %d A*10", value); - if (evse_state_is_charging(evse_get_state())) - { - shapeCurrent(); - } -} - -void setLiveVoltage(int value) -{ - taskENTER_CRITICAL(&mux); - live_voltage = value; - taskEXIT_CRITICAL(&mux); - ESP_LOGD(TAG, "Live voltage: %d V", value); -} - -static void currentshaper_task_func(void *param) -{ - while (true) - { - TickType_t now = xTaskGetTickCount(); - if (evse_state_is_charging(evse_get_state())) - { - taskENTER_CRITICAL(&mux); - if (changed) - { - if (max_current < CHARGING_CURRENT_MIN) - { - evse_set_charging_current(CHARGING_CURRENT_MIN); - if (pause_timer == 0) - pause_timer = now; - } - else if ((pause_timer != 0) && (now - pause_timer) * portTICK_PERIOD_MS >= min_pause_time_ms && - (max_current - CHARGING_CURRENT_MIN >= EVSE_SHAPER_HYSTERESIS)) - { - pause_timer = 0; - } - timer = now; - changed = false; - } - else if (!updated || (now - timer) * portTICK_PERIOD_MS > max_data_interval_ms) - { - if (updated) - { - pause_timer = now; - updated = false; - smoothed_current = live_grid_current; - } - } - taskEXIT_CRITICAL(&mux); - } - vTaskDelay(pdMS_TO_TICKS(1000)); - } -} - -void currentshaper_start() -{ - ESP_LOGI(TAG, "Starting current shaper task"); - xTaskCreate(currentshaper_task_func, "currentshaper_task", 4096, NULL, 5, ¤tshaper_task); -} - -void currentshaper_stop() -{ - ESP_LOGI(TAG, "Stopping current shaper task"); - if (currentshaper_task != NULL) - { - vTaskDelete(currentshaper_task); - currentshaper_task = NULL; - } -} \ No newline at end of file diff --git a/components/currentshaper/src/input_filter.c b/components/currentshaper/src/input_filter.c deleted file mode 100755 index b157064..0000000 --- a/components/currentshaper/src/input_filter.c +++ /dev/null @@ -1,36 +0,0 @@ -// input_filter.c -#include "input_filter.h" -#include "esp_log.h" -#include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include - -static const char *TAG = "inputfilter"; -static TickType_t last_data_time = 0; - -#define MIN_TAU_MS 100 // Minimum time constant in ms - -double getFactor(uint32_t delta_ms, uint32_t tau_ms) -{ - if (tau_ms < MIN_TAU_MS) - { - tau_ms = MIN_TAU_MS; - } - double factor = 1.0 - exp(-((double)delta_ms / (double)tau_ms)); - ESP_LOGD(TAG, "delta_ms=%" PRIu32 ", tau_ms=%" PRIu32 ", factor=%f", delta_ms, tau_ms, factor); - return factor; -} - -double filter(double input, double filtered, uint32_t tau_ms) -{ - TickType_t now = xTaskGetTickCount(); - uint32_t delta_ms = (now - last_data_time) * portTICK_PERIOD_MS; - last_data_time = now; - - double factor = getFactor(delta_ms, tau_ms); - filtered = input * factor + filtered * (1.0 - factor); - - ESP_LOGD(TAG, "input=%f, filtered=%f", input, filtered); - return filtered; -} diff --git a/components/evse/evse_config.c b/components/evse/evse_config.c index 0e0af10..525f335 100755 --- a/components/evse/evse_config.c +++ b/components/evse/evse_config.c @@ -249,7 +249,7 @@ void evse_set_require_auth(bool value) { require_auth = value; nvs_set_u8(nvs, "require_auth", value); nvs_commit(nvs); - ESP_LOGI(TAG, "Require auth adjusted to: %d", require_auth); + ESP_LOGD(TAG, "Require auth adjusted to: %d", require_auth); } // Availability diff --git a/components/evsemeter/CMakeLists.txt b/components/evsemeter/CMakeLists.txt index 7cfb277..e4f7635 100755 --- a/components/evsemeter/CMakeLists.txt +++ b/components/evsemeter/CMakeLists.txt @@ -6,4 +6,4 @@ set(srcs idf_component_register(SRCS "${srcs}" INCLUDE_DIRS "include" - REQUIRES meter_orno_modbus ade7758) + REQUIRES esp_event meter_orno_modbus ade7758) diff --git a/components/gridmeter/CMakeLists.txt b/components/gridmeter/CMakeLists.txt index 170af36..4ffea5e 100755 --- a/components/gridmeter/CMakeLists.txt +++ b/components/gridmeter/CMakeLists.txt @@ -5,4 +5,4 @@ set(srcs idf_component_register(SRCS "${srcs}" INCLUDE_DIRS "include" - REQUIRES meter_orno_modbus) + REQUIRES esp_event meter_orno_modbus) diff --git a/components/loadbalancer/CMakeLists.txt b/components/loadbalancer/CMakeLists.txt index e1c92ec..a5cd8cb 100755 --- a/components/loadbalancer/CMakeLists.txt +++ b/components/loadbalancer/CMakeLists.txt @@ -1,7 +1,7 @@ set(srcs - "src/loadbalancer.c" + "src/input_filter.c" "src/loadbalancer.c" ) idf_component_register(SRCS "${srcs}" INCLUDE_DIRS "include" - REQUIRES gridmeter evsemeter) + REQUIRES esp_event gridmeter evsemeter) diff --git a/components/loadbalancer/include/input_filter.h b/components/loadbalancer/include/input_filter.h old mode 100644 new mode 100755 diff --git a/components/loadbalancer/src/input_filter.c b/components/loadbalancer/src/input_filter.c old mode 100644 new mode 100755 diff --git a/components/loadbalancer/src/loadbalancer.c b/components/loadbalancer/src/loadbalancer.c index 006366a..3607258 100755 --- a/components/loadbalancer/src/loadbalancer.c +++ b/components/loadbalancer/src/loadbalancer.c @@ -79,7 +79,7 @@ void loadbalancer_task(void *param) } ESP_LOGI(TAG, "Setting EVSE current limit: %.1f A", available); - evse_set_current_limit((uint16_t)available); + evse_set_charging_current((uint16_t)available); vTaskDelay(pdMS_TO_TICKS(1000)); } diff --git a/components/meter_orno_modbus/include/orno_modbus.h b/components/meter_orno_modbus/include/orno_modbus.h index 546fe3e..1f016a3 100755 --- a/components/meter_orno_modbus/include/orno_modbus.h +++ b/components/meter_orno_modbus/include/orno_modbus.h @@ -1,23 +1,58 @@ #ifndef ORNO_MODBUS_H_ #define ORNO_MODBUS_H_ +#include #include "esp_err.h" #ifdef __cplusplus extern "C" { #endif +/** + * @brief Tipo do medidor ORNO usado na aplicação. + */ typedef enum { - ORNO_METER_GRID, - ORNO_METER_EVSE + ORNO_METER_GRID, ///< Medidor na entrada da rede elétrica + ORNO_METER_EVSE ///< Medidor na saída da EVSE } orno_meter_type_t; +/** + * @brief Inicializa o driver ORNO Modbus. + */ esp_err_t orno_modbus_init(void); + +/** + * @brief Lê a corrente RMS do medidor especificado. + * + * @param type Tipo do medidor (GRID ou EVSE) + * @param current Ponteiro para armazenar o valor da corrente (em amperes) + * @return esp_err_t ESP_OK em caso de sucesso, erro caso contrário + */ esp_err_t orno_modbus_read_current(orno_meter_type_t type, float *current); + +/** + * @brief Ativa ou desativa o modo de teste (simulação). + */ void orno_modbus_set_meter_test(bool state); + +/** + * @brief Define o modelo usado do medidor (caso afete registros). + */ void orno_modbus_set_model(bool enabled); + +/** + * @brief Retorna o estado atual do medidor (ligado/desligado). + */ bool orno_modbus_get_meter_state(void); + +/** + * @brief Inicia a task interna de comunicação (se usada). + */ void orno_modbus_start(void); + +/** + * @brief Para a task de comunicação (se usada). + */ void orno_modbus_stop(void); #ifdef __cplusplus diff --git a/dependencies.lock b/dependencies.lock index abc60d1..6dcc77b 100755 --- a/dependencies.lock +++ b/dependencies.lock @@ -28,6 +28,6 @@ dependencies: source: type: idf version: 5.3.0 -manifest_hash: 5c32be3773ff644cc40e814191fb19975a757deef1b686db5b80724d8d562fde +manifest_hash: c3d3273b8d8a591808187df8795265e85b441d0c189d0c969b6afb32d9166bac target: esp32 version: 1.0.0 diff --git a/main/idf_component.yml b/main/idf_component.yml index 13715bb..ac10af3 100755 --- a/main/idf_component.yml +++ b/main/idf_component.yml @@ -2,17 +2,5 @@ dependencies: espressif/mdns: "=*" espressif/ntc_driver: "^0.3.0" - ## Required IDF version idf: - version: ">=4.1.0" - # # Put list of dependencies here - # # For components maintained by Espressif: - # component: "~1.0.0" - # # For 3rd party components: - # username/component: ">=1.0.0,<2.0.0" - # username2/component2: - # version: "~1.0.0" - # # For transient dependencies `public` flag can be set. - # # `public` flag doesn't have an effect dependencies of the `main` component. - # # All dependencies of `main` are public by default. - # public: true + version: ">=5.1.0"