0

Read Duty Cycle of Incoming PWM Signal?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Read Duty Cycle of Incoming PWM Signal?

by dpancoe on Fri Jan 18, 2019 4:19 pm

I am hoping to use a BBB in a PCBA test fixture and have figured out how to perform most of the functions I need from within Python using Adafruit_BBIO. One I am not sure about yet - I have to verify the duty cycle of a PWM signal generated by the Board Under Test (BUT). I need to reliably differentiate among multiple duty cycle values at ~10% spacing with a PWM frequency of 1 kHZ.

My questions:
• Would this work well enough using GPIO.RISING and GPIO.FALLING event detection, timers and callbacks?
• If yes, suggested examples would be appreciated.
• If no and it has to involve the PRUSS, suggested examples definitely appreciated!

Thanks,
Don

dpancoe
 
Posts: 10
Joined: Thu Sep 06, 2012 10:28 am

Re: Read Duty Cycle of Incoming PWM Signal?

by drewfustini on Sat Jan 19, 2019 2:49 pm

The SoC in the BeagleBone is the AM3358 which has an eCAP peripheral which I believe can be used for this use case. Unfortunately, I have not used it myself.

I would suggest posting on the BeagleBoard.org mailing list to see if anyone else is currently using it and can provide additional details.
https://beagleboard.org/discuss

This reply by Robert Nelson may be of interest:
https://groups.google.com/d/msg/beagleb ... 8hnmSbCgAJ

drewfustini
 
Posts: 903
Joined: Sat Dec 26, 2015 1:19 pm

Re: Read Duty Cycle of Incoming PWM Signal?

by dpancoe on Sun Jan 20, 2019 12:55 am

Thank you for the information. I will have to look into the eCAP for the following reason.

In my own experimentation, it would seem that the BBIO event functions are not sufficient to measure the duty cycle of a 1kHz PWM signal, although I will have to look at the signal on an oscilloscope to be sure there is nothing else going on.

Here is the test code I used, but the supposed 1 kHz signal appears to have a frequency of about 500 to 600 Hz, and every duty cycle seems to be measured at about 50%. I also had a variant that used event callbacks rather than the "wait for edge" functions, but those results were similar.
Code: Select all | TOGGLE FULL SIZE
import Adafruit_BBIO.PWM as PWM
import Adafruit_BBIO.GPIO as GPIO
import time

pwmOut = "P9_22"
pwmIn = "P9_24"

PWM.start(pwmOut, 0, 1000)
GPIO.setup(pwmIn, GPIO.IN)

while (1):
        dutyCycleOut = input("Duty cycle to send (1 to 99): ")
        if dutyCycleOut < 1 or dutyCycleOut > 99:
                break
        PWM.set_duty_cycle(pwmOut, dutyCycleOut)
        time.sleep(.05)
        GPIO.wait_for_edge(pwmIn, GPIO.RISING)
        firstRise = time.clock()
        GPIO.wait_for_edge(pwmIn, GPIO.FALLING)
        midFall = time.clock()
        GPIO.wait_for_edge(pwmIn, GPIO.RISING)
        secondRise = time.clock()
        period = secondRise - firstRise
        pulseWidth = midFall - firstRise
        frequency = 1 / period
        dutyCycleIn = (pulseWidth / period) * 100.0
        print "Duty cycle: ", round(dutyCycleIn, 1), ", pulse width: ", pulseWidth, ", period: ", period, ", frequency: ", frequency
print("Duty cycle out-of-range, exiting program.")
PWM.stop(pwmOut)
PWM.cleanup()
GPIO.cleanup()

dpancoe
 
Posts: 10
Joined: Thu Sep 06, 2012 10:28 am

Re: Read Duty Cycle of Incoming PWM Signal?

by dpancoe on Sun Jan 20, 2019 12:59 am

It occurs to me that the problem could be in the source of
Code: Select all | TOGGLE FULL SIZE
time.clock()
. Maybe I should try an update to Python 3.3 or greater so I could use
Code: Select all | TOGGLE FULL SIZE
time.perf_counter()
instead. What version of Python is included in the latest (10/7/18) build of Debian for BBB?

dpancoe
 
Posts: 10
Joined: Thu Sep 06, 2012 10:28 am

Re: Read Duty Cycle of Incoming PWM Signal?

by drewfustini on Tue Jan 22, 2019 6:23 am

That Debian image should have Python 3.5.

drewfustini
 
Posts: 903
Joined: Sat Dec 26, 2015 1:19 pm

Re: Read Duty Cycle of Incoming PWM Signal?

by dpancoe on Tue Jan 22, 2019 12:01 pm

Hmmm, wonder why I got a "not a valid command" error or something similar when I tried to use time.perf_counter(). I will have to look into that further.

Thanks,
Don

dpancoe
 
Posts: 10
Joined: Thu Sep 06, 2012 10:28 am

Re: Read Duty Cycle of Incoming PWM Signal?

by dpancoe on Sun Mar 03, 2019 11:34 pm

As a follow-up, I have discovered libpruio, a library to handle the PRU GPIO including the eCAP pins. I ended up successfully testing with the C examples (pwm_cap.c) since I think I want to move my project in that direction, but they do have a set of Python examples as well. They released an updated version late last year and are active on the BeagleBoard Google Group.

Github:
https://github.com/DTJF/libpruio

Doxygen:
http://users.freebasic-portal.de/tjf/Pr ... index.html

Thanks,
Don

dpancoe
 
Posts: 10
Joined: Thu Sep 06, 2012 10:28 am

Please be positive and constructive with your questions and comments.