|
|
@@ -671,23 +671,23 @@ void PLSR_PWM_Start(void) |
|
|
|
switch(g_plsr_route.output_port) |
|
|
|
{ |
|
|
|
case 0: // TIM10 |
|
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, htim10.Init.Period / 2); // 设置占空比为50% |
|
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, htim10.Init.Period / 2); // 设置占空比为50% |
|
|
|
break; |
|
|
|
|
|
|
|
case 1: // TIM11 |
|
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, htim10.Init.Period / 2); |
|
|
|
__HAL_TIM_SET_COMPARE(&htim11, TIM_CHANNEL_1, htim11.Init.Period / 2); // 设置占空比为50% |
|
|
|
break; |
|
|
|
|
|
|
|
case 2: // TIM13 |
|
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, htim10.Init.Period / 2); |
|
|
|
__HAL_TIM_SET_COMPARE(&htim13, TIM_CHANNEL_1, htim13.Init.Period / 2); // 设置占空比为50% |
|
|
|
break; |
|
|
|
|
|
|
|
case 3: // TIM14 |
|
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, htim10.Init.Period / 2); |
|
|
|
__HAL_TIM_SET_COMPARE(&htim14, TIM_CHANNEL_1, htim14.Init.Period / 2); // 设置占空比为50% |
|
|
|
break; |
|
|
|
|
|
|
|
default: // 默认使用TIM10 |
|
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, htim10.Init.Period / 2); |
|
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, htim10.Init.Period / 2); // 设置占空比为50% |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
@@ -710,60 +710,60 @@ void PLSR_Section_PWM_Stop(void) |
|
|
|
switch(g_plsr_route.output_port) |
|
|
|
{ |
|
|
|
case 0: // TIM10 |
|
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, 0); // |
|
|
|
//HAL_TIM_PWM_Stop(&htim10, TIM_CHANNEL_1); |
|
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, 0); // 停止PWM输出 |
|
|
|
//HAL_TIM_PWM_Stop(&htim10, TIM_CHANNEL_1); |
|
|
|
break; |
|
|
|
|
|
|
|
case 1: // TIM11 |
|
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, 0); // |
|
|
|
__HAL_TIM_SET_COMPARE(&htim11, TIM_CHANNEL_1, 0); // 停止PWM输出 |
|
|
|
//HAL_TIM_PWM_Stop(&htim11, TIM_CHANNEL_1); |
|
|
|
break; |
|
|
|
|
|
|
|
case 2: // TIM13 |
|
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, 0); // |
|
|
|
__HAL_TIM_SET_COMPARE(&htim13, TIM_CHANNEL_1, 0); // 停止PWM输出 |
|
|
|
//HAL_TIM_PWM_Stop(&htim13, TIM_CHANNEL_1); |
|
|
|
break; |
|
|
|
|
|
|
|
case 3: // TIM14 |
|
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, 0); // |
|
|
|
__HAL_TIM_SET_COMPARE(&htim14, TIM_CHANNEL_1, 0); // 停止PWM输出 |
|
|
|
//HAL_TIM_PWM_Stop(&htim14, TIM_CHANNEL_1); |
|
|
|
break; |
|
|
|
|
|
|
|
default: // 默认使用TIM10 |
|
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, 0); // |
|
|
|
//HAL_TIM_PWM_Stop(&htim10, TIM_CHANNEL_1); |
|
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, 0); // 停止PWM输出 |
|
|
|
//HAL_TIM_PWM_Stop(&htim10, TIM_CHANNEL_1); |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void PLSR_Route_PWM_Stop() |
|
|
|
{ |
|
|
|
// 根据output_port选择目标定时器并停止PWM输出 |
|
|
|
// 根据output_port选择目标定时器并停止PWM输出 |
|
|
|
switch(g_plsr_route.output_port) |
|
|
|
{ |
|
|
|
case 0: // TIM10 |
|
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, 0xFFFF); // |
|
|
|
//HAL_TIM_PWM_Stop(&htim10, TIM_CHANNEL_1); |
|
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, 0xFFFF); // 完全停止PWM输出 |
|
|
|
//HAL_TIM_PWM_Stop(&htim10, TIM_CHANNEL_1); |
|
|
|
break; |
|
|
|
|
|
|
|
case 1: // TIM11 |
|
|
|
//__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, 0xFFFF); // |
|
|
|
HAL_TIM_PWM_Stop(&htim11, TIM_CHANNEL_1); |
|
|
|
__HAL_TIM_SET_COMPARE(&htim11, TIM_CHANNEL_1, 0xFFFF); // 完全停止PWM输出 |
|
|
|
//HAL_TIM_PWM_Stop(&htim11, TIM_CHANNEL_1); |
|
|
|
break; |
|
|
|
|
|
|
|
case 2: // TIM13 |
|
|
|
//__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, 0xFFFF); // |
|
|
|
HAL_TIM_PWM_Stop(&htim13, TIM_CHANNEL_1); |
|
|
|
__HAL_TIM_SET_COMPARE(&htim13, TIM_CHANNEL_1, 0xFFFF); // 完全停止PWM输出 |
|
|
|
//HAL_TIM_PWM_Stop(&htim13, TIM_CHANNEL_1); |
|
|
|
break; |
|
|
|
|
|
|
|
case 3: // TIM14 |
|
|
|
//__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, 0xFFFF); // |
|
|
|
HAL_TIM_PWM_Stop(&htim14, TIM_CHANNEL_1); |
|
|
|
__HAL_TIM_SET_COMPARE(&htim14, TIM_CHANNEL_1, 0xFFFF); // 完全停止PWM输出 |
|
|
|
// HAL_TIM_PWM_Stop(&htim14, TIM_CHANNEL_1); |
|
|
|
break; |
|
|
|
|
|
|
|
default: // 默认使用TIM10 |
|
|
|
//__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, 0xFFFF); // |
|
|
|
HAL_TIM_PWM_Stop(&htim10, TIM_CHANNEL_1); |
|
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, 0xFFFF); // 完全停止PWM输出 |
|
|
|
//HAL_TIM_PWM_Stop(&htim10, TIM_CHANNEL_1); |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
@@ -1563,7 +1563,7 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) |
|
|
|
PLSR_Section_PWM_Stop(); |
|
|
|
// 精确累加当前段已发送的脉冲数 |
|
|
|
int32_t current_section_pulses = __HAL_TIM_GetAutoreload(&htim2); |
|
|
|
AllPluse += current_section_pulses + 1; |
|
|
|
AllPluse += current_section_pulses; |
|
|
|
g_plsr_route.pulse_count = AllPluse; |
|
|
|
PLSR_UpdateGlobalPulseCount(AllPluse); |
|
|
|
// 三部分状态机处理 |
|
|
@@ -1573,33 +1573,71 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) |
|
|
|
// 判断下一部分 |
|
|
|
if(g_plsr_route.const_pulse_count > 0) |
|
|
|
{ |
|
|
|
// 进入第二部分:匀速 |
|
|
|
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_last_freq = 0; // 清零g_last_freq,防止频率计算时的累加效果 |
|
|
|
|
|
|
|
if(g_plsr_route.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_last_freq = 0; // 清零g_last_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); |
|
|
|
__HAL_TIM_SET_COUNTER(&htim2, 1); |
|
|
|
PLSR_PWM_Start(); |
|
|
|
break; |
|
|
|
} |
|
|
|
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_last_freq = 0; // 清零g_last_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); |
|
|
|
__HAL_TIM_SET_COUNTER(&htim2, 1); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
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 - 1); |
|
|
|
__HAL_TIM_SET_COUNTER(&htim2, 0); |
|
|
|
PLSR_PWM_Start(); |
|
|
|
break; |
|
|
|
} |
|
|
|
else if(g_plsr_route.decel_pulse_count > 0) |
|
|
|
{ |
|
|
|
// 无匀速阶段,直接进入第三部分:减速 |
|
|
|
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; // 更新加减速初始频率 |
|
|
|
g_last_freq = 0; // 清零g_last_freq,防止频率计算时的累加效果 |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, g_plsr_route.decel_pulse_count - 1); |
|
|
|
__HAL_TIM_SET_COUNTER(&htim2, 0); |
|
|
|
PLSR_PWM_Start(); |
|
|
|
break; |
|
|
|
if(g_plsr_route.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; // 更新加减速初始频率 |
|
|
|
g_last_freq = 0; // 清零g_last_freq,防止频率计算时的累加效果 |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, g_plsr_route.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; |
|
|
|
g_plsr_route.initial_freq = g_plsr_route.current_freq; // 更新加减速初始频率 |
|
|
|
g_last_freq = 0; // 清零g_last_freq,防止频率计算时的累加效果 |
|
|
|
uint32_t arr_freq = 0; |
|
|
|
arr_freq = PLSR_Calculate_FreqByPosition(&g_plsr_route,0); |
|
|
|
PLSR_PWM_SetFrequency(arr_freq); |
|
|
|
PLSR_PWM_Start(); |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, g_plsr_route.decel_pulse_count); |
|
|
|
__HAL_TIM_SET_COUNTER(&htim2, 1); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
@@ -1613,14 +1651,30 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) |
|
|
|
// 进入第三部分:减速 |
|
|
|
if(g_plsr_route.decel_pulse_count > 0) |
|
|
|
{ |
|
|
|
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 - 1); |
|
|
|
__HAL_TIM_SET_COUNTER(&htim2, 0); |
|
|
|
PLSR_PWM_Start(); |
|
|
|
break; |
|
|
|
if(g_plsr_route.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); |
|
|
|
__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; |
|
|
|
uint32_t arr_freq = 0; |
|
|
|
arr_freq = PLSR_Calculate_FreqByPosition(&g_plsr_route,0); |
|
|
|
PLSR_PWM_SetFrequency(arr_freq); |
|
|
|
PLSR_PWM_Start(); |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, g_plsr_route.decel_pulse_count); |
|
|
|
__HAL_TIM_SET_COUNTER(&htim2, 1); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
@@ -1753,7 +1807,6 @@ void PLSR_Route_Init(PLSR_RouteConfig_t* route) |
|
|
|
g_last_freq = 0; // 清零上一次计算的频率 |
|
|
|
|
|
|
|
PLSR_TIM6_SetUpdateFreq(100); //初始化TIM6更新频率为1000us(1ms) |
|
|
|
//PLSR_Section_PWM_Stop(); // 确保PWM停止,避免意外输出 |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@@ -1784,8 +1837,33 @@ void PLSR_Route_Start(PLSR_RouteConfig_t* route) |
|
|
|
AllPluse = 0; |
|
|
|
g_first_flag = 0; |
|
|
|
|
|
|
|
// 重置上次记录的总脉冲数,用于实时累加计算 |
|
|
|
s_last_total_pulse = g_plsr_route.pulse_count; |
|
|
|
// 重置静态变量,确保每次启动效果一致 |
|
|
|
s_last_total_pulse = 0; // 重置上次记录的总脉冲数 |
|
|
|
s_last_direction = 0xFF; // 重置方向状态为初始无效值 |
|
|
|
s_pulse_count_direction = 1; // 重置脉冲计数方向为默认值 |
|
|
|
g_plsr_mod_flag = 0; // 重置修改标志 |
|
|
|
|
|
|
|
// 重置等待时间相关静态变量 |
|
|
|
s_wait_time_counter = 0; // 重置等待时间计数器 |
|
|
|
s_wait_time_target = 0; // 重置等待时间目标值 |
|
|
|
s_wait_time_flag = 0; // 重置等待时间到达标志 |
|
|
|
s_act_time_counter = 0; // 重置ACT时间计数器 |
|
|
|
s_act_time_target = 0; // 重置ACT时间目标值 |
|
|
|
s_act_time_flag = 0; // 重置ACT时间到达标志 |
|
|
|
|
|
|
|
// 重置所有PWM定时器状态,确保每次启动都是干净的状态 |
|
|
|
__HAL_TIM_SET_COUNTER(&htim10, 0); // 重置TIM10计数器 |
|
|
|
__HAL_TIM_SET_COUNTER(&htim11, 0); // 重置TIM11计数器 |
|
|
|
__HAL_TIM_SET_COUNTER(&htim13, 0); // 重置TIM13计数器 |
|
|
|
__HAL_TIM_SET_COUNTER(&htim14, 0); // 重置TIM14计数器 |
|
|
|
__HAL_TIM_SET_COUNTER(&htim2, 0); // 重置TIM2计数器 |
|
|
|
__HAL_TIM_SET_COUNTER(&htim6, 0); // 重置TIM6计数器 |
|
|
|
|
|
|
|
// 停止所有PWM输出,确保启动前处于停止状态 |
|
|
|
HAL_TIM_PWM_Stop(&htim10, TIM_CHANNEL_1); |
|
|
|
HAL_TIM_PWM_Stop(&htim11, TIM_CHANNEL_1); |
|
|
|
HAL_TIM_PWM_Stop(&htim13, TIM_CHANNEL_1); |
|
|
|
HAL_TIM_PWM_Stop(&htim14, TIM_CHANNEL_1); |
|
|
|
|
|
|
|
// 启动第一段 |
|
|
|
PLSR_Section_StartNewSection(route); |
|
|
@@ -1817,7 +1895,7 @@ void PLSR_Route_Stop(PLSR_RouteConfig_t* route) |
|
|
|
route->initial_freq = 0; // 重置加减速初始频率 |
|
|
|
|
|
|
|
// 重置计数器 |
|
|
|
__HAL_TIM_SET_COUNTER(&htim2, 0); |
|
|
|
__HAL_TIM_SET_COUNTER(&htim2, 1); |
|
|
|
|
|
|
|
route->freq_step = 0; |
|
|
|
|
|
|
@@ -2123,10 +2201,7 @@ void PLSR_Section_StartNewSection(PLSR_RouteConfig_t* route) |
|
|
|
|
|
|
|
// 统一使用三部分运动状态机设置TIM2参数 |
|
|
|
PLSR_SetupThreePartExecution(route); |
|
|
|
|
|
|
|
// 重置TIM2计数器 |
|
|
|
__HAL_TIM_SET_COUNTER(&htim2, 0); |
|
|
|
|
|
|
|
|
|
|
|
// 清除外部事件标志,确保新段开始时状态干净 |
|
|
|
PLSR_ClearExtEvent(route); |
|
|
|
|
|
|
@@ -2152,10 +2227,69 @@ void PLSR_SetupThreePartExecution(PLSR_RouteConfig_t* route) |
|
|
|
route->part1_state == PLSR_STATE_DECEL || |
|
|
|
route->part1_state == PLSR_STATE_CONST) |
|
|
|
{ |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, route->accel_pulse_count - 1); |
|
|
|
route->run_state = route->part1_state; |
|
|
|
route->target_freq = route->part1_target_freq; |
|
|
|
if(route->run_state == PLSR_STATE_ACCEL) |
|
|
|
{ |
|
|
|
uint32_t arr_freq = 0; |
|
|
|
arr_freq = PLSR_Calculate_FreqByPosition(&g_plsr_route,1); |
|
|
|
route->current_freq = arr_freq; |
|
|
|
// 加速完成检查 |
|
|
|
if (route->run_state == PLSR_STATE_ACCEL && route->current_freq >= route->target_freq) |
|
|
|
{ |
|
|
|
route->current_freq = route->target_freq; // 限制到目标频率 |
|
|
|
PLSR_PWM_SetFrequency(route->current_freq); |
|
|
|
} |
|
|
|
PLSR_PWM_SetFrequency(route->current_freq); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
uint32_t arr_freq = 0; |
|
|
|
arr_freq = PLSR_Calculate_FreqByPosition(&g_plsr_route,0); |
|
|
|
route->current_freq = arr_freq; |
|
|
|
// 减速完成检查 - 只有当减速到目标频率且目标频率大于0时才切换到匀速 |
|
|
|
if (route->run_state == PLSR_STATE_DECEL && route->current_freq <= route->target_freq && route->target_freq > 0) |
|
|
|
{ |
|
|
|
route->current_freq = route->target_freq; // 限制到目标频率 |
|
|
|
PLSR_PWM_SetFrequency(route->current_freq); |
|
|
|
} |
|
|
|
PLSR_PWM_SetFrequency(route->current_freq); |
|
|
|
} |
|
|
|
if(g_first_flag == 0 && route->current_freq != 0) //在这里第一次启动PWM输出避免发默认脉冲 |
|
|
|
{ |
|
|
|
g_first_flag = 1; |
|
|
|
PLSR_PWM_Start(); |
|
|
|
switch (route->output_port) |
|
|
|
{ |
|
|
|
case 0: |
|
|
|
HAL_TIM_PWM_Start(&htim10, TIM_CHANNEL_1); |
|
|
|
// 触发更新事件 - 立即将新参数加载到影子寄存器 |
|
|
|
HAL_TIM_GenerateEvent(&htim10, TIM_EVENTSOURCE_UPDATE); |
|
|
|
break; |
|
|
|
case 1: |
|
|
|
HAL_TIM_PWM_Start(&htim11, TIM_CHANNEL_1); |
|
|
|
break; |
|
|
|
case 2: |
|
|
|
HAL_TIM_PWM_Start(&htim13, TIM_CHANNEL_1); |
|
|
|
break; |
|
|
|
case 3: |
|
|
|
HAL_TIM_PWM_Start(&htim14, TIM_CHANNEL_1); |
|
|
|
break; |
|
|
|
default: |
|
|
|
break; |
|
|
|
} |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, route->accel_pulse_count); |
|
|
|
__HAL_TIM_SET_COUNTER(&htim2,1); |
|
|
|
return; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
PLSR_PWM_Start(); |
|
|
|
} |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, route->accel_pulse_count); |
|
|
|
__HAL_TIM_SET_COUNTER(&htim2,1); |
|
|
|
return; // 设置完成,退出函数 |
|
|
|
|
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
@@ -2169,7 +2303,7 @@ void PLSR_SetupThreePartExecution(PLSR_RouteConfig_t* route) |
|
|
|
// 第二部分:匀速 |
|
|
|
if(route->const_pulse_count > 0) |
|
|
|
{ |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, route->const_pulse_count - 1); |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, route->const_pulse_count); |
|
|
|
route->run_state = route->part2_state; // 通常是PLSR_STATE_CONST |
|
|
|
route->target_freq = route->part2_target_freq; |
|
|
|
return; // 设置完成,退出函数 |
|
|
@@ -2186,7 +2320,7 @@ void PLSR_SetupThreePartExecution(PLSR_RouteConfig_t* route) |
|
|
|
// 第三部分:减速到0 |
|
|
|
if(route->decel_pulse_count > 0) |
|
|
|
{ |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, route->decel_pulse_count - 1); |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, route->decel_pulse_count); |
|
|
|
route->run_state = route->part3_state; // 通常是PLSR_STATE_DECEL |
|
|
|
route->target_freq = route->part3_target_freq; // 通常是0 |
|
|
|
return; // 设置完成,退出函数 |
|
|
@@ -2207,7 +2341,7 @@ void PLSR_SetupThreePartExecution(PLSR_RouteConfig_t* route) |
|
|
|
} |
|
|
|
|
|
|
|
// 所有部分完成或无效状态 |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, 1); // 设置最小值 |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, 0); // 设置最小值 |
|
|
|
route->run_state = PLSR_STATE_CONST; |
|
|
|
} |
|
|
|
|
|
|
@@ -2298,22 +2432,30 @@ uint32_t PLSR_Calculate_FreqByPosition(PLSR_RouteConfig_t* route, uint8_t is_acc |
|
|
|
PLSR_SectionConfig_t* current_section = &route->section[route->current_section_num - 1]; |
|
|
|
|
|
|
|
// 获取当前脉冲位置 |
|
|
|
uint32_t current_tim2_count = __HAL_TIM_GET_COUNTER(&htim2); |
|
|
|
uint32_t executed_pulses = 0; |
|
|
|
uint32_t next_executed_pulses = 0; |
|
|
|
int32_t current_tim2_count = __HAL_TIM_GET_COUNTER(&htim2); |
|
|
|
int32_t executed_pulses = 0; |
|
|
|
int32_t next_executed_pulses = 0; |
|
|
|
// 计算当前部分已经执行的脉冲数 |
|
|
|
if(is_accel) |
|
|
|
if(is_accel == 0) |
|
|
|
{ |
|
|
|
executed_pulses = current_tim2_count; |
|
|
|
next_executed_pulses = current_tim2_count + 1; |
|
|
|
// 减速过程:使用全局脉冲计数器 |
|
|
|
if((current_tim2_count - 1) < 0) |
|
|
|
{ |
|
|
|
executed_pulses = 0; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
executed_pulses = current_tim2_count - 1; |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
// 减速过程:使用全局脉冲计数器 |
|
|
|
executed_pulses = current_tim2_count; |
|
|
|
next_executed_pulses = executed_pulses + 1; |
|
|
|
} |
|
|
|
|
|
|
|
//executed_pulses = current_tim2_count; |
|
|
|
//executed_pulses = ((current_tim2_count - 1) < 0) ? 0 : (current_tim2_count - 1); |
|
|
|
next_executed_pulses = executed_pulses + 1; |
|
|
|
|
|
|
|
// 检查是否需要重新计算:脉冲步数、加减速状态或段号发生变化时才重新计算 |
|
|
|
if (executed_pulses == s_last_executed_pulses && |
|
|
|
is_accel == s_last_is_accel && |
|
|
@@ -2601,12 +2743,40 @@ void PLSR_TaskSectionSwitch(PLSR_RouteConfig_t* route) |
|
|
|
if (PLSR_Wait_CheckExtEvent(route)) |
|
|
|
{ |
|
|
|
AllPluse += __HAL_TIM_GET_COUNTER(&htim2); // 累加当前段已发送的脉冲数 |
|
|
|
/* 外部事件满足,切换到下一段 */ |
|
|
|
PLSR_Section_SwitchNext(route, 0); // 外部事件触发,传入0 |
|
|
|
/* 启动新段,设置新的脉冲参数和频率 */ |
|
|
|
PLSR_Section_StartNewSection(route); |
|
|
|
/* 启动PWM输出 */ |
|
|
|
PLSR_PWM_Start(); |
|
|
|
|
|
|
|
/* 检查当前段是否为最后一段 */ |
|
|
|
if (route->current_section_num >= route->section_num) |
|
|
|
{ |
|
|
|
/* 当前段为最后一段,立即从当前频率减速到0并停止路径 */ |
|
|
|
route->target_freq = 0; // 设置目标频率为0 |
|
|
|
route->run_state = PLSR_STATE_DECEL; // 设置为减速状态 |
|
|
|
route->initial_freq = route->current_freq; |
|
|
|
g_last_freq = 0; |
|
|
|
__HAL_TIM_SetAutoreload(&htim2, 0xffffff); |
|
|
|
__HAL_TIM_SET_COUNTER(&htim2, 1); |
|
|
|
|
|
|
|
/* 如果当前频率已经为0,直接停止 */ |
|
|
|
if (route->current_freq == 0) |
|
|
|
{ |
|
|
|
route->route_state = PLSR_ROUTE_COMPLETED; |
|
|
|
PLSR_Route_Stop(route); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
/* 启动减速到0的过程 */ |
|
|
|
PLSR_PWM_Start(); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
/* 非最后一段,正常进行段切换 */ |
|
|
|
PLSR_Section_SwitchNext(route, 0); // 外部事件触发,传入0 |
|
|
|
/* 启动新段,设置新的脉冲参数和频率 */ |
|
|
|
PLSR_Section_StartNewSection(route); |
|
|
|
/* 启动PWM输出 */ |
|
|
|
PLSR_PWM_Start(); |
|
|
|
} |
|
|
|
|
|
|
|
PLSR_ClearGpioTriggerFlag(); // 清除GPIO触发标志 |
|
|
|
} |
|
|
|
return; // 等待外部事件时不需要继续处理 |
|
|
|