Skip to content

Commit 3a9caf7

Browse files
committed
adding trap 120 and trap 150 support
1 parent d430f69 commit 3a9caf7

File tree

1 file changed

+55
-4
lines changed

1 file changed

+55
-4
lines changed

src/BLDCMotor.cpp

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ void BLDCMotor::move(float new_target) {
224224
// regular sin + cos ~300us (no memory usaage)
225225
// approx _sin + _cos ~110us (400Byte ~ 20% of memory)
226226
void BLDCMotor::setPhaseVoltage(float Uq, float angle_el) {
227+
228+
const bool centered = true;
229+
227230
switch (foc_modulation)
228231
{
229232
case FOCModulationType::SinePWM :
@@ -241,6 +244,14 @@ void BLDCMotor::setPhaseVoltage(float Uq, float angle_el) {
241244
Ua = Ualpha + driver->voltage_power_supply/2;
242245
Ub = -0.5 * Ualpha + _SQRT3_2 * Ubeta + driver->voltage_power_supply/2;
243246
Uc = -0.5 * Ualpha - _SQRT3_2 * Ubeta + driver->voltage_power_supply/2;
247+
248+
if (!centered) {
249+
float Umin = min(Ua, min(Ub, Uc));
250+
Ua -=Umin;
251+
Ub -=Umin;
252+
Uc -=Umin;
253+
}
254+
244255
break;
245256

246257
case FOCModulationType::SpaceVectorPWM :
@@ -262,10 +273,10 @@ void BLDCMotor::setPhaseVoltage(float Uq, float angle_el) {
262273
float T1 = _SQRT3*_sin(sector*_PI_3 - angle_el) * Uq/driver->voltage_power_supply;
263274
float T2 = _SQRT3*_sin(angle_el - (sector-1.0)*_PI_3) * Uq/driver->voltage_power_supply;
264275
// two versions possible
265-
// centered around driver->voltage_power_supply/2
266-
float T0 = 1 - T1 - T2;
267-
// pulled to 0 - better for low power supply voltage
268-
//float T0 = 0;
276+
float T0 = 0; // pulled to 0 - better for low power supply voltage
277+
if (centered) {
278+
T0 = 1 - T1 - T2; //centered around driver->voltage_power_supply/2
279+
}
269280

270281
// calculate the duty cycles(times)
271282
float Ta,Tb,Tc;
@@ -312,6 +323,46 @@ void BLDCMotor::setPhaseVoltage(float Uq, float angle_el) {
312323
Ub = Tb*driver->voltage_power_supply;
313324
Uc = Tc*driver->voltage_power_supply;
314325
break;
326+
327+
case FOCModulationType::Trapezoid_120 :
328+
// see https://www.youtube.com/watch?v=InzXA7mWBWE Slide 5
329+
static int trap_120_map[6][3] = {
330+
{0,1,-1},{-1,1,0},{-1,0,1},{0,-1,1},{1,-1,0},{1,0,-1} // each is 60 degrees with values for 3 phases of 1=positive -1=negative 0=high-z
331+
};
332+
static int trap_120_state = 0;
333+
trap_120_state = 6 * (_normalizeAngle(angle_el + PI/6.0 + zero_electric_angle) / _2PI); // adding PI/6 to align with other modes
334+
335+
Ua = Uq + trap_120_map[trap_120_state][0] * Uq;
336+
Ub = Uq + trap_120_map[trap_120_state][1] * Uq;
337+
Uc = Uq + trap_120_map[trap_120_state][2] * Uq;
338+
339+
if (centered) {
340+
Ua += (voltage_power_supply)/2 -Uq;
341+
Ub += (voltage_power_supply)/2 -Uq;
342+
Uc += (voltage_power_supply)/2 -Uq;
343+
}
344+
break;
345+
346+
case FOCModulationType::Trapezoid_150 :
347+
// see https://www.youtube.com/watch?v=InzXA7mWBWE Slide 8
348+
static int trap_150_map[12][3] = {
349+
{0,1,-1},{-1,1,-1},{-1,1,0},{-1,1,1},{-1,0,1},{-1,-1,1},{0,-1,1},{1,-1,1},{1,-1,0},{1,-1,-1},{1,0,-1},{1,1,-1} // each is 30 degrees with values for 3 phases of 1=positive -1=negative 0=high-z
350+
};
351+
static int trap_150_state = 0;
352+
trap_150_state = 12 * (_normalizeAngle(angle_el + PI/6.0 + zero_electric_angle) / _2PI); // adding PI/6 to align with other modes
353+
354+
Ua = Uq + trap_150_map[trap_150_state][0] * Uq;
355+
Ub = Uq + trap_150_map[trap_150_state][1] * Uq;
356+
Uc = Uq + trap_150_map[trap_150_state][2] * Uq;
357+
358+
//center
359+
if (centered) {
360+
Ua += (voltage_power_supply)/2 -Uq;
361+
Ub += (voltage_power_supply)/2 -Uq;
362+
Uc += (voltage_power_supply)/2 -Uq;
363+
}
364+
365+
break;
315366
}
316367

317368
// set the voltages in driver

0 commit comments

Comments
 (0)