View Full Version : Code for measuring duty cycle?
02-07-2010, 06:07 AM
I want to look at encoder signals with a duty cycle spec of 50% +/- 5%.· If the duty cycle is out of spec I would like to display the duty cycle of the + pulse.· What commands would be good for this?
02-07-2010, 11:47 AM
How will you be measuring the input?
02-07-2010, 04:29 PM
What kind of machine are these encoders in? Mill , Spindle , printer , Are they infared disc encoders or digital? DC Servo Motors with encoders?
I Am currently·working on a basic encoder on a mill to measure spindle position and speed(tach) using a bs-2. Its a rough project when your not a elct. enginr. So there's a lot of research and trail & Error. But I love DIY, Everything from scratch if educationally possible!
02-07-2010, 07:46 PM
The encoder is mounted to a ballscrew. The encoder has 3 outputs; 2 channels and an index pulse that occurrs once every full rotation. I don't need to control position. It is just a test fixture to measure the symmetry of the channels to determine if the encoder is good or bad. I'm new to this and don't know how I could measure the pulse durations.
02-08-2010, 03:37 AM
How fast is it moving? If the pulses are less than 0.131 second in duration, then you can use the PULSIN command to read the individual pulse widths. This is assuming a typical incremental encoder.
pwA0 VAR word ' high pulse width of channel A
pwA1 VAR word ' low width of channel A
pwB0 VAR word ' high width of channel B
pwB1 VAR word ' low width of channel b
PULSIN 0,1,pwA1 ' channel A on BS2 pin p0, high for pwA1 units of 2us
PULSIN 0,0,pwA0 ' channel A on BS2 pin p0, low for pwA0 units of 2us
PULSIN 1,1,pwB1 ' channel B on BS2 pin p1, high for pwB1 units of 2us
PULSIN 1,0,pwB0 ' channel B on BS2 pin p1, low for pwB0 units of 2us
pwA1 = pwA1/5 ' units of 10 us
DEBUG CR, "channel A: ", DEC pwA1/100, ".", DEC2 pwA1, "milliseconds high"
pwA0 = pwA0/5 ' units of 10 us
DEBUG CR, REP 32\11, DEC pwA0/100, ".", DEC2 pwA0, "milliseconds low"
' similar DEBUG code for channel B
From the high and low times you can calculate the duty cycle. It is also possible to do that in code if necessary.
I am wondering why you want to do this? The important thing with an incremental encoder is the phase relationship between channel A and channel B, not the absolute accuracy of the pulse widths.
Post Edited (Tracy Allen) : 2/8/2010 12:52:19 AM GMT
02-08-2010, 09:35 AM
I am doing this for two purposes; 1. school project and 2. I can use it to test some encoders at work. I have found some encoders with poor symmetry that don't function well.
I am extremely new to PBASIC and BASIC STAMPs...After two days of searching I figured out the PULSIN command. I have been struggling with the learning curve.
When you say "pwA1 units of 2us" does that mean that the number returned for pwA1, for example, is multiplied by 2 to get the real duration in u sec?
02-08-2010, 11:14 AM
That's right. The PULSIN command returns the raw count in units of 2 microseconds, and you can multiply by 2 to get the answer in microseconds, but be aware that if the raw count is greater than 32767, multiplying times two will overflow the 16 bit word. That is why I chose to divide by 5 instead, which gives resolution of 10 microseconds, which may be enough.
02-08-2010, 09:10 PM
What does this mean.."REP 32\11" ?
02-08-2010, 11:34 PM
It is one of the SEROUT modifiers and makes the Stamp send 11 spaces (ascii code 32) in a row. Used to indent the display.
02-14-2010, 02:58 AM
The encoder quadrature spec is 90 degrees. How can I determine if one puse (on one channel) starts 90 degrees after the first (on the other channel)?
| | 90 degrees (how can I measure this?)
02-14-2010, 08:52 AM
Do you know how many pulses this encoder puts out per revolution? How fast will it be turning? Did you measure the pulse widths?
"90 degrees" means that the transitions on one channel happen 1/2 way through pulses on the other channel, and vice versa. If the encoder turns slowly and evenly, the following PBASIC should output numbers for two phases. If they are at 90 degrees, the numbers should be equal.
chA VAR in0
chB VAR in1
ticks VAR Word
tocks VAR Word
ticks=0 ' counters
DO WHILE chA=0 : LOOP ' synchronize
DO WHILE chA=1 : LOOP ' stay here while chA=0, move on at chA transition 0->1
DO WHILE chB=0 ' stay here and count while chB=0
ticks = ticks + 1
LOOP ' move on at chB transition 0->1
DO WHILE chA=1 ' stay here and count while chA=1
tocks = tocks + 1
LOOP ' move on when ' move on at chA transition 1->0
DEBUG CR, "chA=", DEC ticks, " chB=", DEC tocks ' show results, numbers should be about the same
02-14-2010, 09:18 PM
I gained the same information for a single channel in the BS2 project under Industrial/ Tachometer in finished projects. Because you are sampling two channels you will miss far fewer event transitions during your scan time. A percent duty cycle calculation will be very easy to implement. I second the concerns of other respondents as-to integer overflow. Note carefully the two calibration constants I used to lick this problem from 400-7000 RPM.
MOORE'S LAW: The capabilities of electronics shall double every 18 months.
cloyd's corollary: Hardware is easy, software is hard.