Skip to content

Commit 7febff8

Browse files
committed
FIX fixed config
1 parent 33c5a19 commit 7febff8

File tree

1 file changed

+54
-23
lines changed

1 file changed

+54
-23
lines changed

src/drivers/hardware_specific/esp32_mcu.cpp

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ stepper_motor_slots_t esp32_stepper_motor_slots[2] = {
6464
// define stepper motor slots array
6565
bldc_6pwm_motor_slots_t esp32_bldc_6pwm_motor_slots[2] = {
6666
{_EMPTY_SLOT, &MCPWM0, MCPWM_UNIT_0, MCPWM_OPR_A, MCPWM_OPR_B, MCPWM0A, MCPWM1A, MCPWM2A, MCPWM0B, MCPWM1B, MCPWM2B}, // 1st motor will be on MCPWM0
67-
{_EMPTY_SLOT, &MCPWM1, MCPWM_UNIT_1, MCPWM_OPR_A, MCPWM_OPR_B, MCPWM0A, MCPWM1A, MCPWM2A, MCPWM0B, MCPWM1B, MCPWM2B}, // 1st motor will be on MCPWM0
67+
{_EMPTY_SLOT, &MCPWM1, MCPWM_UNIT_1, MCPWM_OPR_A, MCPWM_OPR_B, MCPWM0A, MCPWM1A, MCPWM2A, MCPWM0B, MCPWM1B, MCPWM2B}, // 1st motor will be on MCPWM1
6868
};
6969

7070
// configuring high frequency pwm timer
@@ -81,10 +81,6 @@ void _configureTimerFrequency(long pwm_frequency, mcpwm_dev_t* mcpwm_num, mcpwm
8181
mcpwm_init(mcpwm_unit, MCPWM_TIMER_1, &pwm_config); //Configure PWM0A & PWM0B with above settings
8282
mcpwm_init(mcpwm_unit, MCPWM_TIMER_2, &pwm_config); //Configure PWM0A & PWM0B with above settings
8383

84-
mcpwm_deadtime_enable(mcpwm_unit, MCPWM_TIMER_0, MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE, 40, 40);
85-
mcpwm_deadtime_enable(mcpwm_unit, MCPWM_TIMER_1, MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE, 40, 40);
86-
mcpwm_deadtime_enable(mcpwm_unit, MCPWM_TIMER_2, MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE, 40, 40);
87-
8884
_delay(100);
8985

9086
mcpwm_stop(mcpwm_unit, MCPWM_TIMER_0);
@@ -287,34 +283,69 @@ int _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, const
287283
mcpwm_gpio_init(m_slot.mcpwm_unit, m_slot.mcpwm_cl, pinC_l);
288284

289285
// configure the timer
290-
_configureTimerFrequency(pwm_frequency, m_slot.mcpwm_num, m_slot.mcpwm_unit);
286+
_configureTimerFrequency6PWM(pwm_frequency, m_slot.mcpwm_num, m_slot.mcpwm_unit);
291287
// return
292288
return 0;
293289
}
294290

291+
292+
// configuring high frequency pwm timer
293+
// https://hackaday.io/project/169905-esp-32-bldc-robot-actuator-controller
294+
void _configureTimerFrequency6PWM(long pwm_frequency, mcpwm_dev_t* mcpwm_num, mcpwm_unit_t mcpwm_unit){
295+
296+
mcpwm_config_t pwm_config;
297+
298+
pwm_config.frequency = 4000; //frequency = 20000Hz
299+
pwm_config.cmpr_a = 50.0; //duty cycle of PWMxA = 50.0%
300+
pwm_config.cmpr_b = 50.0; //duty cycle of PWMxB = 50.0%
301+
pwm_config.counter_mode = MCPWM_UP_COUNTER; //MCPWM_UP_DOWN_COUNTER; // Up-down counter (triangle wave)
302+
pwm_config.duty_mode = MCPWM_DUTY_MODE_0; // Active HIGH
303+
mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_0, &pwm_config); //Configure PWM0A & PWM0B with above settings
304+
mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_1, &pwm_config); //Configure PWM0A & PWM0B with above settings
305+
mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_2, &pwm_config); //Configure PWM0A & PWM0B with above settings
306+
mcpwm_deadtime_enable(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE, 40, 40);
307+
mcpwm_deadtime_enable(MCPWM_UNIT_0, MCPWM_TIMER_1, MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE, 40, 40);
308+
mcpwm_deadtime_enable(MCPWM_UNIT_0, MCPWM_TIMER_2, MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE, 40, 40);
309+
310+
mcpwm_sync_enable(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_SELECT_SYNC_INT0, 0);
311+
mcpwm_sync_enable(MCPWM_UNIT_0, MCPWM_TIMER_1, MCPWM_SELECT_SYNC_INT0, 0);
312+
mcpwm_sync_enable(MCPWM_UNIT_0, MCPWM_TIMER_2, MCPWM_SELECT_SYNC_INT0, 0);
313+
314+
MCPWM0.timer[0].sync.out_sel = 1;
315+
delayMicroseconds(1000);
316+
MCPWM0.timer[0].sync.out_sel = 0;
317+
}
318+
319+
295320
// Function setting the duty cycle to the pwm pin (ex. analogWrite())
296321
// - BLDC driver - 6PWM setting
297322
// - hardware specific
298323
void _writeDutyCycle6PWM(float dc_a, float dc_b, float dc_c, float dead_zone, int pinA_h, int pinA_l, int pinB_h, int pinB_l, int pinC_h, int pinC_l){
299324
// determine which motor slot is the motor connected to
300-
for(int i = 0; i < 2; i++){
301-
if(esp32_bldc_6pwm_motor_slots[i].pinAH == pinA_h){ // if motor slot found
325+
// for(int i = 0; i < 2; i++){
326+
// if(esp32_bldc_6pwm_motor_slots[i].pinAH == pinA_h){ // if motor slot found
302327
// se the PWM on the slot timers
303328
// transform duty cycle from [0,1] to [0,100.0]
304-
float dc_ah = _constrain(dc_a - dead_zone/2.0 , 0, 1)*100.0;
305-
float dc_bh = _constrain(dc_b - dead_zone/2.0 , 0, 1)*100.0;
306-
float dc_ch = _constrain(dc_c - dead_zone/2.0 , 0, 1)*100.0;
307-
float dc_al = _constrain(dc_a + dead_zone/2.0 , 0, 1)*100.0;
308-
float dc_bl = _constrain(dc_b + dead_zone/2.0 , 0, 1)*100.0;
309-
float dc_cl = _constrain(dc_c + dead_zone/2.0 , 0, 1)*100.0;
310-
mcpwm_set_duty(esp32_bldc_6pwm_motor_slots[i].mcpwm_unit, MCPWM_TIMER_0, MCPWM_OPR_A, dc_ah);
311-
mcpwm_set_duty(esp32_bldc_6pwm_motor_slots[i].mcpwm_unit, MCPWM_TIMER_0, MCPWM_OPR_B, dc_al);
312-
mcpwm_set_duty(esp32_bldc_6pwm_motor_slots[i].mcpwm_unit, MCPWM_TIMER_1, MCPWM_OPR_A, dc_bh);
313-
mcpwm_set_duty(esp32_bldc_6pwm_motor_slots[i].mcpwm_unit, MCPWM_TIMER_1, MCPWM_OPR_B, dc_bl);
314-
mcpwm_set_duty(esp32_bldc_6pwm_motor_slots[i].mcpwm_unit, MCPWM_TIMER_2, MCPWM_OPR_A, dc_ch);
315-
mcpwm_set_duty(esp32_bldc_6pwm_motor_slots[i].mcpwm_unit, MCPWM_TIMER_2, MCPWM_OPR_B, dc_cl);
316-
break;
317-
}
318-
}
329+
// float dc_ah = _constrain(dc_a - dead_zone/2.0 , 0, 1)*100.0;
330+
// float dc_bh = _constrain(dc_b - dead_zone/2.0 , 0, 1)*100.0;
331+
// float dc_ch = _constrain(dc_c - dead_zone/2.0 , 0, 1)*100.0;
332+
// float dc_al = _constrain(dc_a + dead_zone/2.0 , 0, 1)*100.0;
333+
// float dc_bl = _constrain(dc_b + dead_zone/2.0 , 0, 1)*100.0;
334+
// float dc_cl = _constrain(dc_c + dead_zone/2.0 , 0, 1)*100.0;
335+
// mcpwm_set_duty(esp32_bldc_6pwm_motor_slots[i].mcpwm_unit, MCPWM_TIMER_0, MCPWM_OPR_A, dc_a*100.0);
336+
// mcpwm_set_duty(esp32_bldc_6pwm_motor_slots[i].mcpwm_unit, MCPWM_TIMER_0, MCPWM_OPR_B, dc_a*100.0);
337+
// mcpwm_set_duty(esp32_bldc_6pwm_motor_slots[i].mcpwm_unit, MCPWM_TIMER_1, MCPWM_OPR_A, dc_b*100.0);
338+
// mcpwm_set_duty(esp32_bldc_6pwm_motor_slots[i].mcpwm_unit, MCPWM_TIMER_1, MCPWM_OPR_B, dc_b*100.0);
339+
// mcpwm_set_duty(esp32_bldc_6pwm_motor_slots[i].mcpwm_unit, MCPWM_TIMER_2, MCPWM_OPR_A, dc_c*100.0);
340+
// mcpwm_set_duty(esp32_bldc_6pwm_motor_slots[i].mcpwm_unit, MCPWM_TIMER_2, MCPWM_OPR_B, dc_c*100.0);
341+
mcpwm_set_duty(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_A, dc_a*100.0);
342+
mcpwm_set_duty(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_B, dc_a*100.0);
343+
mcpwm_set_duty(MCPWM_UNIT_0, MCPWM_TIMER_1, MCPWM_OPR_A, dc_b*100.0);
344+
mcpwm_set_duty(MCPWM_UNIT_0, MCPWM_TIMER_1, MCPWM_OPR_B, dc_b*100.0);
345+
mcpwm_set_duty(MCPWM_UNIT_0, MCPWM_TIMER_2, MCPWM_OPR_A, dc_c*100.0);
346+
mcpwm_set_duty(MCPWM_UNIT_0, MCPWM_TIMER_2, MCPWM_OPR_B, dc_c*100.0);
347+
// break;
348+
// }
349+
// }
319350
}
320351
#endif

0 commit comments

Comments
 (0)