Add grid and EVSE meter components with load balancer
This commit is contained in:
9
components/evsemeter/CMakeLists.txt
Normal file
9
components/evsemeter/CMakeLists.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
set(srcs
|
||||
"src/evsemeter_modbus.c"
|
||||
"src/evsemeter_ade7758.c"
|
||||
"src/evsemeter_events.c"
|
||||
)
|
||||
|
||||
idf_component_register(SRCS "${srcs}"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES meter_orno_modbus)
|
||||
24
components/evsemeter/include/evsemeter.h
Normal file
24
components/evsemeter/include/evsemeter.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef EVSEMETER_H_
|
||||
#define EVSEMETER_H_
|
||||
|
||||
#include "esp_err.h"
|
||||
#include "esp_event_base.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
ESP_EVENT_DECLARE_BASE(EVSEMETER_EVENT);
|
||||
|
||||
typedef enum {
|
||||
EVSEMETER_EVENT_UPDATE
|
||||
} evsemeter_event_id_t;
|
||||
|
||||
esp_err_t evsemeter_init(void);
|
||||
esp_err_t evsemeter_read_current(float *current);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* EVSEMETER_H_ */
|
||||
21
components/evsemeter/src/evsemeter_ade7758.c
Normal file
21
components/evsemeter/src/evsemeter_ade7758.c
Normal file
@@ -0,0 +1,21 @@
|
||||
#include "evsemeter.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
static const char *TAG = "evsemeter_ade7758";
|
||||
|
||||
esp_err_t evsemeter_init(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Initializing EVSE meter (ADE7758)");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t evsemeter_read_current(float *current)
|
||||
{
|
||||
if (!current) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
*current = 0.0f;
|
||||
esp_event_post(EVSEMETER_EVENT, EVSEMETER_EVENT_UPDATE, current, sizeof(float), portMAX_DELAY);
|
||||
return ESP_OK;
|
||||
}
|
||||
3
components/evsemeter/src/evsemeter_events.c
Normal file
3
components/evsemeter/src/evsemeter_events.c
Normal file
@@ -0,0 +1,3 @@
|
||||
#include "evsemeter.h"
|
||||
|
||||
ESP_EVENT_DEFINE_BASE(EVSEMETER_EVENT);
|
||||
22
components/evsemeter/src/evsemeter_modbus.c
Normal file
22
components/evsemeter/src/evsemeter_modbus.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "evsemeter.h"
|
||||
#include "orno_modbus.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
static const char *TAG = "evsemeter_modbus";
|
||||
|
||||
esp_err_t evsemeter_init(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Initializing EVSE meter (Modbus)");
|
||||
return orno_modbus_init();
|
||||
}
|
||||
|
||||
esp_err_t evsemeter_read_current(float *current)
|
||||
{
|
||||
esp_err_t err = orno_modbus_read_current(ORNO_METER_EVSE, current);
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
esp_event_post(EVSEMETER_EVENT, EVSEMETER_EVENT_UPDATE, current, sizeof(float), portMAX_DELAY);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
8
components/gridmeter/CMakeLists.txt
Normal file
8
components/gridmeter/CMakeLists.txt
Normal file
@@ -0,0 +1,8 @@
|
||||
set(srcs
|
||||
"src/gridmeter_modbus.c"
|
||||
"src/gridmeter_events.c"
|
||||
)
|
||||
|
||||
idf_component_register(SRCS "${srcs}"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES meter_orno_modbus)
|
||||
24
components/gridmeter/include/gridmeter.h
Normal file
24
components/gridmeter/include/gridmeter.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef GRIDMETER_H_
|
||||
#define GRIDMETER_H_
|
||||
|
||||
#include "esp_err.h"
|
||||
#include "esp_event_base.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
ESP_EVENT_DECLARE_BASE(GRIDMETER_EVENT);
|
||||
|
||||
typedef enum {
|
||||
GRIDMETER_EVENT_UPDATE
|
||||
} gridmeter_event_id_t;
|
||||
|
||||
esp_err_t gridmeter_init(void);
|
||||
esp_err_t gridmeter_read_current(float *current);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* GRIDMETER_H_ */
|
||||
3
components/gridmeter/src/gridmeter_events.c
Normal file
3
components/gridmeter/src/gridmeter_events.c
Normal file
@@ -0,0 +1,3 @@
|
||||
#include "gridmeter.h"
|
||||
|
||||
ESP_EVENT_DEFINE_BASE(GRIDMETER_EVENT);
|
||||
22
components/gridmeter/src/gridmeter_modbus.c
Normal file
22
components/gridmeter/src/gridmeter_modbus.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "gridmeter.h"
|
||||
#include "orno_modbus.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
static const char *TAG = "gridmeter_modbus";
|
||||
|
||||
esp_err_t gridmeter_init(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Initializing grid meter (Modbus)");
|
||||
return orno_modbus_init();
|
||||
}
|
||||
|
||||
esp_err_t gridmeter_read_current(float *current)
|
||||
{
|
||||
esp_err_t err = orno_modbus_read_current(ORNO_METER_GRID, current);
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
esp_event_post(GRIDMETER_EVENT, GRIDMETER_EVENT_UPDATE, current, sizeof(float), portMAX_DELAY);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
7
components/loadbalancer/CMakeLists.txt
Normal file
7
components/loadbalancer/CMakeLists.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
set(srcs
|
||||
"src/loadbalancer.c"
|
||||
)
|
||||
|
||||
idf_component_register(SRCS "${srcs}"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES gridmeter evsemeter)
|
||||
20
components/loadbalancer/include/loadbalancer.h
Normal file
20
components/loadbalancer/include/loadbalancer.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef LOADBALANCER_H_
|
||||
#define LOADBALANCER_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void loadbalancer_init(void);
|
||||
void loadbalancer_task(void *param);
|
||||
|
||||
// Compatibility functions
|
||||
void setMaxGridCurrent(int max_grid_current);
|
||||
void setLiveGridCurrent(int live_grid_current);
|
||||
void setLiveVolt(int live_volt);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* LOADBALANCER_H_ */
|
||||
67
components/loadbalancer/src/loadbalancer.c
Normal file
67
components/loadbalancer/src/loadbalancer.c
Normal file
@@ -0,0 +1,67 @@
|
||||
#include "loadbalancer.h"
|
||||
#include "gridmeter.h"
|
||||
#include "evsemeter.h"
|
||||
#include "evse_api.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
static const char *TAG = "loadbalancer";
|
||||
|
||||
static float grid_current = 0.0f;
|
||||
static float evse_current = 0.0f;
|
||||
static float max_grid_current = 32.0f; // amperes
|
||||
|
||||
static void grid_event_handler(void *arg, esp_event_base_t base, int32_t id, void *data)
|
||||
{
|
||||
if (id == GRIDMETER_EVENT_UPDATE && data)
|
||||
{
|
||||
grid_current = *(float *)data;
|
||||
}
|
||||
}
|
||||
|
||||
static void evse_event_handler(void *arg, esp_event_base_t base, int32_t id, void *data)
|
||||
{
|
||||
if (id == EVSEMETER_EVENT_UPDATE && data)
|
||||
{
|
||||
evse_current = *(float *)data;
|
||||
}
|
||||
}
|
||||
|
||||
void loadbalancer_init(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Initializing load balancer");
|
||||
esp_event_handler_register(GRIDMETER_EVENT, GRIDMETER_EVENT_UPDATE, grid_event_handler, NULL);
|
||||
esp_event_handler_register(EVSEMETER_EVENT, EVSEMETER_EVENT_UPDATE, evse_event_handler, NULL);
|
||||
}
|
||||
|
||||
void loadbalancer_task(void *param)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
float available = max_grid_current - grid_current + evse_current;
|
||||
if (available < 6.0f)
|
||||
{
|
||||
available = 6.0f;
|
||||
}
|
||||
ESP_LOGD(TAG, "Setting current limit: %f", available);
|
||||
evse_set_current_limit((uint16_t)available);
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
}
|
||||
}
|
||||
|
||||
void setMaxGridCurrent(int value)
|
||||
{
|
||||
max_grid_current = value / 10.0f; // assume value in A*10
|
||||
}
|
||||
|
||||
void setLiveGridCurrent(int value)
|
||||
{
|
||||
grid_current = value / 10.0f;
|
||||
}
|
||||
|
||||
void setLiveVolt(int value)
|
||||
{
|
||||
(void)value; // unused for now
|
||||
}
|
||||
6
components/meter_orno_modbus/CMakeLists.txt
Normal file
6
components/meter_orno_modbus/CMakeLists.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
set(srcs
|
||||
"src/orno_modbus.c"
|
||||
)
|
||||
|
||||
idf_component_register(SRCS "${srcs}"
|
||||
INCLUDE_DIRS "include")
|
||||
22
components/meter_orno_modbus/include/orno_modbus.h
Normal file
22
components/meter_orno_modbus/include/orno_modbus.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef ORNO_MODBUS_H_
|
||||
#define ORNO_MODBUS_H_
|
||||
|
||||
#include "esp_err.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
ORNO_METER_GRID,
|
||||
ORNO_METER_EVSE
|
||||
} orno_meter_type_t;
|
||||
|
||||
esp_err_t orno_modbus_init(void);
|
||||
esp_err_t orno_modbus_read_current(orno_meter_type_t type, float *current);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* ORNO_MODBUS_H_ */
|
||||
21
components/meter_orno_modbus/src/orno_modbus.c
Normal file
21
components/meter_orno_modbus/src/orno_modbus.c
Normal file
@@ -0,0 +1,21 @@
|
||||
#include "orno_modbus.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
static const char *TAG = "orno_modbus";
|
||||
|
||||
esp_err_t orno_modbus_init(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Initializing ORNO Modbus driver");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t orno_modbus_read_current(orno_meter_type_t type, float *current)
|
||||
{
|
||||
if (!current) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
// Stub implementation - replace with real Modbus queries
|
||||
*current = 0.0f;
|
||||
ESP_LOGD(TAG, "Read current type %d -> %f", type, *current);
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -6,4 +6,4 @@ set(srcs
|
||||
idf_component_register(SRCS "${srcs}"
|
||||
INCLUDE_DIRS "include"
|
||||
PRIV_REQUIRES nvs_flash driver
|
||||
REQUIRES config evse peripherals esp-modbus currentshaper)
|
||||
REQUIRES config evse peripherals esp-modbus loadbalancer)
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#include "mbcontroller.h"
|
||||
#include "sdkconfig.h"
|
||||
#include "evse_api.h"
|
||||
#include "currentshaper.h"
|
||||
#include "loadbalancer.h"
|
||||
#include "meter.h"
|
||||
|
||||
#define TXD_PIN (GPIO_NUM_17)
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#include "mbcontroller.h"
|
||||
#include "sdkconfig.h"
|
||||
#include "evse_api.h"
|
||||
#include "currentshaper.h"
|
||||
#include "loadbalancer.h"
|
||||
#include "meter.h"
|
||||
|
||||
#define TXD_PIN (GPIO_NUM_17)
|
||||
|
||||
@@ -5,4 +5,4 @@ set(srcs
|
||||
idf_component_register(SRCS "${srcs}"
|
||||
INCLUDE_DIRS "include"
|
||||
PRIV_REQUIRES driver
|
||||
REQUIRES config evse currentshaper serial_sync)
|
||||
REQUIRES config evse loadbalancer serial_sync)
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include "driver/gpio.h"
|
||||
#include "serial_mt.h"
|
||||
#include "evse_api.h"
|
||||
#include "currentshaper.h"
|
||||
#include "loadbalancer.h"
|
||||
#include "meter.h"
|
||||
//#include "app_main.h"
|
||||
//#include "sync_master.h"
|
||||
|
||||
@@ -12,7 +12,7 @@ set(srcs
|
||||
idf_component_register(SRCS "${srcs}"
|
||||
INCLUDE_DIRS "include" "protobuf"
|
||||
PRIV_REQUIRES driver esp_timer
|
||||
REQUIRES config evse currentshaper)
|
||||
REQUIRES config evse loadbalancer)
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
// #include "inc/version_autogen.h"
|
||||
|
||||
#include "sync_slave.h"
|
||||
#include "currentshaper.h"
|
||||
#include "loadbalancer.h"
|
||||
#include "evse_api.h"
|
||||
|
||||
#define VERSION_STRING "2.2"
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include "board_config.h"
|
||||
#include "wifi.h"
|
||||
#include "logger.h"
|
||||
#include "loadbalancer.h"
|
||||
|
||||
#include "auth.h"
|
||||
|
||||
@@ -229,6 +230,8 @@ static void init_modules(void)
|
||||
auth_init();
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user