@@ -68,41 +68,27 @@ bldc_6pwm_motor_slots_t esp32_bldc_6pwm_motor_slots[2] = {
68
68
};
69
69
70
70
// configuring high frequency pwm timer
71
+ // a lot of help from this post from Paul Gauld
71
72
// https://hackaday.io/project/169905-esp-32-bldc-robot-actuator-controller
72
- void _configureTimerFrequency (long pwm_frequency, mcpwm_dev_t * mcpwm_num, mcpwm_unit_t mcpwm_unit){
73
+ void _configureTimerFrequency (long pwm_frequency, mcpwm_dev_t * mcpwm_num, mcpwm_unit_t mcpwm_unit, float dead_zone = NOT_SET ){
73
74
74
75
mcpwm_config_t pwm_config;
75
76
pwm_config.frequency = pwm_frequency; // frequency
76
- pwm_config.cmpr_a = 0 ; // duty cycle of PWMxA = 50.0%
77
- pwm_config.cmpr_b = 0 ; // duty cycle of PWMxB = 50.0%
78
77
pwm_config.counter_mode = MCPWM_UP_DOWN_COUNTER; // Up-down counter (triangle wave)
79
78
pwm_config.duty_mode = MCPWM_DUTY_MODE_0; // Active HIGH
80
79
mcpwm_init (mcpwm_unit, MCPWM_TIMER_0, &pwm_config); // Configure PWM0A & PWM0B with above settings
81
80
mcpwm_init (mcpwm_unit, MCPWM_TIMER_1, &pwm_config); // Configure PWM0A & PWM0B with above settings
82
81
mcpwm_init (mcpwm_unit, MCPWM_TIMER_2, &pwm_config); // Configure PWM0A & PWM0B with above settings
83
82
84
- _delay (100 );
85
-
86
- mcpwm_stop (mcpwm_unit, MCPWM_TIMER_0);
87
- mcpwm_stop (mcpwm_unit, MCPWM_TIMER_1);
88
- mcpwm_stop (mcpwm_unit, MCPWM_TIMER_2);
89
- mcpwm_num->clk_cfg .prescale = 0 ;
83
+ 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);
89
+ }
90
90
91
- mcpwm_num->timer [0 ].period .prescale = 4 ;
92
- mcpwm_num->timer [1 ].period .prescale = 4 ;
93
- mcpwm_num->timer [2 ].period .prescale = 4 ;
94
- _delay (1 );
95
- mcpwm_num->timer [0 ].period .period = 2048 ;
96
- mcpwm_num->timer [1 ].period .period = 2048 ;
97
- mcpwm_num->timer [2 ].period .period = 2048 ;
98
- _delay (1 );
99
- mcpwm_num->timer [0 ].period .upmethod = 0 ;
100
- mcpwm_num->timer [1 ].period .upmethod = 0 ;
101
- mcpwm_num->timer [2 ].period .upmethod = 0 ;
102
- _delay (1 );
103
- mcpwm_start (mcpwm_unit, MCPWM_TIMER_0);
104
- mcpwm_start (mcpwm_unit, MCPWM_TIMER_1);
105
- mcpwm_start (mcpwm_unit, MCPWM_TIMER_2);
91
+ _delay (100 );
106
92
107
93
mcpwm_sync_enable (mcpwm_unit, MCPWM_TIMER_0, MCPWM_SELECT_SYNC_INT0, 0 );
108
94
mcpwm_sync_enable (mcpwm_unit, MCPWM_TIMER_1, MCPWM_SELECT_SYNC_INT0, 0 );
@@ -121,7 +107,7 @@ void _configureTimerFrequency(long pwm_frequency, mcpwm_dev_t* mcpwm_num, mcpwm
121
107
void _configure3PWM (long pwm_frequency,const int pinA, const int pinB, const int pinC) {
122
108
123
109
if (!pwm_frequency || pwm_frequency == NOT_SET) pwm_frequency = 40000 ; // default frequency 20khz - centered pwm has twice lower frequency
124
- else pwm_frequency = _constrain (2 *pwm_frequency, 0 , 60000 ); // constrain to 30kHz max - 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
125
111
126
112
bldc_3pwm_motor_slots_t m_slot = {};
127
113
@@ -164,7 +150,7 @@ void _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const int
164
150
void _configure4PWM (long pwm_frequency,const int pinA, const int pinB, const int pinC, const int pinD) {
165
151
166
152
if (!pwm_frequency || pwm_frequency == NOT_SET) pwm_frequency = 40000 ; // default frequency 20khz - centered pwm has twice lower frequency
167
- else pwm_frequency = _constrain (2 *pwm_frequency, 0 , 60000 ); // constrain to 30kHz max - 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
168
154
stepper_motor_slots_t m_slot = {};
169
155
// determine which motor are we connecting
170
156
// and set the appropriate configuration parameters
@@ -210,10 +196,10 @@ void _writeDutyCycle3PWM(float dc_a, float dc_b, float dc_c, int pinA, int pinB
210
196
for (int i = 0 ; i < 4 ; i++){
211
197
if (esp32_bldc_3pwm_motor_slots[i].pinA == pinA){ // if motor slot found
212
198
// se the PWM on the slot timers
213
- // transform duty cycle from [0,1] to [0,2047 ]
214
- esp32_bldc_3pwm_motor_slots[i].mcpwm_num -> channel [ 0 ]. cmpr_value [ esp32_bldc_3pwm_motor_slots[i].mcpwm_operator ]. cmpr_val = dc_a*2047 ;
215
- esp32_bldc_3pwm_motor_slots[i].mcpwm_num -> channel [ 1 ]. cmpr_value [ esp32_bldc_3pwm_motor_slots[i].mcpwm_operator ]. cmpr_val = dc_b*2047 ;
216
- esp32_bldc_3pwm_motor_slots[i].mcpwm_num -> channel [ 2 ]. cmpr_value [ esp32_bldc_3pwm_motor_slots[i].mcpwm_operator ]. cmpr_val = dc_c*2047 ;
199
+ // transform duty cycle from [0,1] to [0,100 ]
200
+ mcpwm_set_duty ( esp32_bldc_3pwm_motor_slots[i].mcpwm_unit , MCPWM_TIMER_0, esp32_bldc_3pwm_motor_slots[i].mcpwm_operator , dc_a*100.0 ) ;
201
+ mcpwm_set_duty ( esp32_bldc_3pwm_motor_slots[i].mcpwm_unit , MCPWM_TIMER_1, esp32_bldc_3pwm_motor_slots[i].mcpwm_operator , dc_b*100.0 ) ;
202
+ mcpwm_set_duty ( esp32_bldc_3pwm_motor_slots[i].mcpwm_unit , MCPWM_TIMER_2, esp32_bldc_3pwm_motor_slots[i].mcpwm_operator , dc_c*100.0 ) ;
217
203
break ;
218
204
}
219
205
}
@@ -228,11 +214,11 @@ void _writeDutyCycle4PWM(float dc_1a, float dc_1b, float dc_2a, float dc_2b, in
228
214
for (int i = 0 ; i < 2 ; i++){
229
215
if (esp32_stepper_motor_slots[i].pin1A == pin1A){ // if motor slot found
230
216
// se the PWM on the slot timers
231
- // transform duty cycle from [0,1] to [0,2047 ]
232
- esp32_stepper_motor_slots[i].mcpwm_num -> channel [ 0 ]. cmpr_value [ esp32_stepper_motor_slots[i].mcpwm_operator1 ]. cmpr_val = dc_1a*2047 ;
233
- esp32_stepper_motor_slots[i].mcpwm_num -> channel [ 1 ]. cmpr_value [ esp32_stepper_motor_slots[i].mcpwm_operator1 ]. cmpr_val = dc_1b*2047 ;
234
- esp32_stepper_motor_slots[i].mcpwm_num -> channel [ 0 ]. cmpr_value [ esp32_stepper_motor_slots[i].mcpwm_operator2 ]. cmpr_val = dc_2a*2047 ;
235
- esp32_stepper_motor_slots[i].mcpwm_num -> channel [ 1 ]. cmpr_value [ esp32_stepper_motor_slots[i].mcpwm_operator2 ]. cmpr_val = dc_2b*2047 ;
217
+ // transform duty cycle from [0,1] to [0,100 ]
218
+ mcpwm_set_duty ( esp32_stepper_motor_slots[i].mcpwm_unit , MCPWM_TIMER_0, esp32_stepper_motor_slots[i].mcpwm_operator1 , dc_1a*100.0 ) ;
219
+ mcpwm_set_duty ( esp32_stepper_motor_slots[i].mcpwm_unit , MCPWM_TIMER_1, esp32_stepper_motor_slots[i].mcpwm_operator1 , dc_1b*100.0 ) ;
220
+ mcpwm_set_duty ( esp32_stepper_motor_slots[i].mcpwm_unit , MCPWM_TIMER_0, esp32_stepper_motor_slots[i].mcpwm_operator2 , dc_2a*100.0 ) ;
221
+ mcpwm_set_duty ( esp32_stepper_motor_slots[i].mcpwm_unit , MCPWM_TIMER_1, esp32_stepper_motor_slots[i].mcpwm_operator2 , dc_2b*100.0 ) ;
236
222
break ;
237
223
}
238
224
}
@@ -257,7 +243,7 @@ int _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, const
257
243
}
258
244
}
259
245
// if no slots available
260
- if (!m_slot. mcpwm_unit ) return -1 ;
246
+ if (slot_num >= 2 ) return -1 ;
261
247
262
248
// disable all the slots with the same MCPWM
263
249
if ( slot_num == 0 ){
@@ -283,7 +269,7 @@ int _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, const
283
269
mcpwm_gpio_init (m_slot.mcpwm_unit , m_slot.mcpwm_cl , pinC_l);
284
270
285
271
// configure the timer
286
- _configureTimerFrequency (pwm_frequency, m_slot.mcpwm_num , m_slot.mcpwm_unit );
272
+ _configureTimerFrequency (pwm_frequency, m_slot.mcpwm_num , m_slot.mcpwm_unit , dead_zone );
287
273
// return
288
274
return 0 ;
289
275
}
@@ -297,18 +283,12 @@ void _writeDutyCycle6PWM(float dc_a, float dc_b, float dc_c, float dead_zone, i
297
283
if (esp32_bldc_6pwm_motor_slots[i].pinAH == pinA_h){ // if motor slot found
298
284
// se the PWM on the slot timers
299
285
// transform duty cycle from [0,1] to [0,100.0]
300
- float dc_ah = _constrain (dc_a - dead_zone/2.0 , 0 , 1 )*100.0 ;
301
- float dc_bh = _constrain (dc_b - dead_zone/2.0 , 0 , 1 )*100.0 ;
302
- float dc_ch = _constrain (dc_c - dead_zone/2.0 , 0 , 1 )*100.0 ;
303
- float dc_al = _constrain (dc_a + dead_zone/2.0 , 0 , 1 )*100.0 ;
304
- float dc_bl = _constrain (dc_b + dead_zone/2.0 , 0 , 1 )*100.0 ;
305
- float dc_cl = _constrain (dc_c + dead_zone/2.0 , 0 , 1 )*100.0 ;
306
- mcpwm_set_duty (esp32_bldc_6pwm_motor_slots[i].mcpwm_unit , MCPWM_TIMER_0, MCPWM_OPR_A, dc_ah);
307
- mcpwm_set_duty (esp32_bldc_6pwm_motor_slots[i].mcpwm_unit , MCPWM_TIMER_0, MCPWM_OPR_B, dc_al);
308
- mcpwm_set_duty (esp32_bldc_6pwm_motor_slots[i].mcpwm_unit , MCPWM_TIMER_1, MCPWM_OPR_A, dc_bh);
309
- mcpwm_set_duty (esp32_bldc_6pwm_motor_slots[i].mcpwm_unit , MCPWM_TIMER_1, MCPWM_OPR_B, dc_bl);
310
- mcpwm_set_duty (esp32_bldc_6pwm_motor_slots[i].mcpwm_unit , MCPWM_TIMER_2, MCPWM_OPR_A, dc_ch);
311
- mcpwm_set_duty (esp32_bldc_6pwm_motor_slots[i].mcpwm_unit , MCPWM_TIMER_2, MCPWM_OPR_B, dc_cl);
286
+ mcpwm_set_duty (esp32_bldc_6pwm_motor_slots[i].mcpwm_unit , MCPWM_TIMER_0, MCPWM_OPR_A, dc_a*100 );
287
+ mcpwm_set_duty (esp32_bldc_6pwm_motor_slots[i].mcpwm_unit , MCPWM_TIMER_0, MCPWM_OPR_B, dc_a*100 );
288
+ mcpwm_set_duty (esp32_bldc_6pwm_motor_slots[i].mcpwm_unit , MCPWM_TIMER_1, MCPWM_OPR_A, dc_b*100 );
289
+ mcpwm_set_duty (esp32_bldc_6pwm_motor_slots[i].mcpwm_unit , MCPWM_TIMER_1, MCPWM_OPR_B, dc_b*100 );
290
+ mcpwm_set_duty (esp32_bldc_6pwm_motor_slots[i].mcpwm_unit , MCPWM_TIMER_2, MCPWM_OPR_A, dc_c*100 );
291
+ mcpwm_set_duty (esp32_bldc_6pwm_motor_slots[i].mcpwm_unit , MCPWM_TIMER_2, MCPWM_OPR_B, dc_c*100 );
312
292
break ;
313
293
}
314
294
}
0 commit comments