@@ -64,7 +64,7 @@ stepper_motor_slots_t esp32_stepper_motor_slots[2] = {
64
64
// define stepper motor slots array
65
65
bldc_6pwm_motor_slots_t esp32_bldc_6pwm_motor_slots[2 ] = {
66
66
{_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
68
68
};
69
69
70
70
// configuring high frequency pwm timer
@@ -81,10 +81,6 @@ void _configureTimerFrequency(long pwm_frequency, mcpwm_dev_t* mcpwm_num, mcpwm
81
81
mcpwm_init (mcpwm_unit, MCPWM_TIMER_1, &pwm_config); // Configure PWM0A & PWM0B with above settings
82
82
mcpwm_init (mcpwm_unit, MCPWM_TIMER_2, &pwm_config); // Configure PWM0A & PWM0B with above settings
83
83
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
-
88
84
_delay (100 );
89
85
90
86
mcpwm_stop (mcpwm_unit, MCPWM_TIMER_0);
@@ -287,34 +283,69 @@ int _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, const
287
283
mcpwm_gpio_init (m_slot.mcpwm_unit , m_slot.mcpwm_cl , pinC_l);
288
284
289
285
// 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 );
291
287
// return
292
288
return 0 ;
293
289
}
294
290
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
+
295
320
// Function setting the duty cycle to the pwm pin (ex. analogWrite())
296
321
// - BLDC driver - 6PWM setting
297
322
// - hardware specific
298
323
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){
299
324
// 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
302
327
// se the PWM on the slot timers
303
328
// 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
+ // }
319
350
}
320
351
#endif
0 commit comments