|
|
@@ -842,7 +842,8 @@ static int32_t s_last_total_pulse = 0; //上次记录的总脉冲数,用于实 |
|
|
|
*/ |
|
|
|
static void PLSR_UpdateGlobalPulseCount(int32_t current_pulse_count) |
|
|
|
{ |
|
|
|
if (current_pulse_count > s_last_total_pulse) { |
|
|
|
if (current_pulse_count > s_last_total_pulse) |
|
|
|
{ |
|
|
|
int32_t pulse_increment = current_pulse_count - s_last_total_pulse; |
|
|
|
// 脉冲计数方向为1:递增 |
|
|
|
PlsrTotalPulseCount += pulse_increment; |
|
|
@@ -870,7 +871,6 @@ void PLSR_HandleSectionEnd(void) |
|
|
|
PlsrRoute.accel_pulse_count = 0; |
|
|
|
PlsrRoute.const_pulse_count = 0; |
|
|
|
PlsrRoute.decel_pulse_count = 0; |
|
|
|
TotalSePulse += PlsrRoute.section[PlsrRoute.current_section_num-1].target_pulse; |
|
|
|
|
|
|
|
// 重置部分状态 |
|
|
|
PlsrRoute.current_part = PLSR_PART_COMPLETE; |
|
|
@@ -914,7 +914,7 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) |
|
|
|
else PLSR_Section_PWM_Stop(); |
|
|
|
|
|
|
|
// 精确累加当前段已发送的脉冲数 |
|
|
|
int32_t current_section_pulses = __HAL_TIM_GetAutoreload(&htim2); |
|
|
|
int32_t current_section_pulses = TIM2->ARR; |
|
|
|
if(PlsrModFlag == 1) |
|
|
|
{ |
|
|
|
current_section_pulses -= 1; |
|
|
@@ -968,6 +968,10 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) |
|
|
|
} |
|
|
|
else if(PlsrRoute.decel_pulse_count > 0) |
|
|
|
{ |
|
|
|
if(PlsrRoute.section[PlsrRoute.current_section_num-1].target_freq == PlsrRoute.part1_target_freq && PlsrRoute.current_part == PLSR_PART_1) |
|
|
|
{ |
|
|
|
PlsrRoute.current_freq = PlsrRoute.target_freq; |
|
|
|
} |
|
|
|
if(PlsrRoute.decel_pulse_count > 1) |
|
|
|
{ |
|
|
|
// 无匀速阶段,直接进入第三部分:减速 |
|
|
@@ -1231,12 +1235,15 @@ void PLSR_Route_Start(PLSR_RouteConfig_t* route) |
|
|
|
} |
|
|
|
|
|
|
|
// 启动第一段 |
|
|
|
PLSR_Section_StartNewSection(route); |
|
|
|
uint8_t start_flag = PLSR_Section_StartNewSection(route); |
|
|
|
|
|
|
|
// 启动定时器 |
|
|
|
PLSR_TIM6_Start(); // 启动TIM6用于频率更新和等待时间计时 |
|
|
|
HAL_TIM_Base_Start_IT(&htim2); // 启动TIM2中断用于段切换 |
|
|
|
PLSR_PWM_Start(); |
|
|
|
if(start_flag == 1) |
|
|
|
{ |
|
|
|
PLSR_TIM6_Start(); // 启动TIM6用于频率更新和等待时间计时 |
|
|
|
HAL_TIM_Base_Start_IT(&htim2); // 启动TIM2中断用于段切换 |
|
|
|
PLSR_PWM_Start(); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@@ -1370,7 +1377,7 @@ static uint8_t PLSR_SetDirectionPin(PLSR_RouteConfig_t* route, PLSR_SectionConfi |
|
|
|
* @retval None |
|
|
|
* @note 初始化新段的参数并确定初始状态 |
|
|
|
*/ |
|
|
|
void PLSR_Section_StartNewSection(PLSR_RouteConfig_t* route) |
|
|
|
uint8_t PLSR_Section_StartNewSection(PLSR_RouteConfig_t* route) |
|
|
|
{ |
|
|
|
// 获取当前段的配置指针(段号从1开始,数组索引从0开始) |
|
|
|
PLSR_SectionConfig_t* current_section = &route->section[route->current_section_num - 1]; |
|
|
@@ -1391,10 +1398,12 @@ void PLSR_Section_StartNewSection(PLSR_RouteConfig_t* route) |
|
|
|
if(PlsrModFlag == 1) |
|
|
|
{ |
|
|
|
PlsrRoute.initial_freq = PlsrRoute.current_freq; // 更新加减速初始频率 |
|
|
|
|
|
|
|
if(current_section->target_pulse > 0) |
|
|
|
current_section->actual_pulse = TotalSePulse + current_section->target_pulse - PlsrTotalPulseCount; |
|
|
|
else |
|
|
|
current_section->actual_pulse = TotalSePulse + (-current_section->target_pulse) - PlsrTotalPulseCount; |
|
|
|
|
|
|
|
current_section->actual_pulse += 1; |
|
|
|
} |
|
|
|
else |
|
|
@@ -1431,6 +1440,7 @@ void PLSR_Section_StartNewSection(PLSR_RouteConfig_t* route) |
|
|
|
else if(current_section->actual_pulse == 0) |
|
|
|
{ |
|
|
|
PLSR_HandleSectionEnd(); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
if(current_section->section_num == route->section_num) |
|
|
@@ -1543,6 +1553,7 @@ void PLSR_Section_StartNewSection(PLSR_RouteConfig_t* route) |
|
|
|
|
|
|
|
// 清除外部事件标志,确保新段开始时状态干净 |
|
|
|
PLSR_ClearExtEvent(route); |
|
|
|
return 1; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@@ -1700,18 +1711,21 @@ void PLSR_Section_SwitchNext(PLSR_RouteConfig_t* route, uint8_t is_pulse_complet |
|
|
|
if (is_pulse_complete) |
|
|
|
{ |
|
|
|
int32_t actual_total = 0; |
|
|
|
|
|
|
|
if(current_section->target_pulse < 0) |
|
|
|
actual_total = -current_section->target_pulse; |
|
|
|
else |
|
|
|
actual_total = current_section->target_pulse; |
|
|
|
|
|
|
|
// 脉冲完成触发:累加整个目标脉冲数 |
|
|
|
route->prevPulseCount += actual_total; |
|
|
|
TotalSePulse += actual_total; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
// 外部事件触发:直接使用全局脉冲计数器作为累计脉冲数 |
|
|
|
route->prevPulseCount = PlsrTotalPulseCount; |
|
|
|
TotalSePulse += PlsrTotalPulseCount; |
|
|
|
TotalSePulse = PlsrTotalPulseCount; |
|
|
|
} |
|
|
|
} |
|
|
|
if(next_section_num == 0) |
|
|
|