Shop OBEX P1 Docs P2 Docs Learn Events
Pulsin Threshold — Parallax Forums

Pulsin Threshold

Eric REric R Posts: 225
edited 2004-12-03 16:33 in BASIC Stamp
I have a square wave that is from an encoder that gives off 4 pulses per rev. Reading this with pulsin gives me 3750 counts at 2000 rpm and 750 counts at 10,000 rpm. I would like to set a threshold value that will hold true regardless of the decrease in pulsewidth.

Sample code is·as follows:
___________________________________________________________________________________________

sample1 var word
sample2 var word
totalSample1 var word
sample3 var word
sample4 var word
totalSample2 var word
grandtotal var word
threshold var word
Grandtenth var word

pulsin 13, 1, sample1
pulsin 13, 0, sample2
totalSample1 = sample1 + sample2········· 'sample 1 collection

pause 100··········································'100Ms update pause

pulsin 13, 1, sample3
pulsin 13, 0, sample4
totalSample2 = sample3 + sample4········· 'sample·2 collection

if totalSample1 > totalsample2 then GrandTotal = totalsample1 - totalsample2···· 'Subtraction only takes place if encoder is accelerating.

grandtenth = 50000 / grandtotal··········· 'best I could come up with for·100mS.·conversion

if grandtenth· > threshold then.....

__________________________________________________________________________________________

There lies my problem, I··would like to look for wild acceleration at anypoint in the RPM range of say 500rpm gain per second or higher, however, as the width of the pulse decreases with rpm my threshold value will also need to decrease in proportion. How could one do this? I had a few ideas that didn't work out well, so I am seeking suggestions!

Thanks,
Eric

Comments

  • Eric REric R Posts: 225
    edited 2004-12-03 04:45
    After plotting the data in excel it appears that in the upper RPM range the comparison could wind up being less than 200uS. I fear this could be hard to accurately control and reliably compare to a threshold. Does anyone see a better way to manage this situation?
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2004-12-03 06:01
    You should probably convert the PULSIN time values to rpm _before_ the subtraction.

    The formula to get from total pulse width to rpm is, according to your figures,
    rpm = 7500000 / totalsample = 50000 * 150 / totalsample
    for example, rpm is 100000 when the pulsin result, totalsample, is 750 counts.
    The division takes two steps on the Stamp to get enough precision for you to make the comparison.

    rpm1=(50000/totalsample1*10)+(50000//totalsample1*10/totalsample1)*15
    · rpm2=(50000/totalsample2*10)+(50000//totalsample2*10/totalsample2)*15
    rpmChange = rpm2 - rpm1 ' with a resolution of 15 rpm
    IF rpmChange.bit15=0 AND rpmChange > 30 THEN ... ' positive and enough

    Quantitatively, an acceleration of 500 rpm per second is 50 rpm in your 0.1 second sampling period. So the above formula with a resolution of 15 rpm should be able to pick it up. The resolution of the BS2 PULSIN command is 2 microseconds.

    The above seems to fit okay with the range of values that you specified, with a minimum of 2000 rpm. The formula can be incorrect if rpm < 1144, totalsample>6553.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Eric REric R Posts: 225
    edited 2004-12-03 11:21
    Thanks Tracy,

    Quick question, The stamp will accept 7500000?

    "rpm = 7500000 / totalsample = 50000 * 150 / totalsample"
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2004-12-03 16:33
    Well, the Stamp won't accept 7500000 directly, because a word variable is 16 bits, 65535 maximum. The trick is to break 7500000 down into parts or to do the computation using double precision. There are a couple of ways to go about it. The one that works will depend on the range of input values and on the precision and efficiency required.

    In this formula,
    rpm1=(50000/totalsample1*10)+(50000//totalsample1*10/totalsample1)*15
    the remainder from the division inside the first () is used in the second () to get one more decimal digit of precision.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
Sign In or Register to comment.