|
|
@@ -31,6 +31,7 @@ uint32_t g_last_freq = 0; // 上一次计算的频率,用于PLSR_Calc |
|
|
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),用于检测是否有频率变化 |
|
|
uint32_t g_plsr_current_target_freq = 0; // 当前段目标频率频率(Hz),用于检测是否有频率变化 |
|
|
static uint8_t s_last_direction = 0xFF; // 初始值设为无效值,确保第一次总是认为有方向变化 |
|
|
static uint8_t s_last_direction = 0xFF; // 初始值设为无效值,确保第一次总是认为有方向变化 |
|
|
|
|
|
uint8_t g_plsr_mod_flag = 0xff; |
|
|
|
|
|
|
|
|
// ==================== PLSR内部变量 ==================== |
|
|
// ==================== PLSR内部变量 ==================== |
|
|
static uint32_t s_tim6_update_freq_us = 1000; // TIM6更新频率(微秒) |
|
|
static uint32_t s_tim6_update_freq_us = 1000; // TIM6更新频率(微秒) |
|
|
@@ -398,8 +399,8 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle) |
|
|
__HAL_RCC_TIM6_CLK_ENABLE(); |
|
|
__HAL_RCC_TIM6_CLK_ENABLE(); |
|
|
|
|
|
|
|
|
// 配置TIM6中断 |
|
|
// 配置TIM6中断 |
|
|
HAL_NVIC_SetPriority(TIM6_DAC_IRQn, 5, 0); // 设置中断优先级为最高(0,0) |
|
|
|
|
|
HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn); // 使能TIM6中断 |
|
|
|
|
|
|
|
|
HAL_NVIC_SetPriority(TIM6_DAC_IRQn, 5, 5); |
|
|
|
|
|
HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn); |
|
|
/* USER CODE BEGIN TIM6_MspInit 1 */ |
|
|
/* USER CODE BEGIN TIM6_MspInit 1 */ |
|
|
|
|
|
|
|
|
/* USER CODE END TIM6_MspInit 1 */ |
|
|
/* USER CODE END TIM6_MspInit 1 */ |
|
|
@@ -412,9 +413,9 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle) |
|
|
// 使能TIM10时钟 |
|
|
// 使能TIM10时钟 |
|
|
__HAL_RCC_TIM10_CLK_ENABLE(); |
|
|
__HAL_RCC_TIM10_CLK_ENABLE(); |
|
|
|
|
|
|
|
|
// 配置TIM10中断 |
|
|
|
|
|
HAL_NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 5, 0); // 设置中断优先级为5 |
|
|
|
|
|
HAL_NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn); // 使能TIM10中断 |
|
|
|
|
|
|
|
|
// // 配置TIM10中断 |
|
|
|
|
|
// HAL_NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 5, 0); // 设置中断优先级为5 |
|
|
|
|
|
// HAL_NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn); // 使能TIM10中断 |
|
|
/* USER CODE BEGIN TIM10_MspInit 1 */ |
|
|
/* USER CODE BEGIN TIM10_MspInit 1 */ |
|
|
|
|
|
|
|
|
/* USER CODE END TIM10_MspInit 1 */ |
|
|
/* USER CODE END TIM10_MspInit 1 */ |
|
|
@@ -753,8 +754,15 @@ static void PLSR_CalculateTimerParams(uint32_t frequency, uint16_t* prescaler, u |
|
|
{ |
|
|
{ |
|
|
if (frequency < PLSR_PWM_FREQ_MIN) frequency = PLSR_PWM_FREQ_MIN; |
|
|
if (frequency < PLSR_PWM_FREQ_MIN) frequency = PLSR_PWM_FREQ_MIN; |
|
|
if (frequency > PLSR_PWM_FREQ_MAX) frequency = PLSR_PWM_FREQ_MAX; |
|
|
if (frequency > PLSR_PWM_FREQ_MAX) frequency = PLSR_PWM_FREQ_MAX; |
|
|
|
|
|
|
|
|
const uint32_t timer_clock = 168000000UL; // 定时器时钟 |
|
|
|
|
|
|
|
|
uint32_t timer_clock = 0; |
|
|
|
|
|
if(g_plsr_route.output_port == 0 || g_plsr_route.output_port == 1) |
|
|
|
|
|
{ |
|
|
|
|
|
timer_clock = 168000000UL; // TIM10/TIM11时钟 (APB2 84MHz) |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
timer_clock = 84000000UL; // TIM13/TIM14时钟 (APB1 42MHz) |
|
|
|
|
|
} |
|
|
uint32_t divider = (timer_clock + frequency / 2) / frequency; // 四舍五入 |
|
|
uint32_t divider = (timer_clock + frequency / 2) / frequency; // 四舍五入 |
|
|
|
|
|
|
|
|
if (divider > 0xFFFFFFFF) divider = 0xFFFFFFFF; // 防止溢出 |
|
|
if (divider > 0xFFFFFFFF) divider = 0xFFFFFFFF; // 防止溢出 |
|
|
@@ -1394,7 +1402,6 @@ void Calculate_PluseNum(PLSR_RouteConfig_t *route) |
|
|
route->run_state = PLSR_STATE_CONST; |
|
|
route->run_state = PLSR_STATE_CONST; |
|
|
route->target_freq = v0; |
|
|
route->target_freq = v0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@@ -1447,9 +1454,6 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) |
|
|
switch(g_plsr_route.current_part) |
|
|
switch(g_plsr_route.current_part) |
|
|
{ |
|
|
{ |
|
|
case PLSR_PART_1: |
|
|
case PLSR_PART_1: |
|
|
|
|
|
|
|
|
g_plsr_route.accel_pulse_count = 0; // 清零第一部分脉冲计数 |
|
|
|
|
|
|
|
|
|
|
|
// 判断下一部分 |
|
|
// 判断下一部分 |
|
|
if(g_plsr_route.const_pulse_count > 0) |
|
|
if(g_plsr_route.const_pulse_count > 0) |
|
|
{ |
|
|
{ |
|
|
@@ -1476,7 +1480,6 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) |
|
|
g_plsr_route.initial_freq = g_plsr_route.current_freq; // 更新加减速初始频率 |
|
|
g_plsr_route.initial_freq = g_plsr_route.current_freq; // 更新加减速初始频率 |
|
|
g_last_freq = 0; // 清零g_last_freq,防止频率计算时的累加效果 |
|
|
g_last_freq = 0; // 清零g_last_freq,防止频率计算时的累加效果 |
|
|
__HAL_TIM_SetAutoreload(&htim2, g_plsr_route.decel_pulse_count); |
|
|
__HAL_TIM_SetAutoreload(&htim2, g_plsr_route.decel_pulse_count); |
|
|
|
|
|
|
|
|
__HAL_TIM_SET_COUNTER(&htim2, 0); |
|
|
__HAL_TIM_SET_COUNTER(&htim2, 0); |
|
|
PLSR_PWM_Start(); |
|
|
PLSR_PWM_Start(); |
|
|
break; |
|
|
break; |
|
|
@@ -1490,9 +1493,6 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
case PLSR_PART_2: |
|
|
case PLSR_PART_2: |
|
|
|
|
|
|
|
|
g_plsr_route.const_pulse_count = 0; // 清零第二部分脉冲计数 |
|
|
|
|
|
|
|
|
|
|
|
// 进入第三部分:减速 |
|
|
// 进入第三部分:减速 |
|
|
if(g_plsr_route.decel_pulse_count > 0) |
|
|
if(g_plsr_route.decel_pulse_count > 0) |
|
|
{ |
|
|
{ |
|
|
@@ -1545,6 +1545,7 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) |
|
|
|
|
|
|
|
|
if(g_plsr_route.section[g_plsr_route.current_section_num-1].target_freq != g_plsr_current_target_freq) |
|
|
if(g_plsr_route.section[g_plsr_route.current_section_num-1].target_freq != g_plsr_current_target_freq) |
|
|
{ |
|
|
{ |
|
|
|
|
|
g_plsr_mod_flag = 1; // 标记当前段目标频率被修改 |
|
|
PLSR_PWM_Stop(); |
|
|
PLSR_PWM_Stop(); |
|
|
AllPluse += current_tim2_count; // 累加当前段已发送的脉冲数 |
|
|
AllPluse += current_tim2_count; // 累加当前段已发送的脉冲数 |
|
|
PLSR_Section_StartNewSection(&g_plsr_route); ///<重新启动当前段 |
|
|
PLSR_Section_StartNewSection(&g_plsr_route); ///<重新启动当前段 |
|
|
@@ -1829,15 +1830,60 @@ void PLSR_Section_StartNewSection(PLSR_RouteConfig_t* route) |
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
|
{ |
|
|
{ |
|
|
if(current_section->target_pulse > 0) |
|
|
|
|
|
|
|
|
if(g_plsr_mod_flag == 1) |
|
|
{ |
|
|
{ |
|
|
current_section->actual_pulse = (current_section->target_pulse - __HAL_TIM_GET_COUNTER(&htim2)); |
|
|
|
|
|
|
|
|
if(route->current_part == PLSR_PART_2) //匀速过程被修改 |
|
|
|
|
|
{ |
|
|
|
|
|
// 目标频率被修改,重新计算实际脉冲数 |
|
|
|
|
|
if(current_section->target_pulse > 0) |
|
|
|
|
|
{ |
|
|
|
|
|
current_section->actual_pulse = (current_section->target_pulse - route->accel_pulse_count - __HAL_TIM_GET_COUNTER(&htim2)); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
current_section->actual_pulse = ((-current_section->target_pulse) - route->accel_pulse_count - __HAL_TIM_GET_COUNTER(&htim2)); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
else if(route->current_part == PLSR_PART_1)//加速过程被修改 |
|
|
|
|
|
{ |
|
|
|
|
|
// 目标频率被修改,重新计算实际脉冲数 |
|
|
|
|
|
if(current_section->target_pulse > 0) |
|
|
|
|
|
{ |
|
|
|
|
|
current_section->actual_pulse = (current_section->target_pulse - __HAL_TIM_GET_COUNTER(&htim2)); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
current_section->actual_pulse = ((-current_section->target_pulse) - __HAL_TIM_GET_COUNTER(&htim2)); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
else if(route->current_part == PLSR_PART_3)//减速过程被修改 |
|
|
|
|
|
{ |
|
|
|
|
|
// 目标频率被修改,重新计算实际脉冲数 |
|
|
|
|
|
if(current_section->target_pulse > 0) |
|
|
|
|
|
{ |
|
|
|
|
|
current_section->actual_pulse = (current_section->target_pulse - |
|
|
|
|
|
route->accel_pulse_count - route->const_pulse_count - __HAL_TIM_GET_COUNTER(&htim2)); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
current_section->actual_pulse = ((-current_section->target_pulse) - |
|
|
|
|
|
route->accel_pulse_count - route->const_pulse_count - __HAL_TIM_GET_COUNTER(&htim2)); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
g_plsr_mod_flag = 0; // 清除修改标记 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
else |
|
|
else |
|
|
{ |
|
|
{ |
|
|
current_section->actual_pulse = ((-current_section->target_pulse) - __HAL_TIM_GET_COUNTER(&htim2)); |
|
|
|
|
|
|
|
|
if(current_section->target_pulse > 0) |
|
|
|
|
|
{ |
|
|
|
|
|
current_section->actual_pulse = (current_section->target_pulse - __HAL_TIM_GET_COUNTER(&htim2)); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
current_section->actual_pulse = ((-current_section->target_pulse) - __HAL_TIM_GET_COUNTER(&htim2)); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 设置方向端子 - 使用计算后的actual_pulse确定方向 |
|
|
// 设置方向端子 - 使用计算后的actual_pulse确定方向 |
|
|
@@ -2653,8 +2699,3 @@ void PLSR_SectionSwitchTask(void *p_arg) |
|
|
/* 如果信号量获取失败,继续等待下一次信号 */ |
|
|
/* 如果信号量获取失败,继续等待下一次信号 */ |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|