feat(FOC): 添加开关机功能并优化定时器配置
This commit is contained in:
+13
-4
@@ -596,10 +596,19 @@ void CAN_transmiter::communication_check() {
|
||||
// }
|
||||
if (receive_message.rx_efid == 0x18ff0200) {
|
||||
FOC.control_mode = receive_message.rx_data[0];
|
||||
FOC.limit_voltage =
|
||||
float(unpack_bytes_to_u16(receive_message.rx_data, 1)) / 1000;
|
||||
FOC.limit_velocity =
|
||||
unpack_bytes_to_float(receive_message.rx_data, 3).f;
|
||||
if (float(unpack_bytes_to_u16(receive_message.rx_data, 1)) / 1000 !=
|
||||
FOC.limit_voltage) {
|
||||
FOC.limit_voltage =
|
||||
float(unpack_bytes_to_u16(receive_message.rx_data, 1)) /
|
||||
1000;
|
||||
FOC.is_shut_down = 100; // 1s滤除启动电流
|
||||
}
|
||||
if (unpack_bytes_to_float(receive_message.rx_data, 3).f !=
|
||||
FOC.limit_velocity) {
|
||||
FOC.limit_velocity =
|
||||
unpack_bytes_to_float(receive_message.rx_data, 3).f;
|
||||
FOC.is_shut_down = 100; // 1s滤除启动电流
|
||||
}
|
||||
FOC.target_angle =
|
||||
unpack_bytes_to_float(receive_message.rx_data, 7).f;
|
||||
FOC.target_velocity =
|
||||
|
||||
+97
-12
@@ -5,8 +5,10 @@
|
||||
#include "adc.h"
|
||||
#include "gd32c10x.h"
|
||||
#include "gd32c10x_gpio.h"
|
||||
#include "gd32c10x_misc.h"
|
||||
#include "gd32c10x_rcu.h"
|
||||
#include "gd32c10x_timer.h"
|
||||
#include "systick.h"
|
||||
#include <cstdio>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
@@ -18,6 +20,8 @@
|
||||
// 然后设置周期为99,即计数100次,1MHz/100 = 10kHz = 100微秒
|
||||
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
|
||||
|
||||
constexpr int TIMER1_PRESCALER = 0; // 不分频,计数器时钟为120MHz
|
||||
constexpr int TIMER1_PERIOD = 5999; // 120MHz / (0+1) / (5999+1) = 20kHz
|
||||
@@ -47,13 +51,14 @@ void nvic_config(void) {
|
||||
nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
|
||||
/* 使能TIMER2中断,设置抢占优先级为1,子优先级为1 */
|
||||
nvic_irq_enable(TIMER2_IRQn, 1, 1);
|
||||
nvic_irq_enable(TIMER3_IRQn, 1, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 配置定时器1
|
||||
* @brief 配置定时器2
|
||||
* @return void
|
||||
*
|
||||
*配置定时器1
|
||||
*配置定时器2,用于定时执行FOC控制、采样、CAN发送等任务
|
||||
*/
|
||||
void TIMER2_config(void) {
|
||||
/* ----------------------------------------------------------------------------
|
||||
@@ -90,6 +95,47 @@ void TIMER2_config(void) {
|
||||
timer_enable(TIMER2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 配置TIMER2中断
|
||||
* @return void
|
||||
@@ -118,45 +164,52 @@ 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;
|
||||
|
||||
|
||||
// 在定时器中断中执行FOC控制,确保控制频率稳定 (最高频操作放在最前)
|
||||
FOC.velocity_open_loop(FOC.target_velocity);
|
||||
|
||||
|
||||
// 每100us执行一次的操作
|
||||
sampler.update_current();
|
||||
|
||||
// 每1ms执行的代码 (100us * 10 = 1ms)
|
||||
if (total_us100_counter >= 10) {
|
||||
total_us100_counter = 0; // 重置计数器
|
||||
|
||||
|
||||
// 设置CAN发送标志
|
||||
CAN0_transmiter.can0_send_flag = SET;
|
||||
|
||||
|
||||
// 更新各种传感器数据
|
||||
sampler.update_mcu_temp();
|
||||
sampler.update_drv_temp();
|
||||
sampler.update_vbus_voltage();
|
||||
|
||||
// 每10ms执行的代码 (1ms * 10 = 10ms)
|
||||
static uint8_t ms10_counter = 0;
|
||||
static u8 ms10_counter = 0;
|
||||
ms10_counter++;
|
||||
if (ms10_counter >= 10) {
|
||||
ms10_counter = 0;
|
||||
// 可以添加每10ms执行的代码
|
||||
|
||||
|
||||
// 每100ms执行的代码 (10ms * 10 = 100ms)
|
||||
static uint8_t ms100_counter = 0;
|
||||
static u8 ms100_counter = 0;
|
||||
ms100_counter++;
|
||||
if (ms100_counter >= 10) {
|
||||
ms100_counter = 0;
|
||||
// 可以添加每100ms执行的代码
|
||||
|
||||
// if (sampler.get_current('U') > 1.0 ||
|
||||
// sampler.get_current('V') > 1.0 ||
|
||||
// sampler.get_current('W') > 1.0) {
|
||||
// if (FOC.is_shut_down == 0) {
|
||||
// FOC.shut_down();
|
||||
// }
|
||||
// }
|
||||
|
||||
// 每1s执行的代码 (100ms * 10 = 1s)
|
||||
static uint8_t sec_counter = 0;
|
||||
static u8 sec_counter = 0;
|
||||
sec_counter++;
|
||||
if (sec_counter >= 10) {
|
||||
sec_counter = 0;
|
||||
@@ -168,6 +221,38 @@ void TIMER2_IRQHandler(void) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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测试代码
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIMER1 GPIO配置函数
|
||||
*/
|
||||
|
||||
@@ -11,6 +11,8 @@ void User_timer_init(void);
|
||||
void Set_Pwm(uint16_t channel, float duty_cycle);
|
||||
void Set_3Pwm(float duty_cycle0, float duty_cycle1,float duty_cycle2);
|
||||
void TIMER2_IRQHandler(void);
|
||||
void TIMER3_config(void);
|
||||
void TIMER3_IRQHandler(void);
|
||||
|
||||
void timer1_pwm_config(void);
|
||||
void timer1_gpio_config(void);
|
||||
|
||||
Reference in New Issue
Block a user