|
|
@@ -26,6 +26,7 @@ |
|
|
|
PLSR_RouteConfig_t g_plsr_route; // 全局PLSR路径控制结构体 |
|
|
|
int32_t g_plsr_total_pulse_count = 0; // 全局累加脉冲计数器(程序运行期间持续累加,支持负数) |
|
|
|
uint8_t g_plsr_ext_event_flag = 0; // 外部事件标志(0-无事件, 1-事件触发) |
|
|
|
int32_t g_plsr_location = 0; // 全局位置计数器(用于记录当前执行位置,支持负数) |
|
|
|
static uint8_t s_pulse_count_direction = 1; // 脉冲计数方向(1-递增, 0-递减),用于替代dir_logic判断计数方向 |
|
|
|
|
|
|
|
// ==================== PLSR内部变量 ==================== |
|
|
@@ -965,16 +966,16 @@ static void PLSR_UpdateGlobalPulseCount(int32_t current_pulse_count) |
|
|
|
int32_t pulse_increment = current_pulse_count - s_last_total_pulse; |
|
|
|
// 脉冲计数方向为1:递增 |
|
|
|
g_plsr_total_pulse_count += pulse_increment; |
|
|
|
// if (s_pulse_count_direction) |
|
|
|
// { |
|
|
|
// // 脉冲计数方向为1:递增 |
|
|
|
// g_plsr_total_pulse_count += pulse_increment; |
|
|
|
// } |
|
|
|
// else |
|
|
|
// { |
|
|
|
// // 脉冲计数方向为0:递减(支持负数显示) |
|
|
|
// g_plsr_total_pulse_count -= pulse_increment; |
|
|
|
// } |
|
|
|
if (s_pulse_count_direction) |
|
|
|
{ |
|
|
|
// 脉冲计数方向为1:递增 |
|
|
|
g_plsr_location += pulse_increment; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
// 脉冲计数方向为0:递减(支持负数显示) |
|
|
|
g_plsr_location -= pulse_increment; |
|
|
|
} |
|
|
|
s_last_total_pulse = current_pulse_count; |
|
|
|
} |
|
|
|
// 将32位全局累加脉冲计数分解为两个16位寄存器(支持负数) |
|
|
@@ -1524,7 +1525,10 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) |
|
|
|
PLSR_UpdateGlobalPulseCount(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); |
|
|
|
|
|
|
|
// 处理加减速过程中的频率更新(使用新的直线加减速算法) |
|
|
|
if(g_plsr_route.run_state == PLSR_STATE_ACCEL || g_plsr_route.run_state == PLSR_STATE_DECEL) |
|
|
@@ -1601,11 +1605,6 @@ void PLSR_Route_Init(PLSR_RouteConfig_t* route) |
|
|
|
// 初始化模式相关参数 |
|
|
|
route->prevPulseCount = 0; // 累积脉冲计数:清零 |
|
|
|
route->pulse_count = 0; // 当前脉冲计数:清零 |
|
|
|
|
|
|
|
// 模式验证:确保mode字段有效 |
|
|
|
if (route->mode != PLSR_MODE_RELATIVE && route->mode != PLSR_MODE_ABSOLUTE) { |
|
|
|
route->mode = PLSR_MODE_RELATIVE; // 默认使用相对模式 |
|
|
|
} |
|
|
|
|
|
|
|
PLSR_TIM6_SetUpdateFreq(100); //初始化TIM6更新频率为1000us |
|
|
|
} |
|
|
@@ -1683,14 +1682,6 @@ void PLSR_Route_Stop(PLSR_RouteConfig_t* route) |
|
|
|
*/ |
|
|
|
// 存储上一段的方向信息,用于检测方向变化 |
|
|
|
static uint8_t s_last_direction = 0xFF; // 初始值设为无效值,确保第一次总是认为有方向变化 |
|
|
|
|
|
|
|
/** |
|
|
|
* @brief 设置方向端子状态 |
|
|
|
* @param route 路径控制结构体指针 |
|
|
|
* @param current_section 当前段配置指针 |
|
|
|
* @return 如果方向发生变化,返回1;否则返回0 |
|
|
|
* @note 根据脉冲方向逻辑和目标脉冲数设置方向端子状态 |
|
|
|
*/ |
|
|
|
static uint8_t PLSR_SetDirectionPin(PLSR_RouteConfig_t* route, PLSR_SectionConfig_t* current_section) |
|
|
|
{ |
|
|
|
if (route == NULL || current_section == NULL) return 0; |
|
|
@@ -1763,12 +1754,12 @@ void PLSR_Section_StartNewSection(PLSR_RouteConfig_t* route) |
|
|
|
if(g_plsr_route.mode == PLSR_MODE_ABSOLUTE) |
|
|
|
{ |
|
|
|
// 绝对模式:计算相对于当前位置的脉冲数 |
|
|
|
current_section->actual_pulse = current_section->target_pulse - g_plsr_total_pulse_count; |
|
|
|
current_section->actual_pulse = current_section->target_pulse - g_plsr_location; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
// 相对模式:直接使用目标脉冲数 |
|
|
|
current_section->actual_pulse = current_section->target_pulse; |
|
|
|
current_section->actual_pulse = current_section->target_pulse + route->prevPulseCount - g_plsr_location; |
|
|
|
} |
|
|
|
|
|
|
|
// 设置方向端子 - 使用计算后的actual_pulse确定方向 |
|
|
@@ -2019,7 +2010,7 @@ void PLSR_Section_SwitchNext(PLSR_RouteConfig_t* route, uint8_t is_pulse_complet |
|
|
|
else |
|
|
|
{ |
|
|
|
// 外部事件触发:直接使用全局脉冲计数器作为累计脉冲数 |
|
|
|
route->prevPulseCount = g_plsr_total_pulse_count; |
|
|
|
route->prevPulseCount = g_plsr_location; |
|
|
|
} |
|
|
|
} |
|
|
|
// 检查下一段是否有效 |
|
|
|