motor.foc_modulation = FOCModulationType::SinePWM; // default // or motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
// 使用FOC将Uq设置到电机的最佳角度 void BLDCMotor::setPhaseVoltage(float Uq, float angle_el) { // 正弦PWM调制 // 逆派克+克拉克变换 // 角度在0和360°之间的归一化 // 仅当使用_sin和_cos近似函数时才需要 angle_el = normalizeAngle(angle_el + zero_electric_angle); // 逆派克变换 Ualpha = -_sin(angle_el) * Uq; // -sin(angle) * Uq; Ubeta = _cos(angle_el) * Uq; // cos(angle) * Uq; // 你克拉克变换 Ua = Ualpha + voltage_power_supply/2; Ub = -0.5 * Ualpha + _SQRT3_2 * Ubeta + voltage_power_supply/2; Uc = -0.5 * Ualpha - _SQRT3_2 * Ubeta + voltage_power_supply/2; // 给硬件设定电压 setPwm(Ua, Ub, Uc); }
Sector | T a | T b | T c |
---|---|---|---|
1 | T 1 + T 2 + T 0 /2 | T 2 + T 0 /2 | T 0 /2 |
2 | T 1 + T 0 /2 | T 1 + T 2 + T 0 /2 | T 0 /2 |
3 | T 0 /2 | T 1 + T 2 + T 0 /2 | T 2 + T 0 /2 |
4 | T 0 /2 | T 1 + T 0 /2 | T 1 + T 2 + T 0 /2 |
5 | T 2 + T 0 /2 | T 0 /2 | T 1 + T 2 + T 0 /2 |
6 | T 1 + T 2 + T 0 /2 | T 0 /2 | T 1 + T 0 /2 |
// 使用FOC将Uq设置到电机的最佳角度 void BLDCMotor::setPhaseVoltage(float Uq, float angle_el) { // 解释SpaceVectorModulation (SVPWM)算法的视频如下 // https://www.youtube.com/watch?v=QMSWUMEAejg // 如果负电压的变化与相相反 // 角度 +180° if(Uq < 0) angle_el += _PI; Uq = abs(Uq); // 角度归一化,在0和360°之间 // 仅当使用_sin和_cos近似函数时才需要 angle_el = normalizeAngle(angle_el + zero_electric_angle + _PI_2); // 找到我们目前所在的象限 int sector = floor(angle_el / _PI_3) + 1; // 计算占空比 float T1 = _SQRT3*_sin(sector*_PI_3 - angle_el) * Uq/voltage_power_supply; float T2 = _SQRT3*_sin(angle_el - (sector-1.0)*_PI_3) * Uq/voltage_power_supply; // 两个版本 // 以电压电源为中心/2 float T0 = 1 - T1 - T2; // 低电源电压,拉到0 //float T0 = 0; // 计算占空比(时间) float Ta,Tb,Tc; switch(sector){ case 1: Ta = T1 + T2 + T0/2; Tb = T2 + T0/2; Tc = T0/2; break; case 2: Ta = T1 + T0/2; Tb = T1 + T2 + T0/2; Tc = T0/2; break; case 3: Ta = T0/2; Tb = T1 + T2 + T0/2; Tc = T2 + T0/2; break; case 4: Ta = T0/2; Tb = T1+ T0/2; Tc = T1 + T2 + T0/2; break; case 5: Ta = T2 + T0/2; Tb = T0/2; Tc = T1 + T2 + T0/2; break; case 6: Ta = T1 + T2 + T0/2; Tb = T0/2; Tc = T1 + T0/2; break; default: // 可能的错误状态 Ta = 0; Tb = 0; Tc = 0; } // 计算相电压和中心 Ua = Ta*voltage_power_supply; Ub = Tb*voltage_power_supply; Uc = Tc*voltage_power_supply; break; } // 设置硬件中的电压 setPwm(Ua, Ub, Uc); }
// 电源电压 motor.voltage_power_supply = 12;