PLSR执行学习
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

330 lines
11 KiB

  1. /**
  2. * @file PLSR_测试示例.c
  3. * @brief PLSR脉冲输出控制系统测试示例
  4. * @version V2.0
  5. * @date 2025-01
  6. *
  7. * 本文件提供了PLSR系统的基本使用示例,包括:
  8. * 1. 系统初始化
  9. * 2. 多段脉冲配置
  10. * 3. 不同等待条件的使用
  11. * 4. 运行时控制和监控
  12. */
  13. #include "tim.h"
  14. #include "main.h"
  15. // 外部全局变量声明
  16. extern PLSR_RouteConfig_t g_plsr_route;
  17. extern uint32_t g_plsr_system_tick;
  18. /**
  19. * @brief PLSR系统初始化示例
  20. * @retval None
  21. */
  22. void PLSR_Example_Init(void)
  23. {
  24. // 1. 初始化PWM和计数器
  25. PLSR_PWM_Init();
  26. // 2. 配置TIM6更新频率为1ms (1000微秒)
  27. PLSR_TIM6_SetUpdateFreq(1000);
  28. // 3. 初始化路径配置
  29. PLSR_Route_Init(&g_plsr_route);
  30. printf("PLSR系统初始化完成\r\n");
  31. }
  32. /**
  33. * @brief 基本两段脉冲输出示例
  34. * @retval None
  35. */
  36. void PLSR_Example_TwoSection(void)
  37. {
  38. // 设置路径基本参数
  39. g_plsr_route.start_freq = 1000; // 起始频率1kHz
  40. g_plsr_route.end_freq = 0; // 结束频率0Hz
  41. g_plsr_route.mode = PLSR_MODE_RELATIVE; // 相对模式
  42. g_plsr_route.direction = PLSR_DIR_FORWARD; // 正向
  43. g_plsr_route.output_port = 1; // 输出端口1
  44. // 配置第1段:加速到5kHz,发送10000个脉冲
  45. g_plsr_route.section[0].section_num = 1;
  46. g_plsr_route.section[0].target_freq = 5000; // 目标频率5kHz
  47. g_plsr_route.section[0].target_pulse = 10000; // 目标脉冲10000个
  48. g_plsr_route.section[0].next_section = 2; // 下一段为第2段
  49. // 第1段加减速配置
  50. g_plsr_route.section[0].accel_config.accel_time_ms = 2000; // 加速时间2s
  51. g_plsr_route.section[0].accel_config.decel_time_ms = 1500; // 减速时间1.5s
  52. g_plsr_route.section[0].accel_config.accel_algorithm = PLSR_ACCEL_CURVE; // S曲线
  53. // 第1段等待条件:脉冲结束后立即切换
  54. g_plsr_route.section[0].wait_condition.wait_type = PLSR_WAIT_PLUSEEND;
  55. // 配置第2段:减速到3kHz,发送5000个脉冲
  56. g_plsr_route.section[1].section_num = 2;
  57. g_plsr_route.section[1].target_freq = 3000; // 目标频率3kHz
  58. g_plsr_route.section[1].target_pulse = 5000; // 目标脉冲5000个
  59. g_plsr_route.section[1].next_section = 0; // 结束(下一段为0)
  60. // 第2段加减速配置
  61. g_plsr_route.section[1].accel_config.accel_time_ms = 1000; // 加速时间1s
  62. g_plsr_route.section[1].accel_config.decel_time_ms = 2000; // 减速时间2s
  63. g_plsr_route.section[1].accel_config.accel_algorithm = PLSR_ACCEL_LINEAR; // 线性
  64. // 第2段等待条件:等待500ms后切换
  65. g_plsr_route.section[1].wait_condition.wait_type = PLSR_WAIT_TIME;
  66. g_plsr_route.section[1].wait_condition.wait_time_ms = 500;
  67. // 启动路径执行
  68. PLSR_Route_Start(&g_plsr_route);
  69. printf("两段脉冲输出启动\r\n");
  70. }
  71. /**
  72. * @brief 复杂多段脉冲输出示例(包含各种等待条件)
  73. * @retval None
  74. */
  75. void PLSR_Example_MultiSection(void)
  76. {
  77. // 设置路径基本参数
  78. g_plsr_route.start_freq = 500; // 起始频率500Hz
  79. g_plsr_route.end_freq = 0; // 结束频率0Hz
  80. g_plsr_route.mode = PLSR_MODE_ABSOLUTE; // 绝对模式
  81. g_plsr_route.direction = PLSR_DIR_FORWARD; // 正向
  82. // 第1段:基本加速段
  83. g_plsr_route.section[0].section_num = 1;
  84. g_plsr_route.section[0].target_freq = 2000;
  85. g_plsr_route.section[0].target_pulse = 5000;
  86. g_plsr_route.section[0].next_section = 2;
  87. g_plsr_route.section[0].accel_config.accel_time_ms = 1500;
  88. g_plsr_route.section[0].accel_config.decel_time_ms = 1000;
  89. g_plsr_route.section[0].accel_config.accel_algorithm = PLSR_ACCEL_SINE;
  90. g_plsr_route.section[0].wait_condition.wait_type = PLSR_WAIT_PLUSEEND;
  91. // 第2段:等待时间条件
  92. g_plsr_route.section[1].section_num = 2;
  93. g_plsr_route.section[1].target_freq = 4000;
  94. g_plsr_route.section[1].target_pulse = 12000;
  95. g_plsr_route.section[1].next_section = 3;
  96. g_plsr_route.section[1].accel_config.accel_time_ms = 2000;
  97. g_plsr_route.section[1].accel_config.decel_time_ms = 1500;
  98. g_plsr_route.section[1].accel_config.accel_algorithm = PLSR_ACCEL_CURVE;
  99. g_plsr_route.section[1].wait_condition.wait_type = PLSR_WAIT_TIME;
  100. g_plsr_route.section[1].wait_condition.wait_time_ms = 1000; // 等待1秒
  101. // 第3段:ACT时间条件
  102. g_plsr_route.section[2].section_num = 3;
  103. g_plsr_route.section[2].target_freq = 3000;
  104. g_plsr_route.section[2].target_pulse = 18000;
  105. g_plsr_route.section[2].next_section = 4;
  106. g_plsr_route.section[2].accel_config.accel_time_ms = 1000;
  107. g_plsr_route.section[2].accel_config.decel_time_ms = 2000;
  108. g_plsr_route.section[2].accel_config.accel_algorithm = PLSR_ACCEL_LINEAR;
  109. g_plsr_route.section[2].wait_condition.wait_type = PLSR_WAIT_ACT_TIME;
  110. g_plsr_route.section[2].wait_condition.act_time_ms = 3000; // ACT时间3秒
  111. // 第4段:外部事件条件
  112. g_plsr_route.section[3].section_num = 4;
  113. g_plsr_route.section[3].target_freq = 1500;
  114. g_plsr_route.section[3].target_pulse = 22000;
  115. g_plsr_route.section[3].next_section = 0; // 结束
  116. g_plsr_route.section[3].accel_config.accel_time_ms = 1500;
  117. g_plsr_route.section[3].accel_config.decel_time_ms = 2500;
  118. g_plsr_route.section[3].accel_config.accel_algorithm = PLSR_ACCEL_CURVE;
  119. g_plsr_route.section[3].wait_condition.wait_type = PLSR_WAIT_EXT_EVENT;
  120. // 启动路径执行
  121. PLSR_Route_Start(&g_plsr_route);
  122. printf("多段脉冲输出启动\r\n");
  123. }
  124. /**
  125. * @brief 运行时控制示例
  126. * @retval None
  127. */
  128. void PLSR_Example_RuntimeControl(void)
  129. {
  130. // 检查运行状态
  131. if (PLSR_Route_IsRunning(&g_plsr_route)) {
  132. printf("路径正在运行中...\r\n");
  133. // 获取当前状态信息
  134. uint8_t current_section = g_plsr_route.current_section_num;
  135. uint32_t current_pulse = g_plsr_route.pulse_count;
  136. uint32_t current_freq = PLSR_PWM_GetFrequency();
  137. PLSR_RunState_t run_state = g_plsr_route.run_state;
  138. printf("当前段号: %d\r\n", current_section);
  139. printf("当前脉冲: %lu\r\n", current_pulse);
  140. printf("当前频率: %lu Hz\r\n", current_freq);
  141. printf("运行状态: %d\r\n", run_state);
  142. // 根据需要触发外部事件
  143. static uint32_t last_trigger_time = 0;
  144. if (g_plsr_system_tick - last_trigger_time > 5000) { // 5秒触发一次
  145. PLSR_SetExtEvent(1);
  146. last_trigger_time = g_plsr_system_tick;
  147. printf("外部事件已触发\r\n");
  148. }
  149. // 在第2段时设置条件标志
  150. if (current_section == 2 && current_pulse > 8000) {
  151. PLSR_SetSectionCondition(&g_plsr_route, 2, 1);
  152. printf("段条件标志已设置\r\n");
  153. }
  154. } else {
  155. printf("路径已停止\r\n");
  156. }
  157. }
  158. /**
  159. * @brief 紧急停止示例
  160. * @retval None
  161. */
  162. void PLSR_Example_EmergencyStop(void)
  163. {
  164. if (PLSR_Route_IsRunning(&g_plsr_route)) {
  165. PLSR_Route_Stop(&g_plsr_route);
  166. printf("紧急停止执行\r\n");
  167. }
  168. }
  169. /**
  170. * @brief 路径重置示例
  171. * @retval None
  172. */
  173. void PLSR_Example_Reset(void)
  174. {
  175. PLSR_Route_Reset(&g_plsr_route);
  176. printf("路径已重置\r\n");
  177. }
  178. /**
  179. * @brief 系统状态监控示例
  180. * @retval None
  181. */
  182. void PLSR_Example_StatusMonitor(void)
  183. {
  184. static uint32_t last_monitor_time = 0;
  185. // 每1秒监控一次
  186. if (g_plsr_system_tick - last_monitor_time >= 1000) {
  187. last_monitor_time = g_plsr_system_tick;
  188. printf("=== PLSR系统状态监控 ===\r\n");
  189. printf("系统时钟: %lu ms\r\n", g_plsr_system_tick);
  190. printf("路径状态: %d\r\n", g_plsr_route.route_state);
  191. printf("运行状态: %d\r\n", g_plsr_route.run_state);
  192. printf("当前段号: %d\r\n", g_plsr_route.current_section_num);
  193. printf("当前频率: %lu Hz\r\n", g_plsr_route.current_freq);
  194. printf("目标频率: %lu Hz\r\n", g_plsr_route.target_freq);
  195. printf("脉冲计数: %lu\r\n", g_plsr_route.pulse_count);
  196. printf("PWM运行: %s\r\n", PLSR_PWM_IsRunning() ? "是" : "否");
  197. printf("TIM6频率: %lu us\r\n", PLSR_TIM6_GetUpdateFreq());
  198. printf("========================\r\n");
  199. }
  200. }
  201. /**
  202. * @brief 主测试函数
  203. * @retval None
  204. */
  205. void PLSR_Example_Main(void)
  206. {
  207. static uint8_t test_stage = 0;
  208. static uint32_t stage_start_time = 0;
  209. switch (test_stage) {
  210. case 0: // 初始化阶段
  211. PLSR_Example_Init();
  212. test_stage = 1;
  213. stage_start_time = g_plsr_system_tick;
  214. break;
  215. case 1: // 等待2秒后启动基本测试
  216. if (g_plsr_system_tick - stage_start_time > 2000) {
  217. PLSR_Example_TwoSection();
  218. test_stage = 2;
  219. stage_start_time = g_plsr_system_tick;
  220. }
  221. break;
  222. case 2: // 运行时监控
  223. PLSR_Example_RuntimeControl();
  224. PLSR_Example_StatusMonitor();
  225. // 如果路径完成,等待5秒后启动复杂测试
  226. if (!PLSR_Route_IsRunning(&g_plsr_route)) {
  227. if (g_plsr_system_tick - stage_start_time > 5000) {
  228. PLSR_Example_Reset();
  229. PLSR_Example_MultiSection();
  230. test_stage = 3;
  231. stage_start_time = g_plsr_system_tick;
  232. }
  233. }
  234. break;
  235. case 3: // 复杂测试监控
  236. PLSR_Example_RuntimeControl();
  237. PLSR_Example_StatusMonitor();
  238. // 测试完成后重置
  239. if (!PLSR_Route_IsRunning(&g_plsr_route)) {
  240. if (g_plsr_system_tick - stage_start_time > 3000) {
  241. PLSR_Example_Reset();
  242. test_stage = 1; // 循环测试
  243. stage_start_time = g_plsr_system_tick;
  244. }
  245. }
  246. break;
  247. default:
  248. test_stage = 0;
  249. break;
  250. }
  251. }
  252. /**
  253. * @brief 按键控制示例(可在按键中断中调用)
  254. * @param key_num: 按键编号
  255. * @retval None
  256. */
  257. void PLSR_Example_KeyControl(uint8_t key_num)
  258. {
  259. switch (key_num) {
  260. case 1: // 按键1:启动基本测试
  261. PLSR_Example_Reset();
  262. PLSR_Example_TwoSection();
  263. break;
  264. case 2: // 按键2:启动复杂测试
  265. PLSR_Example_Reset();
  266. PLSR_Example_MultiSection();
  267. break;
  268. case 3: // 按键3:触发外部事件
  269. PLSR_SetExtEvent(1);
  270. break;
  271. case 4: // 按键4:紧急停止
  272. PLSR_Example_EmergencyStop();
  273. break;
  274. default:
  275. break;
  276. }
  277. }
  278. /* 使用说明:
  279. * 1. 在main函数的主循环中调用 PLSR_Example_Main()
  280. * 2. 在按键中断中调用 PLSR_Example_KeyControl(key_num)
  281. * 3. 确保TIM6中断正常工作
  282. * 4. 根据实际硬件调整PWM输出引脚配置
  283. * 5. 根据需要修改测试参数
  284. */