cancel
Showing results for 
Search instead for 
Did you mean: 

How to generate dutycycle for ESC controler using STM32F4

Bogdan
Senior
Posted on February 12, 2015 at 18:49

I have some brushless simonk ESC`s driving my brushless motors.

I noticed that these esc`s are woring on a 1 to 2 ms pulse from a 50hz signal. Wich in duty cycle terms means that i must work with dutycycle range from 1% to maximum 2%. Can these 1,2% terms to be translated in a numerical value of 1000-2000 units ? How to set for example 1.5%, some functions do not alow floating numbers inputs I have my pwm initialization

GPIO_InitTypeDef GPIO_InitStruct; 
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); 
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 |GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStruct.GPIO_Mode= GPIO_Mode_AF; 
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; 
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; 
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOC, &GPIO_InitStruct); 
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_TIM3); 
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_BaseStruct.TIM_Prescaler =16000;
TIM_BaseStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_BaseStruct.TIM_Period = 100; /* 51Hz PWM */
TIM_BaseStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_BaseStruct.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3, &TIM_BaseStruct);
TIM_Cmd(TIM3, ENABLE);
TIM_OCStruct.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCStruct.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCStruct.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCStruct.TIM_Pulse = 0; 
TIM_OC1Init(TIM3, &TIM_OCStruct);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OCStruct.TIM_Pulse = 0; 
TIM_OC2Init(TIM3, &TIM_OCStruct);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OCStruct.TIM_Pulse = 0; 
TIM_OC3Init(TIM3, &TIM_OCStruct);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OCStruct.TIM_Pulse = 0; 
TIM_OC4Init(TIM3, &TIM_OCStruct);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);

And my set dutycycle routine

void set_pwm(uint8_t channel, uint8_t duty){
if(channel==1){
TIM3->CCR1 = duty;
} else if(channel==2){
TIM3->CCR2 = duty; 
} else if(channel==3){
TIM3->CCR3 = duty;
}else if(channel==4){
TIM3->CCR4 = duty; 
}
}

8 REPLIES 8
Posted on February 12, 2015 at 21:02

Use bigger numbers? Use scaling? Divide them down after you've done the math?

Note Period and Prescaler numbers are N-1

So instead of having 16000-1 and 100-1, consider 1600-1 and 1000-1, or 160-1 and 10000-1 ? For maximum granularity perhaps 32-1 and 50000-1 for 16-bit timers

With a period of 50000 ticks, 2.5% becomes 1250 ticks thereof.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on February 12, 2015 at 21:05

1.5% -> 750

3.05% -> 1525

Percent * 500, or do all your math with *1000 numbers and divide by two as you put it in CCR registers
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
jj2
Associate II
Posted on February 12, 2015 at 21:36

May I express doubt of your report listing a 1 or 2% PWM Duty Cycle?   That would be a very narrow pulse - and the range (1%) is (imho) far too narrow to be (recognizable) by most BLDC motors.   How ever would you achieve speed control with so limited a PWM range?

Is it possible your measure of 1-2mS pulse widths has over-flowed into your expression of 1-2% PWM Duty Cycle.   Far more expected (normal) would be PWM Duty Cycle over the range 3 - 97% Duty Cycle.  Pulse Width and Duty Cycle are not the same - although higher duty cycles do result from wider pulse widths.

Might you detail ''how'' you arrived @ such low ''duty cycle'' numbers.

Clive's methods are fine - but your report of Duty Cycle requirements would be surprising if they are ''real!''    (I strongly suspect they will prove incorrect...)
Bogdan
Senior
Posted on February 13, 2015 at 05:56

Sorry, i mistaken something.... 1 ms is 5% duty, and 2ms is 10% duty out of a total period of 20ms (50hz)

Bogdan
Senior
Posted on February 13, 2015 at 19:46

Thank you all for the useful tips, ive used 160-1 and 10000-1 for timer3, now  i have  a much wider range of controling the pwm duty cycle.

Here is a sneak picture of the project itsel, need pwm to continue my work on the PID routines for the motor

http://postimg.org/image/6ajqb2v5v/

jj2
Associate II
Posted on February 18, 2015 at 17:43

I continue in the belief that (even) your increased duty cycle range of 5-10% proves too narrow for most brushless motor control.

Our small tech firm designs such controllers for ''ISM'' applications - our (normal) duty cycles range from 3 - 98% which yields motor speeds between 300 and 50,000 RPM.

I'd expect a much larger duty cycle range than you report.

And - our PWM usually - but not always - is set to 20KHz - so as not to be disturbing to (most) humans.  50Hz PWM frequency seems most unusual - as does such small (and limited) duty cycle...

Posted on February 18, 2015 at 18:16

1-2ms @ 50Hz sounds like a servo...

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Chris1
Senior III
Posted on February 19, 2015 at 18:21

Right, the ESCs here are Electronic Speed Controls designed for connection to a radio control receiver, like RC servos.  He's commanding the ESCs, the ESCs are controlling the motors (each with their own dedicated micro driving the windings).