| @@ -29,6 +29,7 @@ uint8_t g_plsr_ext_event_flag = 0; // 外部事件标志(0-无事件, 1- | |||||
| int32_t g_plsr_location = 0; // 全局位置计数器(用于记录当前执行位置,支持负数) | int32_t g_plsr_location = 0; // 全局位置计数器(用于记录当前执行位置,支持负数) | ||||
| static uint8_t s_pulse_count_direction = 1; // 脉冲计数方向(1-递增, 0-递减),用于替代dir_logic判断计数方向 | static uint8_t s_pulse_count_direction = 1; // 脉冲计数方向(1-递增, 0-递减),用于替代dir_logic判断计数方向 | ||||
| uint32_t g_plsr_current_target_freq = 0; // 当前段目标频率频率(Hz),用于检测是否有频率变化 | uint32_t g_plsr_current_target_freq = 0; // 当前段目标频率频率(Hz),用于检测是否有频率变化 | ||||
| static uint8_t s_last_direction = 0xFF; // 初始值设为无效值,确保第一次总是认为有方向变化 | |||||
| // ==================== PLSR内部变量 ==================== | // ==================== PLSR内部变量 ==================== | ||||
| static uint32_t s_tim6_update_freq_us = 1000; // TIM6更新频率(微秒) | static uint32_t s_tim6_update_freq_us = 1000; // TIM6更新频率(微秒) | ||||
| @@ -1180,7 +1181,6 @@ void Calculate_PluseNum(PLSR_RouteConfig_t *route) | |||||
| uint32_t a = route->accel_rate; // 加速度 | uint32_t a = route->accel_rate; // 加速度 | ||||
| uint32_t d = route->decel_rate; // 减速度 | uint32_t d = route->decel_rate; // 减速度 | ||||
| // 使用已计算的actual_pulse作为总脉冲数 | |||||
| // 在PLSR_Section_StartNewSection中已经根据模式计算了actual_pulse | // 在PLSR_Section_StartNewSection中已经根据模式计算了actual_pulse | ||||
| int32_t total_pulses = current_section->actual_pulse; // 总脉冲数 | int32_t total_pulses = current_section->actual_pulse; // 总脉冲数 | ||||
| @@ -1684,9 +1684,21 @@ void PLSR_Route_Stop(PLSR_RouteConfig_t* route) | |||||
| // 重置计数器 | // 重置计数器 | ||||
| __HAL_TIM_SET_COUNTER(&htim2, 0); | __HAL_TIM_SET_COUNTER(&htim2, 0); | ||||
| route->prevPulseCount = 0; | |||||
| route->freq_step = 0; | route->freq_step = 0; | ||||
| // 重置方向相关变量 | |||||
| s_last_direction = 0xFF; // 重置为初始无效值,确保下次启动时正确设置方向 | |||||
| s_pulse_count_direction = 1; // 重置脉冲计数方向为默认值 | |||||
| route->accel_pulse_count = 0; | |||||
| route->const_pulse_count = 0; | |||||
| route->decel_pulse_count = 0; | |||||
| // 重置目标频率 | |||||
| route->target_freq = 0; | |||||
| g_plsr_current_target_freq = 0; | |||||
| } | } | ||||
| @@ -1698,7 +1710,6 @@ void PLSR_Route_Stop(PLSR_RouteConfig_t* route) | |||||
| * @note 根据脉冲方向逻辑和目标脉冲数设置方向端子状态 | * @note 根据脉冲方向逻辑和目标脉冲数设置方向端子状态 | ||||
| */ | */ | ||||
| // 存储上一段的方向信息,用于检测方向变化 | // 存储上一段的方向信息,用于检测方向变化 | ||||
| static uint8_t s_last_direction = 0xFF; // 初始值设为无效值,确保第一次总是认为有方向变化 | |||||
| static uint8_t PLSR_SetDirectionPin(PLSR_RouteConfig_t* route, PLSR_SectionConfig_t* current_section) | static uint8_t PLSR_SetDirectionPin(PLSR_RouteConfig_t* route, PLSR_SectionConfig_t* current_section) | ||||
| { | { | ||||
| if (route == NULL || current_section == NULL) return 0; | if (route == NULL || current_section == NULL) return 0; | ||||
| @@ -1720,10 +1731,13 @@ static uint8_t PLSR_SetDirectionPin(PLSR_RouteConfig_t* route, PLSR_SectionConfi | |||||
| s_pulse_count_direction = is_forward ? 1 : 0; | s_pulse_count_direction = is_forward ? 1 : 0; | ||||
| // 根据方向逻辑确定方向端子状态 | // 根据方向逻辑确定方向端子状态 | ||||
| if (route->dir_logic == 0) { | |||||
| if (route->dir_logic == 0) | |||||
| { | |||||
| // 正逻辑:正向脉冲方向端子置ON(1),反向脉冲方向端子置OFF(0) | // 正逻辑:正向脉冲方向端子置ON(1),反向脉冲方向端子置OFF(0) | ||||
| dir_pin_state = is_forward ? GPIO_PIN_SET : GPIO_PIN_RESET; | dir_pin_state = is_forward ? GPIO_PIN_SET : GPIO_PIN_RESET; | ||||
| } else { | |||||
| } | |||||
| else | |||||
| { | |||||
| // 负逻辑:正向脉冲方向端子置OFF(0),反向脉冲方向端子置ON(1) | // 负逻辑:正向脉冲方向端子置OFF(0),反向脉冲方向端子置ON(1) | ||||
| dir_pin_state = is_forward ? GPIO_PIN_RESET : GPIO_PIN_SET; | dir_pin_state = is_forward ? GPIO_PIN_RESET : GPIO_PIN_SET; | ||||
| } | } | ||||
| @@ -1776,8 +1790,17 @@ void PLSR_Section_StartNewSection(PLSR_RouteConfig_t* route) | |||||
| } | } | ||||
| else | else | ||||
| { | { | ||||
| // 相对模式:直接使用目标脉冲数 | |||||
| current_section->actual_pulse = current_section->target_pulse + route->prevPulseCount - g_plsr_location; | |||||
| if(current_section->target_pulse > 0) | |||||
| { | |||||
| current_section->actual_pulse = (current_section->target_pulse - __HAL_TIM_GET_COUNTER(&htim2)); | |||||
| } | |||||
| else | |||||
| { | |||||
| current_section->actual_pulse = ((-current_section->target_pulse) - __HAL_TIM_GET_COUNTER(&htim2)); | |||||
| } | |||||
| // // 相对模式:直接使用目标脉冲数 | |||||
| // current_section->actual_pulse = current_section->target_pulse; | |||||
| } | } | ||||
| // 设置方向端子 - 使用计算后的actual_pulse确定方向 | // 设置方向端子 - 使用计算后的actual_pulse确定方向 | ||||
| @@ -2022,7 +2045,6 @@ void PLSR_Section_SwitchNext(PLSR_RouteConfig_t* route, uint8_t is_pulse_complet | |||||
| if (is_pulse_complete) | if (is_pulse_complete) | ||||
| { | { | ||||
| // 脉冲完成触发:累加整个目标脉冲数 | // 脉冲完成触发:累加整个目标脉冲数 | ||||
| // 使用原始的target_pulse而非actual_pulse,因为prevPulseCount需要记录原始目标值 | |||||
| route->prevPulseCount += current_section->target_pulse; | route->prevPulseCount += current_section->target_pulse; | ||||
| } | } | ||||
| else | else | ||||