feat(FOC): 优化开环速度控制逻辑并提升性能
refactor(ADC): 重构采样逻辑并封装为 sampler 类 refactor(CAN): 更新 CAN 发送器以使用新的sampler 接口 refactor(timer): 调整定时器中断结构并优化任务调度
This commit is contained in:
+97
-74
@@ -1,16 +1,16 @@
|
||||
#include "Timer.h"
|
||||
#include "Can.h"
|
||||
#include "DengFOC.h"
|
||||
#include "Timer.h"
|
||||
#include "adc.h"
|
||||
#include "gd32c10x.h"
|
||||
#include "gd32c10x_gpio.h"
|
||||
#include "gd32c10x_rcu.h"
|
||||
#include "gd32c10x_timer.h"
|
||||
#include "DengFOC.h"
|
||||
#include <cstdio>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include "Timer.h"
|
||||
|
||||
// 配置定时器每100微秒中断一次
|
||||
// 系统时钟为120MHz,要得到100微秒的中断周期
|
||||
@@ -19,8 +19,8 @@
|
||||
constexpr int TIMER2_PRESCALER = 119; // 120MHz / 120 = 1MHz
|
||||
constexpr int TIMER2_PERIOD = 99; // 1MHz / 100 = 100us
|
||||
|
||||
constexpr int TIMER1_PRESCALER = 0; // 不分频,计数器时钟为120MHz
|
||||
constexpr int TIMER1_PERIOD = 2999; // 120MHz / (0+1) / (5999+1) = 20kHz
|
||||
constexpr int TIMER1_PRESCALER = 0; // 不分频,计数器时钟为120MHz
|
||||
constexpr int TIMER1_PERIOD = 5999; // 120MHz / (0+1) / (5999+1) = 20kHz
|
||||
constexpr int TIMER1_CH0_PULSE = 0;
|
||||
constexpr int TIMER1_CH1_PULSE = 0;
|
||||
constexpr int TIMER1_CH2_PULSE = 0;
|
||||
@@ -33,6 +33,8 @@ static volatile std::u64 system_runtime_us = 0;
|
||||
|
||||
// 声明外部FOC控制器对象
|
||||
extern FOC_Controlroller FOC;
|
||||
// 声明外部采样器对象
|
||||
extern sampler sampler;
|
||||
|
||||
/**
|
||||
* @brief 设置引脚中断优先级
|
||||
@@ -99,17 +101,13 @@ void User_timer_init(void) {
|
||||
nvic_config();
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
\brief 获取系统运行时间(毫秒)
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval 系统运行时间,单位为毫秒
|
||||
\brief 获取系统运行时间(毫秒)
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval 系统运行时间,单位为毫秒
|
||||
*/
|
||||
uint64_t get_system_runtime_us(void)
|
||||
{
|
||||
return system_runtime_us;
|
||||
}
|
||||
uint64_t get_system_runtime_us(void) { return system_runtime_us; }
|
||||
|
||||
/**
|
||||
* @brief TIMER2中断处理函数
|
||||
@@ -120,30 +118,51 @@ void TIMER2_IRQHandler(void) {
|
||||
if (timer_interrupt_flag_get(TIMER2, TIMER_INT_UP) != RESET) {
|
||||
// 清除更新中断标志
|
||||
timer_interrupt_flag_clear(TIMER2, TIMER_INT_UP);
|
||||
// 增加总计数器
|
||||
|
||||
// 增加总计数器 (使用更高效的位运算代替取模运算)
|
||||
total_us100_counter++;
|
||||
system_runtime_us += 100;
|
||||
// 每100us执行一次的操作可以放在这里
|
||||
|
||||
// 在定时器中断中执行FOC控制,确保控制频率稳定
|
||||
|
||||
// 在定时器中断中执行FOC控制,确保控制频率稳定 (最高频操作放在最前)
|
||||
FOC.velocity_open_loop(FOC.target_velocity);
|
||||
|
||||
// 每100us执行一次的操作
|
||||
sampler.update_current();
|
||||
|
||||
if (total_us100_counter % 10 == 0) {
|
||||
// 每1ms执行的代码可以放在这里
|
||||
CAN0_transmiter.can0_send_flag = SET;
|
||||
if (total_us100_counter % 100 == 0) {
|
||||
// 每10ms执行的代码可以放在这里
|
||||
}
|
||||
// 每1ms执行的代码 (100us * 10 = 1ms)
|
||||
if (total_us100_counter >= 10) {
|
||||
total_us100_counter = 0; // 重置计数器
|
||||
|
||||
if (total_us100_counter % 1000 == 0) {
|
||||
// 每100ms执行的代码可以放在这里
|
||||
}
|
||||
// 设置CAN发送标志
|
||||
CAN0_transmiter.can0_send_flag = SET;
|
||||
|
||||
// 更新各种传感器数据
|
||||
sampler.update_mcu_temp();
|
||||
sampler.update_drv_temp();
|
||||
sampler.update_vbus_voltage();
|
||||
|
||||
if (total_us100_counter % 10000 == 0) {
|
||||
// 每1s执行的代码可以放在这里
|
||||
|
||||
// 重置计数器避免溢出
|
||||
total_us100_counter = 0;
|
||||
// 每10ms执行的代码 (1ms * 10 = 10ms)
|
||||
static uint8_t ms10_counter = 0;
|
||||
ms10_counter++;
|
||||
if (ms10_counter >= 10) {
|
||||
ms10_counter = 0;
|
||||
// 可以添加每10ms执行的代码
|
||||
|
||||
// 每100ms执行的代码 (10ms * 10 = 100ms)
|
||||
static uint8_t ms100_counter = 0;
|
||||
ms100_counter++;
|
||||
if (ms100_counter >= 10) {
|
||||
ms100_counter = 0;
|
||||
// 可以添加每100ms执行的代码
|
||||
|
||||
// 每1s执行的代码 (100ms * 10 = 1s)
|
||||
static uint8_t sec_counter = 0;
|
||||
sec_counter++;
|
||||
if (sec_counter >= 10) {
|
||||
sec_counter = 0;
|
||||
// 可以添加每1s执行的代码
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -164,7 +183,8 @@ void timer1_gpio_config(void) {
|
||||
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
|
||||
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
|
||||
|
||||
gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0|GPIO_PIN_12);
|
||||
gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,
|
||||
GPIO_PIN_0 | GPIO_PIN_12);
|
||||
gpio_bit_set(GPIOB, GPIO_PIN_0);
|
||||
gpio_bit_set(GPIOB, GPIO_PIN_12);
|
||||
|
||||
@@ -176,52 +196,53 @@ void timer1_gpio_config(void) {
|
||||
* @brief TIMER1 配置函数
|
||||
*/
|
||||
void timer1_pwm_config(void) {
|
||||
timer_parameter_struct timer_initpara;
|
||||
timer_oc_parameter_struct timer_ocinitpara;
|
||||
timer_parameter_struct timer_initpara;
|
||||
timer_oc_parameter_struct timer_ocinitpara;
|
||||
|
||||
// 使能TIMER1时钟
|
||||
rcu_periph_clock_enable(RCU_TIMER1);
|
||||
// 使能TIMER1时钟
|
||||
rcu_periph_clock_enable(RCU_TIMER1);
|
||||
|
||||
// 复位TIMER1
|
||||
timer_deinit(TIMER1);
|
||||
// 复位TIMER1
|
||||
timer_deinit(TIMER1);
|
||||
|
||||
// 配置TIMER1基本参数
|
||||
timer_struct_para_init(&timer_initpara);
|
||||
timer_initpara.prescaler = TIMER1_PRESCALER; // 预分频器
|
||||
timer_initpara.counterdirection = TIMER_COUNTER_UP; // 向上计数
|
||||
timer_initpara.period = TIMER1_PERIOD; // 计数周期
|
||||
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
|
||||
timer_init(TIMER1, &timer_initpara);
|
||||
// 配置TIMER1基本参数
|
||||
timer_struct_para_init(&timer_initpara);
|
||||
timer_initpara.prescaler = TIMER1_PRESCALER; // 预分频器
|
||||
timer_initpara.counterdirection = TIMER_COUNTER_UP; // 向上计数
|
||||
timer_initpara.period = TIMER1_PERIOD; // 计数周期
|
||||
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
|
||||
timer_init(TIMER1, &timer_initpara);
|
||||
|
||||
// 配置通道输出参数
|
||||
timer_channel_output_struct_para_init(&timer_ocinitpara);
|
||||
timer_ocinitpara.outputstate = TIMER_CCX_ENABLE;
|
||||
timer_ocinitpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
|
||||
// 配置通道输出参数
|
||||
timer_channel_output_struct_para_init(&timer_ocinitpara);
|
||||
timer_ocinitpara.outputstate = TIMER_CCX_ENABLE;
|
||||
timer_ocinitpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
|
||||
|
||||
// 配置三个通道为PWM模式
|
||||
timer_channel_output_config(TIMER1, TIMER_CH_0, &timer_ocinitpara);
|
||||
timer_channel_output_config(TIMER1, TIMER_CH_1, &timer_ocinitpara);
|
||||
timer_channel_output_config(TIMER1, TIMER_CH_2, &timer_ocinitpara);
|
||||
// 配置三个通道为PWM模式
|
||||
timer_channel_output_config(TIMER1, TIMER_CH_0, &timer_ocinitpara);
|
||||
timer_channel_output_config(TIMER1, TIMER_CH_1, &timer_ocinitpara);
|
||||
timer_channel_output_config(TIMER1, TIMER_CH_2, &timer_ocinitpara);
|
||||
|
||||
// 设置PWM模式和占空比
|
||||
timer_channel_output_mode_config(TIMER1, TIMER_CH_0, TIMER_OC_MODE_PWM0);
|
||||
timer_channel_output_mode_config(TIMER1, TIMER_CH_1, TIMER_OC_MODE_PWM0);
|
||||
timer_channel_output_mode_config(TIMER1, TIMER_CH_2, TIMER_OC_MODE_PWM0);
|
||||
// 设置PWM模式和占空比
|
||||
timer_channel_output_mode_config(TIMER1, TIMER_CH_0, TIMER_OC_MODE_PWM0);
|
||||
timer_channel_output_mode_config(TIMER1, TIMER_CH_1, TIMER_OC_MODE_PWM0);
|
||||
timer_channel_output_mode_config(TIMER1, TIMER_CH_2, TIMER_OC_MODE_PWM0);
|
||||
|
||||
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_0, TIMER1_CH0_PULSE);
|
||||
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_1, TIMER1_CH1_PULSE);
|
||||
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_2, TIMER1_CH2_PULSE);
|
||||
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_0,
|
||||
TIMER1_CH0_PULSE);
|
||||
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_1,
|
||||
TIMER1_CH1_PULSE);
|
||||
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_2,
|
||||
TIMER1_CH2_PULSE);
|
||||
|
||||
// 使能主输出和自动重载
|
||||
timer_primary_output_config(TIMER1, ENABLE);
|
||||
timer_auto_reload_shadow_enable(TIMER1);
|
||||
|
||||
// 启动TIMER1
|
||||
timer_enable(TIMER1);
|
||||
// 使能主输出和自动重载
|
||||
timer_primary_output_config(TIMER1, ENABLE);
|
||||
timer_auto_reload_shadow_enable(TIMER1);
|
||||
|
||||
// 启动TIMER1
|
||||
timer_enable(TIMER1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
\brief set PWM
|
||||
\param[in] channel: TIMER_CH_0/TIMER_CH_1/TIMER_CH_2/TIMER_CH_3 (valid
|
||||
@@ -233,14 +254,13 @@ void timer1_pwm_config(void) {
|
||||
*/
|
||||
void Set_Pwm(u16 channel, float duty_cycle) {
|
||||
float pulse = duty_cycle * TIMER1_PERIOD;
|
||||
|
||||
|
||||
if (channel <= 3) {
|
||||
timer_channel_output_pulse_value_config(TIMER1, channel, pulse);
|
||||
} else {
|
||||
} // Ensure channel is within valid range
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
\brief set PWM
|
||||
\param[in] channel: TIMER_CH_0/TIMER_CH_1/TIMER_CH_2/TIMER_CH_3 (valid
|
||||
@@ -250,8 +270,11 @@ void Set_Pwm(u16 channel, float duty_cycle) {
|
||||
\retval none
|
||||
\note The channel parameter must be between 0 and 3 inclusive.
|
||||
*/
|
||||
void Set_3Pwm(float duty_cycle0, float duty_cycle1,float duty_cycle2) {
|
||||
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_0, duty_cycle0 * TIMER1_PERIOD);
|
||||
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_1, duty_cycle1 * TIMER1_PERIOD);
|
||||
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_2, duty_cycle2 * TIMER1_PERIOD);
|
||||
void Set_3Pwm(float duty_cycle0, float duty_cycle1, float duty_cycle2) {
|
||||
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_0,
|
||||
duty_cycle0 * TIMER1_PERIOD);
|
||||
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_1,
|
||||
duty_cycle1 * TIMER1_PERIOD);
|
||||
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_2,
|
||||
duty_cycle2 * TIMER1_PERIOD);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user