Skip to content

Commit 5ac6a7d

Browse files
committed
Merge branch 'runger1101001-dev' into dev
2 parents 1bbf193 + fb417ab commit 5ac6a7d

File tree

17 files changed

+129
-45
lines changed

17 files changed

+129
-45
lines changed

src/BLDCMotor.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -453,17 +453,17 @@ void BLDCMotor::setPhaseVoltage(float Uq, float Ud, float angle_el) {
453453
Ua= center;
454454
Ub = trap_120_map[sector][1] * Uq + center;
455455
Uc = trap_120_map[sector][2] * Uq + center;
456-
driver->setPhaseState(_HIGH_IMPEDANCE, _ACTIVE, _ACTIVE); // disable phase if possible
456+
driver->setPhaseState(PhaseState::PHASE_OFF, PhaseState::PHASE_ON, PhaseState::PHASE_ON); // disable phase if possible
457457
}else if(trap_120_map[sector][1] == _HIGH_IMPEDANCE){
458458
Ua = trap_120_map[sector][0] * Uq + center;
459459
Ub = center;
460460
Uc = trap_120_map[sector][2] * Uq + center;
461-
driver->setPhaseState(_ACTIVE, _HIGH_IMPEDANCE, _ACTIVE);// disable phase if possible
461+
driver->setPhaseState(PhaseState::PHASE_ON, PhaseState::PHASE_OFF, PhaseState::PHASE_ON);// disable phase if possible
462462
}else{
463463
Ua = trap_120_map[sector][0] * Uq + center;
464464
Ub = trap_120_map[sector][1] * Uq + center;
465465
Uc = center;
466-
driver->setPhaseState(_ACTIVE,_ACTIVE, _HIGH_IMPEDANCE);// disable phase if possible
466+
driver->setPhaseState(PhaseState::PHASE_ON, PhaseState::PHASE_ON, PhaseState::PHASE_OFF);// disable phase if possible
467467
}
468468

469469
break;
@@ -484,17 +484,17 @@ void BLDCMotor::setPhaseVoltage(float Uq, float Ud, float angle_el) {
484484
Ua= center;
485485
Ub = trap_150_map[sector][1] * Uq + center;
486486
Uc = trap_150_map[sector][2] * Uq + center;
487-
driver->setPhaseState(_HIGH_IMPEDANCE, _ACTIVE, _ACTIVE); // disable phase if possible
487+
driver->setPhaseState(PhaseState::PHASE_OFF, PhaseState::PHASE_ON, PhaseState::PHASE_ON); // disable phase if possible
488488
}else if(trap_150_map[sector][1] == _HIGH_IMPEDANCE){
489489
Ua = trap_150_map[sector][0] * Uq + center;
490490
Ub = center;
491491
Uc = trap_150_map[sector][2] * Uq + center;
492-
driver->setPhaseState(_ACTIVE, _HIGH_IMPEDANCE, _ACTIVE);// disable phase if possible
492+
driver->setPhaseState(PhaseState::PHASE_ON, PhaseState::PHASE_OFF, PhaseState::PHASE_ON);// disable phase if possible
493493
}else{
494494
Ua = trap_150_map[sector][0] * Uq + center;
495495
Ub = trap_150_map[sector][1] * Uq + center;
496496
Uc = center;
497-
driver->setPhaseState(_ACTIVE, _ACTIVE, _HIGH_IMPEDANCE);// disable phase if possible
497+
driver->setPhaseState(PhaseState::PHASE_ON, PhaseState::PHASE_ON, PhaseState::PHASE_OFF);// disable phase if possible
498498
}
499499

500500
break;

src/common/base_classes/BLDCDriver.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33

44
#include "Arduino.h"
55

6+
7+
enum PhaseState : uint8_t {
8+
PHASE_OFF = 0, // both sides of the phase are off
9+
PHASE_ON = 1, // both sides of the phase are driven with PWM, dead time is applied in 6-PWM mode
10+
PHASE_HI = 2, // only the high side of the phase is driven with PWM (6-PWM mode only)
11+
PHASE_LO = 3, // only the low side of the phase is driven with PWM (6-PWM mode only)
12+
};
13+
14+
615
class BLDCDriver{
716
public:
817

@@ -41,7 +50,7 @@ class BLDCDriver{
4150
* @param sb - phase B state : active / disabled ( high impedance )
4251
* @param sa - phase C state : active / disabled ( high impedance )
4352
*/
44-
virtual void setPhaseState(int sa, int sb, int sc) = 0;
53+
virtual void setPhaseState(PhaseState sa, PhaseState sb, PhaseState sc) = 0;
4554
};
4655

4756
#endif

src/current_sense/InlineCurrentSense.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ class InlineCurrentSense: public CurrentSense{
3939
// LowPassFilter lpf_b{DEF_LPF_PER_PHASE_CURRENT_SENSE_Tf}; //!< current B low pass filter
4040
// LowPassFilter lpf_c{DEF_LPF_PER_PHASE_CURRENT_SENSE_Tf}; //!< current C low pass filter
4141

42+
float offset_ia; //!< zero current A voltage value (center of the adc reading)
43+
float offset_ib; //!< zero current B voltage value (center of the adc reading)
44+
float offset_ic; //!< zero current C voltage value (center of the adc reading)
45+
4246
private:
4347

4448
// hardware variables
@@ -55,9 +59,6 @@ class InlineCurrentSense: public CurrentSense{
5559
* Function finding zero offsets of the ADC
5660
*/
5761
void calibrateOffsets();
58-
float offset_ia; //!< zero current A voltage value (center of the adc reading)
59-
float offset_ib; //!< zero current B voltage value (center of the adc reading)
60-
float offset_ic; //!< zero current C voltage value (center of the adc reading)
6162

6263
};
6364

src/drivers/BLDCDriver3PWM.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ int BLDCDriver3PWM::init() {
6464

6565

6666
// Set voltage to the pwm pin
67-
void BLDCDriver3PWM::setPhaseState(int sa, int sb, int sc) {
67+
void BLDCDriver3PWM::setPhaseState(PhaseState sa, PhaseState sb, PhaseState sc) {
6868
// disable if needed
6969
if( _isset(enableA_pin) && _isset(enableB_pin) && _isset(enableC_pin) ){
70-
digitalWrite(enableA_pin, sa == _HIGH_IMPEDANCE ? LOW : HIGH);
71-
digitalWrite(enableB_pin, sb == _HIGH_IMPEDANCE ? LOW : HIGH);
72-
digitalWrite(enableC_pin, sc == _HIGH_IMPEDANCE ? LOW : HIGH);
70+
digitalWrite(enableA_pin, sa == PhaseState::PHASE_ON ? enable_active_high:!enable_active_high);
71+
digitalWrite(enableB_pin, sb == PhaseState::PHASE_ON ? enable_active_high:!enable_active_high);
72+
digitalWrite(enableC_pin, sc == PhaseState::PHASE_ON ? enable_active_high:!enable_active_high);
7373
}
7474
}
7575

src/drivers/BLDCDriver3PWM.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class BLDCDriver3PWM: public BLDCDriver
5656
* @param sb - phase B state : active / disabled ( high impedance )
5757
* @param sa - phase C state : active / disabled ( high impedance )
5858
*/
59-
virtual void setPhaseState(int sa, int sb, int sc) override;
59+
virtual void setPhaseState(PhaseState sa, PhaseState sb, PhaseState sc) override;
6060
private:
6161
};
6262

src/drivers/BLDCDriver6PWM.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ BLDCDriver6PWM::BLDCDriver6PWM(int phA_h,int phA_l,int phB_h,int phB_l,int phC_h
2626
void BLDCDriver6PWM::enable(){
2727
// enable_pin the driver - if enable_pin pin available
2828
if ( _isset(enable_pin) ) digitalWrite(enable_pin, enable_active_high);
29+
// set phase state enabled
30+
setPhaseState(PhaseState::PHASE_ON, PhaseState::PHASE_ON, PhaseState::PHASE_ON);
2931
// set zero to PWM
3032
setPwm(0, 0, 0);
3133
}
3234

3335
// disable motor driver
3436
void BLDCDriver6PWM::disable()
3537
{
38+
// set phase state to disabled
39+
setPhaseState(PhaseState::PHASE_OFF, PhaseState::PHASE_OFF, PhaseState::PHASE_OFF);
3640
// set zero to PWM
3741
setPwm(0, 0, 0);
3842
// disable the driver - if enable_pin pin available
@@ -56,6 +60,14 @@ int BLDCDriver6PWM::init() {
5660
// sanity check for the voltage limit configuration
5761
if( !_isset(voltage_limit) || voltage_limit > voltage_power_supply) voltage_limit = voltage_power_supply;
5862

63+
// set phase state to disabled
64+
phase_state[0] = PhaseState::PHASE_OFF;
65+
phase_state[1] = PhaseState::PHASE_OFF;
66+
phase_state[2] = PhaseState::PHASE_OFF;
67+
68+
// set zero to PWM
69+
dc_a = dc_b = dc_c = 0;
70+
5971
// configure 6pwm
6072
// hardware specific function - depending on driver and mcu
6173
params = _configure6PWM(pwm_frequency, dead_zone, pwmA_h,pwmA_l, pwmB_h,pwmB_l, pwmC_h,pwmC_l);
@@ -77,14 +89,15 @@ void BLDCDriver6PWM::setPwm(float Ua, float Ub, float Uc) {
7789
dc_c = _constrain(Uc / voltage_power_supply, 0.0f , 1.0f );
7890
// hardware specific writing
7991
// hardware specific function - depending on driver and mcu
80-
_writeDutyCycle6PWM(dc_a, dc_b, dc_c, params);
92+
_writeDutyCycle6PWM(dc_a, dc_b, dc_c, phase_state, params);
8193
}
8294

8395

84-
// Set voltage to the pwm pin
85-
void BLDCDriver6PWM::setPhaseState(int sa, int sb, int sc) {
86-
_UNUSED(sa);
87-
_UNUSED(sb);
88-
_UNUSED(sc);
89-
// TODO implement disabling
96+
// Set the phase state
97+
// actually changing the state is only done on the next call to setPwm, and depends
98+
// on the hardware capabilities of the driver and MCU.
99+
void BLDCDriver6PWM::setPhaseState(PhaseState sa, PhaseState sb, PhaseState sc) {
100+
phase_state[0] = sa;
101+
phase_state[1] = sb;
102+
phase_state[2] = sc;
90103
}

src/drivers/BLDCDriver6PWM.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class BLDCDriver6PWM: public BLDCDriver
4141

4242
float dead_zone; //!< a percentage of dead-time(zone) (both high and low side in low) for each pwm cycle [0,1]
4343

44+
PhaseState phase_state[3]; //!< phase state (active / disabled)
45+
46+
4447
/**
4548
* Set phase voltages to the harware
4649
*
@@ -57,7 +60,7 @@ class BLDCDriver6PWM: public BLDCDriver
5760
* @param sb - phase B state : active / disabled ( high impedance )
5861
* @param sa - phase C state : active / disabled ( high impedance )
5962
*/
60-
virtual void setPhaseState(int sa, int sb, int sc) override;
63+
virtual void setPhaseState(PhaseState sa, PhaseState sb, PhaseState sc) override;
6164

6265
private:
6366

src/drivers/hardware_api.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "../common/foc_utils.h"
55
#include "../common/time_utils.h"
66
#include "../communication/SimpleFOCDebug.h"
7+
#include "../common/base_classes/BLDCDriver.h"
78

89

910
// these defines determine the polarity of the PWM output. Normally, the polarity is active-high,
@@ -40,6 +41,7 @@ typedef struct GenericDriverParams {
4041
float dead_zone;
4142
} GenericDriverParams;
4243

44+
4345
/**
4446
* Configuring PWM frequency, resolution and alignment
4547
* - Stepper driver - 2PWM setting
@@ -168,9 +170,10 @@ void _writeDutyCycle4PWM(float dc_1a, float dc_1b, float dc_2a, float dc_2b, vo
168170
* @param dc_a duty cycle phase A [0, 1]
169171
* @param dc_b duty cycle phase B [0, 1]
170172
* @param dc_c duty cycle phase C [0, 1]
173+
* @param phase_state pointer to PhaseState[3] array
171174
* @param params the driver parameters
172175
*
173176
*/
174-
void _writeDutyCycle6PWM(float dc_a, float dc_b, float dc_c, void* params);
177+
void _writeDutyCycle6PWM(float dc_a, float dc_b, float dc_c, PhaseState *phase_state, void* params);
175178

176179
#endif

src/drivers/hardware_specific/atmega/atmega2560_mcu.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,12 @@ void _setPwmPair(int pinH, int pinL, float val, int dead_time)
213213
// - BLDC driver - 6PWM setting
214214
// - hardware specific
215215
// supports Arudino/ATmega328
216-
void _writeDutyCycle6PWM(float dc_a, float dc_b, float dc_c, void* params){
216+
void _writeDutyCycle6PWM(float dc_a, float dc_b, float dc_c, PhaseState *phase_state, void* params){
217217
_setPwmPair(((GenericDriverParams*)params)->pins[0], ((GenericDriverParams*)params)->pins[1], dc_a*255.0, ((GenericDriverParams*)params)->dead_zone*255.0);
218218
_setPwmPair(((GenericDriverParams*)params)->pins[2], ((GenericDriverParams*)params)->pins[3], dc_b*255.0, ((GenericDriverParams*)params)->dead_zone*255.0);
219219
_setPwmPair(((GenericDriverParams*)params)->pins[4], ((GenericDriverParams*)params)->pins[5], dc_c*255.0, ((GenericDriverParams*)params)->dead_zone*255.0);
220+
221+
_UNUSED(phase_state);
220222
}
221223

222224
#endif

src/drivers/hardware_specific/atmega/atmega328_mcu.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,12 @@ void _setPwmPair(int pinH, int pinL, float val, int dead_time)
190190
// - BLDC driver - 6PWM setting
191191
// - hardware specific
192192
// supports Arudino/ATmega328
193-
void _writeDutyCycle6PWM(float dc_a, float dc_b, float dc_c, void* params){
193+
void _writeDutyCycle6PWM(float dc_a, float dc_b, float dc_c, PhaseState *phase_state, void* params){
194194
_setPwmPair(((GenericDriverParams*)params)->pins[0], ((GenericDriverParams*)params)->pins[1], dc_a*255.0, ((GenericDriverParams*)params)->dead_zone*255.0);
195195
_setPwmPair(((GenericDriverParams*)params)->pins[2], ((GenericDriverParams*)params)->pins[3], dc_b*255.0, ((GenericDriverParams*)params)->dead_zone*255.0);
196196
_setPwmPair(((GenericDriverParams*)params)->pins[4], ((GenericDriverParams*)params)->pins[5], dc_c*255.0, ((GenericDriverParams*)params)->dead_zone*255.0);
197+
198+
_UNUSED(phase_state);
197199
}
198200

199201
#endif

0 commit comments

Comments
 (0)