Quellcode durchsuchen

修改了多段运行,EXT信号跳转有问题,只有上电第一次可以执行跳转和直线加减速,脉冲首位频率不对称,需要优化的bug

2
JIU JIALIN vor 1 Monat
Ursprung
Commit
aa8b295fdd
6 geänderte Dateien mit 649 neuen und 616 gelöschten Zeilen
  1. +1
    -0
      PLSR/PLSR/Core/Inc/tim.h
  2. +95
    -64
      PLSR/PLSR/Core/Src/tim.c
  3. +2
    -1
      PLSR/PLSR/Core/Src/usart.c
  4. +551
    -551
      PLSR/PLSR/EWARM/test.1.dep
  5. BIN
      PLSR/PLSR/EWARM/test.1/Exe/test.1.sim
  6. BIN
      需求规格书.doc

+ 1
- 0
PLSR/PLSR/Core/Inc/tim.h Datei anzeigen

@@ -280,6 +280,7 @@ uint32_t OptimalIntermediateFrequency(uint32_t v0, uint32_t vt_desired,
extern PLSR_RouteConfig_t g_plsr_route; // 全局PLSR路径控制结构体
extern uint8_t g_plsr_ext_event_flag; // 外部事件标志
extern int32_t g_plsr_total_pulse_count; // 全局累加脉冲计数器(程序运行期间持续累加,支持负数)
extern int32_t g_plsr_location;
/* USER CODE END Prototypes */

#ifdef __cplusplus


+ 95
- 64
PLSR/PLSR/Core/Src/tim.c Datei anzeigen

@@ -33,6 +33,7 @@ uint32_t g_plsr_current_target_freq = 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;
// ==================== PLSR内部变量 ====================

// ==================== PLSR内部变量 ====================
@@ -961,7 +962,7 @@ static void PLSR_UpdateGlobalPulseCount(int32_t current_pulse_count)
s_last_total_pulse = current_pulse_count;
}
// 将32位全局累加脉冲计数分解为两个16位寄存器(支持负数)
int32_t signed_count = g_plsr_total_pulse_count; // 使用有符号数进行计算
int32_t signed_count = g_plsr_location; // 使用有符号数进行计算
ModbusSlave.holding_regs[0x1000] = signed_count & 0xFFFF; // 低16位
ModbusSlave.holding_regs[0x1001] = (signed_count >> 16) & 0xFFFF; // 高16位
}
@@ -1171,7 +1172,6 @@ void Calculate_PluseNum_Simplified(PLSR_RouteConfig_t *route)
route->accel_pulse_count = accel_pulse_num;
route->const_pulse_count = const_pulse_num;
route->decel_pulse_count = decel_pulse_num;
// if(route->accel_pulse_count > 1) route->accel_pulse_count -= 1;
}
}

@@ -1236,7 +1236,8 @@ void Calculate_PluseNum(PLSR_RouteConfig_t *route)
part1_pulse_num = (uint32_t)(temp_calc / 2000);
if(part1_pulse_num == 0) part1_pulse_num = 1;
}
else {
else
{
part1_pulse_num = 0;
part1_time = 0;
}
@@ -1441,11 +1442,12 @@ uint32_t OptimalIntermediateFrequency(uint32_t v0, uint32_t vt_desired,
// 统一平方项
uint64_t v0_sq = square_u32(v0);

if (v0 < vt_desired) {
if (v0 < vt_desired)
{
// 加速情况
// 公式:vm^2 = [(total + v0^2/(2a)) * 2ad] / (a+d)
uint64_t rhs = (uint64_t)total_pulses * SCALE_FACTOR + (v0_sq / (2ULL * a));
uint64_t vm_sq = (rhs * 2ULL * a * d) / (a + d);
uint64_t rhs = (uint64_t)total_pulses * SCALE_FACTOR + (v0_sq / a); //total*2000 + v0^2/a
uint64_t vm_sq = (rhs * a * d) / (a + d);

if (vm_sq > 0xFFFFFFFFULL) vm_sq = 0xFFFFFFFFULL;
vm = integer_sqrt((uint32_t)vm_sq);
@@ -1497,9 +1499,12 @@ uint32_t BinarySearchOptimalFreq(uint32_t v0, uint32_t vt_desired,
// 计算当前频率下的脉冲数
uint32_t s1, s3;
if (is_accel) {
if (is_accel)
{
s1 = ((uint64_t)mid * mid - (uint64_t)v0 * v0) / (2000ULL * a);
} else {
}
else
{
s1 = ((uint64_t)v0 * v0 - (uint64_t)mid * mid) / (2000ULL * d);
}
s3 = ((uint64_t)mid * mid) / (2000ULL * d);
@@ -1527,6 +1532,7 @@ 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;
// 重置部分状态
g_plsr_route.current_part = PLSR_PART_COMPLETE;
@@ -1570,6 +1576,11 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

// 精确累加当前段已发送的脉冲数
int32_t current_section_pulses = __HAL_TIM_GetAutoreload(&htim2);
if(g_plsr_mod_flag == 1)
{
current_section_pulses -= 1;
g_plsr_mod_flag = 0;
}
AllPluse += current_section_pulses;
g_plsr_route.pulse_count = AllPluse;
PLSR_UpdateGlobalPulseCount(AllPluse);
@@ -1718,7 +1729,8 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
(((uint32_t)ModbusSlave.holding_regs[256+(16*(g_plsr_route.current_section_num-1))]) |
(uint32_t)ModbusSlave.holding_regs[257+(16*(g_plsr_route.current_section_num-1))]<<16);
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_route.current_part != PLSR_PART_COMPLETE)
{
g_plsr_mod_flag = 1; // 标记当前段目标频率被修改
PLSR_Section_PWM_Stop();
@@ -1862,10 +1874,12 @@ void PLSR_Route_Start(PLSR_RouteConfig_t* route)
if(route->ext_port == 0)
{
HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
HAL_NVIC_DisableIRQ(EXTI15_10_IRQn);
}
else
{
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
HAL_NVIC_DisableIRQ(EXTI9_5_IRQn);
}
// 启动第一段
@@ -1953,7 +1967,7 @@ static uint8_t PLSR_SetDirectionPin(PLSR_RouteConfig_t* route, PLSR_SectionConfi
s_pulse_count_direction = is_forward ? 1 : 0;
// 根据方向逻辑确定方向端子状态
if (route->dir_logic == 0)
if (route->dir_logic == 1)
{
// 正逻辑:正向脉冲方向端子置ON(1),反向脉冲方向端子置OFF(0)
dir_pin_state = is_forward ? GPIO_PIN_SET : GPIO_PIN_RESET;
@@ -1967,16 +1981,16 @@ static uint8_t PLSR_SetDirectionPin(PLSR_RouteConfig_t* route, PLSR_SectionConfi
switch (route->dir_port)
{
case 0:
HAL_GPIO_WritePin(GPIOH, GPIO_PIN_6, dir_pin_state);
HAL_GPIO_WritePin(GPIOH, GPIO_PIN_9, dir_pin_state);
break;
case 1:
HAL_GPIO_WritePin(GPIOH, GPIO_PIN_7, dir_pin_state);
HAL_GPIO_WritePin(GPIOH, GPIO_PIN_8, dir_pin_state);
break;
case 2:
HAL_GPIO_WritePin(GPIOH, GPIO_PIN_8, dir_pin_state);
HAL_GPIO_WritePin(GPIOH, GPIO_PIN_7, dir_pin_state);
break;
case 3:
HAL_GPIO_WritePin(GPIOH, GPIO_PIN_9, dir_pin_state);
HAL_GPIO_WritePin(GPIOH, GPIO_PIN_6, dir_pin_state);
break;
default:
break;
@@ -2018,50 +2032,60 @@ void PLSR_Section_StartNewSection(PLSR_RouteConfig_t* route)
{
// 绝对模式:计算相对于当前位置的脉冲数
current_section->actual_pulse = current_section->target_pulse - g_plsr_location;
if(g_plsr_mod_flag == 1)
{
current_section->actual_pulse += 1;
//g_plsr_mod_flag = 0; // 清除修改标记
}
}
else
{
if(g_plsr_mod_flag == 1)
{
g_plsr_route.initial_freq = g_plsr_route.current_freq; // 更新加减速初始频率
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) + 1);
}
else
{
current_section->actual_pulse = ((-current_section->target_pulse) - route->accel_pulse_count - __HAL_TIM_GET_COUNTER(&htim2) + 1);
}
}
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) + 1);
}
else
{
current_section->actual_pulse = ((-current_section->target_pulse) - __HAL_TIM_GET_COUNTER(&htim2) + 1);
}
}
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) + 1);
}
else
{
current_section->actual_pulse = ((-current_section->target_pulse) -
route->accel_pulse_count - route->const_pulse_count - __HAL_TIM_GET_COUNTER(&htim2) + 1);
}
}
if(current_section->target_pulse > 0)
current_section->actual_pulse = total_se_pluse + current_section->target_pulse - g_plsr_total_pulse_count;
else
current_section->actual_pulse = total_se_pluse + (-current_section->target_pulse) - g_plsr_total_pulse_count;
current_section->actual_pulse += 1;
// 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) + 1);
// }
// else
// {
// current_section->actual_pulse = ((-current_section->target_pulse) - route->accel_pulse_count - __HAL_TIM_GET_COUNTER(&htim2) + 1);
// }
// }
// 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) + 1);
// }
// else
// {
// current_section->actual_pulse = ((-current_section->target_pulse) - __HAL_TIM_GET_COUNTER(&htim2) + 1);
// }
// }
// 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) + 1);
// }
// else
// {
// current_section->actual_pulse = ((-current_section->target_pulse) -
// route->accel_pulse_count - route->const_pulse_count - __HAL_TIM_GET_COUNTER(&htim2) + 1);
// }
// }
g_plsr_mod_flag = 0; // 清除修改标记
}
else
@@ -2367,13 +2391,19 @@ void PLSR_Section_SwitchNext(PLSR_RouteConfig_t* route, uint8_t is_pulse_complet
{
if (is_pulse_complete)
{
int32_t actual_total = 0;
if(current_section->target_pulse < 0)
actual_total = -current_section->target_pulse;
else
actual_total = current_section->target_pulse;
// 脉冲完成触发:累加整个目标脉冲数
route->prevPulseCount += current_section->target_pulse;
route->prevPulseCount += actual_total;
}
else
{
// 外部事件触发:直接使用全局脉冲计数器作为累计脉冲数
route->prevPulseCount = g_plsr_location;
route->prevPulseCount = g_plsr_total_pulse_count;
total_se_pluse += g_plsr_total_pulse_count;
}
}
// // 检查下一段是否有效
@@ -2562,17 +2592,18 @@ void PLSR_Accel_Process(PLSR_RouteConfig_t* route)
// // 记录是否是第一次设置非零频率
// uint8_t first_flag = (route->current_freq == 0);
// 记录上一次的运行状态,用于检测状态变化
static PLSR_RunState_t prev_run_state = PLSR_STATE_IDLE;
// // 记录上一次的运行状态,用于检测状态变化
// static PLSR_RunState_t prev_run_state = PLSR_STATE_IDLE;
// 检测状态变化,如果状态发生变化,更新initial_freq并清零g_last_freq
if (prev_run_state != route->run_state) {
// 状态发生变化,将当前频率保存为初始频率
route->initial_freq = route->current_freq;
// 清零g_last_freq,防止频率计算时的累加效果
g_last_freq = 0;
prev_run_state = route->run_state;
}
// // 检测状态变化,如果状态发生变化,更新initial_freq并清零g_last_freq
// if (prev_run_state != route->run_state)
// {
// // 状态发生变化,将当前频率保存为初始频率
// route->initial_freq = route->current_freq;
// // 清零g_last_freq,防止频率计算时的累加效果
// g_last_freq = 0;
// prev_run_state = route->run_state;
// }
// 加速完成检查
if (route->run_state == PLSR_STATE_ACCEL && route->current_freq >= route->target_freq)


+ 2
- 1
PLSR/PLSR/Core/Src/usart.c Datei anzeigen

@@ -350,7 +350,8 @@ void Modbus_Clear_Pluse(uint8_t* frame, uint16_t length)
response[2] = 0; // 无数据返回

g_plsr_total_pulse_count = 0; // 假设0x100寄存器用于存储脉冲计数
int32_t signed_count = g_plsr_total_pulse_count; // 使用有符号数进行计算
g_plsr_location = 0;
int32_t signed_count = g_plsr_location; // 使用有符号数进行计算
ModbusSlave.holding_regs[0x1000] = signed_count & 0xFFFF; // 低16位
ModbusSlave.holding_regs[0x1001] = (signed_count >> 16) & 0xFFFF; // 高16位
Modbus_Send_Response(response, 3);


+ 551
- 551
PLSR/PLSR/EWARM/test.1.dep
Datei-Diff unterdrückt, da er zu groß ist
Datei anzeigen


BIN
PLSR/PLSR/EWARM/test.1/Exe/test.1.sim Datei anzeigen


BIN
需求规格书.doc Datei anzeigen


Laden…
Abbrechen
Speichern