Skip to content

Commit e38a194

Browse files
committed
moving the case statements to avoid cross initialization
1 parent b3c7f24 commit e38a194

File tree

1 file changed

+42
-40
lines changed

1 file changed

+42
-40
lines changed

src/BLDCMotor.cpp

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,50 @@ void BLDCMotor::move(float new_target) {
226226
void BLDCMotor::setPhaseVoltage(float Uq, float angle_el) {
227227

228228
const bool centered = true;
229+
int sector;
229230

230231
switch (foc_modulation)
231232
{
233+
case FOCModulationType::Trapezoid_120 :
234+
// see https://www.youtube.com/watch?v=InzXA7mWBWE Slide 5
235+
static int trap_120_map[6][3] = {
236+
{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
237+
};
238+
// static int trap_120_state = 0;
239+
sector = 6 * (_normalizeAngle(angle_el + PI/6.0 + zero_electric_angle) / _2PI); // adding PI/6 to align with other modes
240+
241+
Ua = Uq + trap_120_map[sector][0] * Uq;
242+
Ub = Uq + trap_120_map[sector][1] * Uq;
243+
Uc = Uq + trap_120_map[sector][2] * Uq;
244+
245+
if (centered) {
246+
Ua += (voltage_power_supply)/2 -Uq;
247+
Ub += (voltage_power_supply)/2 -Uq;
248+
Uc += (voltage_power_supply)/2 -Uq;
249+
}
250+
break;
251+
252+
case FOCModulationType::Trapezoid_150 :
253+
// see https://www.youtube.com/watch?v=InzXA7mWBWE Slide 8
254+
static int trap_150_map[12][3] = {
255+
{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
256+
};
257+
// static int trap_150_state = 0;
258+
sector = 12 * (_normalizeAngle(angle_el + PI/6.0 + zero_electric_angle) / _2PI); // adding PI/6 to align with other modes
259+
260+
Ua = Uq + trap_150_map[sector][0] * Uq;
261+
Ub = Uq + trap_150_map[sector][1] * Uq;
262+
Uc = Uq + trap_150_map[sector][2] * Uq;
263+
264+
//center
265+
if (centered) {
266+
Ua += (voltage_power_supply)/2 -Uq;
267+
Ub += (voltage_power_supply)/2 -Uq;
268+
Uc += (voltage_power_supply)/2 -Uq;
269+
}
270+
271+
break;
272+
232273
case FOCModulationType::SinePWM :
233274
// Sinusoidal PWM modulation
234275
// Inverse Park + Clarke transformation
@@ -268,7 +309,7 @@ void BLDCMotor::setPhaseVoltage(float Uq, float angle_el) {
268309
angle_el = _normalizeAngle(angle_el + zero_electric_angle + _PI_2);
269310

270311
// find the sector we are in currently
271-
int sector = floor(angle_el / _PI_3) + 1;
312+
sector = floor(angle_el / _PI_3) + 1;
272313
// calculate the duty cycles
273314
float T1 = _SQRT3*_sin(sector*_PI_3 - angle_el) * Uq/driver->voltage_power_supply;
274315
float T2 = _SQRT3*_sin(angle_el - (sector-1.0)*_PI_3) * Uq/driver->voltage_power_supply;
@@ -324,45 +365,6 @@ void BLDCMotor::setPhaseVoltage(float Uq, float angle_el) {
324365
Uc = Tc*driver->voltage_power_supply;
325366
break;
326367

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;
366368
}
367369

368370
// set the voltages in driver

0 commit comments

Comments
 (0)