|
|
@@ -743,7 +743,7 @@ static void PLSR_CalculateTimerParams(uint32_t frequency, uint16_t* prescaler, u |
|
|
|
|
|
|
|
uint32_t psc; |
|
|
|
uint32_t arr; |
|
|
|
uint32_t f = (uint32_t)(frequency + 0.5f); |
|
|
|
uint32_t f = frequency; |
|
|
|
|
|
|
|
// PLSR_Calc_TimerParams((uint32_t)(f_hz + 0.5f), &psc, &arr); |
|
|
|
uint32_t timer_clock = 168000000; |
|
|
@@ -1594,6 +1594,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) |
|
|
|
{ |
|
|
|
PLSR_PWM_Stop(); |
|
|
|
AllPluse += current_tim2_count; // 累加当前段已发送的脉冲数 |
|
|
|
PLSR_Section_StartNewSection(&g_plsr_route); ///<重新启动当前段 |
|
|
|
} |
|
|
@@ -2017,6 +2018,7 @@ void PLSR_Section_StartNewSection(PLSR_RouteConfig_t* route) |
|
|
|
if(route->current_freq > 0) |
|
|
|
{ |
|
|
|
PLSR_PWM_SetFrequency(route->current_freq); |
|
|
|
PLSR_PWM_Start(); |
|
|
|
} |
|
|
|
|
|
|
|
// 清除外部事件标志,确保新段开始时状态干净 |
|
|
@@ -2202,12 +2204,8 @@ uint32_t PLSR_Calculate_FreqByPosition(PLSR_RouteConfig_t* route, uint8_t is_acc |
|
|
|
else |
|
|
|
{ |
|
|
|
// 减速过程:使用全局脉冲计数器 |
|
|
|
executed_pulses = g_plsr_route.decel_pulse_count - current_tim2_count; |
|
|
|
next_executed_pulses = executed_pulses-1; |
|
|
|
if(next_executed_pulses < 0) |
|
|
|
{ |
|
|
|
next_executed_pulses = 0; |
|
|
|
} |
|
|
|
executed_pulses = current_tim2_count; |
|
|
|
next_executed_pulses = executed_pulses + 1; |
|
|
|
} |
|
|
|
|
|
|
|
// 检查是否需要重新计算:脉冲步数、加减速状态或段号发生变化时才重新计算 |
|
|
@@ -2219,7 +2217,6 @@ uint32_t PLSR_Calculate_FreqByPosition(PLSR_RouteConfig_t* route, uint8_t is_acc |
|
|
|
return s_last_calculated_freq; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 使用速度位移公式 vt^2 = v0^2 + 2ax 计算当前频率,其中v0使用initial_freq作为初始速度 |
|
|
|
uint32_t v0 = route->initial_freq; // 使用initial_freq作为初始速度v0 |
|
|
|
uint64_t a = 0; |
|
|
@@ -2253,13 +2250,18 @@ uint32_t PLSR_Calculate_FreqByPosition(PLSR_RouteConfig_t* route, uint8_t is_acc |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
uint64_t freq_squared_start = 2 * a * executed_pulses; |
|
|
|
freq_start = integer_sqrt_64(freq_squared_start); |
|
|
|
|
|
|
|
uint64_t v0_squared = (uint64_t)v0 * v0; |
|
|
|
if (v0_squared >= 2 * a * executed_pulses) |
|
|
|
{ |
|
|
|
uint64_t freq_squared_start = v0_squared - 2 * a * executed_pulses; |
|
|
|
freq_start = integer_sqrt_64(freq_squared_start); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
freq_start = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
// 计算当前脉冲结束时的频率(executed_pulses + 1位置) |
|
|
|
if (is_accel) |
|
|
|
{ |
|
|
@@ -2270,8 +2272,15 @@ uint32_t PLSR_Calculate_FreqByPosition(PLSR_RouteConfig_t* route, uint8_t is_acc |
|
|
|
else |
|
|
|
{ |
|
|
|
uint64_t v0_squared = (uint64_t)v0 * v0; |
|
|
|
uint64_t freq_squared_end = 2 * a * next_executed_pulses; |
|
|
|
freq_end = integer_sqrt_64(freq_squared_end); |
|
|
|
if (v0_squared >= 2 * a * next_executed_pulses) |
|
|
|
{ |
|
|
|
uint64_t freq_squared_end = v0_squared - 2 * a * next_executed_pulses; |
|
|
|
freq_end = integer_sqrt_64(freq_squared_end); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
freq_end = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 计算当前脉冲的平均频率 |
|
|
@@ -2279,10 +2288,6 @@ uint32_t PLSR_Calculate_FreqByPosition(PLSR_RouteConfig_t* route, uint8_t is_acc |
|
|
|
|
|
|
|
// 更新全局变量(保存当前脉冲结束时的频率,供下次使用) |
|
|
|
g_last_freq = freq_end; |
|
|
|
// if(executed_pulses > 61) |
|
|
|
// { |
|
|
|
// printf("freq_start: %lu, freq_end: %lu, calculated_freq: %lu\r\n", freq_start, freq_end, calculated_freq); |
|
|
|
// } |
|
|
|
|
|
|
|
// 限制频率范围 |
|
|
|
if (calculated_freq < PLSR_PWM_FREQ_MIN) |
|
|
@@ -2474,8 +2479,8 @@ void PLSR_TaskSectionSwitch(PLSR_RouteConfig_t* route) |
|
|
|
PLSR_Section_SwitchNext(route, 0); // 外部事件触发,传入0 |
|
|
|
/* 启动新段,设置新的脉冲参数和频率 */ |
|
|
|
PLSR_Section_StartNewSection(route); |
|
|
|
/* 启动PWM输出 */ |
|
|
|
PLSR_PWM_Start(); |
|
|
|
// /* 启动PWM输出 */ |
|
|
|
// PLSR_PWM_Start(); |
|
|
|
PLSR_ClearGpioTriggerFlag(); // 清除GPIO触发标志 |
|
|
|
} |
|
|
|
return; // 等待外部事件时不需要继续处理 |
|
|
@@ -2504,8 +2509,8 @@ void PLSR_TaskSectionSwitch(PLSR_RouteConfig_t* route) |
|
|
|
PLSR_Section_SwitchNext(route, 1); // 脉冲完成触发,传入1 |
|
|
|
/* 启动新段,设置新的脉冲参数和频率 */ |
|
|
|
PLSR_Section_StartNewSection(route); |
|
|
|
/* 启动PWM输出 */ |
|
|
|
PLSR_PWM_Start(); |
|
|
|
// /* 启动PWM输出 */ |
|
|
|
// PLSR_PWM_Start(); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
@@ -2517,8 +2522,8 @@ void PLSR_TaskSectionSwitch(PLSR_RouteConfig_t* route) |
|
|
|
PLSR_Section_SwitchNext(route, 0); // 其他等待条件触发,传入0 |
|
|
|
/* 启动新段,设置新的脉冲参数和频率 */ |
|
|
|
PLSR_Section_StartNewSection(route); |
|
|
|
/* 启动PWM输出 */ |
|
|
|
PLSR_PWM_Start(); |
|
|
|
// /* 启动PWM输出 */ |
|
|
|
// PLSR_PWM_Start(); |
|
|
|
} |
|
|
|
/* 如果等待条件未满足,保持等待状态,等待下次检查 */ |
|
|
|
} |
|
|
|