PWM steps

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

User avatar
kulve
Posts: 86
Joined: Mon Nov 28, 2011 8:36 pm

PWM steps

Post by kulve »

Hi

I'm controlling two servos on a F205 based board. I can control them otherwise properly but it looks like there's only something like 10 steps in the full scale.

This is my PWM config:

Code: Select all

static PWMConfig pwmcfg = {
  10000,  // 10 kHz PWM clock frequency
  200,    // 50 Hz (20.0 ms) initial frequency
  NULL,   // No periodic callback.
  {
    // Enable channels 1 through 2
    {PWM_OUTPUT_ACTIVE_HIGH, NULL},
    {PWM_OUTPUT_ACTIVE_HIGH, NULL},
    {PWM_OUTPUT_DISABLED, NULL},
    {PWM_OUTPUT_DISABLED, NULL}
  },
  // HW dependent part
  0, // TIM CR2 register initialization data
#if STM32_PWM_USE_ADVANCED
  0, // TIM BDTR (break & dead-time) register initialization data.
#endif
};


And I'm setting the duty cycle like this:

Code: Select all

pwmEnableChannel(&PWMD3, pwm, PWM_PERCENTAGE_TO_WIDTH(&PWMD3, duty));


Where the duty is something between 500 and 1000. The servo doesn't usually move if I'm adjusting the duty just a bit (~10).

The API documentation is saying this:
Be careful with rounding errors, this is integer math not magic. You can specify tenths of thousandth but make sure you have the proper hardware resolution by carefully choosing the clock source and prescaler settings, see PWM_COMPUTE_PSC.


And even though I understand roughly the meaning of it, I don't understand it in practice.

Any hints what to adjust?

Thanks.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: PWM steps

Post by Giovanni »

Hi,

You specified a PWM clock of 10KHz and a period of 50Hz, this means your PWM has 200 discrete possible duty cycles.

The macro PWM_PERCENTAGE_TO_WIDTH() accepts a duty cycle expressed as an integer from 0 to 10000, this means there is real PWM change only happens by changing the percentage by 50.

If you specify a duty cycle between 500 and 1000 (from 5% to 10%) then you only have 11 real possible duty cycle settings for your PWM.

You can:
1) Increase your PWM clock, in order to increase the PWM resolution (you can have up to 65535 discrete steps, the counter is 16 bits), try 100KHz or 1MHz.
2) Use all the range of the PWM_PERCENTAGE_TO_WIDTH() macro.

Giovanni
User avatar
kulve
Posts: 86
Joined: Mon Nov 28, 2011 8:36 pm

Re: PWM steps

Post by kulve »

Thanks, I will try with 1MHz. Is there a reason why I shouldn't always use higher clock? Increased power consumption?

About the option 2) To my understanding I can't use the whole range because standard servos need 1-2ms duty cycle and 50Hz period.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: PWM steps

Post by Giovanni »

kulve wrote:Thanks, I will try with 1MHz. Is there a reason why I shouldn't always use higher clock? Increased power consumption


Not really, it is just the clock of a simple counter.

Giovanni
User avatar
kulve
Posts: 86
Joined: Mon Nov 28, 2011 8:36 pm

Re: PWM steps

Post by kulve »

With 1MHz clock it runs nicely.

One more question. I'll be calling pwmEnableChannel() very frequently in some use cases. What happens to the currently running period? Does it just reset it while in the duty cycle phase leading to wrong duty cycle or will the change be in effect only in the next period?

Thanks!
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: PWM steps

Post by Giovanni »

The effect starts in the next period regardless the timing of the call (after the next period callback), it is the HW cell that ensures this.

Giovanni
User avatar
kulve
Posts: 86
Joined: Mon Nov 28, 2011 8:36 pm

Re: PWM steps

Post by kulve »

Excellent. Thanks again for your quick and patient support :)
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: PWM steps

Post by Giovanni »

You are welcome :-)
User avatar
kulve
Posts: 86
Joined: Mon Nov 28, 2011 8:36 pm

Re: PWM steps

Post by kulve »

Actually, one more question.

What exactly is the "ticks" when specifying the period in the PWMConfig and in the pwmChangePeriod()?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: PWM steps

Post by Giovanni »

The PWM period is expressed in clock pulses, so period=200 means that the period takes 200 clock pulses. The duty cycle goes from 0 to 200 (still in clock pulses).

Those PWM_xxx() macros convert from percentage/angle/whatever to ticks, you don't even have to use them.

Giovanni
Post Reply