Skip to content

Commit a523500

Browse files
committed
Merge branch 'dev'
2 parents 3e577d3 + 64937c0 commit a523500

File tree

6 files changed

+88
-36
lines changed

6 files changed

+88
-36
lines changed

src/drivers/BLDCDriver3PWM.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ int BLDCDriver3PWM::init() {
5656
// Set voltage to the pwm pin
5757
void BLDCDriver3PWM::setPwm(float Ua, float Ub, float Uc) {
5858
// limit the voltage in driver
59-
Ua = _constrain(Ua, -voltage_limit, voltage_limit);
60-
Ub = _constrain(Ub, -voltage_limit, voltage_limit);
61-
Uc = _constrain(Uc, -voltage_limit, voltage_limit);
59+
Ua = _constrain(Ua, 0.0, voltage_limit);
60+
Ub = _constrain(Ub, 0.0, voltage_limit);
61+
Uc = _constrain(Uc, 0.0, voltage_limit);
6262
// calculate duty cycle
6363
// limited in [0,1]
64-
float dc_a = _constrain(Ua / voltage_power_supply, 0 , 1 );
65-
float dc_b = _constrain(Ub / voltage_power_supply, 0 , 1 );
66-
float dc_c = _constrain(Uc / voltage_power_supply, 0 , 1 );
64+
float dc_a = _constrain(Ua / voltage_power_supply, 0.0 , 1.0 );
65+
float dc_b = _constrain(Ub / voltage_power_supply, 0.0 , 1.0 );
66+
float dc_c = _constrain(Uc / voltage_power_supply, 0.0 , 1.0 );
6767
// hardware specific writing
6868
// hardware specific function - depending on driver and mcu
6969
_writeDutyCycle3PWM(dc_a, dc_b, dc_c, pwmA, pwmB, pwmC);

src/drivers/BLDCDriver6PWM.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ int BLDCDriver6PWM::init() {
6464
// Set voltage to the pwm pin
6565
void BLDCDriver6PWM::setPwm(float Ua, float Ub, float Uc) {
6666
// limit the voltage in driver
67-
Ua = _constrain(Ua, -voltage_limit, voltage_limit);
68-
Ub = _constrain(Ub, -voltage_limit, voltage_limit);
69-
Uc = _constrain(Uc, -voltage_limit, voltage_limit);
67+
Ua = _constrain(Ua, 0, voltage_limit);
68+
Ub = _constrain(Ub, 0, voltage_limit);
69+
Uc = _constrain(Uc, 0, voltage_limit);
7070
// calculate duty cycle
7171
// limited in [0,1]
7272
float dc_a = _constrain(Ua / voltage_power_supply, 0.0 , 1.0 );

src/drivers/StepperDriver4PWM.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,18 @@ int StepperDriver4PWM::init() {
6262
void StepperDriver4PWM::setPwm(float Ualpha, float Ubeta) {
6363
float duty_cycle1A(0.0),duty_cycle1B(0.0),duty_cycle2A(0.0),duty_cycle2B(0.0);
6464
// limit the voltage in driver
65-
Ualpha = _constrain(Ualpha,-voltage_limit,voltage_limit);
66-
Ubeta = _constrain(Ubeta,-voltage_limit,voltage_limit);
65+
Ualpha = _constrain(Ualpha, -voltage_limit, voltage_limit);
66+
Ubeta = _constrain(Ubeta, -voltage_limit, voltage_limit);
6767
// hardware specific writing
6868
if( Ualpha > 0 )
69-
duty_cycle1B = _constrain(abs(Ualpha)/voltage_power_supply,0,1);
69+
duty_cycle1B = _constrain(abs(Ualpha)/voltage_power_supply,0.0,1.0);
7070
else
71-
duty_cycle1A = _constrain(abs(Ualpha)/voltage_power_supply,0,1);
71+
duty_cycle1A = _constrain(abs(Ualpha)/voltage_power_supply,0.0,1.0);
7272

7373
if( Ubeta > 0 )
74-
duty_cycle2B = _constrain(abs(Ubeta)/voltage_power_supply,0,1);
74+
duty_cycle2B = _constrain(abs(Ubeta)/voltage_power_supply,0.0,1.0);
7575
else
76-
duty_cycle2A = _constrain(abs(Ubeta)/voltage_power_supply,0,1);
76+
duty_cycle2A = _constrain(abs(Ubeta)/voltage_power_supply,0.0,1.0);
7777
// write to hardware
7878
_writeDutyCycle4PWM(duty_cycle1A, duty_cycle1B, duty_cycle2A, duty_cycle2B, pwm1A, pwm1B, pwm2A, pwm2B);
7979
}

src/drivers/hardware_specific/esp32_mcu.cpp

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010
#define _EMPTY_SLOT -20
1111
#define _TAKEN_SLOT -21
1212

13+
// ABI bus frequency - would be better to take it from somewhere
14+
// but I did nto find a good exposed variable
15+
#define _MCPWM_FREQ 160e6
16+
17+
// preferred pwm resolution default
18+
#define _PWM_RES_DEF 2048
19+
// min resolution
20+
#define _PWM_RES_MIN 1500
21+
// max resolution
22+
#define _PWM_RES_MAX 3000
23+
1324
// structure containing motor slot configuration
1425
// this library supports up to 4 motors
1526
typedef struct {
@@ -73,23 +84,61 @@ bldc_6pwm_motor_slots_t esp32_bldc_6pwm_motor_slots[2] = {
7384
void _configureTimerFrequency(long pwm_frequency, mcpwm_dev_t* mcpwm_num, mcpwm_unit_t mcpwm_unit, float dead_zone = NOT_SET){
7485

7586
mcpwm_config_t pwm_config;
76-
pwm_config.frequency = pwm_frequency; //frequency
7787
pwm_config.counter_mode = MCPWM_UP_DOWN_COUNTER; // Up-down counter (triangle wave)
7888
pwm_config.duty_mode = MCPWM_DUTY_MODE_0; // Active HIGH
7989
mcpwm_init(mcpwm_unit, MCPWM_TIMER_0, &pwm_config); //Configure PWM0A & PWM0B with above settings
8090
mcpwm_init(mcpwm_unit, MCPWM_TIMER_1, &pwm_config); //Configure PWM0A & PWM0B with above settings
8191
mcpwm_init(mcpwm_unit, MCPWM_TIMER_2, &pwm_config); //Configure PWM0A & PWM0B with above settings
82-
92+
8393
if (dead_zone != NOT_SET){
84-
// dead zone is configured in dead_time x 100 nanoseconds
85-
float dead_time = (float)(1e7 / pwm_frequency) * dead_zone;
86-
mcpwm_deadtime_enable(mcpwm_unit, MCPWM_TIMER_0, MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE, dead_time, dead_time);
87-
mcpwm_deadtime_enable(mcpwm_unit, MCPWM_TIMER_1, MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE, dead_time, dead_time);
88-
mcpwm_deadtime_enable(mcpwm_unit, MCPWM_TIMER_2, MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE, dead_time, dead_time);
94+
// dead zone is configured
95+
float dead_time = (float)(_MCPWM_FREQ / (pwm_frequency)) * dead_zone;
96+
mcpwm_deadtime_enable(mcpwm_unit, MCPWM_TIMER_0, MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE, dead_time/2, dead_time/2);
97+
mcpwm_deadtime_enable(mcpwm_unit, MCPWM_TIMER_1, MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE, dead_time/2, dead_time/2);
98+
mcpwm_deadtime_enable(mcpwm_unit, MCPWM_TIMER_2, MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE, dead_time/2, dead_time/2);
8999
}
90-
91100
_delay(100);
92101

102+
mcpwm_stop(mcpwm_unit, MCPWM_TIMER_0);
103+
mcpwm_stop(mcpwm_unit, MCPWM_TIMER_1);
104+
mcpwm_stop(mcpwm_unit, MCPWM_TIMER_2);
105+
106+
// manual configuration due to the lack of config flexibility in mcpwm_init()
107+
mcpwm_num->clk_cfg.prescale = 0;
108+
// calculate prescaler and period
109+
// step 1: calculate the prescaler using the default pwm resolution
110+
// prescaler = bus_freq / (pwm_freq * default_pwm_res) - 1
111+
int prescaler = ceil(_MCPWM_FREQ / _PWM_RES_DEF / 2.0 / pwm_frequency) - 1;
112+
// constrain prescaler
113+
prescaler = _constrain(prescaler, 0, 128);
114+
// now calculate the real resolution timer period necessary (pwm resolution)
115+
// pwm_res = bus_freq / (pwm_freq * (prescaler + 1))
116+
int resolution_corrected = _MCPWM_FREQ / 2.0 / pwm_frequency / (prescaler + 1);
117+
// if pwm resolution too low lower the prescaler
118+
if(resolution_corrected < _PWM_RES_MIN && prescaler > 0 )
119+
resolution_corrected = _MCPWM_FREQ / 2.0 / pwm_frequency / (--prescaler + 1);
120+
resolution_corrected = _constrain(resolution_corrected, _PWM_RES_MIN, _PWM_RES_MAX);
121+
122+
// set prescaler
123+
mcpwm_num->timer[0].period.prescale = prescaler;
124+
mcpwm_num->timer[1].period.prescale = prescaler;
125+
mcpwm_num->timer[2].period.prescale = prescaler;
126+
_delay(1);
127+
//set period
128+
mcpwm_num->timer[0].period.period = resolution_corrected;
129+
mcpwm_num->timer[1].period.period = resolution_corrected;
130+
mcpwm_num->timer[2].period.period = resolution_corrected;
131+
_delay(1);
132+
mcpwm_num->timer[0].period.upmethod = 0;
133+
mcpwm_num->timer[1].period.upmethod = 0;
134+
mcpwm_num->timer[2].period.upmethod = 0;
135+
_delay(1);
136+
//restart the timers
137+
mcpwm_start(mcpwm_unit, MCPWM_TIMER_0);
138+
mcpwm_start(mcpwm_unit, MCPWM_TIMER_1);
139+
mcpwm_start(mcpwm_unit, MCPWM_TIMER_2);
140+
_delay(1);
141+
93142
mcpwm_sync_enable(mcpwm_unit, MCPWM_TIMER_0, MCPWM_SELECT_SYNC_INT0, 0);
94143
mcpwm_sync_enable(mcpwm_unit, MCPWM_TIMER_1, MCPWM_SELECT_SYNC_INT0, 0);
95144
mcpwm_sync_enable(mcpwm_unit, MCPWM_TIMER_2, MCPWM_SELECT_SYNC_INT0, 0);
@@ -106,8 +155,8 @@ void _configureTimerFrequency(long pwm_frequency, mcpwm_dev_t* mcpwm_num, mcpwm
106155
// supports Arudino/ATmega328, STM32 and ESP32
107156
void _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const int pinC) {
108157

109-
if(!pwm_frequency || pwm_frequency == NOT_SET) pwm_frequency = 40000; // default frequency 20khz - centered pwm has twice lower frequency
110-
else pwm_frequency = _constrain(2*pwm_frequency, 0, 100000); // constrain to 50kHz max - centered pwm has twice lower frequency
158+
if(!pwm_frequency || pwm_frequency == NOT_SET) pwm_frequency = 20000; // default frequency 20khz - centered pwm has twice lower frequency
159+
else pwm_frequency = _constrain(pwm_frequency, 0, 40000); // constrain to 40kHz max - centered pwm has twice lower frequency
111160

112161
bldc_3pwm_motor_slots_t m_slot = {};
113162

@@ -149,8 +198,8 @@ void _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const int
149198
// - hardware speciffic
150199
void _configure4PWM(long pwm_frequency,const int pinA, const int pinB, const int pinC, const int pinD) {
151200

152-
if(!pwm_frequency || pwm_frequency == NOT_SET) pwm_frequency = 40000; // default frequency 20khz - centered pwm has twice lower frequency
153-
else pwm_frequency = _constrain(2*pwm_frequency, 0, 100000); // constrain to 50kHz max - centered pwm has twice lower frequency
201+
if(!pwm_frequency || pwm_frequency == NOT_SET) pwm_frequency = 20000; // default frequency 20khz - centered pwm has twice lower frequency
202+
else pwm_frequency = _constrain(pwm_frequency, 0, 40000); // constrain to 50kHz max - centered pwm has twice lower frequency
154203
stepper_motor_slots_t m_slot = {};
155204
// determine which motor are we connecting
156205
// and set the appropriate configuration parameters
@@ -229,8 +278,8 @@ void _writeDutyCycle4PWM(float dc_1a, float dc_1b, float dc_2a, float dc_2b, in
229278
// - hardware specific
230279
int _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, const int pinA_l, const int pinB_h, const int pinB_l, const int pinC_h, const int pinC_l){
231280

232-
if(!pwm_frequency || pwm_frequency == NOT_SET) pwm_frequency = 40000; // default frequency 20khz - centered pwm has twice lower frequency
233-
else pwm_frequency = _constrain(2*pwm_frequency, 0, 60000); // constrain to 30kHz max - centered pwm has twice lower frequency
281+
if(!pwm_frequency || pwm_frequency == NOT_SET) pwm_frequency = 20000; // default frequency 20khz - centered pwm has twice lower frequency
282+
else pwm_frequency = _constrain(pwm_frequency, 0, 40000); // constrain to 40kHz max - centered pwm has twice lower frequency
234283
bldc_6pwm_motor_slots_t m_slot = {};
235284
// determine which motor are we connecting
236285
// and set the appropriate configuration parameters

src/sensors/HallSensor.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ void HallSensor::updateState() {
4545
long new_pulse_timestamp = _micros();
4646
hall_state = C_active + (B_active << 1) + (A_active << 2);
4747
int8_t new_electric_sector = ELECTRIC_SECTORS[hall_state];
48+
static Direction old_direction;
4849
if (new_electric_sector - electric_sector > 3) {
4950
//underflow
5051
direction = static_cast<Direction>(natural_direction * -1);
@@ -57,9 +58,16 @@ void HallSensor::updateState() {
5758
direction = (new_electric_sector > electric_sector)? static_cast<Direction>(natural_direction) : static_cast<Direction>(natural_direction * (-1));
5859
}
5960
electric_sector = new_electric_sector;
60-
pulse_diff = new_pulse_timestamp - pulse_timestamp;
61+
if (direction == old_direction) {
62+
// not oscilating or just changed direction
63+
pulse_diff = new_pulse_timestamp - pulse_timestamp;
64+
} else {
65+
pulse_diff = 0;
66+
}
67+
6168
pulse_timestamp = new_pulse_timestamp;
6269
total_interrupts++;
70+
old_direction = direction;
6371
if (onSectorChange != nullptr) onSectorChange(electric_sector);
6472
}
6573

@@ -86,8 +94,7 @@ float HallSensor::getAngle() {
8694
function using mixed time and frequency measurement technique
8795
*/
8896
float HallSensor::getVelocity(){
89-
90-
if (pulse_diff == 0 || ((_micros() - pulse_timestamp) > STALE_HALL_DATA_MICROS) ) { // last velocity isn't accurate if too old
97+
if (pulse_diff == 0 || ((_micros() - pulse_timestamp) > pulse_diff) ) { // last velocity isn't accurate if too old
9198
return 0;
9299
} else {
93100
return direction * (_2PI / cpr) / (pulse_diff / 1000000.0);

src/sensors/HallSensor.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
#include "../common/foc_utils.h"
77
#include "../common/time_utils.h"
88

9-
#ifndef STALE_HALL_DATA_MICROS
10-
#define STALE_HALL_DATA_MICROS 500000
11-
#endif
12-
139
// seq 1 > 5 > 4 > 6 > 2 > 3 > 1 000 001 010 011 100 101 110 111
1410
const int8_t ELECTRIC_SECTORS[8] = { -1, 0, 4, 5, 2, 1, 3 , -1 };
1511

0 commit comments

Comments
 (0)