| @@ -33,7 +33,7 @@ uint32_t PlsrCurrentTargetFreq = 0; // 当前段目标频率频率(Hz),用于 | |||||
| static uint8_t s_last_direction = 0xFF; // 初始值设为无效值,确保第一次总是认为有方向变化 | static uint8_t s_last_direction = 0xFF; // 初始值设为无效值,确保第一次总是认为有方向变化 | ||||
| uint8_t PlsrModFlag = 0xff; | uint8_t PlsrModFlag = 0xff; | ||||
| static uint8_t FirstFlag = 0; | static uint8_t FirstFlag = 0; | ||||
| uint32_t TotalSePulse = 0; | |||||
| uint32_t SectionUpdatePluse = 0; | |||||
| uint8_t StopFlag = 0; | uint8_t StopFlag = 0; | ||||
| // ==================== PLSR内部变量 ==================== | // ==================== PLSR内部变量 ==================== | ||||
| @@ -831,7 +831,7 @@ void PLSR_TIM6_Stop(void) | |||||
| HAL_TIM_Base_Stop_IT(&htim6); | HAL_TIM_Base_Stop_IT(&htim6); | ||||
| } | } | ||||
| static int32_t AllPulse = 0; //总脉冲个数 | |||||
| static int32_t PartUpdatePluse = 0; //总脉冲个数 | |||||
| static int32_t s_last_total_pulse = 0; //上次记录的总脉冲数,用于实时累加 | static int32_t s_last_total_pulse = 0; //上次记录的总脉冲数,用于实时累加 | ||||
| @@ -867,10 +867,23 @@ static void PLSR_UpdateGlobalPulseCount(int32_t current_pulse_count) | |||||
| void PLSR_HandleSectionEnd(void) | void PLSR_HandleSectionEnd(void) | ||||
| { | { | ||||
| // 获取当前段的配置指针(段号从1开始,数组索引从0开始) | |||||
| PLSR_SectionConfig_t* current_section = &PlsrRoute.section[PlsrRoute.current_section_num - 1]; | |||||
| // 清零所有部分的脉冲计数 | // 清零所有部分的脉冲计数 | ||||
| PlsrRoute.accel_pulse_count = 0; | PlsrRoute.accel_pulse_count = 0; | ||||
| PlsrRoute.const_pulse_count = 0; | PlsrRoute.const_pulse_count = 0; | ||||
| PlsrRoute.decel_pulse_count = 0; | PlsrRoute.decel_pulse_count = 0; | ||||
| if(PlsrRoute.mode == PLSR_MODE_RELATIVE) | |||||
| { | |||||
| if(current_section->target_pulse > 0) | |||||
| { | |||||
| SectionUpdatePluse += current_section->target_pulse; | |||||
| } | |||||
| else | |||||
| { | |||||
| SectionUpdatePluse -= current_section->target_pulse; | |||||
| } | |||||
| } | |||||
| // 重置部分状态 | // 重置部分状态 | ||||
| PlsrRoute.current_part = PLSR_PART_COMPLETE; | PlsrRoute.current_part = PLSR_PART_COMPLETE; | ||||
| @@ -920,9 +933,9 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) | |||||
| current_section_pulses -= 1; | current_section_pulses -= 1; | ||||
| PlsrModFlag = 0; | PlsrModFlag = 0; | ||||
| } | } | ||||
| AllPulse += current_section_pulses; | |||||
| PlsrRoute.pulse_count = AllPulse; | |||||
| PLSR_UpdateGlobalPulseCount(AllPulse); | |||||
| PartUpdatePluse += current_section_pulses; | |||||
| PlsrRoute.pulse_count = PartUpdatePluse; | |||||
| PLSR_UpdateGlobalPulseCount(PartUpdatePluse); | |||||
| // 三部分状态机处理 | // 三部分状态机处理 | ||||
| switch(PlsrRoute.current_part) | switch(PlsrRoute.current_part) | ||||
| { | { | ||||
| @@ -1059,7 +1072,7 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) | |||||
| uint32_t current_tim2_count = __HAL_TIM_GET_COUNTER(&htim2); | uint32_t current_tim2_count = __HAL_TIM_GET_COUNTER(&htim2); | ||||
| // 计算当前段已发送的总脉冲数 | // 计算当前段已发送的总脉冲数 | ||||
| int32_t current_pulse_count = AllPulse + current_tim2_count; | |||||
| int32_t current_pulse_count = PartUpdatePluse + current_tim2_count; | |||||
| PlsrRoute.pulse_count = current_pulse_count; | PlsrRoute.pulse_count = current_pulse_count; | ||||
| // 实时累加脉冲增量到全局计数器(根据方向逻辑决定递增或递减) | // 实时累加脉冲增量到全局计数器(根据方向逻辑决定递增或递减) | ||||
| PLSR_UpdateGlobalPulseCount(current_pulse_count); | PLSR_UpdateGlobalPulseCount(current_pulse_count); | ||||
| @@ -1080,7 +1093,7 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) | |||||
| { | { | ||||
| PlsrModFlag = 1; // 标记当前段目标频率被修改 | PlsrModFlag = 1; // 标记当前段目标频率被修改 | ||||
| PLSR_Section_PWM_Stop(); | PLSR_Section_PWM_Stop(); | ||||
| AllPulse += current_tim2_count; // 累加当前段已发送的脉冲数 | |||||
| PartUpdatePluse += current_tim2_count; // 累加当前段已发送的脉冲数 | |||||
| PLSR_Section_StartNewSection(&PlsrRoute); ///<重新启动当前段 | PLSR_Section_StartNewSection(&PlsrRoute); ///<重新启动当前段 | ||||
| PLSR_PWM_Start(); | PLSR_PWM_Start(); | ||||
| } | } | ||||
| @@ -1205,7 +1218,7 @@ void PLSR_Route_Start(PLSR_RouteConfig_t* route) | |||||
| route->run_state = PLSR_STATE_IDLE; // 设置运行状态为空闲 | route->run_state = PLSR_STATE_IDLE; // 设置运行状态为空闲 | ||||
| // 重置全局脉冲计数器 | // 重置全局脉冲计数器 | ||||
| AllPulse = 0; | |||||
| PartUpdatePluse = 0; | |||||
| FirstFlag = 0; | FirstFlag = 0; | ||||
| // 重置静态变量,确保每次启动效果一致 | // 重置静态变量,确保每次启动效果一致 | ||||
| @@ -1400,10 +1413,10 @@ uint8_t PLSR_Section_StartNewSection(PLSR_RouteConfig_t* route) | |||||
| PlsrRoute.initial_freq = PlsrRoute.current_freq; // 更新加减速初始频率 | PlsrRoute.initial_freq = PlsrRoute.current_freq; // 更新加减速初始频率 | ||||
| if(current_section->target_pulse > 0) | if(current_section->target_pulse > 0) | ||||
| current_section->actual_pulse = TotalSePulse + current_section->target_pulse - PlsrTotalPulseCount; | |||||
| current_section->actual_pulse = SectionUpdatePluse + current_section->target_pulse - PlsrTotalPulseCount; | |||||
| else | else | ||||
| current_section->actual_pulse = TotalSePulse + (-current_section->target_pulse) - PlsrTotalPulseCount; | |||||
| current_section->actual_pulse = SectionUpdatePluse + (-current_section->target_pulse) - PlsrTotalPulseCount; | |||||
| current_section->actual_pulse += 1; | current_section->actual_pulse += 1; | ||||
| } | } | ||||
| else | else | ||||
| @@ -1719,13 +1732,12 @@ void PLSR_Section_SwitchNext(PLSR_RouteConfig_t* route, uint8_t is_pulse_complet | |||||
| // 脉冲完成触发:累加整个目标脉冲数 | // 脉冲完成触发:累加整个目标脉冲数 | ||||
| route->prevPulseCount += actual_total; | route->prevPulseCount += actual_total; | ||||
| TotalSePulse += actual_total; | |||||
| } | } | ||||
| else | else | ||||
| { | { | ||||
| // 外部事件触发:直接使用全局脉冲计数器作为累计脉冲数 | // 外部事件触发:直接使用全局脉冲计数器作为累计脉冲数 | ||||
| route->prevPulseCount = PlsrTotalPulseCount; | route->prevPulseCount = PlsrTotalPulseCount; | ||||
| TotalSePulse = PlsrTotalPulseCount; | |||||
| SectionUpdatePluse = PlsrTotalPulseCount; | |||||
| } | } | ||||
| } | } | ||||
| if(next_section_num == 0) | if(next_section_num == 0) | ||||
| @@ -2050,8 +2062,8 @@ void PLSR_TaskSectionSwitch(PLSR_RouteConfig_t* route) | |||||
| if (PLSR_Wait_CheckExtEvent(route)) | if (PLSR_Wait_CheckExtEvent(route)) | ||||
| { | { | ||||
| /*检查是否完成,在外部事件这种特殊情况下对脉冲数进行计数*/ | /*检查是否完成,在外部事件这种特殊情况下对脉冲数进行计数*/ | ||||
| AllPulse += __HAL_TIM_GET_COUNTER(&htim2); // 累加当前段已发送的脉冲数 | |||||
| TotalSePulse = AllPulse; | |||||
| PartUpdatePluse += __HAL_TIM_GET_COUNTER(&htim2); // 累加当前段已发送的脉冲数 | |||||
| SectionUpdatePluse = PartUpdatePluse; | |||||
| if(PLSR_Section_CheckPulseComplete(route)) | if(PLSR_Section_CheckPulseComplete(route)) | ||||
| { | { | ||||
| route->current_freq = 0; | route->current_freq = 0; | ||||
| @@ -2059,8 +2071,8 @@ void PLSR_TaskSectionSwitch(PLSR_RouteConfig_t* route) | |||||
| } | } | ||||
| else | else | ||||
| { | { | ||||
| AllPulse -= 1; | |||||
| TotalSePulse -= 1; | |||||
| PartUpdatePluse -= 1; | |||||
| SectionUpdatePluse -= 1; | |||||
| } | } | ||||
| //PLSR_Route_PWM_Stop(); | //PLSR_Route_PWM_Stop(); | ||||
| /* 检查当前段是否为最后一段 */ | /* 检查当前段是否为最后一段 */ | ||||