|
- /**
- * @file PLSR_测试示例.c
- * @brief PLSR脉冲输出控制系统测试示例
- * @version V2.0
- * @date 2025-01
- *
- * 本文件提供了PLSR系统的基本使用示例,包括:
- * 1. 系统初始化
- * 2. 多段脉冲配置
- * 3. 不同等待条件的使用
- * 4. 运行时控制和监控
- */
-
- #include "tim.h"
- #include "main.h"
-
- // 外部全局变量声明
- extern PLSR_RouteConfig_t g_plsr_route;
- extern uint32_t g_plsr_system_tick;
-
- /**
- * @brief PLSR系统初始化示例
- * @retval None
- */
- void PLSR_Example_Init(void)
- {
- // 1. 初始化PWM和计数器
- PLSR_PWM_Init();
-
- // 2. 配置TIM6更新频率为1ms (1000微秒)
- PLSR_TIM6_SetUpdateFreq(1000);
-
- // 3. 初始化路径配置
- PLSR_Route_Init(&g_plsr_route);
-
- printf("PLSR系统初始化完成\r\n");
- }
-
- /**
- * @brief 基本两段脉冲输出示例
- * @retval None
- */
- void PLSR_Example_TwoSection(void)
- {
- // 设置路径基本参数
- g_plsr_route.start_freq = 1000; // 起始频率1kHz
- g_plsr_route.end_freq = 0; // 结束频率0Hz
- g_plsr_route.mode = PLSR_MODE_RELATIVE; // 相对模式
- g_plsr_route.direction = PLSR_DIR_FORWARD; // 正向
- g_plsr_route.output_port = 1; // 输出端口1
-
- // 配置第1段:加速到5kHz,发送10000个脉冲
- g_plsr_route.section[0].section_num = 1;
- g_plsr_route.section[0].target_freq = 5000; // 目标频率5kHz
- g_plsr_route.section[0].target_pulse = 10000; // 目标脉冲10000个
- g_plsr_route.section[0].next_section = 2; // 下一段为第2段
-
- // 第1段加减速配置
- g_plsr_route.section[0].accel_config.accel_time_ms = 2000; // 加速时间2s
- g_plsr_route.section[0].accel_config.decel_time_ms = 1500; // 减速时间1.5s
- g_plsr_route.section[0].accel_config.accel_algorithm = PLSR_ACCEL_CURVE; // S曲线
-
- // 第1段等待条件:脉冲结束后立即切换
- g_plsr_route.section[0].wait_condition.wait_type = PLSR_WAIT_PLUSEEND;
-
- // 配置第2段:减速到3kHz,发送5000个脉冲
- g_plsr_route.section[1].section_num = 2;
- g_plsr_route.section[1].target_freq = 3000; // 目标频率3kHz
- g_plsr_route.section[1].target_pulse = 5000; // 目标脉冲5000个
- g_plsr_route.section[1].next_section = 0; // 结束(下一段为0)
-
- // 第2段加减速配置
- g_plsr_route.section[1].accel_config.accel_time_ms = 1000; // 加速时间1s
- g_plsr_route.section[1].accel_config.decel_time_ms = 2000; // 减速时间2s
- g_plsr_route.section[1].accel_config.accel_algorithm = PLSR_ACCEL_LINEAR; // 线性
-
- // 第2段等待条件:等待500ms后切换
- g_plsr_route.section[1].wait_condition.wait_type = PLSR_WAIT_TIME;
- g_plsr_route.section[1].wait_condition.wait_time_ms = 500;
-
- // 启动路径执行
- PLSR_Route_Start(&g_plsr_route);
-
- printf("两段脉冲输出启动\r\n");
- }
-
- /**
- * @brief 复杂多段脉冲输出示例(包含各种等待条件)
- * @retval None
- */
- void PLSR_Example_MultiSection(void)
- {
- // 设置路径基本参数
- g_plsr_route.start_freq = 500; // 起始频率500Hz
- g_plsr_route.end_freq = 0; // 结束频率0Hz
- g_plsr_route.mode = PLSR_MODE_ABSOLUTE; // 绝对模式
- g_plsr_route.direction = PLSR_DIR_FORWARD; // 正向
-
- // 第1段:基本加速段
- g_plsr_route.section[0].section_num = 1;
- g_plsr_route.section[0].target_freq = 2000;
- g_plsr_route.section[0].target_pulse = 5000;
- g_plsr_route.section[0].next_section = 2;
- g_plsr_route.section[0].accel_config.accel_time_ms = 1500;
- g_plsr_route.section[0].accel_config.decel_time_ms = 1000;
- g_plsr_route.section[0].accel_config.accel_algorithm = PLSR_ACCEL_SINE;
- g_plsr_route.section[0].wait_condition.wait_type = PLSR_WAIT_PLUSEEND;
-
- // 第2段:等待时间条件
- g_plsr_route.section[1].section_num = 2;
- g_plsr_route.section[1].target_freq = 4000;
- g_plsr_route.section[1].target_pulse = 12000;
- g_plsr_route.section[1].next_section = 3;
- g_plsr_route.section[1].accel_config.accel_time_ms = 2000;
- g_plsr_route.section[1].accel_config.decel_time_ms = 1500;
- g_plsr_route.section[1].accel_config.accel_algorithm = PLSR_ACCEL_CURVE;
- g_plsr_route.section[1].wait_condition.wait_type = PLSR_WAIT_TIME;
- g_plsr_route.section[1].wait_condition.wait_time_ms = 1000; // 等待1秒
-
- // 第3段:ACT时间条件
- g_plsr_route.section[2].section_num = 3;
- g_plsr_route.section[2].target_freq = 3000;
- g_plsr_route.section[2].target_pulse = 18000;
- g_plsr_route.section[2].next_section = 4;
- g_plsr_route.section[2].accel_config.accel_time_ms = 1000;
- g_plsr_route.section[2].accel_config.decel_time_ms = 2000;
- g_plsr_route.section[2].accel_config.accel_algorithm = PLSR_ACCEL_LINEAR;
- g_plsr_route.section[2].wait_condition.wait_type = PLSR_WAIT_ACT_TIME;
- g_plsr_route.section[2].wait_condition.act_time_ms = 3000; // ACT时间3秒
-
- // 第4段:外部事件条件
- g_plsr_route.section[3].section_num = 4;
- g_plsr_route.section[3].target_freq = 1500;
- g_plsr_route.section[3].target_pulse = 22000;
- g_plsr_route.section[3].next_section = 0; // 结束
- g_plsr_route.section[3].accel_config.accel_time_ms = 1500;
- g_plsr_route.section[3].accel_config.decel_time_ms = 2500;
- g_plsr_route.section[3].accel_config.accel_algorithm = PLSR_ACCEL_CURVE;
- g_plsr_route.section[3].wait_condition.wait_type = PLSR_WAIT_EXT_EVENT;
-
- // 启动路径执行
- PLSR_Route_Start(&g_plsr_route);
-
- printf("多段脉冲输出启动\r\n");
- }
-
- /**
- * @brief 运行时控制示例
- * @retval None
- */
- void PLSR_Example_RuntimeControl(void)
- {
- // 检查运行状态
- if (PLSR_Route_IsRunning(&g_plsr_route)) {
- printf("路径正在运行中...\r\n");
-
- // 获取当前状态信息
- uint8_t current_section = g_plsr_route.current_section_num;
- uint32_t current_pulse = g_plsr_route.pulse_count;
- uint32_t current_freq = PLSR_PWM_GetFrequency();
- PLSR_RunState_t run_state = g_plsr_route.run_state;
-
- printf("当前段号: %d\r\n", current_section);
- printf("当前脉冲: %lu\r\n", current_pulse);
- printf("当前频率: %lu Hz\r\n", current_freq);
- printf("运行状态: %d\r\n", run_state);
-
- // 根据需要触发外部事件
- static uint32_t last_trigger_time = 0;
- if (g_plsr_system_tick - last_trigger_time > 5000) { // 5秒触发一次
- PLSR_SetExtEvent(1);
- last_trigger_time = g_plsr_system_tick;
- printf("外部事件已触发\r\n");
- }
-
- // 在第2段时设置条件标志
- if (current_section == 2 && current_pulse > 8000) {
- PLSR_SetSectionCondition(&g_plsr_route, 2, 1);
- printf("段条件标志已设置\r\n");
- }
- } else {
- printf("路径已停止\r\n");
- }
- }
-
- /**
- * @brief 紧急停止示例
- * @retval None
- */
- void PLSR_Example_EmergencyStop(void)
- {
- if (PLSR_Route_IsRunning(&g_plsr_route)) {
- PLSR_Route_Stop(&g_plsr_route);
- printf("紧急停止执行\r\n");
- }
- }
-
- /**
- * @brief 路径重置示例
- * @retval None
- */
- void PLSR_Example_Reset(void)
- {
- PLSR_Route_Reset(&g_plsr_route);
- printf("路径已重置\r\n");
- }
-
- /**
- * @brief 系统状态监控示例
- * @retval None
- */
- void PLSR_Example_StatusMonitor(void)
- {
- static uint32_t last_monitor_time = 0;
-
- // 每1秒监控一次
- if (g_plsr_system_tick - last_monitor_time >= 1000) {
- last_monitor_time = g_plsr_system_tick;
-
- printf("=== PLSR系统状态监控 ===\r\n");
- printf("系统时钟: %lu ms\r\n", g_plsr_system_tick);
- printf("路径状态: %d\r\n", g_plsr_route.route_state);
- printf("运行状态: %d\r\n", g_plsr_route.run_state);
- printf("当前段号: %d\r\n", g_plsr_route.current_section_num);
- printf("当前频率: %lu Hz\r\n", g_plsr_route.current_freq);
- printf("目标频率: %lu Hz\r\n", g_plsr_route.target_freq);
- printf("脉冲计数: %lu\r\n", g_plsr_route.pulse_count);
- printf("PWM运行: %s\r\n", PLSR_PWM_IsRunning() ? "是" : "否");
- printf("TIM6频率: %lu us\r\n", PLSR_TIM6_GetUpdateFreq());
- printf("========================\r\n");
- }
- }
-
- /**
- * @brief 主测试函数
- * @retval None
- */
- void PLSR_Example_Main(void)
- {
- static uint8_t test_stage = 0;
- static uint32_t stage_start_time = 0;
-
- switch (test_stage) {
- case 0: // 初始化阶段
- PLSR_Example_Init();
- test_stage = 1;
- stage_start_time = g_plsr_system_tick;
- break;
-
- case 1: // 等待2秒后启动基本测试
- if (g_plsr_system_tick - stage_start_time > 2000) {
- PLSR_Example_TwoSection();
- test_stage = 2;
- stage_start_time = g_plsr_system_tick;
- }
- break;
-
- case 2: // 运行时监控
- PLSR_Example_RuntimeControl();
- PLSR_Example_StatusMonitor();
-
- // 如果路径完成,等待5秒后启动复杂测试
- if (!PLSR_Route_IsRunning(&g_plsr_route)) {
- if (g_plsr_system_tick - stage_start_time > 5000) {
- PLSR_Example_Reset();
- PLSR_Example_MultiSection();
- test_stage = 3;
- stage_start_time = g_plsr_system_tick;
- }
- }
- break;
-
- case 3: // 复杂测试监控
- PLSR_Example_RuntimeControl();
- PLSR_Example_StatusMonitor();
-
- // 测试完成后重置
- if (!PLSR_Route_IsRunning(&g_plsr_route)) {
- if (g_plsr_system_tick - stage_start_time > 3000) {
- PLSR_Example_Reset();
- test_stage = 1; // 循环测试
- stage_start_time = g_plsr_system_tick;
- }
- }
- break;
-
- default:
- test_stage = 0;
- break;
- }
- }
-
- /**
- * @brief 按键控制示例(可在按键中断中调用)
- * @param key_num: 按键编号
- * @retval None
- */
- void PLSR_Example_KeyControl(uint8_t key_num)
- {
- switch (key_num) {
- case 1: // 按键1:启动基本测试
- PLSR_Example_Reset();
- PLSR_Example_TwoSection();
- break;
-
- case 2: // 按键2:启动复杂测试
- PLSR_Example_Reset();
- PLSR_Example_MultiSection();
- break;
-
- case 3: // 按键3:触发外部事件
- PLSR_SetExtEvent(1);
- break;
-
- case 4: // 按键4:紧急停止
- PLSR_Example_EmergencyStop();
- break;
-
- default:
- break;
- }
- }
-
- /* 使用说明:
- * 1. 在main函数的主循环中调用 PLSR_Example_Main()
- * 2. 在按键中断中调用 PLSR_Example_KeyControl(key_num)
- * 3. 确保TIM6中断正常工作
- * 4. 根据实际硬件调整PWM输出引脚配置
- * 5. 根据需要修改测试参数
- */
|