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 );
0 commit comments