| @@ -57,7 +57,9 @@ extern TIM_HandleTypeDef htim14; | |||||
| #define PLSR_MAX_SECTIONS 10 // 最大段数 | #define PLSR_MAX_SECTIONS 10 // 最大段数 | ||||
| #define PLSR_PWM_FREQ_MIN 1 // 最小PWM频率 1Hz | #define PLSR_PWM_FREQ_MIN 1 // 最小PWM频率 1Hz | ||||
| #define PLSR_PWM_FREQ_MAX 100000 // 最大PWM频率 100kHz | #define PLSR_PWM_FREQ_MAX 100000 // 最大PWM频率 100kHz | ||||
| #define CLAMP_FREQUENCY(v) ((v) > PLSR_PWM_FREQ_MAX ? PLSR_PWM_FREQ_MAX : (uint32_t)(v)) | |||||
| #define CLAMP_MIN(val, min) ((val) < (min) ? (min) : (val)) | |||||
| #define DIV_ROUND(a, b) (((a) + ((b)>>1)) / (b)) | |||||
| // TIM2硬件计数器已删除 - 改用TIM10中断计数 | // TIM2硬件计数器已删除 - 改用TIM10中断计数 | ||||
| // 等待条件类型枚举 | // 等待条件类型枚举 | ||||
| @@ -146,8 +148,8 @@ typedef struct { | |||||
| typedef struct | typedef struct | ||||
| { | { | ||||
| uint32_t target_freq; ///< 目标频率(Hz) | uint32_t target_freq; ///< 目标频率(Hz) | ||||
| int32_t target_pulse; ///< 目标脉冲数 | |||||
| int32_t actual_pulse; ///< 实际可发脉冲数 | |||||
| int64_t target_pulse; ///< 目标脉冲数 | |||||
| int64_t actual_pulse; ///< 实际可发脉冲数 | |||||
| PLSR_WaitCondition_t wait_condition; ///< 等待条件 | PLSR_WaitCondition_t wait_condition; ///< 等待条件 | ||||
| uint8_t section_num; ///< 段号(1-10) | uint8_t section_num; ///< 段号(1-10) | ||||
| uint8_t next_section; ///< 下一段号(0表示结束) | uint8_t next_section; ///< 下一段号(0表示结束) | ||||
| @@ -166,11 +168,11 @@ typedef struct | |||||
| uint32_t freq_step; ///< 频率步长 | uint32_t freq_step; ///< 频率步长 | ||||
| uint32_t default_freq; ///< 脉冲默认速度 | uint32_t default_freq; ///< 脉冲默认速度 | ||||
| int32_t pulse_count; ///< 当前脉冲计数 | |||||
| int32_t prevPulseCount; ///< 上阶段目标脉冲 | |||||
| int32_t accel_pulse_count; ///< 第一部分脉冲数(可能是加速、减速或匀速) | |||||
| int32_t const_pulse_count; ///< 第二部分脉冲数(匀速) | |||||
| int32_t decel_pulse_count; ///< 第三部分脉冲数(减速到0) | |||||
| uint64_t pulse_count; ///< 当前脉冲计数 | |||||
| int64_t prevPulseCount; ///< 上阶段目标脉冲 | |||||
| int64_t accel_pulse_count; ///< 第一部分脉冲数(可能是加速、减速或匀速) | |||||
| int64_t const_pulse_count; ///< 第二部分脉冲数(匀速) | |||||
| int64_t decel_pulse_count; ///< 第三部分脉冲数(减速到0) | |||||
| uint32_t wait_start_tick; ///< 等待开始时间 | uint32_t wait_start_tick; ///< 等待开始时间 | ||||
| uint32_t act_start_tick; ///< ACT开始时间 | uint32_t act_start_tick; ///< ACT开始时间 | ||||
| @@ -267,15 +269,15 @@ void PLSR_SectionSwitchInit(void); | |||||
| void PLSR_SectionSwitchSignal(void); //<发送段切换信号量(中断中调用) | void PLSR_SectionSwitchSignal(void); //<发送段切换信号量(中断中调用) | ||||
| uint32_t BinarySearchOptimalFreq(uint32_t v0, uint32_t vt_desired, | uint32_t BinarySearchOptimalFreq(uint32_t v0, uint32_t vt_desired, | ||||
| int32_t total_pulses, uint32_t a, uint32_t d, | |||||
| int64_t total_pulses, uint32_t a, uint32_t d, | |||||
| uint8_t is_accel); // 二分搜索优化目标频率 | uint8_t is_accel); // 二分搜索优化目标频率 | ||||
| uint32_t OptimalIntermediateFrequency(uint32_t v0, uint32_t vt_desired, | uint32_t OptimalIntermediateFrequency(uint32_t v0, uint32_t vt_desired, | ||||
| int32_t total_pulses, uint32_t a, uint32_t d); // 计算最优中间频率 | |||||
| int64_t total_pulses, uint32_t a, uint32_t d); // 计算最优中间频率 | |||||
| // ==================== PLSR全局变量声明 ==================== | // ==================== PLSR全局变量声明 ==================== | ||||
| extern PLSR_RouteConfig_t PlsrRoute; // 全局PLSR路径控制结构体 | extern PLSR_RouteConfig_t PlsrRoute; // 全局PLSR路径控制结构体 | ||||
| extern uint8_t PlsrExtEventFlag; // 外部事件标志 | extern uint8_t PlsrExtEventFlag; // 外部事件标志 | ||||
| extern int32_t PlsrTotalPulseCount; // 全局累加脉冲计数器(程序运行期间持续累加,支持负数) | |||||
| extern int32_t PlsrLocation; | |||||
| extern int64_t PlsrTotalPulseCount; // 全局累加脉冲计数器(程序运行期间持续累加,支持负数) | |||||
| extern int64_t PlsrLocation; | |||||
| /* USER CODE END Prototypes */ | /* USER CODE END Prototypes */ | ||||
| #ifdef __cplusplus | #ifdef __cplusplus | ||||
| @@ -25,9 +25,9 @@ | |||||
| /* USER CODE BEGIN 0 */ | /* USER CODE BEGIN 0 */ | ||||
| // ==================== PLSR全局变量声明 ==================== | // ==================== PLSR全局变量声明 ==================== | ||||
| PLSR_RouteConfig_t PlsrRoute; // 全局PLSR路径控制结构体 | PLSR_RouteConfig_t PlsrRoute; // 全局PLSR路径控制结构体 | ||||
| int32_t PlsrTotalPulseCount = 0; // 全局累加脉冲计数器(程序运行期间持续累加,支持负数) | |||||
| int64_t PlsrTotalPulseCount = 0; // 全局累加脉冲计数器(程序运行期间持续累加,支持负数) | |||||
| uint8_t PlsrExtEventFlag = 0; // 外部事件标志(0-无事件, 1-事件触发) | uint8_t PlsrExtEventFlag = 0; // 外部事件标志(0-无事件, 1-事件触发) | ||||
| int32_t PlsrLocation = 0; // 全局位置计数器(用于记录当前执行位置,支持负数) | |||||
| int64_t PlsrLocation = 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 PlsrCurrentTargetFreq = 0; // 当前段目标频率频率(Hz),用于检测是否有频率变化 | uint32_t PlsrCurrentTargetFreq = 0; // 当前段目标频率频率(Hz),用于检测是否有频率变化 | ||||
| static uint8_t s_last_direction = 0xFF; // 初始值设为无效值,确保第一次总是认为有方向变化 | static uint8_t s_last_direction = 0xFF; // 初始值设为无效值,确保第一次总是认为有方向变化 | ||||
| @@ -834,8 +834,8 @@ void PLSR_TIM6_Stop(void) | |||||
| HAL_TIM_Base_Stop_IT(&htim6); | HAL_TIM_Base_Stop_IT(&htim6); | ||||
| } | } | ||||
| static int32_t PartUpdatePluse = 0; //总脉冲个数 | |||||
| static int32_t s_last_total_pulse = 0; //上次记录的总脉冲数,用于实时累加 | |||||
| static int64_t PartUpdatePluse = 0; //总脉冲个数 | |||||
| static int64_t s_last_total_pulse = 0; //上次记录的总脉冲数,用于实时累加 | |||||
| /** | /** | ||||
| @@ -843,11 +843,11 @@ static int32_t s_last_total_pulse = 0; //上次记录的总脉冲数,用于实 | |||||
| * @param current_pulse_count 当前脉冲计数值 | * @param current_pulse_count 当前脉冲计数值 | ||||
| * @note 根据方向逻辑决定脉冲计数方向,支持TIM2和TIM6中断的不同计算方式 | * @note 根据方向逻辑决定脉冲计数方向,支持TIM2和TIM6中断的不同计算方式 | ||||
| */ | */ | ||||
| static void PLSR_UpdateGlobalPulseCount(int32_t current_pulse_count) | |||||
| static void PLSR_UpdateGlobalPulseCount(int64_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; | |||||
| int64_t pulse_increment = current_pulse_count - s_last_total_pulse; | |||||
| // 脉冲计数方向为1:递增 | // 脉冲计数方向为1:递增 | ||||
| PlsrTotalPulseCount += pulse_increment; | PlsrTotalPulseCount += pulse_increment; | ||||
| if (s_pulse_count_direction) | if (s_pulse_count_direction) | ||||
| @@ -863,7 +863,7 @@ static void PLSR_UpdateGlobalPulseCount(int32_t current_pulse_count) | |||||
| s_last_total_pulse = current_pulse_count; | s_last_total_pulse = current_pulse_count; | ||||
| } | } | ||||
| // 将32位全局累加脉冲计数分解为两个16位寄存器(支持负数) | // 将32位全局累加脉冲计数分解为两个16位寄存器(支持负数) | ||||
| int32_t signed_count = PlsrLocation; // 使用有符号数进行计算 | |||||
| int64_t signed_count = PlsrLocation; // 使用有符号数进行计算 | |||||
| backup_data->holding_regs[73] = signed_count & 0xFFFF; | backup_data->holding_regs[73] = signed_count & 0xFFFF; | ||||
| backup_data->holding_regs[74] = (signed_count >> 16) & 0xFFFF; | backup_data->holding_regs[74] = (signed_count >> 16) & 0xFFFF; | ||||
| } | } | ||||
| @@ -894,6 +894,10 @@ void PLSR_HandleSectionEnd(void) | |||||
| SectionUpdatePluse = PlsrTotalPulseCount; | SectionUpdatePluse = PlsrTotalPulseCount; | ||||
| } | } | ||||
| } | } | ||||
| else | |||||
| { | |||||
| SectionUpdatePluse = PlsrTotalPulseCount; | |||||
| } | |||||
| // 重置部分状态 | // 重置部分状态 | ||||
| PlsrRoute.current_part = PLSR_PART_COMPLETE; | PlsrRoute.current_part = PLSR_PART_COMPLETE; | ||||
| // 检查是否为最后一段且下一段为0(路径结束) | // 检查是否为最后一段且下一段为0(路径结束) | ||||
| @@ -937,7 +941,7 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) | |||||
| else PLSR_Section_PWM_Stop(); | else PLSR_Section_PWM_Stop(); | ||||
| // 精确累加当前段已发送的脉冲数 | // 精确累加当前段已发送的脉冲数 | ||||
| int32_t current_section_pulses = TIM2->ARR; | |||||
| int64_t current_section_pulses = TIM2->ARR; | |||||
| if(PlsrModFlag == 1) | if(PlsrModFlag == 1) | ||||
| { | { | ||||
| current_section_pulses -= 1; | current_section_pulses -= 1; | ||||
| @@ -1083,7 +1087,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 = PartUpdatePluse + current_tim2_count; | |||||
| int64_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); | ||||
| @@ -1163,8 +1167,8 @@ void PLSR_Route_Set(PLSR_RouteConfig_t* route) | |||||
| ((uint32_t)FlashSave_Get_Holding_Register(seg_base_addr + MODBUS_OFFSET_FREQ_HIGH) << 16); | ((uint32_t)FlashSave_Get_Holding_Register(seg_base_addr + MODBUS_OFFSET_FREQ_HIGH) << 16); | ||||
| /* 目标脉冲数 = 低16位 | (高16位 << 16) */ | /* 目标脉冲数 = 低16位 | (高16位 << 16) */ | ||||
| route->section[i].target_pulse = ((int32_t)FlashSave_Get_Holding_Register(seg_base_addr + MODBUS_OFFSET_PULSE_COUNT_LOW)) | | |||||
| ((int32_t)FlashSave_Get_Holding_Register(seg_base_addr + MODBUS_OFFSET_PULSE_COUNT_HIGH) << 16); | |||||
| route->section[i].target_pulse = ((int64_t)FlashSave_Get_Holding_Register(seg_base_addr + MODBUS_OFFSET_PULSE_COUNT_LOW)) | | |||||
| ((int64_t)FlashSave_Get_Holding_Register(seg_base_addr + MODBUS_OFFSET_PULSE_COUNT_HIGH) << 16); | |||||
| /* 等待条件和跳转编号 */ | /* 等待条件和跳转编号 */ | ||||
| route->section[i].wait_condition.wait_type = FlashSave_Get_Holding_Register(seg_base_addr + MODBUS_OFFSET_WAIT_CONDITION); | route->section[i].wait_condition.wait_type = FlashSave_Get_Holding_Register(seg_base_addr + MODBUS_OFFSET_WAIT_CONDITION); | ||||
| @@ -1577,6 +1581,9 @@ uint8_t PLSR_Section_StartNewSection(PLSR_RouteConfig_t* route) | |||||
| // 清除外部事件标志,确保新段开始时状态干净 | // 清除外部事件标志,确保新段开始时状态干净 | ||||
| PLSR_ClearExtEvent(route); | PLSR_ClearExtEvent(route); | ||||
| if(PlsrRoute.accel_config.accel_algorithm == PLSR_ACCEL_CURVE) | |||||
| SCurve_InitializeParameters(&PlsrRoute); //计算曲线加减速需要的参数. | |||||
| return 1; | return 1; | ||||
| } | } | ||||
| @@ -1734,7 +1741,7 @@ void PLSR_Section_SwitchNext(PLSR_RouteConfig_t* route, uint8_t is_pulse_complet | |||||
| { | { | ||||
| if (is_pulse_complete) | if (is_pulse_complete) | ||||
| { | { | ||||
| int32_t actual_total = 0; | |||||
| int64_t actual_total = 0; | |||||
| if(current_section->target_pulse < 0) | if(current_section->target_pulse < 0) | ||||
| actual_total = -current_section->target_pulse; | actual_total = -current_section->target_pulse; | ||||
| @@ -1751,6 +1758,12 @@ void PLSR_Section_SwitchNext(PLSR_RouteConfig_t* route, uint8_t is_pulse_complet | |||||
| SectionUpdatePluse = PlsrTotalPulseCount; | SectionUpdatePluse = PlsrTotalPulseCount; | ||||
| } | } | ||||
| } | } | ||||
| else | |||||
| { | |||||
| // // 外部事件触发:直接使用全局脉冲计数器作为累计脉冲数 | |||||
| // route->prevPulseCount = PlsrTotalPulseCount; | |||||
| SectionUpdatePluse = PlsrTotalPulseCount; | |||||
| } | |||||
| if(next_section_num == 0) | if(next_section_num == 0) | ||||
| { | { | ||||
| route->current_section_num = current_section->section_num + 1; | route->current_section_num = current_section->section_num + 1; | ||||
| @@ -1785,9 +1798,9 @@ uint32_t PLSR_Calculate_FreqByPosition(PLSR_RouteConfig_t* route, uint8_t is_acc | |||||
| static uint8_t s_last_section_num = 0xFF; // 初始化为无效值 | static uint8_t s_last_section_num = 0xFF; // 初始化为无效值 | ||||
| // 获取当前脉冲位置 | // 获取当前脉冲位置 | ||||
| int32_t current_tim2_count = __HAL_TIM_GET_COUNTER(&htim2); | |||||
| int32_t executed_pulses = 0; | |||||
| int32_t next_executed_pulses = 0; | |||||
| int64_t current_tim2_count = __HAL_TIM_GET_COUNTER(&htim2); | |||||
| int64_t executed_pulses = 0; | |||||
| int64_t next_executed_pulses = 0; | |||||
| if((current_tim2_count - 1) < 0) | if((current_tim2_count - 1) < 0) | ||||
| { | { | ||||
| @@ -1944,7 +1957,7 @@ void PLSR_Accel_Process(PLSR_RouteConfig_t* route) | |||||
| } | } | ||||
| if(FirstFlag == 0 && route->current_freq != 0) //在这里第一次启动PWM输出避免发默认脉冲 | if(FirstFlag == 0 && route->current_freq != 0) //在这里第一次启动PWM输出避免发默认脉冲 | ||||
| { | |||||
| { | |||||
| FirstFlag = 1; | FirstFlag = 1; | ||||
| PLSR_PWM_Start(); | PLSR_PWM_Start(); | ||||
| switch (route->output_port) | switch (route->output_port) | ||||
| @@ -2155,7 +2168,7 @@ uint8_t PLSR_Section_CheckPulseComplete(PLSR_RouteConfig_t* route) | |||||
| if (current_section->wait_condition.wait_type == PLSR_WAIT_PLUSEEND || | if (current_section->wait_condition.wait_type == PLSR_WAIT_PLUSEEND || | ||||
| current_section->wait_condition.wait_type == PLSR_WAIT_EXT_OR_END) | current_section->wait_condition.wait_type == PLSR_WAIT_EXT_OR_END) | ||||
| { | { | ||||
| int32_t target_pulse = current_section->actual_pulse; | |||||
| int64_t target_pulse = current_section->actual_pulse; | |||||
| if(target_pulse < 0) target_pulse = -target_pulse; | if(target_pulse < 0) target_pulse = -target_pulse; | ||||
| target_pulse += route->prevPulseCount; // 累加上次的脉冲计数 | target_pulse += route->prevPulseCount; // 累加上次的脉冲计数 | ||||
| @@ -60,9 +60,9 @@ void PLSR_CalculateTimerParams(uint32_t frequency, uint16_t* prescaler, uint32_t | |||||
| */ | */ | ||||
| void Calculate_PluseNum_Simplified(PLSR_RouteConfig_t *route) | void Calculate_PluseNum_Simplified(PLSR_RouteConfig_t *route) | ||||
| { | { | ||||
| int32_t accel_pulse_num = 0; // 加速过程脉冲数 | |||||
| int32_t decel_pulse_num = 0; // 减速过程脉冲数 | |||||
| int32_t const_pulse_num = 0; // 匀速过程脉冲数 | |||||
| int64_t accel_pulse_num = 0; // 加速过程脉冲数 | |||||
| int64_t decel_pulse_num = 0; // 减速过程脉冲数 | |||||
| int64_t const_pulse_num = 0; // 匀速过程脉冲数 | |||||
| uint32_t accel_time = 0; // 加速时间(ms) | uint32_t accel_time = 0; // 加速时间(ms) | ||||
| uint32_t decel_time = 0; // 减速时间(ms) | uint32_t decel_time = 0; // 减速时间(ms) | ||||
| @@ -80,7 +80,7 @@ void Calculate_PluseNum_Simplified(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; // 减速度 | ||||
| // 使用实际可发脉冲数,不再区分相对/绝对模式 | // 使用实际可发脉冲数,不再区分相对/绝对模式 | ||||
| int32_t total_pulses = current_section->actual_pulse; // 总脉冲数 | |||||
| int64_t total_pulses = current_section->actual_pulse; // 总脉冲数 | |||||
| // 防止除零错误 | // 防止除零错误 | ||||
| @@ -99,7 +99,7 @@ void Calculate_PluseNum_Simplified(PLSR_RouteConfig_t *route) | |||||
| // 加速阶段脉冲数 = (起始频率 + 目标频率) * 时间 / 2000 | // 加速阶段脉冲数 = (起始频率 + 目标频率) * 时间 / 2000 | ||||
| // 使用梯形积分公式:面积 = (上底 + 下底) * 高 / 2 | // 使用梯形积分公式:面积 = (上底 + 下底) * 高 / 2 | ||||
| uint64_t temp_accel = (uint64_t)(v0 + vt) * accel_time; | uint64_t temp_accel = (uint64_t)(v0 + vt) * accel_time; | ||||
| int32_t required_accel_pulses = (int32_t)(temp_accel / 2000); | |||||
| int64_t required_accel_pulses = (int64_t)(temp_accel / 2000); | |||||
| if(required_accel_pulses == 0) required_accel_pulses = 1; // 至少1个脉冲 | if(required_accel_pulses == 0) required_accel_pulses = 1; // 至少1个脉冲 | ||||
| if (required_accel_pulses <= total_pulses) | if (required_accel_pulses <= total_pulses) | ||||
| @@ -146,7 +146,7 @@ void Calculate_PluseNum_Simplified(PLSR_RouteConfig_t *route) | |||||
| // 减速阶段脉冲数 = (起始频率 + 目标频率) * 时间 / 2000 | // 减速阶段脉冲数 = (起始频率 + 目标频率) * 时间 / 2000 | ||||
| uint64_t temp_decel = (uint64_t)(v0 + vt) * decel_time; | uint64_t temp_decel = (uint64_t)(v0 + vt) * decel_time; | ||||
| int32_t required_decel_pulses = (int32_t)(temp_decel / 2000); | |||||
| int64_t required_decel_pulses = (int64_t)(temp_decel / 2000); | |||||
| if(required_decel_pulses == 0) required_decel_pulses = 1; // 至少1个脉冲 | if(required_decel_pulses == 0) required_decel_pulses = 1; // 至少1个脉冲 | ||||
| if (required_decel_pulses <= total_pulses) | if (required_decel_pulses <= total_pulses) | ||||
| @@ -200,9 +200,9 @@ void Calculate_PluseNum_Simplified(PLSR_RouteConfig_t *route) | |||||
| void Calculate_PluseNum(PLSR_RouteConfig_t *route) | void Calculate_PluseNum(PLSR_RouteConfig_t *route) | ||||
| { | { | ||||
| int32_t part1_pulse_num = 0; // 第一部分脉冲数 | |||||
| int32_t part2_pulse_num = 0; // 第二部分脉冲数 | |||||
| int32_t part3_pulse_num = 0; // 第三部分脉冲数 | |||||
| int64_t part1_pulse_num = 0; // 第一部分脉冲数 | |||||
| int64_t part2_pulse_num = 0; // 第二部分脉冲数 | |||||
| int64_t part3_pulse_num = 0; // 第三部分脉冲数 | |||||
| uint32_t part1_time = 0; // 第一部分时间(ms) | uint32_t part1_time = 0; // 第一部分时间(ms) | ||||
| uint32_t part3_time = 0; // 第三部分时间(ms) | uint32_t part3_time = 0; // 第三部分时间(ms) | ||||
| @@ -225,7 +225,7 @@ void Calculate_PluseNum(PLSR_RouteConfig_t *route) | |||||
| uint32_t d = route->decel_rate; // 减速度 | uint32_t d = route->decel_rate; // 减速度 | ||||
| // 在PLSR_Section_StartNewSection中已经根据模式计算了actual_pulse | // 在PLSR_Section_StartNewSection中已经根据模式计算了actual_pulse | ||||
| int32_t total_pulses = current_section->actual_pulse; // 总脉冲数 | |||||
| int64_t total_pulses = current_section->actual_pulse; // 总脉冲数 | |||||
| // 防止除零错误 | // 防止除零错误 | ||||
| if (a == 0) a = 1; | if (a == 0) a = 1; | ||||
| @@ -280,7 +280,7 @@ void Calculate_PluseNum(PLSR_RouteConfig_t *route) | |||||
| } | } | ||||
| // 检查脉冲数是否足够 | // 检查脉冲数是否足够 | ||||
| int32_t used_pulses = part1_pulse_num + part3_pulse_num; | |||||
| int64_t used_pulses = part1_pulse_num + part3_pulse_num; | |||||
| if (used_pulses <= total_pulses) | if (used_pulses <= total_pulses) | ||||
| { | { | ||||
| @@ -349,7 +349,7 @@ void Calculate_PluseNum(PLSR_RouteConfig_t *route) | |||||
| } | } | ||||
| // 确保总脉冲数完全匹配 | // 确保总脉冲数完全匹配 | ||||
| int32_t calculated_total = part1_pulse_num + part3_pulse_num; | |||||
| int64_t calculated_total = part1_pulse_num + part3_pulse_num; | |||||
| if (calculated_total != total_pulses) | if (calculated_total != total_pulses) | ||||
| { | { | ||||
| if (calculated_total > total_pulses) | if (calculated_total > total_pulses) | ||||
| @@ -375,7 +375,7 @@ void Calculate_PluseNum(PLSR_RouteConfig_t *route) | |||||
| else | else | ||||
| { | { | ||||
| // 脉冲数不足,将剩余脉冲分配给匀速阶段 | // 脉冲数不足,将剩余脉冲分配给匀速阶段 | ||||
| int32_t remaining_pulses = total_pulses - calculated_total; | |||||
| int64_t remaining_pulses = total_pulses - calculated_total; | |||||
| // 将剩余脉冲分配给第二阶段(匀速阶段) | // 将剩余脉冲分配给第二阶段(匀速阶段) | ||||
| // 这样既不影响加减速的数学精确性,又能充分利用所有脉冲 | // 这样既不影响加减速的数学精确性,又能充分利用所有脉冲 | ||||
| @@ -444,7 +444,7 @@ void Calculate_PluseNum(PLSR_RouteConfig_t *route) | |||||
| * @brief 计算最优中间频率 | * @brief 计算最优中间频率 | ||||
| */ | */ | ||||
| uint32_t OptimalIntermediateFrequency(uint32_t v0, uint32_t vt_desired, | uint32_t OptimalIntermediateFrequency(uint32_t v0, uint32_t vt_desired, | ||||
| int32_t total_pulses, uint32_t a, uint32_t d) | |||||
| int64_t total_pulses, uint32_t a, uint32_t d) | |||||
| { | { | ||||
| if (total_pulses <= 0 || a == 0 || d == 0) { | if (total_pulses <= 0 || a == 0 || d == 0) { | ||||
| return v0; // 无法优化 | return v0; // 无法优化 | ||||
| @@ -502,7 +502,7 @@ uint32_t OptimalIntermediateFrequency(uint32_t v0, uint32_t vt_desired, | |||||
| * @return 最优频率 | * @return 最优频率 | ||||
| */ | */ | ||||
| uint32_t BinarySearchOptimalFreq(uint32_t v0, uint32_t vt_desired, | uint32_t BinarySearchOptimalFreq(uint32_t v0, uint32_t vt_desired, | ||||
| int32_t total_pulses, uint32_t a, uint32_t d, | |||||
| int64_t total_pulses, uint32_t a, uint32_t d, | |||||
| uint8_t is_accel) | uint8_t is_accel) | ||||
| { | { | ||||
| uint32_t low = is_accel ? v0 : 0; | uint32_t low = is_accel ? v0 : 0; | ||||
| @@ -525,7 +525,7 @@ uint32_t BinarySearchOptimalFreq(uint32_t v0, uint32_t vt_desired, | |||||
| } | } | ||||
| s3 = ((uint64_t)mid * mid) / (2000ULL * d); | s3 = ((uint64_t)mid * mid) / (2000ULL * d); | ||||
| int32_t total_calc = s1 + s3; | |||||
| int64_t total_calc = s1 + s3; | |||||
| if (total_calc == total_pulses) { | if (total_calc == total_pulses) { | ||||
| return mid; // 找到精确解 | return mid; // 找到精确解 | ||||
| @@ -649,7 +649,8 @@ void SCurve_InitializeParameters(PLSR_RouteConfig_t* route) | |||||
| uint32_t delta_vel_accel = (vel_target_accel > vel_start_accel) ? | uint32_t delta_vel_accel = (vel_target_accel > vel_start_accel) ? | ||||
| (vel_target_accel - vel_start_accel) : 0; | (vel_target_accel - vel_start_accel) : 0; | ||||
| if (delta_vel_accel > 0) { | |||||
| if (delta_vel_accel > 0) | |||||
| { | |||||
| /* 计算加速时间参数 */ | /* 计算加速时间参数 */ | ||||
| uint32_t total_accel_time_ms = CLAMP_MIN(delta_vel_accel / accel_rate, 1); | uint32_t total_accel_time_ms = CLAMP_MIN(delta_vel_accel / accel_rate, 1); | ||||
| uint32_t jerk_time_ms = CLAMP_MIN(total_accel_time_ms / 2, 1); | uint32_t jerk_time_ms = CLAMP_MIN(total_accel_time_ms / 2, 1); | ||||
| @@ -681,9 +682,12 @@ void SCurve_InitializeParameters(PLSR_RouteConfig_t* route) | |||||
| uint32_t calculated_total = phase1_pulses + phase2_pulses; | uint32_t calculated_total = phase1_pulses + phase2_pulses; | ||||
| uint32_t actual_total = route->accel_pulse_count; | uint32_t actual_total = route->accel_pulse_count; | ||||
| if (calculated_total != actual_total && calculated_total > 0) { | if (calculated_total != actual_total && calculated_total > 0) { | ||||
| if (actual_total > calculated_total) { | |||||
| if (actual_total > calculated_total) | |||||
| { | |||||
| phase2_pulses += (actual_total - calculated_total); | phase2_pulses += (actual_total - calculated_total); | ||||
| } else { | |||||
| } | |||||
| else | |||||
| { | |||||
| phase1_pulses = (uint32_t)(((uint64_t)phase1_pulses * actual_total) / calculated_total); | phase1_pulses = (uint32_t)(((uint64_t)phase1_pulses * actual_total) / calculated_total); | ||||
| phase2_pulses = actual_total - phase1_pulses; | phase2_pulses = actual_total - phase1_pulses; | ||||
| } | } | ||||
| @@ -695,7 +699,9 @@ void SCurve_InitializeParameters(PLSR_RouteConfig_t* route) | |||||
| route->scurve.accel_jerk_time_ms = jerk_time_ms; | route->scurve.accel_jerk_time_ms = jerk_time_ms; | ||||
| route->scurve.accel_phase1_pulses = phase1_pulses; | route->scurve.accel_phase1_pulses = phase1_pulses; | ||||
| route->scurve.accel_phase2_pulses = phase2_pulses; | route->scurve.accel_phase2_pulses = phase2_pulses; | ||||
| } else { | |||||
| } | |||||
| else | |||||
| { | |||||
| /* 无加速段,清零参数 */ | /* 无加速段,清零参数 */ | ||||
| route->scurve.accel_jerk = 0; | route->scurve.accel_jerk = 0; | ||||
| route->scurve.accel_max = 0; | route->scurve.accel_max = 0; | ||||