Shop OBEX P1 Docs P2 Docs Learn Events
BS2 tachometer/digital encoder project — Parallax Forums

BS2 tachometer/digital encoder project

dmaintenancedmaintenance Posts: 6
edited 2016-07-01 12:06 in BASIC Stamp
Hi,

It’s been a while since I’ve done more than basic switching with the BS2 I have.

Hoping you can help and point me in the right direction with a project I need to accomplish.

I need a simple system to trigger an output being a buzzer and light indicator when a product is longer than the specified length when passing under a part of the machine. i.e. instead of 1 piece of paper has been fed past a fixed point on the conveyor belt, 2 pieces have fed incorrectly overlapping and being longer than expected.

I need to be able to measure the length of the product so I’ve ordered a digital encoder (tachometer) and photoelectric cell (photocell).

http://www.ebay.co.uk/itm/222122320381?_trksid=p2057872.m2749.l2649&ssPageName=STRK:MEBIDX:IT

http://www.ebay.co.uk/itm/331640970982?_trksid=p2057872.m2749.l2649&ssPageName=STRK:MEBIDX:IT


I need to be able to input the maximum length and change this length as and when required via keypad and LED or LCD display maybe or something simpler and not via reprogramming the code each time.

I have already built a system using 2 sensors and working on a physical trigger of both for the output error, but I need to go to system that measures the distance via the encoder and one sensor at a fixed point.

I’m thinking this is well within the capabilities of the Basic stamp and the BS2 I have already?
Being a while since using, it’s a bit like where do I start again……????

Can you advise on possible code for this? and any schematics if anyone has done a similar project?

Thanks for any help or assistance.

Comments

  • This is 12/24v (5v tends to be more common) quadrature encoder so you will need to decode the quadrature signals.

    I could help if you were using the Prop and PropBASIC as I have been doing this for some time now.

    There are quadrature decode objects for SPIN in the OBEX.
  • Hi Mickster,

    so ill order a 5-12vdc quad encoder. My systems are 24vdc with a power supply based so hence the choice.

    excuse my ignorance but what do you mean by "There are quadrature decode objects for SPIN in the OBEX"?
  • On the top banner, you will see "Home", Store", "Learn" and "OBEX". Obex is the Object Exchange; a collection of SPIN programs.
  • An example of using the search function in the OBEX for "Quadrature":

    http://obex.parallax.com/search/quadrature

  • Nothing wrong with 24v because you will need to buffer the signals anyway. I use opto-couplers or line receivers.

    Having re-read the OP, you might not need quad-decode, after all. It might be simply a case of counting the pulses of only one of the channels, if the paper is always moving in the same direction and 360 pulses/rev provides enough resolution. Quad-decode would provide an up/down count and 1,440 pulses/rev.

    SPIN is the Propeller's interpreted language and PASM is Propeller assembly language. The quad-decode stuff is written in PASM and runs in a separate cog. The encoder count is stored in the hub memory so that other cogs may access it.

    OBEX is here.
  • Hi Mickster,

    so ill order a 5-12vdc quad encoder. My systems are 24vdc with a power supply based so hence the choice.

    excuse my ignorance but what do you mean by "There are quadrature decode objects for SPIN in the OBEX"?

    Spin is a language used in programming the Propellor chip. Not gonna help much for your BS-2 project.

  • i was thinking of using solid state relays to optically isolate. A good get around any voltage issues. Plus i will install a suitable DC power supply with multiple voltage outputs (+5vdc and +24vdc etc) so i think ive that side of it covered.

    Ive been hacking around the manuals and started a basic start on the code. Ive got stuck on the PULSIN command and it wanting a variable??

    Ive read many web pages with no real solution to what im doing wrong. Basic stuff im sure but if you dont know and a steep learning curve then to be expected.

    Plus the Parallax manuals and the student tutorial aren't giving me the answer.

    Its a rough work in progress but any ideas why im getting errors on the PULSIN line?

    Im presuming PULSIN is the command best suited?

    code as follows:


    Main:
    DO
    LOW 0

    PULSIN 1, 1, value[/u][/b] "push button input" ' measure pulse input
    DEBUG DEC value, CR




    IF (value > 4000) THEN ' evaluate duration
    DEBUG "Value was greater than 4000"

    HIGH 0 'Buzzer & light output 1
    PAUSE 1000
    LOW 0
    PAUSE 1000
    DEBUG ? 0

    HIGH 0 'Buzzer & light output 2
    PAUSE 1000
    LOW 0
    PAUSE 1000
    DEBUG ? 0

    HIGH 0 'Buzzer & light output 3
    PAUSE 1000
    LOW 0
    PAUSE 1000
    DEBUG ? 0

    ELSEIF (value = 4000) THEN
    DEBUG "Value was equal to 4000"
    ELSE
    LOW 0

    DEBUG "Value was less than 4000"
    ENDIF
    DEBUG CR, CR
    PAUSE 1000

    LOOP






    ' notes:

    ' use IF THEN ELSE FOR encoder signal to preset value to give an output to buzzer/light.
    ' greater or less than function

    ' Comparison Operator Symbol Definition

    ' = Equal
    ' <> NOT Equal
    ' > Greater Than
    ' < Less Than
    ' >= Greater Than OR Equal TO
    ' <= Less Than OR Equal TO

    'Clock Speed: BS2 is 2mu's per unit of measure. 0.0002143 of a second.

    'The units of time FOR PULSIN AND PULSOUT are 2 microseconds (µs).
    'A microsecond is 10-6 seconds. FOR instance, IF we sent a PULSOUT with a value of 25,

    'we would have the length of the pulse be: {25 x 2mu's = 25 x (2 x 10^{-6})= 50 x 10^{-6} = 50mu's =0.05ms}

    'The reason why we make the conversion above from microseconds to
    ' milliseconds is that many devices display their requirements in terms of milliseconds
    'It is always important FOR the programmer to be able to convert between different units.




    ' quadrature 5vdc encoder. pulsin? or count command?

    'chapter 6 digital display
    'chapter 5 measuring rotation
    'pulsin? count?
  • i was thinking of using solid state relays to optically isolate. A good get around any voltage issues. Plus i will install a suitable DC power supply with multiple voltage outputs (+5vdc and +24vdc etc) so i think ive that side of it covered.

    I was referring to interfacing the encoder to the microcontroller....you're not intending to achieve this with an SSR, surely?
  • Duane DegnDuane Degn Posts: 10,588
    edited 2016-07-01 19:22
    The second ebay link in the original post is to a 360 pulse per rev encoder. I'm just finishing a project using 100 pulse per rev encoders which I think work very similar to the 360ppr one.

    The encoders I used work with voltages from 5V to 24V. The signal lines needs to be pulled high to Vdd (Vdd is the voltage of the microcontroller being used).

    Here's a link to a 394MB video of the Propeller gizmo I made to read the encoder (sorry, I haven't uploaded this to YouTube, if you open the link in a new tab a video player should open). I could spin the encoder at 2,400 rpm and the Propeller could keep up with the 100ppr encoders just fine.

    The video linked above was made for a client of mine but I doubt he minds my sharing the video here (we've talked about this sort of thing in the past).

    I also doubt he'd mind if I shared the quadrature code. The quadrature code is pretty darn simple but it's written in PASM so it's pretty fast. By "pretty fast" I mean it has about a 1.25us max loop time. This should allow it to capture up to 800 transitions per second. I'm not sure if the pull-up resistors will restore the open drain lines fast enough for an encoder to work at these sorts of speeds, but as you can see in the video, it keeps up with a 2,400 rpm drill just fine.

    Any Propeller board would work as an interface board.

    It's certainly possible to read a quadrature encoder with a Basic Stamp (erco has a nice video showing this) but Basic Stamp wouldn't be able to handle speeds nearly as high as a Propeller could handle.
  • Heck, Duane, I've been obsessing about high encoder speeds for a long time now and my PropBASIC code is good for 2M quad counts/sec....worst case hub write time. With software filtering (reading transitions twice), I'm good for 1.6M quad counts/sec.
  • Mickster wrote: »
    Heck, Duane, I've been obsessing about high encoder speeds for a long time now and my PropBASIC code is good for 2M quad counts/sec....worst case hub write time. With software filtering (reading transitions twice), I'm good for 1.6M quad counts/sec.

    I wondered about trying to find some the older quadrature encoder threads and look as some of the fast code but I was pretty sure I didn't need to worry much about speed. The max expected RPM was 1,800 with a 100ppr encoder so I figured a loop time less than 83us would be fine.

    The code below writes to the hub twice with each transition. I wrote the direction value to hub as part of a debugging aid and I never removed it. I'm not positive I counted all the clock cycles correctly but I came up with 100 clock cycles for a worst case loop.

    If you happen to recall the thread where you posted quadrature encoder code, I'd be interested to see it.

    With 100ppr encoder at 2,400 rpm I figure I'm getting 62.5us between transitions (16K quad counts per second). I'm don't think I have an encoder I could use to test 1.6M quad counts a second.

    As I've been thinking a bit about how to speed up the code I realized I'm doing things the slow way by checking for each possible encoder state.

    The more I look at my code, the more I see ways it can be improved. I'm going to leave the code posted here but no fair holding it against me in the future.

    I just mailed all my encoder reading hardware. I'll need to wire up another Propeller board so I can test any improved code I write (or test other's code).

    I suppose I could use one cog to generate quadrature output and feed this output to encoder input pins.
    DAT                     org
    
    quadratureCode          rdlong  quadCountCog, par ' The first encoder pin is stored in hub at par
                            ' quadCountCog now holds the pin used by the encoder
                            mov     directionAddress, par
                            add     directionAddress, #4
                            
                            mov     quadMask, #3
                            shl     quadMask, quadCountCog
                            'shl     state00, quadCountCog 
                            shl     state01, quadCountCog 
                            shl     state10, quadCountCog 
                            shl     state11, quadCountCog 
                            mov     quadCountCog, #0
                            wrlong  quadCountCog, par
                            mov     oldQuadScan, ina
                            and     oldQuadScan, quadMask
                            
    '-------------------------------------------------------------------------------
                            
    readQuadPin             mov     newQuadScan, ina            '4
                            and     newQuadScan, quadMask       '8
                            cmp     newQuadScan, oldQuadScan wz '12
                  if_z      jmp     #readQuadPin                '16
                            'jmp     #addQuadBlip                       
    '-------------------------------------------------------------------------------
    '' Positive direction pattern = 00, 01, 11, 10, 00
    addQuadBlip             mov     directionCog, #1        ' default, program with reverse if needed '20
                            cmp     state00, oldQuadScan wz  '24
                  if_z      jmp     #case00
                            cmp     state01, oldQuadScan wz   '32
                  if_z      jmp     #case01
                            cmp     state10, oldQuadScan wz  '40
                  if_z      jmp     #case10                   '44
    
    case11                  cmp     state01, newQuadScan wz   
                  if_z      mov     directionCog, negativeOne 
    
    continueBlip            wrlong  directionCog, directionAddress  'up to 64
                            add     quadCountCog, directionCog      '72 
                            mov     oldQuadScan, newQuadScan        '76 
                            wrlong  quadCountCog, par               '84
                            jmp     #readQuadPin                    '88
    '-------------------------------------------------------------------------------
    case00                  cmp     state10, newQuadScan wz
                  if_z      mov     directionCog, negativeOne               
                            jmp     #continueBlip
    '-------------------------------------------------------------------------------
    case01                  cmp     state00, newQuadScan wz    '48 
                  if_z      mov     directionCog, negativeOne  '52             
                            jmp     #continueBlip              '56          
    '-------------------------------------------------------------------------------
    case10                  cmp     state11, newQuadScan wz
                  if_z      mov     directionCog, negativeOne               
                            jmp     #continueBlip                        
    
    '-------------------------------------------------------------------------------
    
    negativeOne             long -1
    state00                 long %00
    state01                 long %01
    state10                 long %10
    state11                 long %11
    quadMask                res 1
    quadCountCog            res 1
    oldQuadScan             res 1
    newQuadScan             res 1
    directionAddress        res 1
    directionCog            res 1
    
                            fit
    

  • Here are a couple links to threads about using Basic Stamps to monitor encoders.

    http://forums.parallax.com/discussion/121179/reading-quadrature-encoders

    http://forums.parallax.com/discussion/119343/make-your-own-encoders/p1

    The first link includes a link to erco's YouTube video where he experiments with reading quadrature encoders with a BS2e. Here's a link to part 2 of his experiment.

    With the Propeller, one cog can be counting encoder pulses while another cog can be calculating speed.
  • When I visualize what I think you're doing, I see the encoder turning only one direction. Is that correct?
  • Duane Degn wrote: »
    If you happen to recall the thread where you posted quadrature encoder code, I'd be interested to see it.

    I took a very simplistic approach, knowing that; any change of state could be one of only two possibilities so I created separate loops for each possible state. What is not shown is the hub write which, in fact, happens each time the local variable is Inc'd or Dec'd.

    http://forums.parallax.com/discussion/143397/propbasic-routine-for-quadrature-encoder-reading-fast-and-worked-first-time#latest

  • When I visualize what I think you're doing, I see the encoder turning only one direction. Is that correct?

    yeah one direction. the rpm wont be that high as with a 50mm approx disc on the encoder to run on a spring loaded pivoting mount running the wheel directly on the conveyor belt transporting the product.

    direct shaft mounting isnt needed and normally a pain to install.

  • Mickster wrote: »
    i was thinking of using solid state relays to optically isolate. A good get around any voltage issues. Plus i will install a suitable DC power supply with multiple voltage outputs (+5vdc and +24vdc etc) so i think ive that side of it covered.

    I was referring to interfacing the encoder to the microcontroller....you're not intending to achieve this with an SSR, surely?

    sorry i meant ssr for the outputs and inputs other than the encoder. The 5-12vdc encoders are little more than £10 on ebay and compact in size so not a problem to purchase a lower voltage one to suit the BS2.
  • Interesting project. My policy is to buffer everything that is external to the microcontroller and with encoders, I always provide a dedicated power source.

    If the conveyor is a constant speed (???) I wonder if the encoder is really necessary.
  • Mickster wrote: »
    Interesting project. My policy is to buffer everything that is external to the microcontroller and with encoders, I always provide a dedicated power source.

    If the conveyor is a constant speed (???) I wonder if the encoder is really necessary.

    the conveyor may vary or be at a constant spped. The need for the Tachometer is to measure distance of a product via a lead edge trigger on the photocell.

    So i can trigger an output if the length is longer than specified via input of a keypad maybe with an LCD or LED display.

Sign In or Register to comment.