DRV8313兼容版

refactor(algorithm): 移除冗余头文件
移除不必要的stdint.h头文件引用,统一使用<cstdint>

refactor(sys): 清理系统级头文件依赖
移除未使用的头文件引用,统一整型类型定义,优化初始
化过程中的GPIO配置
This commit is contained in:
cbb
2026-01-24 01:50:39 +08:00
parent 3f24292ac1
commit 1596b2beb0
24 changed files with 76 additions and 64 deletions
+2 -2
View File
@@ -10,7 +10,6 @@
#include <arm_math.h> #include <arm_math.h>
#include <cmath> #include <cmath>
#include <gd32c10x.h> #include <gd32c10x.h>
#include <stdint.h>
FOC_Controlroller FOC; // FOC控制初始化 FOC_Controlroller FOC; // FOC控制初始化
@@ -58,7 +57,7 @@ inline float FOC_Controlroller::cal_Iq_Id(float current_a, float current_b,
// arm_park_f32(I_alpha, I_beta, &I_d, &I_q, sin_el, cos_el); // arm_park_f32(I_alpha, I_beta, &I_d, &I_q, sin_el, cos_el);
return I_q * 1000.0f - return I_q * 1000.0f -
KE_FACTOR * rt_velocity; // 返回Iq值,乘以1000以转换为mA KE_FACTOR * rt_velocity; // 返回Iq值,乘以100以转换为mA
} }
/** /**
@@ -79,6 +78,7 @@ u8 FOC_Controlroller::init() {
ang_inVelAngloop.set_PID(ang_angloop_p * 100 / limit_voltage, 0.0f, ang_inVelAngloop.set_PID(ang_angloop_p * 100 / limit_voltage, 0.0f,
5e-4); // 设置电机角度控制参数 5e-4); // 设置电机角度控制参数
return 0; return 0;
} }
+12 -3
View File
@@ -3,12 +3,15 @@
#define _FOC_H #define _FOC_H
#include "config.h" #include "config.h"
#include "pid.h" #include "pid.h"
#include <stdint.h> #include <cstdint>
class FOC_Controlroller { class FOC_Controlroller {
public: public:
FOC_Controlroller(); FOC_Controlroller();
// 初始化函数
u8 init();
u8 control_mode = 0; u8 control_mode = 0;
float limit_voltage = 0.0f; float limit_voltage = 0.0f;
@@ -22,10 +25,9 @@ class FOC_Controlroller {
bool voltage_error = false; bool voltage_error = false;
bool is_shut_down = false; bool is_shut_down = false;
float voltage_power_supply = 24.0f; // 电源电压 float voltage_power_supply = 24.0f; // 电源电压
float v_safe = voltage_power_supply * 1.05f; float v_safe = voltage_power_supply * 0.95f;
float duty_a = 0, duty_b = 0, duty_c = 0; float duty_a = 0, duty_b = 0, duty_c = 0;
float Ua = 0, Ub = 0, Uc = 0; float Ua = 0, Ub = 0, Uc = 0;
u8 init(void);
float velocity_open_loop(void); float velocity_open_loop(void);
float angle_control(void); float angle_control(void);
float angle_control_with_velocity_limit(void); float angle_control_with_velocity_limit(void);
@@ -50,6 +52,13 @@ class FOC_Controlroller {
PIDController ang_inVelAngloop = PIDController ang_inVelAngloop =
PIDController{ang_angloop_p * 100, 0.0f, 5e-4, 1000000, 999}; PIDController{ang_angloop_p * 100, 0.0f, 5e-4, 1000000, 999};
void update_pid_limits() {
vel_loop.limit = voltage_power_supply;
angle_loop.limit = voltage_power_supply;
current_loop.limit = voltage_power_supply;
vel_inVelAngloop.limit = voltage_power_supply;
}
protected: protected:
float shaft_angle = 0; float shaft_angle = 0;
float Ualpha, Ubeta = 0; float Ualpha, Ubeta = 0;
-1
View File
@@ -4,7 +4,6 @@
#include "config.h" #include "config.h"
#include <algorithm> #include <algorithm>
#include <cstring> #include <cstring>
#include <stdint.h>
extern angle_sensor KTH7823; extern angle_sensor KTH7823;
extern CAN_transmiter CAN0_transmiter; extern CAN_transmiter CAN0_transmiter;
+1 -1
View File
@@ -14,7 +14,7 @@ extern "C" {
#ifdef __cplusplus #ifdef __cplusplus
#include <cstring> #include <cstring>
#include <stdint.h> #include <cstdint>
float medium_filter(float values[], u8 size); float medium_filter(float values[], u8 size);
+2 -1
View File
@@ -2,7 +2,8 @@
#define _constrain(amt, low, high) \ #define _constrain(amt, low, high) \
((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt))) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt)))
#include "Can.h" #include "Can.h"
#include "math.h" #include <algorithm>
#include <cmath>
extern CAN_transmiter CAN0_transmiter; extern CAN_transmiter CAN0_transmiter;
constexpr float TIME_SLICE = 1e-4; constexpr float TIME_SLICE = 1e-4;
+16 -17
View File
@@ -1,26 +1,25 @@
#ifndef PID_H #ifndef PID_H
#define PID_H #define PID_H
class PIDController class PIDController {
{ public:
public: PIDController(float P, float I, float D, float ramp, float limit);
PIDController(float P, float I, float D, float ramp, float limit); ~PIDController() = default;
~PIDController() = default;
float operator() (float error); float operator()(float error);
void set_PID(float P, float I, float D); void set_PID(float P, float I, float D);
void set_P_percent(float pp); void set_P_percent(float pp);
float P; //!< 比例增益(P环增益) float P; //!< 比例增益(P环增益)
float I; //!< 积分增益(I环增益) float I; //!< 积分增益(I环增益)
float D; //!< 微分增益(D环增益) float D; //!< 微分增益(D环增益)
float output_ramp; float output_ramp;
float limit; float limit;
protected:
float error_prev; //!< 最后的跟踪误差值
float output_prev; //!< 最后一个 pid 输出值
float integral_prev; //!< 最后一个积分分量值
unsigned long timestamp_prev; //!< 上次执行时间戳
};
protected:
float error_prev; //!< 最后的跟踪误差值
float output_prev; //!< 最后一个 pid 输出值
float integral_prev; //!< 最后一个积分分量值
unsigned long timestamp_prev; //!< 上次执行时间戳
};
#endif #endif
+1 -2
View File
@@ -2,13 +2,12 @@
#include "Can.h" #include "Can.h"
#include "FOC.h" #include "FOC.h"
#include "Filter.h" #include "Filter.h"
#include "arm_math.h"
#include "config.h" #include "config.h"
#include "math.h" #include "math.h"
#include "systick.h" #include "systick.h"
#include <cstring> #include <cstring>
#include <gd32c10x_dma.h> #include <gd32c10x_dma.h>
#include <stdint.h> #include <cstdint>
constexpr float ADC_SCALE = 3.3f / 4096.0f; constexpr float ADC_SCALE = 3.3f / 4096.0f;
constexpr float TEMP_FACTOR = 1000.0f / 4.3f; constexpr float TEMP_FACTOR = 1000.0f / 4.3f;
+5 -5
View File
@@ -17,9 +17,6 @@
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <random> #include <random>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
constexpr u16 CANX = CAN0; constexpr u16 CANX = CAN0;
@@ -55,8 +52,10 @@ u8 can_gpio_init() {
gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9); // tx gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9); // tx
gpio_pin_remap_config(GPIO_CAN0_PARTIAL_REMAP, ENABLE); gpio_pin_remap_config(GPIO_CAN0_PARTIAL_REMAP, ENABLE);
/* enable the EN GPIO clock */ /* enable the EN GPIO clock */
gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13); gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,
GPIO_PIN_13 | GPIO_PIN_14);
gpio_bit_reset(GPIOB, GPIO_PIN_13); gpio_bit_reset(GPIOB, GPIO_PIN_13);
gpio_bit_reset(GPIOB, GPIO_PIN_14);
return 0; return 0;
} }
@@ -604,7 +603,8 @@ void CAN_transmiter::communication_check() {
P_config = unpack_bytes_to_u16(receive_message.rx_data, 19); P_config = unpack_bytes_to_u16(receive_message.rx_data, 19);
FOC.angle_loop.set_P_percent(angle_loop_p * P_config / voltage); FOC.angle_loop.set_P_percent(angle_loop_p * P_config / voltage);
P_config = unpack_bytes_to_u16(receive_message.rx_data, 21); P_config = unpack_bytes_to_u16(receive_message.rx_data, 21);
FOC.vel_inVelAngloop.set_P_percent(vel_angloop_p * P_config / voltage); FOC.vel_inVelAngloop.set_P_percent(vel_angloop_p * P_config /
voltage);
} else if (receive_message.rx_efid == CANFD_RESET_ID) { } else if (receive_message.rx_efid == CANFD_RESET_ID) {
u8 first_byte = receive_message.rx_data[0]; u8 first_byte = receive_message.rx_data[0];
u8 eigth_byte = receive_message.rx_data[7]; u8 eigth_byte = receive_message.rx_data[7];
-2
View File
@@ -7,8 +7,6 @@ extern "C" {
#endif #endif
#include "gd32c10x_can.h" #include "gd32c10x_can.h"
#include "stdint.h"
#include <stdio.h>
// 定义一个联合体类型用于float和byte数组之间的转换 // 定义一个联合体类型用于float和byte数组之间的转换
typedef union { typedef union {
-3
View File
@@ -8,9 +8,6 @@
#include "gd32c10x_rcu.h" #include "gd32c10x_rcu.h"
#include "gd32c10x_timer.h" #include "gd32c10x_timer.h"
#include "initial.h" #include "initial.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
volatile u32 total_us100_counter = 0; // 总计数器,每100微秒增加1 volatile u32 total_us100_counter = 0; // 总计数器,每100微秒增加1
extern u16 adc_value[7]; extern u16 adc_value[7];
+2 -2
View File
@@ -5,10 +5,10 @@
extern "C" { extern "C" {
#endif #endif
#include <stdint.h> #include <cstdint>
void User_timer_init(void); void User_timer_init(void);
void Set_Pwm(uint16_t channel, float duty_cycle); void Set_Pwm(u16 channel, float duty_cycle);
void Set_3Pwm(float duty_cycle0, float duty_cycle1,float duty_cycle2); void Set_3Pwm(float duty_cycle0, float duty_cycle1,float duty_cycle2);
void TIMER2_IRQHandler(void); void TIMER2_IRQHandler(void);
-1
View File
@@ -5,7 +5,6 @@
extern "C" { extern "C" {
#endif #endif
#include "stdint.h"
#include "config.h" #include "config.h"
class sampler { class sampler {
+1 -1
View File
@@ -4,7 +4,7 @@
extern "C" { extern "C" {
#endif #endif
#include "gd32c10x_eval.h" #include "gd32c10x_eval.h"
#include "stdio.h" #include <cstring>
#include "config.h" #include "config.h"
u16 spi_rw_half_word(u32 spi_periph, const u16 i_HalfWord); u16 spi_rw_half_word(u32 spi_periph, const u16 i_HalfWord);
+16 -2
View File
@@ -1,6 +1,7 @@
#include "motor.h" #include "motor.h"
#include "Timer.h" #include "Timer.h"
#include "gd32c10x_eval.h" #include "gd32c10x_eval.h"
#include "gd32c10x_gpio.h"
#include <cstdio> #include <cstdio>
/** /**
@@ -16,6 +17,11 @@ void motor_init(void) {
/* 配置GPIOB的PB14为推挽输出 SPICS */ /* 配置GPIOB的PB14为推挽输出 SPICS */
gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,
GPIO_PIN_14 | GPIO_PIN_12); GPIO_PIN_14 | GPIO_PIN_12);
// 使能GPIOC时钟,用于电机驱动使能引脚
rcu_periph_clock_enable(RCU_MOTOR_EN_GPIO);
// 配置PC13为推挽输出,用于控制电机驱动器使能
gpio_init(MOTOR_EN_GPIO_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, MOTOR_EN_GPIO_PIN);
} }
/** /**
@@ -30,9 +36,17 @@ void motor_reset(void) {
/** /**
* @brief 电机休眠 * @brief 电机休眠
*/ */
void motor_sleep(void) { gpio_bit_reset(GPIOB, GPIO_PIN_12); } void motor_sleep(void) {
gpio_bit_reset(GPIOB, GPIO_PIN_12);
// 同时禁用电机驱动器
gpio_bit_reset(MOTOR_EN_GPIO_PORT, MOTOR_EN_GPIO_PIN);
}
/** /**
* @brief 电机启动 * @brief 电机启动
*/ */
void motor_resume(void) { gpio_bit_set(GPIOB, GPIO_PIN_12); } void motor_resume(void) {
gpio_bit_set(GPIOB, GPIO_PIN_12);
// 同时启用电机驱动器
gpio_bit_set(MOTOR_EN_GPIO_PORT, MOTOR_EN_GPIO_PIN);
}
+4 -1
View File
@@ -1,11 +1,14 @@
#ifndef _MOTOR_H #ifndef _MOTOR_H
#define _MOTOR_H #define _MOTOR_H
// 定义电机驱动使能引脚
#define MOTOR_EN_GPIO_PORT GPIOC
#define MOTOR_EN_GPIO_PIN GPIO_PIN_13
#define RCU_MOTOR_EN_GPIO RCU_GPIOC
void motor_init(void); void motor_init(void);
void motor_reset(void); void motor_reset(void);
void motor_resume(void); void motor_resume(void);
void motor_sleep(void); void motor_sleep(void);
#endif #endif
+4 -9
View File
@@ -9,13 +9,8 @@
#include "health.h" #include "health.h"
#include "initial.h" #include "initial.h"
#include "motor.h" #include "motor.h"
#include "sys/flash.h"
#include "sys/initial.h"
#include "systick.h" #include "systick.h"
#include <arm_math.h>
#include <cmath>
#include <cstdio>
#include <stdint.h>
extern CAN_transmiter CAN0_transmiter; // CAN初始化 extern CAN_transmiter CAN0_transmiter; // CAN初始化
extern sampler sampler; // 采样器 extern sampler sampler; // 采样器
extern angle_sensor KTH7823; // 角度传感器 extern angle_sensor KTH7823; // 角度传感器
@@ -96,11 +91,11 @@ void do_task(u32 currentTasks) {
// 角度闭环控制,带速度限制 // 角度闭环控制,带速度限制
&FOC_Controlroller::velocity_control, // 2: 速度闭环控制 &FOC_Controlroller::velocity_control, // 2: 速度闭环控制
&FOC_Controlroller::current_control, // 3: 电流闭环控制 &FOC_Controlroller::current_control, // 3: 电流闭环控制
&FOC_Controlroller::velocity_open_loop // 4: 速度开环控制 &FOC_Controlroller::velocity_open_loop, // 4: 速度开环控制
}; };
// 边界检查,防止数组越界 // 边界检查,防止数组越界
if (FOC.control_mode >= 0 && FOC.control_mode < 5) { if (FOC.control_mode >= 0 && FOC.control_mode < 5) { // 更新为7个控制模式
// 调用对应的控制函数 // 调用对应的控制函数
(FOC.*control_functions[FOC.control_mode])(); (FOC.*control_functions[FOC.control_mode])();
} }
@@ -120,7 +115,7 @@ void do_task(u32 currentTasks) {
if (currentTasks & (1 << TASK_VBUS_UPDATE)) { // 更新vbus电压值 if (currentTasks & (1 << TASK_VBUS_UPDATE)) { // 更新vbus电压值
power_manage(); power_manage();
} }
if (currentTasks & (1 << TASK_CAN_TRANSMIT)) { // CAN通信 if (currentTasks & (1 << TASK_CAN_TRANSMIT)) { // CAN通信
// CAN0_transmiter.testfloat_union.f = cnt_test; // 负载检测 // CAN0_transmiter.testfloat_union.f = cnt_test; // 负载检测
cnt_test = 0; cnt_test = 0;
CAN0_transmiter.can0_send_flag = SET; CAN0_transmiter.can0_send_flag = SET;
+1 -1
View File
@@ -34,7 +34,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef MAIN_H #ifndef MAIN_H
#define MAIN_H #define MAIN_H
#include "stdint.h" #include <cstdint>
void do_task(u32 currentTasks); void do_task(u32 currentTasks);
u8 hardware_init(void); u8 hardware_init(void);
+2 -2
View File
@@ -1,6 +1,6 @@
#ifndef __CONFIG_H #ifndef __CONFIG_H
#define __CONFIG_H #define __CONFIG_H
#include <stdint.h> #include <cstdint>
// 如果使用CMSIS DSP库,需要定义ARM_MATH_CM4 // 如果使用CMSIS DSP库,需要定义ARM_MATH_CM4
#define ARM_MATH_CM4 #define ARM_MATH_CM4
@@ -12,7 +12,7 @@
// 版本号定义 - 可根据实际项目进行调整 // 版本号定义 - 可根据实际项目进行调整
constexpr u16 SOFTWARE_VERSION_MAJOR = 26; constexpr u16 SOFTWARE_VERSION_MAJOR = 26;
constexpr u16 SOFTWARE_VERSION_MINOR = 1; constexpr u16 SOFTWARE_VERSION_MINOR = 1;
constexpr u16 SOFTWARE_VERSION_PATCH = 20; constexpr u16 SOFTWARE_VERSION_PATCH = 24;
constexpr u16 SOFTWARE_VERSION_BUILD = 1; constexpr u16 SOFTWARE_VERSION_BUILD = 1;
constexpr u16 HARDWARE_VERSION_MAJOR = 25; constexpr u16 HARDWARE_VERSION_MAJOR = 25;
+1 -1
View File
@@ -2,7 +2,7 @@
#define __FLASH_H #define __FLASH_H
#include "gd32c10x.h" // 添加GD32主头文件以确保外设函数声明可用 #include "gd32c10x.h" // 添加GD32主头文件以确保外设函数声明可用
#include <stdint.h> #include <cstdint>
// 修改后 - 强制4字节对齐 // 修改后 - 强制4字节对齐
#define ALIGN_4BYTE(addr) ((addr) & 0xFFFFFFFC) // 4字节对齐 #define ALIGN_4BYTE(addr) ((addr) & 0xFFFFFFFC) // 4字节对齐
-1
View File
@@ -1,6 +1,5 @@
#include "health.h" #include "health.h"
#include "config.h" #include "config.h"
#include <stdint.h>
#include "Can.h" #include "Can.h"
#include "FOC.h" #include "FOC.h"
#include "adc.h" #include "adc.h"
+1 -1
View File
@@ -1,7 +1,7 @@
#ifndef _HEALTH_H #ifndef _HEALTH_H
#define _HEALTH_H #define _HEALTH_H
#include "stdint.h" #include <cstdint>
typedef int32_t q16_16_t; // Q16.16格式 typedef int32_t q16_16_t; // Q16.16格式
// 定点数运算宏 // 定点数运算宏
#define Q16_16_ONE 65536 // 1.0 in Q16.16 #define Q16_16_ONE 65536 // 1.0 in Q16.16
+2 -2
View File
@@ -162,10 +162,10 @@ void unused_pin_init() {
gpio_init(GPIOB, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ, gpio_init(GPIOB, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ,
GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_15); GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_15);
gpio_init(GPIOC, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ, gpio_init(GPIOC, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ,
GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15); GPIO_PIN_14 | GPIO_PIN_15);
gpio_bit_reset(GPIOA, GPIO_PIN_8 | GPIO_PIN_11 | GPIO_PIN_12); gpio_bit_reset(GPIOA, GPIO_PIN_8 | GPIO_PIN_11 | GPIO_PIN_12);
gpio_bit_reset(GPIOB, GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_15); gpio_bit_reset(GPIOB, GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_15);
gpio_bit_reset(GPIOC, GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15); gpio_bit_reset(GPIOC, GPIO_PIN_14 | GPIO_PIN_15);
} }
/*! /*!
+2 -2
View File
@@ -7,8 +7,8 @@ extern "C" {
#include "config.h" #include "config.h"
#include "gd32c10x_eval.h" #include "gd32c10x_eval.h"
#include "stdint.h" #include <cstdint>
#include <stdio.h> #include <cstdio>
// 任务标志定义 // 任务标志定义
typedef enum { typedef enum {
+1 -1
View File
@@ -38,7 +38,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#include <stdint.h> #include <cstdint>
/* configure systick */ /* configure systick */
void systick_config(void); void systick_config(void);