Files
Motor_v2/Robot_V2/Algorithm/DengFOC.cpp
T

207 lines
5.4 KiB
C++
Raw Normal View History

2025-10-16 21:43:34 +08:00
#include "DengFOC.h"
2025-11-06 02:07:15 +08:00
#include "Can.h"
2025-10-30 20:20:14 +08:00
#include "Filter.h"
2025-10-16 21:43:34 +08:00
#include "Timer.h"
2025-10-30 20:20:14 +08:00
#include "adc.h"
#include "ang_sensor.h"
#include "initial.h"
2025-10-30 20:20:14 +08:00
#include "pid.h"
2025-10-16 21:43:34 +08:00
#include <cmath>
#include <gd32c10x.h>
2025-10-16 21:43:34 +08:00
#include <stdint.h>
constexpr double PI = 3.1415926535897932384626433832795f;
2025-11-06 02:07:15 +08:00
constexpr float ANGLE_FACTOR = 25.0f / 360.0f * 2.0f * 3.14159265358979323846f;
2025-10-30 20:20:14 +08:00
constexpr float _1_SQRT3 = 0.57735026919f;
constexpr float _2_SQRT3 = 1.15470053838f;
2025-10-16 21:43:34 +08:00
constexpr float _3PI_2 = 4.71238898038f;
constexpr u8 PP = 11;
2025-11-06 02:07:15 +08:00
constexpr float DIR = 1.0;
constexpr float sqrt3 = 1.73205080757f; // sqrt(3)的预计算值
2025-10-30 20:20:14 +08:00
constexpr float zero_electric_angle = 0.0f;
extern sampler sampler;
extern angle_sensor KTH7823;
2025-11-06 02:07:15 +08:00
extern CAN_transmiter CAN0_transmiter;
2025-10-30 20:20:14 +08:00
CurrentKalman curU_kalman(0.0f, 0.0026f, 4.8f, 0.0001f, 1e-6f, 0.02f);
CurrentKalman curV_kalman(0.0f, 0.0026f, 4.8f, 0.0001f, 1e-6f, 0.02f);
CurrentKalman curW_kalman(0.0f, 0.0026f, 4.8f, 0.0001f, 1e-6f, 0.02f);
2025-10-16 21:43:34 +08:00
// 初始变量及函数定义
#define _constrain(amt, low, high) \
((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt)))
#define constrain(value, min, max, target_min, target_max) \
((value - min) / (max - min) * (target_max - target_min))
/**
* @brief 归一化角度
*
* @param angle
* @return float
*/
inline float _normalizeAngle(float angle) {
2025-10-16 21:43:34 +08:00
float a = fmod(angle, 2 * PI);
// 取余运算可以用于归一化,列出特殊值例子算便知
return a >= 0 ? a : (a + 2 * PI);
}
/**
* @brief 电机控制类构造函数
*
*/
FOC_Controlroller::FOC_Controlroller() { ; }
/**
* @brief 设置电机输出,使用优化的SVPWM算法
*
* @param Uq: q轴电压
* @param Ud: d轴电压
* @param angle_el: 电机轴角度
*/
inline void FOC_Controlroller::setPhaseVoltage(float Uq, float Ud) {
// 计算电气角度
float angle_el = _normalizeAngle(shaft_angle * PP);
2025-11-06 02:07:15 +08:00
// 帕克逆变换 (直接计算,避免函数调用开销)
const float sin_el = sin(angle_el);
const float cos_el = cos(angle_el);
Ualpha = -Uq * sin_el;
Ubeta = Uq * cos_el;
// 克拉克逆变换 (预计算常量值)
Ua = Ualpha + v_half;
Ub = (sqrt3 * Ubeta - Ualpha) * 0.5f + v_half;
Uc = (-Ualpha - sqrt3 * Ubeta) * 0.5f + v_half;
2025-10-16 21:43:34 +08:00
// 计算占空比
float dc_a = _constrain(Ua / v_safe, 0.0f, 1.0f);
float dc_b = _constrain(Ub / v_safe, 0.0f, 1.0f);
float dc_c = _constrain(Uc / v_safe, 0.0f, 1.0f);
// 设置PWM输出
dc_a = dc_a * limit_voltage / v_safe;
dc_b = dc_b * limit_voltage / v_safe;
dc_c = dc_c * limit_voltage / v_safe;
2025-10-16 21:43:34 +08:00
Set_3Pwm(dc_a, dc_b, dc_c);
// std::printf("PWM:%f,%f,%f\n", dc_a, dc_b, dc_c);
}
2025-11-06 02:07:15 +08:00
/**
* @brief 电机关机
*
*/
void FOC_Controlroller::shut_down() {
thermal_uncontrol = 0;
is_shut_down = 1;
2025-11-06 02:07:15 +08:00
gpio_bit_reset(GPIOB, GPIO_PIN_12);
}
/**
* @brief 电机恢复
*
*/
void FOC_Controlroller::resume() {
thermal_uncontrol = false;
voltage_error = false;
is_shut_down = false;
2025-11-06 02:07:15 +08:00
gpio_bit_set(GPIOB, GPIO_PIN_12);
}
/**
* @brief 更新电源电压
*
*/
void FOC_Controlroller::update_power(void) {
voltage_power_supply = sampler.get_vbus_voltage();
if (voltage_power_supply < 36.0f and voltage_power_supply > 8.0f) {
voltage_error = false;
v_half = voltage_power_supply * 0.5f;
v_safe = voltage_power_supply * 0.95f;
}
else {
voltage_error = true;
}
2025-11-06 02:07:15 +08:00
}
2025-10-16 21:43:34 +08:00
/**
* @brief 速度开环控制
*
* @param target_velocity: 目标速度
* @return float: q轴电压
*/
float FOC_Controlroller::velocity_open_loop() {
2025-10-16 21:43:34 +08:00
// 通过时间间隔和目标速度计算需要转动的机械角度,存储在shaft_angle中,在此之前,还需要对轴角度归一化
// 预计算常量 1e-4 = 100微秒控制周期
2025-11-06 02:07:15 +08:00
shaft_angle =
_normalizeAngle(shaft_angle + target_velocity * ANGLE_FACTOR * 1e-4f);
// 因为在开环控制中,Ud始终为0,只控制Uq
2025-10-30 20:20:14 +08:00
const float Uq = voltage_power_supply / 2.0f;
setPhaseVoltage(Uq, 0.0f);
return limit_voltage;
2025-10-16 21:43:34 +08:00
}
/**
* @brief 电机初始化
*
* @param _PP: 电机极队数
*/
// void FOC_Controlroller::DFOC_alignSensor(int _PP) {
// u8 PP = _PP;
// setPhaseVoltage(3, 0, _3PI_2);
// delay_1ms(3000);
// zero_electric_angle = _electricalAngle();
// setPhaseVoltage(0, 0, _3PI_2);
// std::printf("0电角度:%f", zero_electric_angle);
// }
2025-11-06 02:07:15 +08:00
/**
* @brief 角度闭环控制
*
* @param target_angle: 目标角度
* @return float: 当前角度
2025-11-06 02:07:15 +08:00
*/
float FOC_Controlroller::angle_control(float target_angle) {
// PID 控制计算
float Uq = angle_loop(target_angle - rt_angle);
// 获取电机轴角度
shaft_angle = KTH7823.getcurrent_sensorangle() * DEG2RAD;
2025-11-06 02:07:15 +08:00
// 设置相电压
setPhaseVoltage(Uq, 0.0f);
return shaft_angle;
}
/**
* @brief 角度闭环控制
*
* @param target_angle: 目标角度
* @return float: 当前角度
*/
float FOC_Controlroller::angle_control_NPID(float target_angle) {
static float Kp = 20.0f;
// 控制力矩计算
float Uq = _constrain(Kp * (target_angle - rt_angle), -voltage_power_supply,
voltage_power_supply);
// 获取电机轴角度
shaft_angle = KTH7823.getcurrent_sensorangle() * DEG2RAD;
// 设置相电压
setPhaseVoltage(Uq, 0.0f);
2025-11-06 02:07:15 +08:00
return shaft_angle;
2025-10-30 20:20:14 +08:00
}
/**
2025-11-06 02:07:15 +08:00
* @brief 速度闭环控制
*
* @param target_velocity: 目标速度
* @return float: 当前速度
*/
2025-11-06 02:07:15 +08:00
float FOC_Controlroller::velocity_control(float target_velocity) {
// PID 控制计算
float Uq = vel_loop(target_velocity - rt_velocity);
// 获取电机轴角度
shaft_angle = KTH7823.getcurrent_sensorangle() * DEG2RAD;
2025-11-06 02:07:15 +08:00
// 设置相电压
setPhaseVoltage(Uq, 0.0f);
2025-11-06 02:07:15 +08:00
return rt_velocity;
2025-11-02 01:54:01 +08:00
}