Skip to content

Commit 7348368

Browse files
author
root
committed
20250811 提交充电功率分配模块
1 parent a352d68 commit 7348368

File tree

12 files changed

+1730
-25
lines changed

12 files changed

+1730
-25
lines changed

components/PowerModule/Kconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
menu "Power Module"
2+
config RT_USING_POWER_MODULE
3+
bool "Using Power Module"
4+
default n
5+
help
6+
Enable Power Module support
7+
8+
if RT_USING_POWER_MODULE
9+
config POWER_MODULE_EXAMPLE
10+
bool "Enable Power Module Example"
11+
default n
12+
help
13+
Enable Power Module example code
14+
15+
endif
16+
endmenu

components/PowerModule/SConscript

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from building import *
2+
3+
# 获取当前目录
4+
cwd = GetCurrentDir()
5+
6+
# 添加源文件
7+
src = []
8+
src += Glob('src/power_hal.c')
9+
src += Glob('src/power_rounter.c')
10+
src += Glob('src/power_engine.c')
11+
12+
# 如果需要编译示例代码,添加示例文件
13+
if GetDepend('POWER_MODULE_EXAMPLE'):
14+
src += Glob('examples/rounter_example.c')
15+
src += Glob('examples/power_system_example.c')
16+
17+
# 添加头文件搜索路径
18+
CPPPATH = [cwd + '/inc']
19+
20+
# 定义group
21+
group = DefineGroup('PowerModule', src, depend = ['RT_USING_POWER_MODULE'], CPPPATH = CPPPATH)
22+
23+
Return('group')
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
#include "power_engine.h"
2+
#include "power_hal.h"
3+
#include "power_rounter.h"
4+
#include <rtthread.h>
5+
#include <stdio.h>
6+
7+
// 全局对象
8+
static PowerHAL* g_hal = RT_NULL;
9+
static Class_RounterAgent g_router;
10+
static PowerEngine* g_engine = RT_NULL;
11+
12+
// 示例函数:初始化硬件抽象层
13+
static void init_hal_example(void)
14+
{
15+
// 创建硬件抽象层
16+
g_hal = power_hal_create();
17+
if (g_hal == RT_NULL)
18+
{
19+
rt_kprintf("Failed to create HAL\n");
20+
return;
21+
}
22+
23+
// 创建一些功率模块
24+
for (int i = 0; i < 10; i++)
25+
{
26+
PowerModule* module = power_module_create(g_hal, i, 50.0f); // 50A最大电流
27+
if (module != RT_NULL)
28+
{
29+
rt_kprintf("Created power module %d\n", i);
30+
}
31+
}
32+
33+
// 创建一些接触器
34+
for (int i = 0; i < 15; i++)
35+
{
36+
Contactor* contactor = contactor_create(g_hal, i);
37+
if (contactor != RT_NULL)
38+
{
39+
rt_kprintf("Created contactor %d\n", i);
40+
}
41+
}
42+
43+
// 创建一些功率节点
44+
for (int i = 0; i < 8; i++)
45+
{
46+
PowerNode* node = power_node_create(g_hal, i, 100.0f); // 100A最大负载
47+
if (node != RT_NULL)
48+
{
49+
rt_kprintf("Created power node %d\n", i);
50+
}
51+
}
52+
53+
rt_kprintf("HAL initialized with %d modules, %d contactors, %d nodes\n",
54+
g_hal->module_count, g_hal->contactor_count, g_hal->node_count);
55+
}
56+
57+
// 示例函数:初始化路由器
58+
static void init_router_example(void)
59+
{
60+
// 初始化路由器
61+
rounter_init(&g_router);
62+
63+
// 创建一些枪节点
64+
for (int i = 0; i < 5; i++)
65+
{
66+
Class_GunNode* gun = create_gun_node(&g_router, i);
67+
if (gun != RT_NULL)
68+
{
69+
rt_kprintf("Created gun node %d\n", i);
70+
}
71+
}
72+
73+
// 创建一些功率模块节点
74+
for (int i = 0; i < 10; i++)
75+
{
76+
Class_PmNode* pm = create_pm_node(&g_router, i);
77+
if (pm != RT_NULL)
78+
{
79+
pm->max_power = 50.0f; // 50KW最大功率
80+
rt_kprintf("Created PM node %d\n", i);
81+
}
82+
}
83+
84+
rt_kprintf("Router initialized with %d guns, %d PMs\n",
85+
g_router.gun_count, g_router.pm_count);
86+
}
87+
88+
// 示例函数:初始化功率分配引擎
89+
static void init_engine_example(void)
90+
{
91+
// 创建功率分配引擎
92+
g_engine = power_engine_create(&g_router, g_hal);
93+
if (g_engine == RT_NULL)
94+
{
95+
rt_kprintf("Failed to create power engine\n");
96+
return;
97+
}
98+
99+
// 设置分配策略
100+
power_engine_set_strategy(g_engine, POWER_ALLOCATION_STRATEGY_BALANCE);
101+
102+
rt_kprintf("Power engine created\n");
103+
}
104+
105+
// 示例函数:模拟枪连接和功率分配
106+
static void simulate_gun_connection(void)
107+
{
108+
// 模拟枪0连接,需要60KW功率
109+
rt_list_t *node;
110+
Class_GunNode* gun = RT_NULL;
111+
112+
// 查找枪0
113+
rt_list_for_each(node, &g_router.gun_list)
114+
{
115+
gun = rt_list_entry(node, Class_GunNode, list);
116+
if (gun->id == 0)
117+
{
118+
break;
119+
}
120+
gun = RT_NULL;
121+
}
122+
123+
if (gun != RT_NULL)
124+
{
125+
gun->is_connected = 1; // 连接枪
126+
gun->required_power = 60.0f; // 需要60KW功率
127+
rt_kprintf("Gun 0 connected, required power: %.2f KW\n", gun->required_power);
128+
129+
// 为枪0分配功率模块
130+
int pm_count = 0;
131+
rt_list_t *pm_node;
132+
Class_PmNode* pm;
133+
rt_list_for_each(pm_node, &g_router.pm_list)
134+
{
135+
if (pm_count >= 2) break; // 分配2个功率模块
136+
137+
pm = rt_list_entry(pm_node, Class_PmNode, list);
138+
if (!pm->is_assigned)
139+
{
140+
// 分配功率模块给枪
141+
if (assign_pm_to_gun(&g_router, gun, pm) == RT_EOK)
142+
{
143+
pm->current_power = 30.0f; // 每个模块分配30KW
144+
rt_kprintf("Assigned PM %d to Gun 0\n", pm->id);
145+
pm_count++;
146+
}
147+
}
148+
}
149+
150+
rt_kprintf("Assigned %d PMs to Gun 0, total power: %.2f KW\n",
151+
gun->assigned_pm_count, calculate_total_assigned_power(gun));
152+
}
153+
}
154+
155+
// 示例函数:启动功率分配引擎
156+
static void start_engine_example(void)
157+
{
158+
if (g_engine != RT_NULL)
159+
{
160+
rt_err_t result = power_engine_start(g_engine);
161+
if (result == RT_EOK)
162+
{
163+
rt_kprintf("Power engine started\n");
164+
}
165+
else
166+
{
167+
rt_kprintf("Failed to start power engine: %d\n", result);
168+
}
169+
}
170+
}
171+
172+
// 示例函数:清理资源
173+
static void cleanup_example(void)
174+
{
175+
if (g_engine != RT_NULL)
176+
{
177+
power_engine_stop(g_engine);
178+
power_engine_destroy(g_engine);
179+
g_engine = RT_NULL;
180+
}
181+
182+
if (g_hal != RT_NULL)
183+
{
184+
power_hal_destroy(g_hal);
185+
g_hal = RT_NULL;
186+
}
187+
188+
rt_kprintf("Resources cleaned up\n");
189+
}
190+
191+
// 主示例函数
192+
static int power_system_example(void)
193+
{
194+
rt_kprintf("=== Power System Example ===\n");
195+
196+
// 初始化硬件抽象层
197+
init_hal_example();
198+
199+
// 初始化路由器
200+
init_router_example();
201+
202+
// 初始化功率分配引擎
203+
init_engine_example();
204+
205+
// 模拟枪连接和功率分配
206+
simulate_gun_connection();
207+
208+
// 启动功率分配引擎
209+
start_engine_example();
210+
211+
rt_kprintf("=== Power System Example Completed ===\n");
212+
213+
return 0;
214+
}
215+
216+
// 注册为msh命令
217+
MSH_CMD_EXPORT(power_system_example, power system example);
218+
219+
// 清理命令
220+
static int power_system_cleanup(void)
221+
{
222+
cleanup_example();
223+
return 0;
224+
}
225+
226+
MSH_CMD_EXPORT(power_system_cleanup, power system cleanup);
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include "power_rounter.h"
2+
#include <rtthread.h>
3+
#include <stdio.h>
4+
5+
// 定义路由器对象
6+
static Class_RounterAgent g_router;
7+
8+
// 示例gun节点初始化函数
9+
void example_init_gun_node(void *p_argus)
10+
{
11+
Class_GunNode* gun = (Class_GunNode*)p_argus;
12+
rt_kprintf("Initializing gun node %d\n", gun->id);
13+
}
14+
15+
// 示例pm节点初始化函数
16+
void example_init_pm_node(void *p_argus)
17+
{
18+
Class_PmNode* pm = (Class_PmNode*)p_argus;
19+
rt_kprintf("Initializing pm node %d\n", pm->id);
20+
}
21+
22+
static int rounter_example(void)
23+
{
24+
// 初始化路由器
25+
rounter_init(&g_router);
26+
rt_kprintf("Router initialized\n");
27+
28+
// 创建10个gun节点
29+
for (int i = 0; i < 10; i++)
30+
{
31+
Class_GunNode* gun = create_gun_node(&g_router, i);
32+
if (gun != RT_NULL)
33+
{
34+
// 设置gun节点的初始化函数
35+
gun->gun_node_fops.init_gun_node = example_init_gun_node;
36+
37+
// 调用初始化函数
38+
if (gun->gun_node_fops.init_gun_node != RT_NULL)
39+
{
40+
gun->gun_node_fops.init_gun_node(gun);
41+
}
42+
}
43+
else
44+
{
45+
rt_kprintf("Failed to create gun node %d\n", i);
46+
}
47+
}
48+
49+
// 创建10个pm节点
50+
for (int i = 0; i < 10; i++)
51+
{
52+
Class_PmNode* pm = create_pm_node(&g_router, i);
53+
if (pm != RT_NULL)
54+
{
55+
// 设置pm节点的初始化函数
56+
pm->pm_fops.init_pm_node = example_init_pm_node;
57+
58+
// 调用初始化函数
59+
if (pm->pm_fops.init_pm_node != RT_NULL)
60+
{
61+
pm->pm_fops.init_pm_node(pm);
62+
}
63+
}
64+
else
65+
{
66+
rt_kprintf("Failed to create pm node %d\n", i);
67+
}
68+
}
69+
70+
rt_kprintf("Created %d gun nodes and %d pm nodes\n",
71+
g_router.gun_count, g_router.pm_count);
72+
73+
// 遍历并打印所有gun节点
74+
rt_kprintf("Gun nodes:\n");
75+
rt_list_t *node;
76+
rt_list_for_each(node, &g_router.gun_list)
77+
{
78+
Class_GunNode *gun = rt_list_entry(node, Class_GunNode, list);
79+
rt_kprintf(" Gun ID: %d\n", gun->id);
80+
}
81+
82+
// 遍历并打印所有pm节点
83+
rt_kprintf("PM nodes:\n");
84+
rt_list_for_each(node, &g_router.pm_list)
85+
{
86+
Class_PmNode *pm = rt_list_entry(node, Class_PmNode, list);
87+
rt_kprintf(" PM ID: %d\n", pm->id);
88+
}
89+
90+
return 0;
91+
}
92+
93+
// 注册为msh命令
94+
MSH_CMD_EXPORT(rounter_example, router example);

0 commit comments

Comments
 (0)