2025-10-16 21:43:34 +08:00
|
|
|
// 函数声明
|
2026-01-04 22:26:22 +08:00
|
|
|
#ifndef _FOC_H
|
|
|
|
|
#define _FOC_H
|
|
|
|
|
#include "config.h"
|
2025-11-03 02:38:43 +08:00
|
|
|
#include "pid.h"
|
2026-01-24 01:50:39 +08:00
|
|
|
#include <cstdint>
|
2025-11-23 18:31:47 +08:00
|
|
|
|
2025-10-16 21:43:34 +08:00
|
|
|
class FOC_Controlroller {
|
2025-12-05 00:46:19 +08:00
|
|
|
|
2025-10-16 21:43:34 +08:00
|
|
|
public:
|
|
|
|
|
FOC_Controlroller();
|
2026-01-24 01:50:39 +08:00
|
|
|
|
|
|
|
|
// 初始化函数
|
|
|
|
|
u8 init();
|
2025-10-16 21:43:34 +08:00
|
|
|
|
2025-11-12 00:46:26 +08:00
|
|
|
u8 control_mode = 0;
|
2025-10-30 20:20:14 +08:00
|
|
|
float limit_voltage = 0.0f;
|
2025-10-16 21:43:34 +08:00
|
|
|
float limit_velocity = 0.0f;
|
|
|
|
|
float target_angle = 0.0f;
|
|
|
|
|
float target_velocity = 0.0f;
|
2026-01-04 22:26:22 +08:00
|
|
|
float target_current = 0.0f;
|
2025-11-01 20:29:11 +08:00
|
|
|
float rt_angle = 0.0f;
|
|
|
|
|
float rt_velocity = 0.0f;
|
2025-12-05 00:46:19 +08:00
|
|
|
bool over_current = false;
|
2025-11-13 02:04:58 +08:00
|
|
|
bool voltage_error = false;
|
|
|
|
|
bool is_shut_down = false;
|
|
|
|
|
float voltage_power_supply = 24.0f; // 电源电压
|
2026-01-24 01:50:39 +08:00
|
|
|
float v_safe = voltage_power_supply * 0.95f;
|
2025-12-05 00:46:19 +08:00
|
|
|
float duty_a = 0, duty_b = 0, duty_c = 0;
|
|
|
|
|
float Ua = 0, Ub = 0, Uc = 0;
|
2026-01-04 22:26:22 +08:00
|
|
|
float velocity_open_loop(void);
|
|
|
|
|
float angle_control(void);
|
|
|
|
|
float angle_control_with_velocity_limit(void);
|
|
|
|
|
float velocity_control(void);
|
|
|
|
|
float current_control(void);
|
2025-11-03 02:38:43 +08:00
|
|
|
|
2025-11-02 01:54:01 +08:00
|
|
|
void update_power(void);
|
2025-11-03 02:38:43 +08:00
|
|
|
void DFOC_alignSensor(int _PP);
|
2025-10-29 21:22:10 +08:00
|
|
|
void shut_down(void);
|
|
|
|
|
void resume(void);
|
2025-11-22 23:59:43 +08:00
|
|
|
void drv_stop(void);
|
2026-01-04 22:26:22 +08:00
|
|
|
float drv_hold(void);
|
2026-01-20 01:28:52 +08:00
|
|
|
PIDController vel_loop = PIDController{
|
|
|
|
|
vel_loop_p * 100, 0.20f, 5e-5, 1000000, voltage_power_supply}; // 12V
|
|
|
|
|
PIDController angle_loop =
|
|
|
|
|
PIDController{angle_loop_p * 100, 100.0f, 0.2f, 10000000,
|
|
|
|
|
voltage_power_supply}; // 12V
|
|
|
|
|
PIDController current_loop = PIDController{
|
|
|
|
|
current_loop_p * 100, 5e-7f, 7e-8, 500, 24}; // 12V
|
|
|
|
|
PIDController vel_inVelAngloop = PIDController{
|
|
|
|
|
vel_angloop_p * 100, 3.0f, 5e-4, 1000000, voltage_power_supply};
|
|
|
|
|
PIDController ang_inVelAngloop =
|
|
|
|
|
PIDController{ang_angloop_p * 100, 0.0f, 5e-4, 1000000, 999};
|
2025-10-16 21:43:34 +08:00
|
|
|
|
2026-01-24 01:50:39 +08:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-01 20:29:11 +08:00
|
|
|
protected:
|
2025-10-16 21:43:34 +08:00
|
|
|
float shaft_angle = 0;
|
2025-12-05 00:46:19 +08:00
|
|
|
float Ualpha, Ubeta = 0;
|
2025-10-16 21:43:34 +08:00
|
|
|
float zero_electric_angle = 0;
|
2025-11-06 02:07:15 +08:00
|
|
|
float v_half = voltage_power_supply * 0.5f; // 电源电压的一半
|
2025-11-08 02:15:14 +08:00
|
|
|
void setPhaseVoltage(float Uq, float Ud);
|
2025-12-05 00:46:19 +08:00
|
|
|
float cal_Iq_Id(float current_a, float current_b, float current_c);
|
2025-10-16 21:43:34 +08:00
|
|
|
};
|
|
|
|
|
|
2026-01-04 22:26:22 +08:00
|
|
|
// 定义函数指针类型
|
|
|
|
|
typedef float (FOC_Controlroller::*FOCFunctionPtr)(void);
|
|
|
|
|
|
2026-01-07 23:13:21 +08:00
|
|
|
inline float _normalizeAngle(float angle);
|
2025-11-03 02:38:43 +08:00
|
|
|
inline float _electricalAngle();
|
2025-11-23 18:31:47 +08:00
|
|
|
|
2025-10-30 20:20:14 +08:00
|
|
|
#endif
|