BrachioGraph icon indicating copy to clipboard operation
BrachioGraph copied to clipboard

Hardware PWM?

Open shetty-777 opened this issue 2 years ago • 1 comments

I tried my best (I am a complete noob) but couldn't get it to work. I tried the hardware_PWM() function (is it a class?) of pigpio. It worked independently when I ran it, just set a frequency of 50 and duty of 500000. But it tried to go over the limit of my servo. And when I replaced the set_PWM_frequency() in plotter.py with the hardware_PWM class, nothing happened. I just thought hardware PWM would be more reliable and accurate, and when it is available on the RPi (on 2 channels, 4 pins) why not use each channel for elbow and shoulder? Has anybody tried it? Or did I just waste my time and it is useless?

shetty-777 avatar Oct 29 '23 04:10 shetty-777

Your duty cycle of 500'000 is an order of magnitude too big.

A duty cycle of 500'000 is 50% of the time. At 50Hz a full cycle is 20'000 microseconds, so that means a duty cycle of 10'000 microseconds.

The formula you need is:

duty_cycle = PWM_freq * pulse_width

Where the duty_cycle is out of 1 million, PWM_freq is Hz, and pulse_width is in microseconds.

A 1500 microsecond duty cycle would be 75'000 (at 50Hz).

I have tried this and it works as expected to swing the arm back and forth, where b.rpi is my instance:

pwm_freq = 50
start = 600
end = 2400
step = 1
x = (*range(start, end, step), *range(end, start, -step))
while True:
    for pw in x: 
        b.rpi.hardware_PWM(18, pwm_freq, pw*pwm_freq)

Is there any advantage? I don't know. See What advantage is there in using hardware PWM with pigpio? on StackExchange.

evildmp avatar Dec 16 '24 14:12 evildmp