|
|
@@ -23,17 +23,18 @@ |
|
|
|
#include <math.h> // 用于sinf函数 |
|
|
|
|
|
|
|
/* USER CODE BEGIN 0 */ |
|
|
|
// ==================== PLSR全局变量定义 ==================== |
|
|
|
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; // 全局位置计数器(用于记录当前执行位置,支持负数) |
|
|
|
// ==================== PLSR全局变量声明 ==================== |
|
|
|
PLSR_RouteConfig_t PlsrRoute; // 全局PLSR路径控制结构体 |
|
|
|
int32_t PlsrTotalPulseCount = 0; // 全局累加脉冲计数器(程序运行期间持续累加,支持负数) |
|
|
|
uint8_t PlsrExtEventFlag = 0; // 外部事件标志(0-无事件, 1-事件触发) |
|
|
|
int32_t PlsrLocation = 0; // 全局位置计数器(用于记录当前执行位置,支持负数) |
|
|
|
static uint8_t s_pulse_count_direction = 1; // 脉冲计数方向(1-递增, 0-递减),用于替代dir_logic判断计数方向 |
|
|
|
uint32_t g_plsr_current_target_freq = 0; // 当前段目标频率频率(Hz),用于检测是否有频率变化 |
|
|
|
uint32_t PlsrCurrentTargetFreq = 0; // 当前段目标频率频率(Hz),用于检测是否有频率变化 |
|
|
|
static uint8_t s_last_direction = 0xFF; // 初始值设为无效值,确保第一次总是认为有方向变化 |
|
|
|
uint8_t g_plsr_mod_flag = 0xff; |
|
|
|
static uint8_t g_first_flag = 0; |
|
|
|
uint32_t total_se_pluse = 0; |
|
|
|
uint8_t PlsrModFlag = 0xff; |
|
|
|
static uint8_t FirstFlag = 0; |
|
|
|
uint32_t TotalSePulse = 0; |
|
|
|
uint8_t StopFlag = 0; |
|
|
|
// ==================== PLSR内部变量 ==================== |
|
|
|
|
|
|
|
// ==================== PLSR内部变量 ==================== |
|
|
@@ -316,7 +317,7 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle) |
|
|
|
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
|
|
|
|
|
|
|
/* TIM2 interrupt Init */ |
|
|
|
HAL_NVIC_SetPriority(TIM2_IRQn, 4, 4); |
|
|
|
HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0); |
|
|
|
HAL_NVIC_EnableIRQ(TIM2_IRQn); |
|
|
|
/* USER CODE BEGIN TIM2_MspInit 1 */ |
|
|
|
|
|
|
@@ -590,14 +591,15 @@ void PLSR_PWM_Init(void) |
|
|
|
/** |
|
|
|
* @brief 启动PWM输出 |
|
|
|
* @retval None |
|
|
|
* @note 根据g_plsr_route.output_port选择目标定时器:0-TIM10, 1-TIM11, 2-TIM13, 3-TIM14 |
|
|
|
* @note 根据PlsrRoute.output_port选择目标定时器:0-TIM10, 1-TIM11, 2-TIM13, 3-TIM14 |
|
|
|
*/ |
|
|
|
void PLSR_PWM_Start(void) |
|
|
|
{ |
|
|
|
if (g_plsr_route.route_state == PLSR_ROUTE_RUNNING) //<只有在路径运行状态下才可以进行pwm输出 |
|
|
|
if (PlsrRoute.route_state == PLSR_ROUTE_RUNNING) //<只有在路径运行状态下才可以进行pwm输出 |
|
|
|
{ |
|
|
|
StopFlag = 0; |
|
|
|
// 根据output_port选择目标定时器并启动PWM输出 |
|
|
|
switch(g_plsr_route.output_port) |
|
|
|
switch(PlsrRoute.output_port) |
|
|
|
{ |
|
|
|
case 0: // TIM10 |
|
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, htim10.Init.Period / 2); // 设置占空比为50% |
|
|
@@ -631,12 +633,13 @@ void PLSR_PWM_Start(void) |
|
|
|
/** |
|
|
|
* @brief 停止PWM输出 |
|
|
|
* @retval None |
|
|
|
* @note 根据g_plsr_route.output_port选择目标定时器:0-TIM10, 1-TIM11, 2-TIM13, 3-TIM14 |
|
|
|
* @note 根据PlsrRoute.output_port选择目标定时器:0-TIM10, 1-TIM11, 2-TIM13, 3-TIM14 |
|
|
|
*/ |
|
|
|
void PLSR_Section_PWM_Stop(void) |
|
|
|
{ |
|
|
|
StopFlag = 1; |
|
|
|
// 根据output_port选择目标定时器并停止PWM输出 |
|
|
|
switch(g_plsr_route.output_port) |
|
|
|
switch(PlsrRoute.output_port) |
|
|
|
{ |
|
|
|
case 0: // TIM10 |
|
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, 0); // 停止PWM输出 |
|
|
@@ -668,7 +671,7 @@ void PLSR_Section_PWM_Stop(void) |
|
|
|
void PLSR_Route_PWM_Stop() |
|
|
|
{ |
|
|
|
// 根据output_port选择目标定时器并停止PWM输出 |
|
|
|
switch(g_plsr_route.output_port) |
|
|
|
switch(PlsrRoute.output_port) |
|
|
|
{ |
|
|
|
case 0: // TIM10 |
|
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, 0xFFFF); // 完全停止PWM输出 |
|
|
@@ -700,7 +703,7 @@ void PLSR_Route_PWM_Stop() |
|
|
|
/** |
|
|
|
* @brief 设置PWM频率 |
|
|
|
* @param frequency: PWM频率 (1Hz-100kHz) |
|
|
|
* @note 根据g_plsr_route.output_port选择目标定时器:0-TIM10, 1-TIM11, 2-TIM13, 3-TIM14 |
|
|
|
* @note 根据PlsrRoute.output_port选择目标定时器:0-TIM10, 1-TIM11, 2-TIM13, 3-TIM14 |
|
|
|
*/ |
|
|
|
void PLSR_PWM_SetFrequency(uint32_t frequency) |
|
|
|
{ |
|
|
@@ -721,40 +724,45 @@ void PLSR_PWM_SetFrequency(uint32_t frequency) |
|
|
|
{ |
|
|
|
return; // period无效,直接返回,避免HardFault异常 |
|
|
|
} |
|
|
|
|
|
|
|
// 根据output_port选择目标定时器并更新参数 |
|
|
|
switch(g_plsr_route.output_port) |
|
|
|
switch(PlsrRoute.output_port) |
|
|
|
{ |
|
|
|
case 0: // TIM10 |
|
|
|
__HAL_TIM_SET_PRESCALER(&htim10, prescaler); |
|
|
|
__HAL_TIM_SET_AUTORELOAD(&htim10, period); |
|
|
|
if(StopFlag == 0) |
|
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, period / 2); |
|
|
|
break; |
|
|
|
|
|
|
|
case 1: // TIM11 |
|
|
|
__HAL_TIM_SET_PRESCALER(&htim11, prescaler); |
|
|
|
__HAL_TIM_SET_AUTORELOAD(&htim11, period); |
|
|
|
if(StopFlag == 0) |
|
|
|
__HAL_TIM_SET_COMPARE(&htim11, TIM_CHANNEL_1, period / 2); |
|
|
|
break; |
|
|
|
|
|
|
|
case 2: // TIM13 |
|
|
|
__HAL_TIM_SET_PRESCALER(&htim13, prescaler); |
|
|
|
__HAL_TIM_SET_AUTORELOAD(&htim13, period); |
|
|
|
if(StopFlag == 0) |
|
|
|
__HAL_TIM_SET_COMPARE(&htim13, TIM_CHANNEL_1, period / 2); |
|
|
|
break; |
|
|
|
|
|
|
|
case 3: // TIM14 |
|
|
|
__HAL_TIM_SET_PRESCALER(&htim14, prescaler); |
|
|
|
__HAL_TIM_SET_AUTORELOAD(&htim14, period); |
|
|
|
if(StopFlag == 0) |
|
|
|
__HAL_TIM_SET_COMPARE(&htim14, TIM_CHANNEL_1, period / 2); |
|
|
|
break; |
|
|
|
|
|
|
|
default: // 默认使用TIM10 |
|
|
|
__HAL_TIM_SET_PRESCALER(&htim10, prescaler); |
|
|
|
__HAL_TIM_SET_AUTORELOAD(&htim10, period); |
|
|
|
if(StopFlag == 0) |
|
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, period / 2); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// ==================== PLSR TIM6频率配置函数实现 ==================== |
|
|
@@ -823,7 +831,7 @@ void PLSR_TIM6_Stop(void) |
|
|
|
HAL_TIM_Base_Stop_IT(&htim6); |
|
|
|
} |
|
|
|
|
|
|
|
static int32_t AllPluse = 0; //总脉冲个数 |
|
|
|
static int32_t AllPulse = 0; //总脉冲个数 |
|
|
|
static int32_t s_last_total_pulse = 0; //上次记录的总脉冲数,用于实时累加 |
|
|
|
|
|
|
|
|
|
|
@@ -837,21 +845,21 @@ static void PLSR_UpdateGlobalPulseCount(int32_t current_pulse_count) |
|
|
|
if (current_pulse_count > s_last_total_pulse) { |
|
|
|
int32_t pulse_increment = current_pulse_count - s_last_total_pulse; |
|
|
|
// 脉冲计数方向为1:递增 |
|
|
|
g_plsr_total_pulse_count += pulse_increment; |
|
|
|
PlsrTotalPulseCount += pulse_increment; |
|
|
|
if (s_pulse_count_direction) |
|
|
|
{ |
|
|
|
// 脉冲计数方向为1:递增 |
|
|
|
g_plsr_location += pulse_increment; |
|
|
|
PlsrLocation += pulse_increment; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
// 脉冲计数方向为0:递减(支持负数显示) |
|
|
|
g_plsr_location -= pulse_increment; |
|
|
|
PlsrLocation -= pulse_increment; |
|
|
|
} |
|
|
|
s_last_total_pulse = current_pulse_count; |
|
|
|
} |
|
|
|
// 将32位全局累加脉冲计数分解为两个16位寄存器(支持负数) |
|
|
|
int32_t signed_count = g_plsr_location; // 使用有符号数进行计算 |
|
|
|
int32_t signed_count = PlsrLocation; // 使用有符号数进行计算 |
|
|
|
backup_data->holding_regs[73] = signed_count & 0xFFFF; |
|
|
|
backup_data->holding_regs[74] = (signed_count >> 16) & 0xFFFF; |
|
|
|
} |
|
|
@@ -859,34 +867,34 @@ static void PLSR_UpdateGlobalPulseCount(int32_t current_pulse_count) |
|
|
|
void PLSR_HandleSectionEnd(void) |
|
|
|
{ |
|
|
|
// 清零所有部分的脉冲计数 |
|
|
|
g_plsr_route.accel_pulse_count = 0; |
|
|
|
g_plsr_route.const_pulse_count = 0; |
|
|
|
g_plsr_route.decel_pulse_count = 0; |
|
|
|
total_se_pluse += g_plsr_route.section[g_plsr_route.current_section_num-1].target_pulse; |
|
|
|
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; |
|
|
|
|
|
|
|
// 重置部分状态 |
|
|
|
g_plsr_route.current_part = PLSR_PART_COMPLETE; |
|
|
|
|
|
|
|
if(g_plsr_route.current_section_num >= g_plsr_route.section_num |
|
|
|
&& g_plsr_route.section[g_plsr_route.current_section_num - 1].next_section == 0) |
|
|
|
PlsrRoute.current_part = PLSR_PART_COMPLETE; |
|
|
|
// 检查是否为最后一段且下一段为0(路径结束) |
|
|
|
if(PlsrRoute.current_section_num >= PlsrRoute.section_num |
|
|
|
&& PlsrRoute.section[PlsrRoute.current_section_num - 1].next_section == 0) |
|
|
|
{ |
|
|
|
// 最后一段完成,路径结束 |
|
|
|
PLSR_Route_Stop(&g_plsr_route); |
|
|
|
PLSR_Route_Stop(&PlsrRoute); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
// 非最后一段,进入等待状态准备切换到下一段 |
|
|
|
g_plsr_route.run_state = PLSR_STATE_WAIT; |
|
|
|
PlsrRoute.run_state = PLSR_STATE_WAIT; |
|
|
|
|
|
|
|
if(g_plsr_route.section[g_plsr_route.current_section_num - 1].wait_condition.wait_type == PLSR_WAIT_PLUSEEND) |
|
|
|
if(PlsrRoute.section[PlsrRoute.current_section_num - 1].wait_condition.wait_type == PLSR_WAIT_PLUSEEND) |
|
|
|
{ |
|
|
|
// g_plsr_route.current_freq = g_plsr_route.section[g_plsr_route.current_section_num - 1].target_freq; //加速不到目标频率的情况可能不成立. |
|
|
|
// PlsrRoute.current_freq = PlsrRoute.section[PlsrRoute.current_section_num - 1].target_freq; //加速不到目标频率的情况可能不成立. |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
// 否则设置为0,表示停止 |
|
|
|
g_plsr_route.current_freq = 0; |
|
|
|
g_plsr_route.initial_freq = 0; |
|
|
|
PlsrRoute.current_freq = 0; |
|
|
|
PlsrRoute.initial_freq = 0; |
|
|
|
} |
|
|
|
PLSR_SectionSwitchSignal(); |
|
|
|
} |
|
|
@@ -897,46 +905,48 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) |
|
|
|
// TIM2中断:负责段切换逻辑 |
|
|
|
if(htim->Instance == TIM2) |
|
|
|
{ |
|
|
|
if(g_plsr_route.current_section_num >= g_plsr_route.section_num && |
|
|
|
g_plsr_route.current_part != PLSR_PART_1 && |
|
|
|
g_plsr_route.run_state == PLSR_STATE_DECEL) |
|
|
|
if(PlsrRoute.current_section_num >= PlsrRoute.section_num && |
|
|
|
PlsrRoute.current_part != PLSR_PART_1 && |
|
|
|
PlsrRoute.run_state == PLSR_STATE_DECEL) |
|
|
|
{ |
|
|
|
PLSR_Route_PWM_Stop(); |
|
|
|
} |
|
|
|
else PLSR_Section_PWM_Stop(); |
|
|
|
if(g_plsr_route.part1_target_freq == g_plsr_route.section[g_plsr_route.current_section_num - 1].target_freq |
|
|
|
&& g_plsr_route.current_part == 1) |
|
|
|
if(PlsrRoute.part1_target_freq == PlsrRoute.section[PlsrRoute.current_section_num - 1].target_freq |
|
|
|
&& PlsrRoute.current_part == 1) |
|
|
|
{ |
|
|
|
g_plsr_route.current_freq = g_plsr_route.target_freq; |
|
|
|
PlsrRoute.current_freq = PlsrRoute.target_freq; |
|
|
|
} |
|
|
|
// 精确累加当前段已发送的脉冲数 |
|
|
|
int32_t current_section_pulses = __HAL_TIM_GetAutoreload(&htim2); |
|
|
|
if(g_plsr_mod_flag == 1) |
|
|
|
if(PlsrModFlag == 1) |
|
|
|
{ |
|
|
|
current_section_pulses; |
|
|
|
g_plsr_mod_flag = 0; |
|
|
|
current_section_pulses -= 1; |
|
|
|
PlsrModFlag = 0; |
|
|
|
} |
|
|
|
AllPluse += current_section_pulses; |
|
|
|
g_plsr_route.pulse_count = AllPluse; |
|
|
|
PLSR_UpdateGlobalPulseCount(AllPluse); |
|
|
|
AllPulse += current_section_pulses; |
|
|
|
PlsrRoute.pulse_count = AllPulse; |
|
|
|
PLSR_UpdateGlobalPulseCount(AllPulse); |
|
|
|
// 三部分状态机处理 |
|
|
|
switch(g_plsr_route.current_part) |
|
|
|
switch(PlsrRoute.current_part) |
|
|
|
{ |
|
|
|
case PLSR_PART_1: |
|
|
|
// 判断下一部分 |
|
|
|
if(g_plsr_route.const_pulse_count > 0) |
|
|
|
if(PlsrRoute.const_pulse_count > 0) |
|
|
|
{ |
|
|
|
if(g_plsr_route.const_pulse_count > 1) |
|
|
|
if(PlsrRoute.const_pulse_count > 1) |
|
|
|
{ |
|
|
|
// 进入第二部分:匀速 |
|
|
|
g_plsr_route.current_part = PLSR_PART_2; |
|
|
|
g_plsr_route.run_state = g_plsr_route.part2_state; |
|
|
|
g_plsr_route.target_freq = g_plsr_route.part2_target_freq; |
|
|
|
g_plsr_route.initial_freq = g_plsr_route.part2_target_freq; // 更新加减速初始频率 |
|
|
|
|
|
|
|
g_plsr_route.current_freq = g_plsr_route.target_freq; |
|
|
|
PLSR_PWM_SetFrequency(g_plsr_route.current_freq); |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, g_plsr_route.const_pulse_count); |
|
|
|
PlsrRoute.current_part = PLSR_PART_2; |
|
|
|
PlsrRoute.run_state = PlsrRoute.part2_state; |
|
|
|
PlsrRoute.target_freq = PlsrRoute.part2_target_freq; |
|
|
|
PlsrRoute.initial_freq = PlsrRoute.part2_target_freq; // 更新加减速初始频率 |
|
|
|
|
|
|
|
PlsrRoute.current_freq = PlsrRoute.target_freq; |
|
|
|
PLSR_PWM_SetFrequency(PlsrRoute.current_freq); |
|
|
|
// if(PlsrRoute.current_freq >= 90000) |
|
|
|
// HAL_TIM_GenerateEvent(&htim10, TIM_EVENTSOURCE_UPDATE); |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, PlsrRoute.const_pulse_count); |
|
|
|
__HAL_TIM_SET_COUNTER(&htim2, 1); |
|
|
|
PLSR_PWM_Start(); |
|
|
|
break; |
|
|
@@ -944,29 +954,29 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) |
|
|
|
else |
|
|
|
{ |
|
|
|
// 进入第二部分:匀速 |
|
|
|
g_plsr_route.current_part = PLSR_PART_2; |
|
|
|
g_plsr_route.run_state = g_plsr_route.part2_state; |
|
|
|
g_plsr_route.target_freq = g_plsr_route.part2_target_freq; |
|
|
|
g_plsr_route.initial_freq = g_plsr_route.part2_target_freq; // 更新加减速初始频率 |
|
|
|
g_plsr_route.current_freq = g_plsr_route.target_freq; |
|
|
|
PLSR_PWM_SetFrequency(g_plsr_route.current_freq); |
|
|
|
PLSR_PWM_Start(); |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, g_plsr_route.const_pulse_count); |
|
|
|
PlsrRoute.current_part = PLSR_PART_2; |
|
|
|
PlsrRoute.run_state = PlsrRoute.part2_state; |
|
|
|
PlsrRoute.target_freq = PlsrRoute.part2_target_freq; |
|
|
|
PlsrRoute.initial_freq = PlsrRoute.part2_target_freq; // 更新加减速初始频率 |
|
|
|
PlsrRoute.current_freq = PlsrRoute.target_freq; |
|
|
|
PLSR_PWM_SetFrequency(PlsrRoute.current_freq); |
|
|
|
PLSR_PWM_Start(); |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, PlsrRoute.const_pulse_count); |
|
|
|
__HAL_TIM_SET_COUNTER(&htim2, 1); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
else if(g_plsr_route.decel_pulse_count > 0) |
|
|
|
else if(PlsrRoute.decel_pulse_count > 0) |
|
|
|
{ |
|
|
|
if(g_plsr_route.decel_pulse_count > 1) |
|
|
|
if(PlsrRoute.decel_pulse_count > 1) |
|
|
|
{ |
|
|
|
// 无匀速阶段,直接进入第三部分:减速 |
|
|
|
g_plsr_route.current_part = PLSR_PART_3; |
|
|
|
g_plsr_route.run_state = g_plsr_route.part3_state; |
|
|
|
g_plsr_route.target_freq = g_plsr_route.part3_target_freq; |
|
|
|
g_plsr_route.initial_freq = g_plsr_route.current_freq; // 更新加减速初始频率 |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, g_plsr_route.decel_pulse_count); |
|
|
|
PlsrRoute.current_part = PLSR_PART_3; |
|
|
|
PlsrRoute.run_state = PlsrRoute.part3_state; |
|
|
|
PlsrRoute.target_freq = PlsrRoute.part3_target_freq; |
|
|
|
PlsrRoute.initial_freq = PlsrRoute.current_freq; // 更新加减速初始频率 |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, PlsrRoute.decel_pulse_count); |
|
|
|
__HAL_TIM_SET_COUNTER(&htim2, 1); |
|
|
|
PLSR_PWM_Start(); |
|
|
|
break; |
|
|
@@ -974,15 +984,15 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) |
|
|
|
else |
|
|
|
{ |
|
|
|
// 无匀速阶段,直接进入第三部分:减速 |
|
|
|
g_plsr_route.current_part = PLSR_PART_3; |
|
|
|
g_plsr_route.run_state = g_plsr_route.part3_state; |
|
|
|
g_plsr_route.target_freq = g_plsr_route.part3_target_freq; |
|
|
|
g_plsr_route.initial_freq = g_plsr_route.current_freq; // 更新加减速初始频率 |
|
|
|
PlsrRoute.current_part = PLSR_PART_3; |
|
|
|
PlsrRoute.run_state = PlsrRoute.part3_state; |
|
|
|
PlsrRoute.target_freq = PlsrRoute.part3_target_freq; |
|
|
|
PlsrRoute.initial_freq = PlsrRoute.current_freq; // 更新加减速初始频率 |
|
|
|
uint32_t arr_freq = 0; |
|
|
|
arr_freq = PLSR_Calculate_FreqByPosition(&g_plsr_route,0); |
|
|
|
arr_freq = PLSR_Calculate_FreqByPosition(&PlsrRoute,0); |
|
|
|
PLSR_PWM_SetFrequency(arr_freq); |
|
|
|
PLSR_PWM_Start(); |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, g_plsr_route.decel_pulse_count); |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, PlsrRoute.decel_pulse_count); |
|
|
|
__HAL_TIM_SET_COUNTER(&htim2, 1); |
|
|
|
break; |
|
|
|
} |
|
|
@@ -991,39 +1001,38 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) |
|
|
|
else |
|
|
|
{ |
|
|
|
// 第一部分完成且无后续部分,当前段结束 |
|
|
|
g_plsr_route.current_part = PLSR_PART_COMPLETE; |
|
|
|
PlsrRoute.current_part = PLSR_PART_COMPLETE; |
|
|
|
PLSR_HandleSectionEnd(); |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
case PLSR_PART_2: |
|
|
|
// 进入第三部分:减速 |
|
|
|
if(g_plsr_route.decel_pulse_count > 0) |
|
|
|
if(PlsrRoute.decel_pulse_count > 0) |
|
|
|
{ |
|
|
|
if(g_plsr_route.decel_pulse_count > 1) |
|
|
|
if(PlsrRoute.decel_pulse_count > 1) |
|
|
|
{ |
|
|
|
g_plsr_route.current_part = PLSR_PART_3; |
|
|
|
g_plsr_route.run_state = g_plsr_route.part3_state; |
|
|
|
g_plsr_route.target_freq = g_plsr_route.part3_target_freq; |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, g_plsr_route.decel_pulse_count); |
|
|
|
PlsrRoute.current_part = PLSR_PART_3; |
|
|
|
PlsrRoute.run_state = PlsrRoute.part3_state; |
|
|
|
PlsrRoute.target_freq = PlsrRoute.part3_target_freq; |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, PlsrRoute.decel_pulse_count); |
|
|
|
__HAL_TIM_SET_COUNTER(&htim2, 1); |
|
|
|
PLSR_PWM_Start(); |
|
|
|
break; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
g_plsr_route.current_part = PLSR_PART_3; |
|
|
|
g_plsr_route.run_state = g_plsr_route.part3_state; |
|
|
|
g_plsr_route.target_freq = g_plsr_route.part3_target_freq; |
|
|
|
PlsrRoute.current_part = PLSR_PART_3; |
|
|
|
PlsrRoute.run_state = PlsrRoute.part3_state; |
|
|
|
PlsrRoute.target_freq = PlsrRoute.part3_target_freq; |
|
|
|
uint32_t arr_freq = 0; |
|
|
|
arr_freq = PLSR_Calculate_FreqByPosition(&g_plsr_route,0); |
|
|
|
arr_freq = PLSR_Calculate_FreqByPosition(&PlsrRoute,0); |
|
|
|
PLSR_PWM_SetFrequency(arr_freq); |
|
|
|
PLSR_PWM_Start(); |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, g_plsr_route.decel_pulse_count); |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, PlsrRoute.decel_pulse_count); |
|
|
|
__HAL_TIM_SET_COUNTER(&htim2, 1); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
@@ -1047,34 +1056,36 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) |
|
|
|
uint32_t current_tim2_count = __HAL_TIM_GET_COUNTER(&htim2); |
|
|
|
|
|
|
|
// 计算当前段已发送的总脉冲数 |
|
|
|
int32_t current_pulse_count = AllPluse + current_tim2_count; |
|
|
|
|
|
|
|
int32_t current_pulse_count = AllPulse + current_tim2_count; |
|
|
|
PlsrRoute.pulse_count = current_pulse_count; |
|
|
|
// 实时累加脉冲增量到全局计数器(根据方向逻辑决定递增或递减) |
|
|
|
PLSR_UpdateGlobalPulseCount(current_pulse_count); |
|
|
|
// 更新路由结构体的脉冲计数(使用原子操作保证一致性) |
|
|
|
g_plsr_route.pulse_count = current_pulse_count; |
|
|
|
|
|
|
|
// 计算当前段的Modbus地址基址 |
|
|
|
uint16_t current_seg_base_addr = MODBUS_ADDR_SEGMENT_BASE + ((g_plsr_route.current_section_num-1) * MODBUS_ADDR_SEGMENT_OFFSET); |
|
|
|
uint16_t current_seg_base_addr = MODBUS_ADDR_SEGMENT_BASE + ((PlsrRoute.current_section_num-1) * MODBUS_ADDR_SEGMENT_OFFSET); |
|
|
|
|
|
|
|
uint16_t freq_low_index = 13 + (PlsrRoute.current_section_num - 1) * 6 + MODBUS_OFFSET_FREQ_LOW; |
|
|
|
uint16_t freq_high_index = 13 + (PlsrRoute.current_section_num - 1) * 6 + MODBUS_OFFSET_FREQ_HIGH; |
|
|
|
|
|
|
|
g_plsr_route.section[g_plsr_route.current_section_num-1].target_freq = |
|
|
|
((uint32_t)FlashSave_Get_Holding_Register(current_seg_base_addr + MODBUS_OFFSET_FREQ_LOW)) | |
|
|
|
((uint32_t)FlashSave_Get_Holding_Register(current_seg_base_addr + MODBUS_OFFSET_FREQ_HIGH) << 16); |
|
|
|
PlsrRoute.section[PlsrRoute.current_section_num-1].target_freq = |
|
|
|
((uint32_t)backup_data->holding_regs[freq_low_index]) | |
|
|
|
((uint32_t)backup_data->holding_regs[freq_high_index] << 16); |
|
|
|
|
|
|
|
if(g_plsr_route.section[g_plsr_route.current_section_num-1].target_freq != g_plsr_current_target_freq && |
|
|
|
g_plsr_route.current_part != PLSR_PART_COMPLETE) |
|
|
|
if(PlsrRoute.section[PlsrRoute.current_section_num-1].target_freq != PlsrCurrentTargetFreq && |
|
|
|
PlsrRoute.current_part != PLSR_PART_COMPLETE) |
|
|
|
{ |
|
|
|
g_plsr_mod_flag = 1; // 标记当前段目标频率被修改 |
|
|
|
PlsrModFlag = 1; // 标记当前段目标频率被修改 |
|
|
|
PLSR_Section_PWM_Stop(); |
|
|
|
AllPluse += current_tim2_count; // 累加当前段已发送的脉冲数 |
|
|
|
PLSR_Section_StartNewSection(&g_plsr_route); ///<重新启动当前段 |
|
|
|
AllPulse += current_tim2_count; // 累加当前段已发送的脉冲数 |
|
|
|
PLSR_Section_StartNewSection(&PlsrRoute); ///<重新启动当前段 |
|
|
|
PLSR_PWM_Start(); |
|
|
|
} |
|
|
|
|
|
|
|
// 处理加减速过程中的频率更新(使用新的直线加减速算法) |
|
|
|
if(g_plsr_route.run_state == PLSR_STATE_ACCEL || g_plsr_route.run_state == PLSR_STATE_DECEL) |
|
|
|
if(PlsrRoute.run_state == PLSR_STATE_ACCEL || PlsrRoute.run_state == PLSR_STATE_DECEL) |
|
|
|
{ |
|
|
|
PLSR_Accel_Process(&g_plsr_route); |
|
|
|
PLSR_Accel_Process(&PlsrRoute); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@@ -1179,7 +1190,7 @@ void PLSR_Route_Start(PLSR_RouteConfig_t* route) |
|
|
|
if (route->route_state == PLSR_ROUTE_RUNNING) |
|
|
|
return; |
|
|
|
|
|
|
|
PLSR_Route_Init(&g_plsr_route); |
|
|
|
PLSR_Route_Init(&PlsrRoute); |
|
|
|
// 启动时初始化用户可配置参数 |
|
|
|
PLSR_Route_Set(route); |
|
|
|
|
|
|
@@ -1191,14 +1202,14 @@ void PLSR_Route_Start(PLSR_RouteConfig_t* route) |
|
|
|
route->run_state = PLSR_STATE_IDLE; // 设置运行状态为空闲 |
|
|
|
|
|
|
|
// 重置全局脉冲计数器 |
|
|
|
AllPluse = 0; |
|
|
|
g_first_flag = 0; |
|
|
|
AllPulse = 0; |
|
|
|
FirstFlag = 0; |
|
|
|
|
|
|
|
// 重置静态变量,确保每次启动效果一致 |
|
|
|
s_last_total_pulse = 0; // 重置上次记录的总脉冲数 |
|
|
|
s_last_direction = 0xFF; // 重置方向状态为初始无效值 |
|
|
|
s_pulse_count_direction = 1; // 重置脉冲计数方向为默认值 |
|
|
|
g_plsr_mod_flag = 0; // 重置修改标志 |
|
|
|
PlsrModFlag = 0; // 重置修改标志 |
|
|
|
|
|
|
|
// 重置所有PWM定时器状态,确保每次启动都是干净的状态 |
|
|
|
__HAL_TIM_SET_COUNTER(&htim10, 0); // 重置TIM10计数器 |
|
|
@@ -1265,7 +1276,7 @@ void PLSR_Route_Stop(PLSR_RouteConfig_t* route) |
|
|
|
|
|
|
|
// 重置目标频率 |
|
|
|
route->target_freq = 0; |
|
|
|
g_plsr_current_target_freq = 0; |
|
|
|
PlsrCurrentTargetFreq = 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
@@ -1365,28 +1376,27 @@ void PLSR_Section_StartNewSection(PLSR_RouteConfig_t* route) |
|
|
|
// 获取当前段的配置指针(段号从1开始,数组索引从0开始) |
|
|
|
PLSR_SectionConfig_t* current_section = &route->section[route->current_section_num - 1]; |
|
|
|
|
|
|
|
g_plsr_current_target_freq = current_section->target_freq; // 更新当前目标频率 |
|
|
|
PlsrCurrentTargetFreq = current_section->target_freq; // 更新当前目标频率 |
|
|
|
// 统一使用相对模式计算实际可发脉冲数 |
|
|
|
if(g_plsr_route.mode == PLSR_MODE_ABSOLUTE) |
|
|
|
if(PlsrRoute.mode == PLSR_MODE_ABSOLUTE) |
|
|
|
{ |
|
|
|
// 绝对模式:计算相对于当前位置的脉冲数 |
|
|
|
current_section->actual_pulse = current_section->target_pulse - g_plsr_location; |
|
|
|
if(g_plsr_mod_flag == 1) |
|
|
|
current_section->actual_pulse = current_section->target_pulse - PlsrLocation; |
|
|
|
if(PlsrModFlag == 1) |
|
|
|
{ |
|
|
|
current_section->actual_pulse; |
|
|
|
//g_plsr_mod_flag = 0; // 清除修改标记 |
|
|
|
current_section->actual_pulse += 1; |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
if(g_plsr_mod_flag == 1) |
|
|
|
if(PlsrModFlag == 1) |
|
|
|
{ |
|
|
|
g_plsr_route.initial_freq = g_plsr_route.current_freq; // 更新加减速初始频率 |
|
|
|
PlsrRoute.initial_freq = PlsrRoute.current_freq; // 更新加减速初始频率 |
|
|
|
if(current_section->target_pulse > 0) |
|
|
|
current_section->actual_pulse = total_se_pluse + current_section->target_pulse - g_plsr_total_pulse_count; |
|
|
|
current_section->actual_pulse = TotalSePulse + current_section->target_pulse - PlsrTotalPulseCount; |
|
|
|
else |
|
|
|
current_section->actual_pulse = total_se_pluse + (-current_section->target_pulse) - g_plsr_total_pulse_count; |
|
|
|
current_section->actual_pulse; |
|
|
|
current_section->actual_pulse = TotalSePulse + (-current_section->target_pulse) - PlsrTotalPulseCount; |
|
|
|
current_section->actual_pulse += 1; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
@@ -1419,6 +1429,10 @@ void PLSR_Section_StartNewSection(PLSR_RouteConfig_t* route) |
|
|
|
{ |
|
|
|
current_section->actual_pulse = -current_section->actual_pulse; |
|
|
|
} |
|
|
|
else if(current_section->actual_pulse == 0) |
|
|
|
{ |
|
|
|
PLSR_HandleSectionEnd(); |
|
|
|
} |
|
|
|
|
|
|
|
if(current_section->section_num == route->section_num) |
|
|
|
{ |
|
|
@@ -1555,7 +1569,7 @@ void PLSR_SetupThreePartExecution(PLSR_RouteConfig_t* route) |
|
|
|
if(route->run_state == PLSR_STATE_ACCEL) |
|
|
|
{ |
|
|
|
uint32_t arr_freq = 0; |
|
|
|
arr_freq = PLSR_Calculate_FreqByPosition(&g_plsr_route,1); |
|
|
|
arr_freq = PLSR_Calculate_FreqByPosition(&PlsrRoute,1); |
|
|
|
route->current_freq = arr_freq; |
|
|
|
// 加速完成检查 |
|
|
|
if (route->run_state == PLSR_STATE_ACCEL && route->current_freq >= route->target_freq) |
|
|
@@ -1564,11 +1578,13 @@ void PLSR_SetupThreePartExecution(PLSR_RouteConfig_t* route) |
|
|
|
PLSR_PWM_SetFrequency(route->current_freq); |
|
|
|
} |
|
|
|
PLSR_PWM_SetFrequency(route->current_freq); |
|
|
|
if(PlsrRoute.current_freq >= 15000) |
|
|
|
HAL_TIM_GenerateEvent(&htim10, TIM_EVENTSOURCE_UPDATE); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
uint32_t arr_freq = 0; |
|
|
|
arr_freq = PLSR_Calculate_FreqByPosition(&g_plsr_route,0); |
|
|
|
arr_freq = PLSR_Calculate_FreqByPosition(&PlsrRoute,0); |
|
|
|
route->current_freq = arr_freq; |
|
|
|
// 减速完成检查 - 只有当减速到目标频率且目标频率大于0时才切换到匀速 |
|
|
|
if (route->run_state == PLSR_STATE_DECEL && route->current_freq <= route->target_freq && route->target_freq > 0) |
|
|
@@ -1577,10 +1593,12 @@ void PLSR_SetupThreePartExecution(PLSR_RouteConfig_t* route) |
|
|
|
PLSR_PWM_SetFrequency(route->current_freq); |
|
|
|
} |
|
|
|
PLSR_PWM_SetFrequency(route->current_freq); |
|
|
|
if(PlsrRoute.current_freq >= 15000) |
|
|
|
HAL_TIM_GenerateEvent(&htim10, TIM_EVENTSOURCE_UPDATE); |
|
|
|
} |
|
|
|
if(g_first_flag == 0 && route->current_freq != 0) //在这里第一次启动PWM输出避免发默认脉冲 |
|
|
|
{ |
|
|
|
g_first_flag = 1; |
|
|
|
if(FirstFlag == 0 && route->current_freq != 0) //在这里第一次启动PWM输出避免发默认脉冲 |
|
|
|
{ |
|
|
|
FirstFlag = 1; |
|
|
|
switch (route->output_port) |
|
|
|
{ |
|
|
|
case 0: |
|
|
@@ -1693,8 +1711,8 @@ void PLSR_Section_SwitchNext(PLSR_RouteConfig_t* route, uint8_t is_pulse_complet |
|
|
|
else |
|
|
|
{ |
|
|
|
// 外部事件触发:直接使用全局脉冲计数器作为累计脉冲数 |
|
|
|
route->prevPulseCount = g_plsr_total_pulse_count; |
|
|
|
total_se_pluse += g_plsr_total_pulse_count; |
|
|
|
route->prevPulseCount = PlsrTotalPulseCount; |
|
|
|
TotalSePulse += PlsrTotalPulseCount; |
|
|
|
} |
|
|
|
} |
|
|
|
if(next_section_num == 0) |
|
|
@@ -1875,6 +1893,7 @@ void PLSR_Accel_Process(PLSR_RouteConfig_t* route) |
|
|
|
if (calculated_freq >= route->target_freq) |
|
|
|
{ |
|
|
|
calculated_freq = route->target_freq; // 限制到目标频率 |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// 只有当计算出的频率与当前频率不同时才更新 |
|
|
@@ -1882,11 +1901,12 @@ void PLSR_Accel_Process(PLSR_RouteConfig_t* route) |
|
|
|
{ |
|
|
|
route->current_freq = calculated_freq; |
|
|
|
PLSR_PWM_SetFrequency(route->current_freq); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
if(g_first_flag == 0 && route->current_freq != 0) //在这里第一次启动PWM输出避免发默认脉冲 |
|
|
|
{ |
|
|
|
g_first_flag = 1; |
|
|
|
if(FirstFlag == 0 && route->current_freq != 0) //在这里第一次启动PWM输出避免发默认脉冲 |
|
|
|
{ |
|
|
|
FirstFlag = 1; |
|
|
|
PLSR_PWM_Start(); |
|
|
|
switch (route->output_port) |
|
|
|
{ |
|
|
@@ -1906,7 +1926,8 @@ void PLSR_Accel_Process(PLSR_RouteConfig_t* route) |
|
|
|
break; |
|
|
|
default: |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
// ==================== 减速处理 ==================== |
|
|
@@ -1917,7 +1938,8 @@ void PLSR_Accel_Process(PLSR_RouteConfig_t* route) |
|
|
|
uint32_t calculated_freq = PLSR_Calculate_FreqByPosition(route, 0); |
|
|
|
|
|
|
|
// 检查是否达到目标频率 |
|
|
|
if (calculated_freq <= route->target_freq) { |
|
|
|
if (calculated_freq <= route->target_freq) |
|
|
|
{ |
|
|
|
calculated_freq = route->target_freq; // 限制到目标频率 |
|
|
|
|
|
|
|
// 如果目标频率为0,说明减速到停止 |
|
|
@@ -1929,13 +1951,15 @@ void PLSR_Accel_Process(PLSR_RouteConfig_t* route) |
|
|
|
} |
|
|
|
|
|
|
|
// 只有当计算出的频率与当前频率不同时才更新PWM频率 |
|
|
|
if (calculated_freq != route->current_freq) { |
|
|
|
if (calculated_freq != route->current_freq) |
|
|
|
{ |
|
|
|
route->current_freq = calculated_freq; |
|
|
|
|
|
|
|
if (route->current_freq > 0) |
|
|
|
{ |
|
|
|
PLSR_PWM_SetFrequency(route->current_freq); |
|
|
|
} |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@@ -2016,7 +2040,7 @@ void PLSR_TaskSectionSwitch(PLSR_RouteConfig_t* route) |
|
|
|
/* 如果当前段等待外部事件,检查是否有事件触发 */ |
|
|
|
if (PLSR_Wait_CheckExtEvent(route)) |
|
|
|
{ |
|
|
|
AllPluse += __HAL_TIM_GET_COUNTER(&htim2); // 累加当前段已发送的脉冲数 |
|
|
|
AllPulse += __HAL_TIM_GET_COUNTER(&htim2); // 累加当前段已发送的脉冲数 |
|
|
|
|
|
|
|
/* 检查当前段是否为最后一段 */ |
|
|
|
if (route->current_section_num >= route->section_num) |
|
|
@@ -2184,8 +2208,8 @@ void PLSR_SetExtEvent(PLSR_RouteConfig_t* route) |
|
|
|
PLSR_WaitCondition_t* wait_cond = ¤t_section->wait_condition; |
|
|
|
|
|
|
|
// 设置外部事件标志 |
|
|
|
wait_cond->ext_event_flag = g_plsr_ext_event_flag; |
|
|
|
g_plsr_ext_event_flag = 0; // 清除全局标志 |
|
|
|
wait_cond->ext_event_flag = PlsrExtEventFlag; |
|
|
|
PlsrExtEventFlag = 0; // 清除全局标志 |
|
|
|
} |
|
|
|
|
|
|
|
void PLSR_ClearExtEvent(PLSR_RouteConfig_t* route) |
|
|
@@ -2256,7 +2280,7 @@ void PLSR_SectionSwitchTask(void *p_arg) |
|
|
|
|
|
|
|
if (err == OS_ERR_NONE) { |
|
|
|
/* 信号量获取成功,执行段切换逻辑 */ |
|
|
|
PLSR_TaskSectionSwitch(&g_plsr_route); |
|
|
|
PLSR_TaskSectionSwitch(&PlsrRoute); |
|
|
|
} |
|
|
|
} |
|
|
|
} |