From 1596b2beb080db3f3fe1a3a8f4b215ccd39d45db Mon Sep 17 00:00:00 2001 From: cbb <2625274797@qq.com> Date: Sat, 24 Jan 2026 01:50:39 +0800 Subject: [PATCH] =?UTF-8?q?DRV8313=E5=85=BC=E5=AE=B9=E7=89=88=20refactor(a?= =?UTF-8?q?lgorithm):=20=E7=A7=BB=E9=99=A4=E5=86=97=E4=BD=99=E5=A4=B4?= =?UTF-8?q?=E6=96=87=E4=BB=B6=20=E7=A7=BB=E9=99=A4=E4=B8=8D=E5=BF=85?= =?UTF-8?q?=E8=A6=81=E7=9A=84stdint.h=E5=A4=B4=E6=96=87=E4=BB=B6=E5=BC=95?= =?UTF-8?q?=E7=94=A8=EF=BC=8C=E7=BB=9F=E4=B8=80=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refactor(sys): 清理系统级头文件依赖 移除未使用的头文件引用,统一整型类型定义,优化初始 化过程中的GPIO配置 --- Robot_V2/Algorithm/FOC.cpp | 4 ++-- Robot_V2/Algorithm/FOC.h | 15 ++++++++++++--- Robot_V2/Algorithm/Filter.cpp | 1 - Robot_V2/Algorithm/Filter.h | 2 +- Robot_V2/Algorithm/pid.cpp | 3 ++- Robot_V2/Algorithm/pid.h | 33 ++++++++++++++++----------------- Robot_V2/device/ADC.cpp | 3 +-- Robot_V2/device/Can.cpp | 10 +++++----- Robot_V2/device/Can.h | 2 -- Robot_V2/device/Timer.cpp | 3 --- Robot_V2/device/Timer.h | 4 ++-- Robot_V2/device/adc.h | 1 - Robot_V2/device/ang_sensor.h | 2 +- Robot_V2/device/motor.cpp | 18 ++++++++++++++++-- Robot_V2/device/motor.h | 5 ++++- Robot_V2/main.cpp | 13 ++++--------- Robot_V2/main.h | 2 +- Robot_V2/sys/config.h | 4 ++-- Robot_V2/sys/flash.h | 2 +- Robot_V2/sys/health.cpp | 1 - Robot_V2/sys/health.h | 2 +- Robot_V2/sys/initial.cpp | 4 ++-- Robot_V2/sys/initial.h | 4 ++-- Robot_V2/sys/systick.h | 2 +- 24 files changed, 76 insertions(+), 64 deletions(-) diff --git a/Robot_V2/Algorithm/FOC.cpp b/Robot_V2/Algorithm/FOC.cpp index d82f9a9..e2e875e 100644 --- a/Robot_V2/Algorithm/FOC.cpp +++ b/Robot_V2/Algorithm/FOC.cpp @@ -10,7 +10,6 @@ #include #include #include -#include 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); 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, 5e-4); // 设置电机角度控制参数 + return 0; } diff --git a/Robot_V2/Algorithm/FOC.h b/Robot_V2/Algorithm/FOC.h index 27204a5..9c40f1c 100644 --- a/Robot_V2/Algorithm/FOC.h +++ b/Robot_V2/Algorithm/FOC.h @@ -3,12 +3,15 @@ #define _FOC_H #include "config.h" #include "pid.h" -#include +#include class FOC_Controlroller { public: FOC_Controlroller(); + + // 初始化函数 + u8 init(); u8 control_mode = 0; float limit_voltage = 0.0f; @@ -22,10 +25,9 @@ class FOC_Controlroller { bool voltage_error = false; bool is_shut_down = false; 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 Ua = 0, Ub = 0, Uc = 0; - u8 init(void); float velocity_open_loop(void); float angle_control(void); float angle_control_with_velocity_limit(void); @@ -50,6 +52,13 @@ class FOC_Controlroller { PIDController ang_inVelAngloop = 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: float shaft_angle = 0; float Ualpha, Ubeta = 0; diff --git a/Robot_V2/Algorithm/Filter.cpp b/Robot_V2/Algorithm/Filter.cpp index 449dc00..4a5f776 100644 --- a/Robot_V2/Algorithm/Filter.cpp +++ b/Robot_V2/Algorithm/Filter.cpp @@ -4,7 +4,6 @@ #include "config.h" #include #include -#include extern angle_sensor KTH7823; extern CAN_transmiter CAN0_transmiter; diff --git a/Robot_V2/Algorithm/Filter.h b/Robot_V2/Algorithm/Filter.h index 26096bb..a0b3fb8 100644 --- a/Robot_V2/Algorithm/Filter.h +++ b/Robot_V2/Algorithm/Filter.h @@ -14,7 +14,7 @@ extern "C" { #ifdef __cplusplus #include -#include +#include float medium_filter(float values[], u8 size); diff --git a/Robot_V2/Algorithm/pid.cpp b/Robot_V2/Algorithm/pid.cpp index e650a96..82406bd 100644 --- a/Robot_V2/Algorithm/pid.cpp +++ b/Robot_V2/Algorithm/pid.cpp @@ -2,7 +2,8 @@ #define _constrain(amt, low, high) \ ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt))) #include "Can.h" -#include "math.h" +#include +#include extern CAN_transmiter CAN0_transmiter; constexpr float TIME_SLICE = 1e-4; diff --git a/Robot_V2/Algorithm/pid.h b/Robot_V2/Algorithm/pid.h index d8fb2f1..59dfecf 100644 --- a/Robot_V2/Algorithm/pid.h +++ b/Robot_V2/Algorithm/pid.h @@ -1,26 +1,25 @@ #ifndef PID_H #define PID_H -class PIDController -{ -public: - PIDController(float P, float I, float D, float ramp, float limit); - ~PIDController() = default; +class PIDController { + public: + PIDController(float P, float I, float D, float ramp, float limit); + ~PIDController() = default; - float operator() (float error); + float operator()(float error); void set_PID(float P, float I, float D); void set_P_percent(float pp); - float P; //!< 比例增益(P环增益) - float I; //!< 积分增益(I环增益) - float D; //!< 微分增益(D环增益) - float output_ramp; - float limit; -protected: - float error_prev; //!< 最后的跟踪误差值 - float output_prev; //!< 最后一个 pid 输出值 - float integral_prev; //!< 最后一个积分分量值 - unsigned long timestamp_prev; //!< 上次执行时间戳 -}; + float P; //!< 比例增益(P环增益) + float I; //!< 积分增益(I环增益) + float D; //!< 微分增益(D环增益) + float output_ramp; + float limit; + protected: + float error_prev; //!< 最后的跟踪误差值 + float output_prev; //!< 最后一个 pid 输出值 + float integral_prev; //!< 最后一个积分分量值 + unsigned long timestamp_prev; //!< 上次执行时间戳 +}; #endif \ No newline at end of file diff --git a/Robot_V2/device/ADC.cpp b/Robot_V2/device/ADC.cpp index 8d6ebf8..2ea86bf 100644 --- a/Robot_V2/device/ADC.cpp +++ b/Robot_V2/device/ADC.cpp @@ -2,13 +2,12 @@ #include "Can.h" #include "FOC.h" #include "Filter.h" -#include "arm_math.h" #include "config.h" #include "math.h" #include "systick.h" #include #include -#include +#include constexpr float ADC_SCALE = 3.3f / 4096.0f; constexpr float TEMP_FACTOR = 1000.0f / 4.3f; diff --git a/Robot_V2/device/Can.cpp b/Robot_V2/device/Can.cpp index 4e191c4..648afc3 100644 --- a/Robot_V2/device/Can.cpp +++ b/Robot_V2/device/Can.cpp @@ -17,9 +17,6 @@ #include #include #include -#include -#include -#include 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_pin_remap_config(GPIO_CAN0_PARTIAL_REMAP, ENABLE); /* 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_14); return 0; } @@ -604,7 +603,8 @@ void CAN_transmiter::communication_check() { P_config = unpack_bytes_to_u16(receive_message.rx_data, 19); FOC.angle_loop.set_P_percent(angle_loop_p * P_config / voltage); 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) { u8 first_byte = receive_message.rx_data[0]; u8 eigth_byte = receive_message.rx_data[7]; diff --git a/Robot_V2/device/Can.h b/Robot_V2/device/Can.h index eeaf828..d897add 100644 --- a/Robot_V2/device/Can.h +++ b/Robot_V2/device/Can.h @@ -7,8 +7,6 @@ extern "C" { #endif #include "gd32c10x_can.h" -#include "stdint.h" -#include // 定义一个联合体类型用于float和byte数组之间的转换 typedef union { diff --git a/Robot_V2/device/Timer.cpp b/Robot_V2/device/Timer.cpp index 18ca60e..5838bdd 100644 --- a/Robot_V2/device/Timer.cpp +++ b/Robot_V2/device/Timer.cpp @@ -8,9 +8,6 @@ #include "gd32c10x_rcu.h" #include "gd32c10x_timer.h" #include "initial.h" -#include -#include -#include volatile u32 total_us100_counter = 0; // 总计数器,每100微秒增加1 extern u16 adc_value[7]; diff --git a/Robot_V2/device/Timer.h b/Robot_V2/device/Timer.h index 9029aeb..248b317 100644 --- a/Robot_V2/device/Timer.h +++ b/Robot_V2/device/Timer.h @@ -5,10 +5,10 @@ extern "C" { #endif -#include +#include 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 TIMER2_IRQHandler(void); diff --git a/Robot_V2/device/adc.h b/Robot_V2/device/adc.h index 65e3834..3283034 100644 --- a/Robot_V2/device/adc.h +++ b/Robot_V2/device/adc.h @@ -5,7 +5,6 @@ extern "C" { #endif -#include "stdint.h" #include "config.h" class sampler { diff --git a/Robot_V2/device/ang_sensor.h b/Robot_V2/device/ang_sensor.h index b9cd594..2503b12 100644 --- a/Robot_V2/device/ang_sensor.h +++ b/Robot_V2/device/ang_sensor.h @@ -4,7 +4,7 @@ extern "C" { #endif #include "gd32c10x_eval.h" -#include "stdio.h" +#include #include "config.h" u16 spi_rw_half_word(u32 spi_periph, const u16 i_HalfWord); diff --git a/Robot_V2/device/motor.cpp b/Robot_V2/device/motor.cpp index 083740e..1dffc51 100644 --- a/Robot_V2/device/motor.cpp +++ b/Robot_V2/device/motor.cpp @@ -1,6 +1,7 @@ #include "motor.h" #include "Timer.h" #include "gd32c10x_eval.h" +#include "gd32c10x_gpio.h" #include /** @@ -16,6 +17,11 @@ void motor_init(void) { /* 配置GPIOB的PB14为推挽输出 SPICS */ gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, 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 电机休眠 */ -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 电机启动 */ -void motor_resume(void) { gpio_bit_set(GPIOB, GPIO_PIN_12); } \ No newline at end of file +void motor_resume(void) { + gpio_bit_set(GPIOB, GPIO_PIN_12); + // 同时启用电机驱动器 + gpio_bit_set(MOTOR_EN_GPIO_PORT, MOTOR_EN_GPIO_PIN); +} \ No newline at end of file diff --git a/Robot_V2/device/motor.h b/Robot_V2/device/motor.h index dc198a3..d4c74c1 100644 --- a/Robot_V2/device/motor.h +++ b/Robot_V2/device/motor.h @@ -1,11 +1,14 @@ #ifndef _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_reset(void); void motor_resume(void); void motor_sleep(void); - #endif \ No newline at end of file diff --git a/Robot_V2/main.cpp b/Robot_V2/main.cpp index 2a54fd6..ba0b5af 100644 --- a/Robot_V2/main.cpp +++ b/Robot_V2/main.cpp @@ -9,13 +9,8 @@ #include "health.h" #include "initial.h" #include "motor.h" -#include "sys/flash.h" -#include "sys/initial.h" #include "systick.h" -#include -#include -#include -#include + extern CAN_transmiter CAN0_transmiter; // CAN初始化 extern sampler sampler; // 采样器 extern angle_sensor KTH7823; // 角度传感器 @@ -96,11 +91,11 @@ void do_task(u32 currentTasks) { // 角度闭环控制,带速度限制 &FOC_Controlroller::velocity_control, // 2: 速度闭环控制 &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])(); } @@ -120,7 +115,7 @@ void do_task(u32 currentTasks) { if (currentTasks & (1 << TASK_VBUS_UPDATE)) { // 更新vbus电压值 power_manage(); } - if (currentTasks & (1 << TASK_CAN_TRANSMIT)) { // CAN通信 + if (currentTasks & (1 << TASK_CAN_TRANSMIT)) { // CAN通信 // CAN0_transmiter.testfloat_union.f = cnt_test; // 负载检测 cnt_test = 0; CAN0_transmiter.can0_send_flag = SET; diff --git a/Robot_V2/main.h b/Robot_V2/main.h index f39ea43..ab1c246 100644 --- a/Robot_V2/main.h +++ b/Robot_V2/main.h @@ -34,7 +34,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef MAIN_H #define MAIN_H -#include "stdint.h" +#include void do_task(u32 currentTasks); u8 hardware_init(void); diff --git a/Robot_V2/sys/config.h b/Robot_V2/sys/config.h index fd15d78..9a4e872 100644 --- a/Robot_V2/sys/config.h +++ b/Robot_V2/sys/config.h @@ -1,6 +1,6 @@ #ifndef __CONFIG_H #define __CONFIG_H -#include +#include // 如果使用CMSIS DSP库,需要定义ARM_MATH_CM4 #define ARM_MATH_CM4 @@ -12,7 +12,7 @@ // 版本号定义 - 可根据实际项目进行调整 constexpr u16 SOFTWARE_VERSION_MAJOR = 26; 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 HARDWARE_VERSION_MAJOR = 25; diff --git a/Robot_V2/sys/flash.h b/Robot_V2/sys/flash.h index 4958a8f..849fb09 100644 --- a/Robot_V2/sys/flash.h +++ b/Robot_V2/sys/flash.h @@ -2,7 +2,7 @@ #define __FLASH_H #include "gd32c10x.h" // 添加GD32主头文件以确保外设函数声明可用 -#include +#include // 修改后 - 强制4字节对齐 #define ALIGN_4BYTE(addr) ((addr) & 0xFFFFFFFC) // 4字节对齐 diff --git a/Robot_V2/sys/health.cpp b/Robot_V2/sys/health.cpp index 2b7af17..cd0a76b 100644 --- a/Robot_V2/sys/health.cpp +++ b/Robot_V2/sys/health.cpp @@ -1,6 +1,5 @@ #include "health.h" #include "config.h" -#include #include "Can.h" #include "FOC.h" #include "adc.h" diff --git a/Robot_V2/sys/health.h b/Robot_V2/sys/health.h index a3b22cf..81925b4 100644 --- a/Robot_V2/sys/health.h +++ b/Robot_V2/sys/health.h @@ -1,7 +1,7 @@ #ifndef _HEALTH_H #define _HEALTH_H -#include "stdint.h" +#include typedef int32_t q16_16_t; // Q16.16格式 // 定点数运算宏 #define Q16_16_ONE 65536 // 1.0 in Q16.16 diff --git a/Robot_V2/sys/initial.cpp b/Robot_V2/sys/initial.cpp index 25811fe..a1d8118 100644 --- a/Robot_V2/sys/initial.cpp +++ b/Robot_V2/sys/initial.cpp @@ -162,10 +162,10 @@ void unused_pin_init() { gpio_init(GPIOB, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ, GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_15); 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(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); } /*! diff --git a/Robot_V2/sys/initial.h b/Robot_V2/sys/initial.h index b2b20f8..088ab54 100644 --- a/Robot_V2/sys/initial.h +++ b/Robot_V2/sys/initial.h @@ -7,8 +7,8 @@ extern "C" { #include "config.h" #include "gd32c10x_eval.h" -#include "stdint.h" -#include +#include +#include // 任务标志定义 typedef enum { diff --git a/Robot_V2/sys/systick.h b/Robot_V2/sys/systick.h index 99dd74c..494cf61 100644 --- a/Robot_V2/sys/systick.h +++ b/Robot_V2/sys/systick.h @@ -38,7 +38,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifdef __cplusplus extern "C" { #endif -#include +#include /* configure systick */ void systick_config(void);