Shop OBEX P1 Docs P2 Docs Learn Events
Code for measuring duty cycle? — Parallax Forums

Code for measuring duty cycle?

RoboronRoboron Posts: 9
edited 2010-02-14 14:18 in BASIC Stamp
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?

Comments

  • FranklinFranklin Posts: 4,747
    edited 2010-02-07 04:47
    How will you be measuring the input?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • Radridz3Radridz3 Posts: 49
    edited 2010-02-07 09:29
    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!
  • RoboronRoboron Posts: 9
    edited 2010-02-07 12:46
    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.

    thanks
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2010-02-07 20:37
    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
    
    DO
      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
      PAUSE 1000
    LOOP
    



    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.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com

    Post Edited (Tracy Allen) : 2/8/2010 12:52:19 AM GMT
  • RoboronRoboron Posts: 9
    edited 2010-02-08 02:35
    Thank you.

    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?
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2010-02-08 04:14
    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.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • RoboronRoboron Posts: 9
    edited 2010-02-08 14:10
    What does this mean.."REP 32\11" ?

    Thanks.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2010-02-08 16:34
    REP= REPeat
    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.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • RoboronRoboron Posts: 9
    edited 2010-02-13 19:58
    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)?

    _____|XXXXX|_______First Pulse

    ________|XXXXX|_______Second pulse

    | | 90 degrees (how can I measure this?)
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2010-02-14 01:52
    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
    top:
      ticks=0   ' counters
      tocks=0
      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
      GOTO top
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • yarisboyyarisboy Posts: 245
    edited 2010-02-14 14:18
    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.

    http://www.parallax.com/tabid/774/Default.aspx

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    MOORE'S LAW: The capabilities of electronics shall double every 18 months.
    cloyd's corollary: Hardware is easy, software is hard.
Sign In or Register to comment.