Skip to content

Commit c9f641a

Browse files
committed
trap 150 bugfix #214
1 parent ee48ee7 commit c9f641a

File tree

1 file changed

+43
-16
lines changed

1 file changed

+43
-16
lines changed

src/BLDCMotor.cpp

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
11
#include "BLDCMotor.h"
22
#include "./communication/SimpleFOCDebug.h"
33

4+
5+
// see https://www.youtube.com/watch?v=InzXA7mWBWE Slide 5
6+
// each is 60 degrees with values for 3 phases of 1=positive -1=negative 0=high-z
7+
int trap_120_map[6][3] = {
8+
{_HIGH_IMPEDANCE,1,-1},
9+
{-1,1,_HIGH_IMPEDANCE},
10+
{-1,_HIGH_IMPEDANCE,1},
11+
{_HIGH_IMPEDANCE,-1,1},
12+
{1,-1,_HIGH_IMPEDANCE},
13+
{1,_HIGH_IMPEDANCE,-1}
14+
};
15+
16+
// see https://www.youtube.com/watch?v=InzXA7mWBWE Slide 8
17+
// each is 30 degrees with values for 3 phases of 1=positive -1=negative 0=high-z
18+
int trap_150_map[12][3] = {
19+
{_HIGH_IMPEDANCE,1,-1},
20+
{-1,1,-1},
21+
{-1,1,_HIGH_IMPEDANCE},
22+
{-1,1,1},
23+
{-1,_HIGH_IMPEDANCE,1},
24+
{-1,-1,1},
25+
{_HIGH_IMPEDANCE,-1,1},
26+
{1,-1,1},
27+
{1,-1,_HIGH_IMPEDANCE},
28+
{1,-1,-1},
29+
{1,_HIGH_IMPEDANCE,-1},
30+
{1,1,-1}
31+
};
32+
433
// BLDCMotor( int pp , float R)
534
// - pp - pole pair number
635
// - R - motor phase resistance
@@ -427,7 +456,7 @@ void BLDCMotor::move(float new_target) {
427456
// Function implementing Space Vector PWM and Sine PWM algorithms
428457
//
429458
// Function using sine approximation
430-
// regular sin + cos ~300us (no memory usaage)
459+
// regular sin + cos ~300us (no memory usage)
431460
// approx _sin + _cos ~110us (400Byte ~ 20% of memory)
432461
void BLDCMotor::setPhaseVoltage(float Uq, float Ud, float angle_el) {
433462

@@ -439,13 +468,10 @@ void BLDCMotor::setPhaseVoltage(float Uq, float Ud, float angle_el) {
439468
{
440469
case FOCModulationType::Trapezoid_120 :
441470
// see https://www.youtube.com/watch?v=InzXA7mWBWE Slide 5
442-
static int trap_120_map[6][3] = {
443-
{_HIGH_IMPEDANCE,1,-1},{-1,1,_HIGH_IMPEDANCE},{-1,_HIGH_IMPEDANCE,1},{_HIGH_IMPEDANCE,-1,1},{1,-1,_HIGH_IMPEDANCE},{1,_HIGH_IMPEDANCE,-1} // each is 60 degrees with values for 3 phases of 1=positive -1=negative 0=high-z
444-
};
445-
// static int trap_120_state = 0;
471+
// determine the sector
446472
sector = 6 * (_normalizeAngle(angle_el + _PI_6 ) / _2PI); // adding PI/6 to align with other modes
447473
// centering the voltages around either
448-
// modulation_centered == true > driver.volage_limit/2
474+
// modulation_centered == true > driver.voltage_limit/2
449475
// modulation_centered == false > or Adaptable centering, all phases drawn to 0 when Uq=0
450476
center = modulation_centered ? (driver->voltage_limit)/2 : Uq;
451477

@@ -470,13 +496,10 @@ void BLDCMotor::setPhaseVoltage(float Uq, float Ud, float angle_el) {
470496

471497
case FOCModulationType::Trapezoid_150 :
472498
// see https://www.youtube.com/watch?v=InzXA7mWBWE Slide 8
473-
static int trap_150_map[12][3] = {
474-
{_HIGH_IMPEDANCE,1,-1},{-1,1,-1},{-1,1,_HIGH_IMPEDANCE},{-1,1,1},{-1,_HIGH_IMPEDANCE,1},{-1,-1,1},{_HIGH_IMPEDANCE,-1,1},{1,-1,1},{1,-1,_HIGH_IMPEDANCE},{1,-1,-1},{1,_HIGH_IMPEDANCE,-1},{1,1,-1} // each is 30 degrees with values for 3 phases of 1=positive -1=negative 0=high-z
475-
};
476-
// static int trap_150_state = 0;
499+
// determine the sector
477500
sector = 12 * (_normalizeAngle(angle_el + _PI_6 ) / _2PI); // adding PI/6 to align with other modes
478501
// centering the voltages around either
479-
// modulation_centered == true > driver.volage_limit/2
502+
// modulation_centered == true > driver.voltage_limit/2
480503
// modulation_centered == false > or Adaptable centering, all phases drawn to 0 when Uq=0
481504
center = modulation_centered ? (driver->voltage_limit)/2 : Uq;
482505

@@ -489,12 +512,17 @@ void BLDCMotor::setPhaseVoltage(float Uq, float Ud, float angle_el) {
489512
Ua = trap_150_map[sector][0] * Uq + center;
490513
Ub = center;
491514
Uc = trap_150_map[sector][2] * Uq + center;
492-
driver->setPhaseState(PhaseState::PHASE_ON, PhaseState::PHASE_OFF, PhaseState::PHASE_ON);// disable phase if possible
493-
}else{
515+
driver->setPhaseState(PhaseState::PHASE_ON, PhaseState::PHASE_OFF, PhaseState::PHASE_ON); // disable phase if possible
516+
}else if(trap_150_map[sector][2] == _HIGH_IMPEDANCE){
494517
Ua = trap_150_map[sector][0] * Uq + center;
495518
Ub = trap_150_map[sector][1] * Uq + center;
496519
Uc = center;
497-
driver->setPhaseState(PhaseState::PHASE_ON, PhaseState::PHASE_ON, PhaseState::PHASE_OFF);// disable phase if possible
520+
driver->setPhaseState(PhaseState::PHASE_ON, PhaseState::PHASE_ON, PhaseState::PHASE_OFF); // disable phase if possible
521+
}else{
522+
Ua = trap_150_map[sector][0] * Uq + center;
523+
Ub = trap_150_map[sector][1] * Uq + center;
524+
Uc = trap_150_map[sector][2] * Uq + center;
525+
driver->setPhaseState(PhaseState::PHASE_ON, PhaseState::PHASE_ON, PhaseState::PHASE_ON); // enable all phases
498526
}
499527

500528
break;
@@ -525,7 +553,7 @@ void BLDCMotor::setPhaseVoltage(float Uq, float Ud, float angle_el) {
525553
Ub -= Umin;
526554
Uc -= Umin;
527555
}
528-
driver->setPhaseState(PhaseState::PHASE_ON, PhaseState::PHASE_ON, PhaseState::PHASE_ON);
556+
529557
break;
530558

531559
case FOCModulationType::SpaceVectorPWM :
@@ -611,7 +639,6 @@ void BLDCMotor::setPhaseVoltage(float Uq, float Ud, float angle_el) {
611639
Ua = Ta*driver->voltage_limit;
612640
Ub = Tb*driver->voltage_limit;
613641
Uc = Tc*driver->voltage_limit;
614-
driver->setPhaseState(PhaseState::PHASE_ON, PhaseState::PHASE_ON, PhaseState::PHASE_ON);
615642
break;
616643

617644
}

0 commit comments

Comments
 (0)