Files
Motor_v2/Robot_V2/device/Timer.cpp
T

381 lines
11 KiB
C++
Raw Normal View History

2025-09-23 00:27:51 +08:00
#include "Timer.h"
2025-10-14 00:49:58 +08:00
#include "Can.h"
#include "DengFOC.h"
2025-11-06 02:07:15 +08:00
#include "Filter.h"
#include "Timer.h"
2025-10-02 02:52:07 +08:00
#include "adc.h"
2025-11-02 01:54:01 +08:00
#include "ang_sensor.h"
2025-09-23 00:27:51 +08:00
#include "gd32c10x.h"
#include "gd32c10x_gpio.h"
#include "gd32c10x_misc.h"
2025-10-16 21:43:34 +08:00
#include "gd32c10x_rcu.h"
2025-09-23 00:27:51 +08:00
#include "gd32c10x_timer.h"
2025-11-06 02:07:15 +08:00
#include <cstdio>
2025-09-23 00:27:51 +08:00
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
2025-10-16 21:43:34 +08:00
constexpr int TIMER2_PRESCALER = 119; // 120MHz / 120 = 1MHz
constexpr int TIMER2_PERIOD = 99; // 1MHz / 100 = 100us
constexpr int TIMER3_PRESCALER = 119; // 120MHz / 120 = 1MHz
constexpr int TIMER3_PERIOD = 9999; // 1MHz / 10000 = 100Hz = 10ms
2025-10-02 02:52:07 +08:00
constexpr int TIMER1_PRESCALER = 0; // 不分频,计数器时钟为120MHz
constexpr int TIMER1_PERIOD = 5999; // 120MHz / (0+1) / (5999+1) = 20kHz
2025-10-16 21:43:34 +08:00
constexpr int TIMER1_CH0_PULSE = 0;
constexpr int TIMER1_CH1_PULSE = 0;
constexpr int TIMER1_CH2_PULSE = 0;
2025-10-14 00:49:58 +08:00
static u32 total_us100_counter = 0; // 总计数器,每100微秒增加1
2025-10-02 02:52:07 +08:00
extern u16 adc_value[7];
2025-10-14 00:49:58 +08:00
extern CAN_transmiter CAN0_transmiter;
2025-10-16 21:43:34 +08:00
// 添加系统运行时间计数器
static volatile std::u64 system_runtime_us = 0;
// 声明外部FOC控制器对象
extern FOC_Controlroller FOC;
// 声明外部采样器对象
extern sampler sampler;
2025-11-02 01:54:01 +08:00
extern angle_sensor KTH7823;
2025-11-06 02:07:15 +08:00
extern EncoderVelocityKalman Vel_kalman;
2025-09-23 00:27:51 +08:00
/**
* @brief 设置引脚中断优先级
* @return void
*
*设置引脚中断优先级
*/
void nvic_config(void) {
/* 设置NVIC优先级分组,1位抢占优先级,3位响应优先级 */
2025-10-02 02:52:07 +08:00
nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
2025-10-16 21:43:34 +08:00
/* 使能TIMER2中断,设置抢占优先级为1,子优先级为1 */
nvic_irq_enable(TIMER2_IRQn, 1, 1);
nvic_irq_enable(TIMER3_IRQn, 1, 2);
2025-09-23 00:27:51 +08:00
}
/**
* @brief 配置定时器2
2025-09-23 00:27:51 +08:00
* @return void
*
*配置定时器2,用于定时执行FOC控制、采样、CAN发送等任务
2025-09-23 00:27:51 +08:00
*/
2025-10-16 21:43:34 +08:00
void TIMER2_config(void) {
2025-09-23 00:27:51 +08:00
/* ----------------------------------------------------------------------------
2025-10-16 21:43:34 +08:00
TIMER2 Configuration:
TIMER2CLK = SystemCoreClock/120 = 1MHz, the period is 100us(100/1000000 =
2025-10-14 00:49:58 +08:00
100us).
2025-09-23 00:27:51 +08:00
----------------------------------------------------------------------------
*/
2025-10-02 02:52:07 +08:00
timer_parameter_struct timer_struct;
2025-09-23 00:27:51 +08:00
2025-10-16 21:43:34 +08:00
/* 使能TIMER2时钟 */
rcu_periph_clock_enable(RCU_TIMER2);
2025-09-23 00:27:51 +08:00
2025-10-16 21:43:34 +08:00
/* 复位TIMER2外设到默认状态 */
timer_deinit(TIMER2);
2025-09-23 00:27:51 +08:00
/* 初始化TIMER初始化参数结构体 */
2025-10-02 02:52:07 +08:00
timer_struct_para_init(&timer_struct);
2025-10-16 21:43:34 +08:00
/* TIMER2配置参数设置 */
2025-10-02 02:52:07 +08:00
timer_struct.prescaler =
2025-10-16 21:43:34 +08:00
TIMER2_PRESCALER; /* 预分频值:120分频(从0开始计数) */
2025-10-02 02:52:07 +08:00
timer_struct.alignedmode = TIMER_COUNTER_EDGE; /* 边沿对齐模式 */
timer_struct.counterdirection = TIMER_COUNTER_UP; /* 向上计数模式 */
timer_struct.period =
2025-10-16 21:43:34 +08:00
TIMER2_PERIOD; /* 自动重装载值:100次计数(从0开始计数) */
2025-10-02 02:52:07 +08:00
timer_struct.clockdivision = TIMER_CKDIV_DIV1; /* 时钟分频值:1分频 */
2025-10-16 21:43:34 +08:00
timer_init(TIMER2, &timer_struct);
2025-09-23 00:27:51 +08:00
/* 清除更新中断标志位 */
2025-10-16 21:43:34 +08:00
timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_UP);
2025-09-23 00:27:51 +08:00
/* 使能更新中断 */
2025-10-16 21:43:34 +08:00
timer_interrupt_enable(TIMER2, TIMER_INT_UP);
2025-09-23 00:27:51 +08:00
2025-10-16 21:43:34 +08:00
/* 使能TIMER2 */
timer_enable(TIMER2);
2025-09-23 00:27:51 +08:00
}
/**
* @brief 配置定时器3
* @return void
*
*配置定时器3,用于诊断恢复
*/
void TIMER3_config(void) {
/* ----------------------------------------------------------------------------
TIMER3 Configuration:
TIMER3CLK = SystemCoreClock/120 = 1MHz, the period is 10ms(10000/1000000 =
10ms).
----------------------------------------------------------------------------
*/
timer_parameter_struct timer_struct;
/* 使能TIMER3时钟 */
rcu_periph_clock_enable(RCU_TIMER3);
/* 复位TIMER3外设到默认状态 */
timer_deinit(TIMER3);
/* 初始化TIMER初始化参数结构体 */
timer_struct_para_init(&timer_struct);
/* TIMER3配置参数设置 */
timer_struct.prescaler =
TIMER3_PRESCALER; /* 预分频值:120分频(从0开始计数) */
timer_struct.alignedmode = TIMER_COUNTER_EDGE; /* 边沿对齐模式 */
timer_struct.counterdirection = TIMER_COUNTER_UP; /* 向上计数模式 */
timer_struct.period =
TIMER3_PERIOD; /* 自动重装载值:10000次计数(从0开始计数) */
timer_struct.clockdivision = TIMER_CKDIV_DIV1; /* 时钟分频值:1分频 */
timer_init(TIMER3, &timer_struct);
/* 清除更新中断标志位 */
timer_interrupt_flag_clear(TIMER3, TIMER_INT_FLAG_UP);
/* 使能更新中断 */
timer_interrupt_enable(TIMER3, TIMER_INT_UP);
/* 使能TIMER3 */
timer_enable(TIMER3);
}
2025-09-23 00:27:51 +08:00
/**
2025-10-16 21:43:34 +08:00
* @brief 配置TIMER2中断
2025-09-23 00:27:51 +08:00
* @return void
*/
void User_timer_init(void) {
2025-10-16 21:43:34 +08:00
/* 配置TIMER2 */
TIMER2_config();
/* 配置TIMER2中断 */
2025-10-02 02:52:07 +08:00
nvic_config();
2025-09-23 00:27:51 +08:00
}
2025-10-16 21:43:34 +08:00
/*!
\brief 获取系统运行时间(毫秒)
\param[in] none
\param[out] none
\retval 系统运行时间,单位为毫秒
2025-10-16 21:43:34 +08:00
*/
uint64_t get_system_runtime_us(void) { return system_runtime_us; }
2025-10-16 21:43:34 +08:00
2025-09-23 00:27:51 +08:00
/**
2025-10-16 21:43:34 +08:00
* @brief TIMER2中断处理函数
2025-09-23 00:27:51 +08:00
* @return void
*/
2025-10-16 21:43:34 +08:00
void TIMER2_IRQHandler(void) {
2025-11-06 02:07:15 +08:00
static u8 mode = 0;
2025-10-02 02:52:07 +08:00
// 检查更新中断标志并处理
2025-10-16 21:43:34 +08:00
if (timer_interrupt_flag_get(TIMER2, TIMER_INT_UP) != RESET) {
2025-10-02 02:52:07 +08:00
// 清除更新中断标志
2025-10-16 21:43:34 +08:00
timer_interrupt_flag_clear(TIMER2, TIMER_INT_UP);
// 增加总计数器 (使用更高效的位运算代替取模运算)
2025-10-14 00:49:58 +08:00
total_us100_counter++;
// system_runtime_us += 100;
// 每100us执行一次的操作
sampler.update_current(); // 更新电流采样值
KTH7823.update(); // 更新角度传感器值
2025-11-06 02:07:15 +08:00
FOC.rt_velocity = Vel_kalman.update(KTH7823.getVelocity());
FOC.rt_angle = KTH7823.getAngle();
2025-11-06 02:07:15 +08:00
if (FOC.control_mode == 1) {
FOC.angle_control(FOC.target_angle); // 角度闭环控制
2025-11-06 02:07:15 +08:00
} else if (FOC.control_mode == 2) {
FOC.velocity_control(FOC.target_velocity); // 速度闭环控制
} else if (FOC.control_mode == 3) {
FOC.angle_control_NPID(FOC.target_angle); // 角度无PID控制
2025-11-06 02:07:15 +08:00
} else if (FOC.control_mode == 4) {
FOC.velocity_open_loop(); // 速度开环控制
}
2025-10-16 21:43:34 +08:00
if (total_us100_counter >= 10) {
// 每1ms执行的代码
total_us100_counter = 0; // 重置计数器
// 设置CAN发送标志
CAN0_transmiter.can0_send_flag = SET;
2025-11-06 02:07:15 +08:00
mode = FOC.control_mode;
// 过流检测
if (sampler.get_current('U') > 2000 ||
sampler.get_current('V') > 2000 ||
sampler.get_current('W') > 2000) {
FOC.over_current++;
} else {
FOC.over_current = 0;
}
// 更新各种传感器数据
sampler.update_mcu_temp();
sampler.update_drv_temp();
sampler.update_vbus_voltage();
2025-09-23 00:27:51 +08:00
// 停机判断及冷却
if (FOC.over_current > 10) {
FOC.is_shut_down = 10;
}
static u8 ms10_counter = 0;
ms10_counter++;
if (ms10_counter >= 10) {
ms10_counter = 0;
// 可以添加每10ms执行的代码
2025-11-06 02:07:15 +08:00
FOC.update_power();
static u8 ms100_counter = 0;
ms100_counter++;
if (ms100_counter >= 10) {
ms100_counter = 0;
// 可以添加每100ms执行的代码
static u8 sec_counter = 0;
sec_counter++;
if (sec_counter >= 10) {
sec_counter = 0;
// 可以添加每1s执行的代码
}
}
2025-10-02 02:52:07 +08:00
}
}
2025-09-23 00:27:51 +08:00
}
}
/**
* @brief TIMER3中断处理函数
* @return void
*/
void TIMER3_IRQHandler(void) {
// 检查更新中断标志并处理
if (timer_interrupt_flag_get(TIMER3, TIMER_INT_UP) != RESET) {
// 清除更新中断标志
timer_interrupt_flag_clear(TIMER3, TIMER_INT_UP);
static u8 count = 0;
count++;
if (FOC.is_shut_down > 0)
FOC.is_shut_down--;
else
FOC.resume();
// 10ms测试代码
if (count >= 10) {
count = 0;
// 100ms测试代码
static u8 count_10 = 0;
count_10++;
if (count_10 >= 10) {
count_10 = 0;
// 1s测试代码
2025-11-02 01:54:01 +08:00
FOC.update_power();
}
}
}
}
2025-09-23 00:27:51 +08:00
/**
2025-10-16 21:43:34 +08:00
* @brief TIMER1 GPIO配置函数
2025-09-23 00:27:51 +08:00
*/
2025-10-16 21:43:34 +08:00
void timer1_gpio_config(void) {
2025-09-23 00:27:51 +08:00
// 时钟使能
rcu_periph_clock_enable(RCU_GPIOA);
2025-10-16 21:43:34 +08:00
rcu_periph_clock_enable(RCU_GPIOB);
2025-09-23 00:27:51 +08:00
rcu_periph_clock_enable(RCU_AF);
2025-10-16 21:43:34 +08:00
rcu_periph_clock_enable(RCU_TIMER1);
2025-09-23 00:27:51 +08:00
2025-10-16 21:43:34 +08:00
// TIMER1 GPIO初始化
2025-09-23 00:27:51 +08:00
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
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);
2025-10-16 21:43:34 +08:00
gpio_bit_set(GPIOB, GPIO_PIN_0);
gpio_bit_set(GPIOB, GPIO_PIN_12);
/* 配置TIMER1完全重映射 - 应该使用DISABLE,因为我们使用的是默认引脚 */
gpio_pin_remap_config(GPIO_TIMER1_FULL_REMAP, DISABLE);
2025-09-23 00:27:51 +08:00
}
/**
2025-10-16 21:43:34 +08:00
* @brief TIMER1 配置函数
2025-09-23 00:27:51 +08:00
*/
2025-10-16 21:43:34 +08:00
void timer1_pwm_config(void) {
timer_parameter_struct timer_initpara;
timer_oc_parameter_struct timer_ocinitpara;
2025-09-23 00:27:51 +08:00
// 使能TIMER1时钟
rcu_periph_clock_enable(RCU_TIMER1);
2025-09-23 00:27:51 +08:00
// 复位TIMER1
timer_deinit(TIMER1);
2025-09-23 00:27:51 +08:00
// 配置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);
2025-09-23 00:27:51 +08:00
// 配置通道输出参数
timer_channel_output_struct_para_init(&timer_ocinitpara);
timer_ocinitpara.outputstate = TIMER_CCX_ENABLE;
timer_ocinitpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
2025-09-23 00:27:51 +08:00
// 配置三个通道为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);
2025-09-23 00:27:51 +08:00
// 设置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);
2025-09-23 00:27:51 +08:00
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);
2025-09-23 00:27:51 +08:00
// 使能主输出和自动重载
timer_primary_output_config(TIMER1, ENABLE);
timer_auto_reload_shadow_enable(TIMER1);
// 启动TIMER1
timer_enable(TIMER1);
2025-09-23 00:27:51 +08:00
}
/*!
\brief set PWM
\param[in] channel: TIMER_CH_0/TIMER_CH_1/TIMER_CH_2/TIMER_CH_3 (valid
values: 0, 1, 2, 3)
\param[in] pulse: PWM pulse value
\param[out] none
\retval none
\note The channel parameter must be between 0 and 3 inclusive.
*/
2025-10-16 21:43:34 +08:00
void Set_Pwm(u16 channel, float duty_cycle) {
float pulse = duty_cycle * TIMER1_PERIOD;
2025-09-23 00:27:51 +08:00
if (channel <= 3) {
2025-10-16 21:43:34 +08:00
timer_channel_output_pulse_value_config(TIMER1, channel, pulse);
2025-09-23 00:27:51 +08:00
} else {
} // Ensure channel is within valid range
}
2025-10-16 21:43:34 +08:00
/*!
\brief set PWM
\param[in] channel: TIMER_CH_0/TIMER_CH_1/TIMER_CH_2/TIMER_CH_3 (valid
values: 0, 1, 2, 3)
\param[in] pulse: PWM pulse value
\param[out] none
\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);
2025-10-16 21:43:34 +08:00
}