|
@@ -34,6 +34,7 @@ static uint8_t s_last_direction = 0xFF; // 初始值设为无效值,确保第 |
|
|
uint8_t g_plsr_mod_flag = 0xff; |
|
|
uint8_t g_plsr_mod_flag = 0xff; |
|
|
static uint8_t g_first_flag = 0; |
|
|
static uint8_t g_first_flag = 0; |
|
|
uint32_t total_se_pluse = 0; |
|
|
uint32_t total_se_pluse = 0; |
|
|
|
|
|
uint8_t stop_flag = 0; |
|
|
// ==================== PLSR内部变量 ==================== |
|
|
// ==================== PLSR内部变量 ==================== |
|
|
|
|
|
|
|
|
// ==================== PLSR内部变量 ==================== |
|
|
// ==================== PLSR内部变量 ==================== |
|
@@ -316,7 +317,7 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle) |
|
|
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
|
|
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
|
|
|
|
|
|
|
|
/* TIM2 interrupt Init */ |
|
|
/* TIM2 interrupt Init */ |
|
|
HAL_NVIC_SetPriority(TIM2_IRQn, 4, 4); |
|
|
|
|
|
|
|
|
HAL_NVIC_SetPriority(TIM2_IRQn, 2, 3); |
|
|
HAL_NVIC_EnableIRQ(TIM2_IRQn); |
|
|
HAL_NVIC_EnableIRQ(TIM2_IRQn); |
|
|
/* USER CODE BEGIN TIM2_MspInit 1 */ |
|
|
/* USER CODE BEGIN TIM2_MspInit 1 */ |
|
|
|
|
|
|
|
@@ -596,6 +597,7 @@ void PLSR_PWM_Start(void) |
|
|
{ |
|
|
{ |
|
|
if (g_plsr_route.route_state == PLSR_ROUTE_RUNNING) //<只有在路径运行状态下才可以进行pwm输出 |
|
|
if (g_plsr_route.route_state == PLSR_ROUTE_RUNNING) //<只有在路径运行状态下才可以进行pwm输出 |
|
|
{ |
|
|
{ |
|
|
|
|
|
stop_flag = 0; |
|
|
// 根据output_port选择目标定时器并启动PWM输出 |
|
|
// 根据output_port选择目标定时器并启动PWM输出 |
|
|
switch(g_plsr_route.output_port) |
|
|
switch(g_plsr_route.output_port) |
|
|
{ |
|
|
{ |
|
@@ -635,6 +637,7 @@ void PLSR_PWM_Start(void) |
|
|
*/ |
|
|
*/ |
|
|
void PLSR_Section_PWM_Stop(void) |
|
|
void PLSR_Section_PWM_Stop(void) |
|
|
{ |
|
|
{ |
|
|
|
|
|
stop_flag = 1; |
|
|
// 根据output_port选择目标定时器并停止PWM输出 |
|
|
// 根据output_port选择目标定时器并停止PWM输出 |
|
|
switch(g_plsr_route.output_port) |
|
|
switch(g_plsr_route.output_port) |
|
|
{ |
|
|
{ |
|
@@ -721,40 +724,45 @@ void PLSR_PWM_SetFrequency(uint32_t frequency) |
|
|
{ |
|
|
{ |
|
|
return; // period无效,直接返回,避免HardFault异常 |
|
|
return; // period无效,直接返回,避免HardFault异常 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 根据output_port选择目标定时器并更新参数 |
|
|
// 根据output_port选择目标定时器并更新参数 |
|
|
switch(g_plsr_route.output_port) |
|
|
switch(g_plsr_route.output_port) |
|
|
{ |
|
|
{ |
|
|
case 0: // TIM10 |
|
|
case 0: // TIM10 |
|
|
__HAL_TIM_SET_PRESCALER(&htim10, prescaler); |
|
|
__HAL_TIM_SET_PRESCALER(&htim10, prescaler); |
|
|
__HAL_TIM_SET_AUTORELOAD(&htim10, period); |
|
|
__HAL_TIM_SET_AUTORELOAD(&htim10, period); |
|
|
|
|
|
if(stop_flag == 0) |
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, period / 2); |
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, period / 2); |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
case 1: // TIM11 |
|
|
case 1: // TIM11 |
|
|
__HAL_TIM_SET_PRESCALER(&htim11, prescaler); |
|
|
__HAL_TIM_SET_PRESCALER(&htim11, prescaler); |
|
|
__HAL_TIM_SET_AUTORELOAD(&htim11, period); |
|
|
__HAL_TIM_SET_AUTORELOAD(&htim11, period); |
|
|
|
|
|
if(stop_flag == 0) |
|
|
__HAL_TIM_SET_COMPARE(&htim11, TIM_CHANNEL_1, period / 2); |
|
|
__HAL_TIM_SET_COMPARE(&htim11, TIM_CHANNEL_1, period / 2); |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
case 2: // TIM13 |
|
|
case 2: // TIM13 |
|
|
__HAL_TIM_SET_PRESCALER(&htim13, prescaler); |
|
|
__HAL_TIM_SET_PRESCALER(&htim13, prescaler); |
|
|
__HAL_TIM_SET_AUTORELOAD(&htim13, period); |
|
|
__HAL_TIM_SET_AUTORELOAD(&htim13, period); |
|
|
|
|
|
if(stop_flag == 0) |
|
|
__HAL_TIM_SET_COMPARE(&htim13, TIM_CHANNEL_1, period / 2); |
|
|
__HAL_TIM_SET_COMPARE(&htim13, TIM_CHANNEL_1, period / 2); |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
case 3: // TIM14 |
|
|
case 3: // TIM14 |
|
|
__HAL_TIM_SET_PRESCALER(&htim14, prescaler); |
|
|
__HAL_TIM_SET_PRESCALER(&htim14, prescaler); |
|
|
__HAL_TIM_SET_AUTORELOAD(&htim14, period); |
|
|
__HAL_TIM_SET_AUTORELOAD(&htim14, period); |
|
|
|
|
|
if(stop_flag == 0) |
|
|
__HAL_TIM_SET_COMPARE(&htim14, TIM_CHANNEL_1, period / 2); |
|
|
__HAL_TIM_SET_COMPARE(&htim14, TIM_CHANNEL_1, period / 2); |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
default: // 默认使用TIM10 |
|
|
default: // 默认使用TIM10 |
|
|
__HAL_TIM_SET_PRESCALER(&htim10, prescaler); |
|
|
__HAL_TIM_SET_PRESCALER(&htim10, prescaler); |
|
|
__HAL_TIM_SET_AUTORELOAD(&htim10, period); |
|
|
__HAL_TIM_SET_AUTORELOAD(&htim10, period); |
|
|
|
|
|
if(stop_flag == 0) |
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, period / 2); |
|
|
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, period / 2); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// ==================== PLSR TIM6频率配置函数实现 ==================== |
|
|
// ==================== PLSR TIM6频率配置函数实现 ==================== |
|
@@ -913,7 +921,7 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) |
|
|
int32_t current_section_pulses = __HAL_TIM_GetAutoreload(&htim2); |
|
|
int32_t current_section_pulses = __HAL_TIM_GetAutoreload(&htim2); |
|
|
if(g_plsr_mod_flag == 1) |
|
|
if(g_plsr_mod_flag == 1) |
|
|
{ |
|
|
{ |
|
|
current_section_pulses; |
|
|
|
|
|
|
|
|
current_section_pulses -= 1; |
|
|
g_plsr_mod_flag = 0; |
|
|
g_plsr_mod_flag = 0; |
|
|
} |
|
|
} |
|
|
AllPluse += current_section_pulses; |
|
|
AllPluse += current_section_pulses; |
|
@@ -935,7 +943,10 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) |
|
|
g_plsr_route.initial_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; |
|
|
g_plsr_route.current_freq = g_plsr_route.target_freq; |
|
|
|
|
|
|
|
|
PLSR_PWM_SetFrequency(g_plsr_route.current_freq); |
|
|
PLSR_PWM_SetFrequency(g_plsr_route.current_freq); |
|
|
|
|
|
// if(g_plsr_route.current_freq >= 100000) |
|
|
|
|
|
// HAL_TIM_GenerateEvent(&htim10, TIM_EVENTSOURCE_UPDATE); |
|
|
__HAL_TIM_SetAutoreload(&htim2, g_plsr_route.const_pulse_count); |
|
|
__HAL_TIM_SetAutoreload(&htim2, g_plsr_route.const_pulse_count); |
|
|
__HAL_TIM_SET_COUNTER(&htim2, 1); |
|
|
__HAL_TIM_SET_COUNTER(&htim2, 1); |
|
|
PLSR_PWM_Start(); |
|
|
PLSR_PWM_Start(); |
|
@@ -1048,11 +1059,10 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) |
|
|
|
|
|
|
|
|
// 计算当前段已发送的总脉冲数 |
|
|
// 计算当前段已发送的总脉冲数 |
|
|
int32_t current_pulse_count = AllPluse + current_tim2_count; |
|
|
int32_t current_pulse_count = AllPluse + current_tim2_count; |
|
|
|
|
|
|
|
|
|
|
|
g_plsr_route.pulse_count = current_pulse_count; |
|
|
// 实时累加脉冲增量到全局计数器(根据方向逻辑决定递增或递减) |
|
|
// 实时累加脉冲增量到全局计数器(根据方向逻辑决定递增或递减) |
|
|
PLSR_UpdateGlobalPulseCount(current_pulse_count); |
|
|
PLSR_UpdateGlobalPulseCount(current_pulse_count); |
|
|
// 更新路由结构体的脉冲计数(使用原子操作保证一致性) |
|
|
// 更新路由结构体的脉冲计数(使用原子操作保证一致性) |
|
|
g_plsr_route.pulse_count = current_pulse_count; |
|
|
|
|
|
|
|
|
|
|
|
// 计算当前段的Modbus地址基址 |
|
|
// 计算当前段的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 + ((g_plsr_route.current_section_num-1) * MODBUS_ADDR_SEGMENT_OFFSET); |
|
@@ -1373,8 +1383,7 @@ void PLSR_Section_StartNewSection(PLSR_RouteConfig_t* route) |
|
|
current_section->actual_pulse = current_section->target_pulse - g_plsr_location; |
|
|
current_section->actual_pulse = current_section->target_pulse - g_plsr_location; |
|
|
if(g_plsr_mod_flag == 1) |
|
|
if(g_plsr_mod_flag == 1) |
|
|
{ |
|
|
{ |
|
|
current_section->actual_pulse; |
|
|
|
|
|
//g_plsr_mod_flag = 0; // 清除修改标记 |
|
|
|
|
|
|
|
|
current_section->actual_pulse += 1; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
@@ -1386,7 +1395,7 @@ void PLSR_Section_StartNewSection(PLSR_RouteConfig_t* route) |
|
|
current_section->actual_pulse = total_se_pluse + current_section->target_pulse - g_plsr_total_pulse_count; |
|
|
current_section->actual_pulse = total_se_pluse + current_section->target_pulse - g_plsr_total_pulse_count; |
|
|
else |
|
|
else |
|
|
current_section->actual_pulse = total_se_pluse + (-current_section->target_pulse) - g_plsr_total_pulse_count; |
|
|
current_section->actual_pulse = total_se_pluse + (-current_section->target_pulse) - g_plsr_total_pulse_count; |
|
|
current_section->actual_pulse; |
|
|
|
|
|
|
|
|
current_section->actual_pulse += 1; |
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
|
{ |
|
|
{ |
|
@@ -1564,6 +1573,8 @@ void PLSR_SetupThreePartExecution(PLSR_RouteConfig_t* route) |
|
|
PLSR_PWM_SetFrequency(route->current_freq); |
|
|
PLSR_PWM_SetFrequency(route->current_freq); |
|
|
} |
|
|
} |
|
|
PLSR_PWM_SetFrequency(route->current_freq); |
|
|
PLSR_PWM_SetFrequency(route->current_freq); |
|
|
|
|
|
if(g_plsr_route.current_freq >= 15000) |
|
|
|
|
|
HAL_TIM_GenerateEvent(&htim10, TIM_EVENTSOURCE_UPDATE); |
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
|
{ |
|
|
{ |
|
@@ -1577,6 +1588,8 @@ void PLSR_SetupThreePartExecution(PLSR_RouteConfig_t* route) |
|
|
PLSR_PWM_SetFrequency(route->current_freq); |
|
|
PLSR_PWM_SetFrequency(route->current_freq); |
|
|
} |
|
|
} |
|
|
PLSR_PWM_SetFrequency(route->current_freq); |
|
|
PLSR_PWM_SetFrequency(route->current_freq); |
|
|
|
|
|
if(g_plsr_route.current_freq >= 15000) |
|
|
|
|
|
HAL_TIM_GenerateEvent(&htim10, TIM_EVENTSOURCE_UPDATE); |
|
|
} |
|
|
} |
|
|
if(g_first_flag == 0 && route->current_freq != 0) //在这里第一次启动PWM输出避免发默认脉冲 |
|
|
if(g_first_flag == 0 && route->current_freq != 0) //在这里第一次启动PWM输出避免发默认脉冲 |
|
|
{ |
|
|
{ |
|
@@ -1875,6 +1888,7 @@ void PLSR_Accel_Process(PLSR_RouteConfig_t* route) |
|
|
if (calculated_freq >= route->target_freq) |
|
|
if (calculated_freq >= route->target_freq) |
|
|
{ |
|
|
{ |
|
|
calculated_freq = route->target_freq; // 限制到目标频率 |
|
|
calculated_freq = route->target_freq; // 限制到目标频率 |
|
|
|
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 只有当计算出的频率与当前频率不同时才更新 |
|
|
// 只有当计算出的频率与当前频率不同时才更新 |
|
@@ -1882,6 +1896,7 @@ void PLSR_Accel_Process(PLSR_RouteConfig_t* route) |
|
|
{ |
|
|
{ |
|
|
route->current_freq = calculated_freq; |
|
|
route->current_freq = calculated_freq; |
|
|
PLSR_PWM_SetFrequency(route->current_freq); |
|
|
PLSR_PWM_SetFrequency(route->current_freq); |
|
|
|
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if(g_first_flag == 0 && route->current_freq != 0) //在这里第一次启动PWM输出避免发默认脉冲 |
|
|
if(g_first_flag == 0 && route->current_freq != 0) //在这里第一次启动PWM输出避免发默认脉冲 |
|
@@ -1906,7 +1921,8 @@ void PLSR_Accel_Process(PLSR_RouteConfig_t* route) |
|
|
break; |
|
|
break; |
|
|
default: |
|
|
default: |
|
|
break; |
|
|
break; |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
return; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
// ==================== 减速处理 ==================== |
|
|
// ==================== 减速处理 ==================== |
|
@@ -1917,7 +1933,8 @@ void PLSR_Accel_Process(PLSR_RouteConfig_t* route) |
|
|
uint32_t calculated_freq = PLSR_Calculate_FreqByPosition(route, 0); |
|
|
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; // 限制到目标频率 |
|
|
calculated_freq = route->target_freq; // 限制到目标频率 |
|
|
|
|
|
|
|
|
// 如果目标频率为0,说明减速到停止 |
|
|
// 如果目标频率为0,说明减速到停止 |
|
@@ -1929,13 +1946,15 @@ void PLSR_Accel_Process(PLSR_RouteConfig_t* route) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 只有当计算出的频率与当前频率不同时才更新PWM频率 |
|
|
// 只有当计算出的频率与当前频率不同时才更新PWM频率 |
|
|
if (calculated_freq != route->current_freq) { |
|
|
|
|
|
|
|
|
if (calculated_freq != route->current_freq) |
|
|
|
|
|
{ |
|
|
route->current_freq = calculated_freq; |
|
|
route->current_freq = calculated_freq; |
|
|
|
|
|
|
|
|
if (route->current_freq > 0) |
|
|
if (route->current_freq > 0) |
|
|
{ |
|
|
{ |
|
|
PLSR_PWM_SetFrequency(route->current_freq); |
|
|
PLSR_PWM_SetFrequency(route->current_freq); |
|
|
} |
|
|
} |
|
|
|
|
|
return; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|