Page 1 of 1
Glitches on hardware pwm
Posted: Tue Jun 06, 2017 8:18 am
by piveloper
Hi,
I have connected the Rapsberry Pi to a Stepper driver which controls a stepper motor. Recently I observed step losses on the motor every second or two. This is only noticable at higher RPM (>500 Hz PWM). With a scope I can see that the raspberry pi skips rectangle pulses (see screenshot). Are these glitches normal or is my raspbbery pi broken? Any Ideas if a misconfiguration of the pi can lead to this phenomena?
I used following code to produce the pwm in the screenshot:
Code: Select all
import RPi.GPIO as GPIO
import sys # exit program
import time
import signal
GPIO.setmode(GPIO.BCM)
pin_step = 18
GPIO.setup(pin_step, GPIO.OUT)
pwm = GPIO.PWM(pin_step, 1)
pwm.start(10.0)
freq = 1000
pwm.ChangeFrequency(freq)
def signalHandlerSIGINT(signal, frame):
pwm.stop()
GPIO.cleanup()
sys.exit(0)
signal.signal(signal.SIGINT, signalHandlerSIGINT)
while True:
time.sleep(10)
Best regards
Re: Glitches on hardware pwm
Posted: Tue Jun 06, 2017 8:48 am
by MaxK1
Which O/S and is it fully up-to-date? What Pi model? Overclocked or not? What else is connected? What else is running?
Re: Glitches on hardware pwm
Posted: Tue Jun 06, 2017 8:52 am
by mattmiller
AFAIK - although pin 12 (BCM18) is capable of using hardware PCM - the RPi.GPIO library treats it like all the others and uses software PWM on it
Try using pigpiod if you need hardware pwm on pins
Re: Glitches on hardware pwm
Posted: Thu Jun 08, 2017 2:31 pm
by piveloper
mattmiller wrote:AFAIK - although pin 12 (BCM18) is capable of using hardware PCM - the RPi.GPIO library treats it like all the others and uses software PWM on it
Try using pigpiod if you need hardware pwm on pins
Thanks for this information! Using pigpiod I was able to get a much more stable pwm signal. Unfortunately pigpiod needs a daemon already running in the background which consumes 5-10% of the cpu time.
I resolved this issue using wiringpi+hardwarepwm:
https://raspberrypi.stackexchange.com/q ... /9725#9725
Code: Select all
import wiringpi
pin = 18
freq = 50
wiringpi.wiringPiSetupGpio()
wiringpi.pinMode(pin, 2)
wiringpi.pwmSetMode(wiringpi.PWM_MODE_MS)
piPwmClock = 19.2e6
range_value=200
clock_value=int(piPwmClock / ( range_value*freq))
wiringpi.pwmSetClock(clock_value)
wiringpi.pwmSetRange(range_value)
dutyCycle = 0.5
value = int(dutyCycle*range_value)
wiringpi.pwmWrite(pin,value)
The code above results in a stable hardware pwm with 50 Hz.
I was only able to get the hardware pwm on channel 13 and 18 simultaneously with same frequency. Is there a way to have two hardware pwms with different frequencies on my rpi3 model B?
Re: Glitches on hardware pwm
Posted: Thu Jun 08, 2017 3:06 pm
by joan
There are two hardware PWM channels: channel 0 and channel 1. Each channel can have its own frequency. Channel 0 can be fed to GPIO 12/18, channel 1 can be fed to GPIO 13/19.
pigpio will let you set the frequency and duty cycle (e.g.
http://abyz.co.uk/rpi/pigpio/pigs.html#HP ).
Re: Glitches on hardware pwm
Posted: Thu Jun 08, 2017 3:30 pm
by piveloper
joan wrote:There are two hardware PWM channels: channel 0 and channel 1. Each channel can have its own frequency. Channel 0 can be fed to GPIO 12/18, channel 1 can be fed to GPIO 13/19.
pigpio will let you set the frequency and duty cycle (e.g.
http://abyz.co.uk/rpi/pigpio/pigs.html#HP ).
I want to avoid running pigpio running in the background as is consumes 5-10% CPU time permanently. But if pigpio can use both hardware pwms there must be a way to use them without pigpio daemon, right?
I searched for it a lot, but I could not find any
python code snippet on the internet which enables two hardware pwms (one on BCM18 and one on BCM13).
Re: Glitches on hardware pwm
Posted: Thu Jun 08, 2017 4:00 pm
by joan
piveloper wrote:
...
I want to avoid running pigpio running in the background as is consumes 5-10% CPU time permanently. But if pigpio can use both hardware pwms there must be a way to use them without pigpio daemon, right?
I searched for it a lot, but I could not find any python code snippet on the internet which enables two hardware pwms (one on BCM18 and one on BCM13).
You pays your money and you takes your choice.
http://abyz.co.uk/rpi/pigpio/python.html#hardware_PWM
Re: Glitches on hardware pwm
Posted: Thu Jun 08, 2017 5:31 pm
by piveloper
joan wrote:piveloper wrote:
...
I want to avoid running pigpio running in the background as is consumes 5-10% CPU time permanently. But if pigpio can use both hardware pwms there must be a way to use them without pigpio daemon, right?
I searched for it a lot, but I could not find any python code snippet on the internet which enables two hardware pwms (one on BCM18 and one on BCM13).
You pays your money and you takes your choice.
http://abyz.co.uk/rpi/pigpio/python.html#hardware_PWM

well, for that I still need pigpio daemon running. Would be great if there is a solution without a CPU consuming background task.
Re: Glitches on hardware pwm
Posted: Thu Jun 08, 2017 5:50 pm
by joan
It would be quite easy to do. You could take the appropriate parts of gpioHardwarePWM from pigpio.c and incorporate it into
http://abyz.co.uk/rpi/pigpio/examples.h ... nimal_gpio
Re: Glitches on hardware pwm
Posted: Thu Jun 08, 2017 6:42 pm
by piveloper
That may work. But it will take some time for me to figure out how to do it, since I never wrote a c package for python.
Thank you!