|
@@ -28,6 +28,7 @@ int32_t g_plsr_total_pulse_count = 0; // 全局累加脉冲计数器(程序运 |
|
|
uint8_t g_plsr_ext_event_flag = 0; // 外部事件标志(0-无事件, 1-事件触发) |
|
|
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),用于检测是否有频率变化 |
|
|
|
|
|
|
|
|
// ==================== PLSR内部变量 ==================== |
|
|
// ==================== PLSR内部变量 ==================== |
|
|
static uint32_t s_tim6_update_freq_us = 1000; // TIM6更新频率(微秒) |
|
|
static uint32_t s_tim6_update_freq_us = 1000; // TIM6更新频率(微秒) |
|
@@ -1054,6 +1055,7 @@ void Calculate_PluseNum_Simplified(PLSR_RouteConfig_t *route) |
|
|
// 使用梯形积分公式:面积 = (上底 + 下底) * 高 / 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); |
|
|
int32_t required_accel_pulses = (int32_t)(temp_accel / 2000); |
|
|
|
|
|
if(required_accel_pulses == 0) required_accel_pulses = 1; // 至少1个脉冲 |
|
|
|
|
|
|
|
|
if (required_accel_pulses <= total_pulses) |
|
|
if (required_accel_pulses <= total_pulses) |
|
|
{ |
|
|
{ |
|
@@ -1100,6 +1102,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); |
|
|
int32_t required_decel_pulses = (int32_t)(temp_decel / 2000); |
|
|
|
|
|
if(required_decel_pulses == 0) required_decel_pulses = 1; // 至少1个脉冲 |
|
|
|
|
|
|
|
|
if (required_decel_pulses <= total_pulses) |
|
|
if (required_decel_pulses <= total_pulses) |
|
|
{ |
|
|
{ |
|
@@ -1525,10 +1528,16 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) |
|
|
PLSR_UpdateGlobalPulseCount(current_pulse_count); |
|
|
PLSR_UpdateGlobalPulseCount(current_pulse_count); |
|
|
// 更新路由结构体的脉冲计数(使用原子操作保证一致性) |
|
|
// 更新路由结构体的脉冲计数(使用原子操作保证一致性) |
|
|
g_plsr_route.pulse_count = current_pulse_count; |
|
|
g_plsr_route.pulse_count = current_pulse_count; |
|
|
|
|
|
|
|
|
// g_plsr_route.section[g_plsr_route.current_section_num-1].target_freq = |
|
|
|
|
|
// (((uint32_t)ModbusSlave.holding_regs[256+(16*(g_plsr_route.current_section_num-1))]) | |
|
|
|
|
|
// (uint32_t)ModbusSlave.holding_regs[257+(16*(g_plsr_route.current_section_num-1))]<<16); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
g_plsr_route.section[g_plsr_route.current_section_num-1].target_freq = |
|
|
|
|
|
(((uint32_t)ModbusSlave.holding_regs[256+(16*(g_plsr_route.current_section_num-1))]) | |
|
|
|
|
|
(uint32_t)ModbusSlave.holding_regs[257+(16*(g_plsr_route.current_section_num-1))]<<16); |
|
|
|
|
|
|
|
|
|
|
|
if(g_plsr_route.section[g_plsr_route.current_section_num-1].target_freq != g_plsr_current_target_freq) |
|
|
|
|
|
{ |
|
|
|
|
|
AllPluse += current_tim2_count; // 累加当前段已发送的脉冲数 |
|
|
|
|
|
PLSR_Section_StartNewSection(&g_plsr_route); ///<重新启动当前段 |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 处理加减速过程中的频率更新(使用新的直线加减速算法) |
|
|
// 处理加减速过程中的频率更新(使用新的直线加减速算法) |
|
|
if(g_plsr_route.run_state == PLSR_STATE_ACCEL || g_plsr_route.run_state == PLSR_STATE_DECEL) |
|
|
if(g_plsr_route.run_state == PLSR_STATE_ACCEL || g_plsr_route.run_state == PLSR_STATE_DECEL) |
|
@@ -1543,6 +1552,7 @@ void PLSR_Route_Set(PLSR_RouteConfig_t* route) |
|
|
{ |
|
|
{ |
|
|
route->output_port = ModbusSlave.holding_regs[0]; |
|
|
route->output_port = ModbusSlave.holding_regs[0]; |
|
|
route->dir_port = ModbusSlave.holding_regs[1]; |
|
|
route->dir_port = ModbusSlave.holding_regs[1]; |
|
|
|
|
|
route->ext_port = ModbusSlave.holding_regs[2]; |
|
|
route->dir_delay = ModbusSlave.holding_regs[3]; |
|
|
route->dir_delay = ModbusSlave.holding_regs[3]; |
|
|
route->dir_logic = ModbusSlave.holding_regs[4]; |
|
|
route->dir_logic = ModbusSlave.holding_regs[4]; |
|
|
route->accel_config.accel_algorithm = ModbusSlave.holding_regs[5]; |
|
|
route->accel_config.accel_algorithm = ModbusSlave.holding_regs[5]; |
|
@@ -1556,7 +1566,14 @@ void PLSR_Route_Set(PLSR_RouteConfig_t* route) |
|
|
// 新增:设置默认加减速时间 |
|
|
// 新增:设置默认加减速时间 |
|
|
route->default_accel_time_ms = ModbusSlave.holding_regs[11]; // 使用相同的加速时间 |
|
|
route->default_accel_time_ms = ModbusSlave.holding_regs[11]; // 使用相同的加速时间 |
|
|
route->default_decel_time_ms = ModbusSlave.holding_regs[12]; // 使用相同的减速时间 |
|
|
route->default_decel_time_ms = ModbusSlave.holding_regs[12]; // 使用相同的减速时间 |
|
|
|
|
|
|
|
|
|
|
|
if(route->ext_port == 0) |
|
|
|
|
|
{ |
|
|
|
|
|
HAL_NVIC_EnableIRQ(EXTI9_5_IRQn); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
HAL_NVIC_DisableIRQ(EXTI15_10_IRQn); |
|
|
|
|
|
} |
|
|
// 自动更新加减速度 |
|
|
// 自动更新加减速度 |
|
|
PLSR_Accel_UpdateRates(route); |
|
|
PLSR_Accel_UpdateRates(route); |
|
|
|
|
|
|
|
@@ -1749,7 +1766,8 @@ void PLSR_Section_StartNewSection(PLSR_RouteConfig_t* route) |
|
|
{ |
|
|
{ |
|
|
// 获取当前段的配置指针(段号从1开始,数组索引从0开始) |
|
|
// 获取当前段的配置指针(段号从1开始,数组索引从0开始) |
|
|
PLSR_SectionConfig_t* current_section = &route->section[route->current_section_num - 1]; |
|
|
PLSR_SectionConfig_t* current_section = &route->section[route->current_section_num - 1]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
g_plsr_current_target_freq = current_section->target_freq; // 更新当前目标频率 |
|
|
// 统一使用相对模式计算实际可发脉冲数 |
|
|
// 统一使用相对模式计算实际可发脉冲数 |
|
|
if(g_plsr_route.mode == PLSR_MODE_ABSOLUTE) |
|
|
if(g_plsr_route.mode == PLSR_MODE_ABSOLUTE) |
|
|
{ |
|
|
{ |
|
|