Shop OBEX P1 Docs P2 Docs Learn Events
PBASIC program for reading a quadrature encoder — Parallax Forums

PBASIC program for reading a quadrature encoder

ClintClint Posts: 95
edited 2007-02-01 17:57 in BASIC Stamp
I understand how quadrature encoders work and I have used them with PLCs before, but being completely new to PBASIC programming, I am not sure how to tackle the programming.

Does anyone know of a sample program I could look at for ideas?

I basically·need to be able to count rising edges and falling edges of a square wave signal. The PLCs I've worked with have a "Pulse Up" or "Pulse Down" function, but I can't find anything like it in my reference manual.

I have a BS2 and Board of Education.

Thanks for any help smile.gif

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-01-02 02:06
    Hello,
    ·
    ·· Reading just pulses won’t be enough for such an encoder, however I do have a sample programs for the BASIC Stamp for reading one connected to two I/O pins. ·It doesn’t matter if the COM is high or low, but the I/O pins need 10K pull-ups to the opposite side of the P/S. ·I hope this helps.· Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • stamptrolstamptrol Posts: 1,731
    edited 2007-01-02 15:01
    I've posted a variation of a quadrature counter on my website's "downloads" section.

    It provides the output as a binary output on one side of the stamp.

    Feel free to have a look at it.

    Regards,

    Tom

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
    ·
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-01-02 16:14
    One other thing you can factor into your encoder is the ability to limit the range of the value.· Here is the encoder subroutine from our Solder Pot Controller.· It only reads the encoder when the ‘set’ button is pressed, at which time it goes from displaying the current temperature to the set temperature.· When the button is released, the new set temperature is stored in the EEPROM and is used at next power-up.· Notice the MIN/MAX operators used to limit the range from 450 to 550.· This can be useful in many applications, such as this one.· Take care.

    Set_Temp:
      tempF = setTemp
      GOSUB Show_TempF
      newBits = INA & %0011
      oldBits = newBits
      DO WHILE SetButton = 0
        newBits = INA & %0011               ' Get State Of I/O Pins 0 - 1
        IF newBits <> oldBits THEN          ' Have Bits Changed?
          setTemp = setTemp - 1 + (2 * (newBits.BIT0 ^ oldBits.BIT1)) MIN 450 MAX 550
          tempF = setTemp
          GOSUB Show_TempF
          oldBits = newBits                 ' Update oldBits
        ENDIF
      LOOP
      STORE 0
      WRITE 0, Word setTemp
      RETURN
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2007-01-02 16:15
    Tom -

    Just a passing comment. When you list your web site in your SIG, as you have, that doesn't show up in the email output. I've no idea whether it's supposed to show up in the email output or not.

    Some folks (like me) don't log onto the forum unless we see something of interest, something to which we want to reply, or something we want to download, preferring to scan quickly through the email output instead.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • stamptrolstamptrol Posts: 1,731
    edited 2007-01-02 17:12
    Hi Bruce,

    I just assumed if I could see it in the post's Signature area, everyone else could, too. But I take it, you've got a notification coming direct to email which evidently only brings in the body, not the sig.

    Ah well, these computers are mysterious beasts!

    Cheers

    Tom

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
    ·
  • ClintClint Posts: 95
    edited 2007-01-02 18:33
    Chris, thank you for posting the most recent example. I am confused though as to how this works:

    setTemp = setTemp - 1 + (2 * (newBits.BIT0 ^ oldBits.BIT1)) MIN 450 MAX 550
    Am I correct in understanding that oldbits and newbits each·have two bits of information corresponding to channel A and channel B of the encoder? Where BIT0 could contain input from·channel A and BIT1 could be from channel B. Oldbits would contain the channel A and channel B states for the previous scan·of the·do loop while newbits would contain the current states from this scan.

    Am I right?

    If so, I don't understand how the line in blue above would read the quadrature signal properly. I only see two cases where setTemp would be reduced by 1 whereas the other·six cases would result in an addition of 1 to settemp. Here is my "truth table" of sorts showing the two cases that I think this function does not properly decode the quadrature signal.

    newBits·oldBits: result
    00·01: subtract
    01·00: add
    11·10:·add <- should subtract!
    10 11: add
    10 00: add <- should subtract!
    11 01: add
    01 11: subtract
    00 10: add

    Post Edited (TheBandit) : 1/2/2007 6:38:08 PM GMT
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-01-02 19:53
    Here's yet another tutorial:

    www.emesys.com/BS2fsm.htm#twobit]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-01-02 20:26
    The first examples I attached may or may not help make it more clear.· That of course and Tracy’s link, which goes into more detail.· Essentially the one from the Solder Pot Controller was an attempt to combine 3 lines of code into one, so I can see where it might be hard to follow.
      DO
        newBits = INA & %0011               ' Get State Of I/O Pins 0 - 3
        IF newBits <> oldBits THEN          ' Have Bits Changed?
          direction = newBits.BIT0 ^ oldBits.BIT1
          counter = counter - 1 + (2 * direction)
          DEBUG HOME, DEC3 counter          ' Show New Counter On DEBUG Screen
          oldBits = newBits                 ' Update oldBits
        ENDIF
      LOOP
    
    

    Notice that the first line in the loop reads the current state of the pins (some applications may require masking of lines, which is done here).· The next line checks to see if anything changed.· There’s no point wasting time inside the routine if nothing has changed.· The next line determines the direction by XORing bits from both readings.· In the line after that the value is incremented or decremented based on the result of direction.· Finally, in the Solder Pot code these last two lines were combined along with range limiting (MIN/MAX).· You could experiment with this code by adding a DEBUG to print the current value of newbits every time they change.· That would generate your truth table.· I hope this helps.· Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • ClintClint Posts: 95
    edited 2007-01-31 16:26
    I am having some issues with the BS2 reading a 24ppr encoder. If I move very slowly, the program in the previous post works fine for decoding the signal and counting. However if I move at reasonable finger turning speed, it does strange stuff.

    I have a three pin, 2-bit encoder. The input is connected to VDD. The outputs A and B are connected to P0 and P1 with 10k pulldown resistors connected between each output and VSS.

    Any advice? Is the BS2 too slow to read this?
  • stamptrolstamptrol Posts: 1,731
    edited 2007-01-31 18:04
    · Clint,

    ·· If I recall, the counting on a BS2 seemed to work OK up to about 50 pulses per second, depending on the other stuff that needs to get done before reading the pulses again.

    ·· With 24 ppr, it would be pretty easy to get to that point; only 2 revs per sec.

    ···What I did on one project was use a commercial quad counter (by Red Lion) with programmable outputs. With a serial link, the stamp would set the setpoints or ask what the current count was, but the counter did the critical counting stuff.

    · Cheers,

    ·Tom

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
    ·
  • ClintClint Posts: 95
    edited 2007-01-31 18:37
    Tom, thanks for the feedback. I've tried a 9 ppr encoder and it works very well. Unfortunately I need to read a 360ppr encoder at some point. x1 interpretation would be fine.

    I am I/O pin limited right now. Is there any way to read something like this using only two pins on the STAMP?
  • ClintClint Posts: 95
    edited 2007-01-31 19:14
    Perhaps the BS2SX at 50mhz would be better suited for this?
  • stamptrolstamptrol Posts: 1,731
    edited 2007-01-31 20:02
    Clint,
    Even the BS2sx will only speed you up by 2.5 times.

    You may have to move to an SX or even Propeller to get the capacity you need. I don't have enough time with either of those yet to give a reasonable opinion, but hopefully someone else can jump in with some comments.

    Regarding the external counter, two spare pins would be fine, or you could use the programming port. And, the encoder signals go directly to the counter, so that may give you a couple more free stamp pins.The counter I used could use rs232 or rs485 for communication so theres a bit of flexibility.
    Cheers,
    Tom

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com


    Post Edited (stamptrol) : 1/31/2007 8:09:52 PM GMT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-01-31 21:07
    Clint,

    You would need an SX Microcontroller (not a BS2sx) or a Propeller to do what you ask. The BS2px is our fastest and what was used in the project mentioned above. Still it was possible when spinning fast to miss counts and that was on a 16 ppr encoder. The thing that slows it down are the calculations that need to be done within the loop. The more stuff you add the worse it gets. An SX would be the next logical choice mainly for speed. I hope this helps. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • ClintClint Posts: 95
    edited 2007-01-31 22:19
    I have been reading on the Parallax website since your last post researching the SX and Propeller chips. It looks like programming the Propeller might be an easier transition for me coming from the PBASIC. Do you think a Propeller could keep up with a 360ppr encoder moving 2 rev/sec?
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-01-31 22:35
    Clint,

    Sure it could…So could an SX. Don’t forget the SX-IDE contains the free SX/B which comes pretty close to PBASIC but with the speed benefit of the SX. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • ClintClint Posts: 95
    edited 2007-01-31 23:33
    Wow Chris, I don't know how I missed SX/B. I think maybe I should give the SX a shot.
    Now what the heck am I going to do with all the BS2 components I bought?
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-02-01 00:12
    Clint,

    The BASIC Stamp is not obsolete in any way. The BASIC Stamp is not only very capable but greatly reduces development time for a great many projects. Often it’s just easier to use. We often use the SX as a co-processor to the BASIC Stamp where the SX is doing some task that requires great speed or accuracy and communicating with the BASIC Stamp. Nonetheless the SX is needed in some cases. Still I wouldn’t throw away your BASIC Stamps just yet…Good luck with your project and let us know how you progress. If you do use the SX and have code questions the SX Forum is a great place to get those answers. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2007-02-01 06:50
    Clint -

    In the spirit of full disclosure, and since all of the tools necessary to program the PBASIC Stamp are free, you may wish to review the following, extracted from the SX/B lead page on the Parallax web site:

    "Programming the SX chip requires an SX Tech Tool Kit and some understanding of the SX architecture. Specifically, knowledge of oscillator speeds, page switching, and managing an interrupt service routine will be necessary to get the most performance from an SX chip."

    The "SX Tool Kit" (SX-Key or SX-Blitz) is required for programming the SX chip using SX assembler or SX/B. This is essentially a sophisticated hardware programmer for the SX chip. There is no such similar device required to program the PBASIC Stamp in PBASIC.

    Just a matter of information, and nothing more.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • ClintClint Posts: 95
    edited 2007-02-01 16:42
    Thanks for the headsup. I am a little worried about the learning curve, but for my application it's critical that the encoder is read properly.

    I am strongly considering the SX Tool Kit Plus. Will I be able to use the USB to 232 adaptor that came with my Basic Stamp Discovery Kit? The computer I'm using doesn't have a serial port.

    Is there a particular piece of literature, website, or forum thread that I should look at to help me transition from PBASIC to SX/B?

    Thanks for all your help. I couldn't be happier with the Parallax support or the people on this forum.

    *EDIT* I downloaded and installed SX-Key v 3.2.3· and the Help has answered a lot of questions for me already. Parallax documentation is just incredible! It looks like most of the functions and syntax I have been using in PBASIC·carry over directly to SX/B. smile.gif

    Post Edited (Clint) : 2/1/2007 5:12:15 PM GMT
  • ClintClint Posts: 95
    edited 2007-02-01 17:57
    Followup: I called Parallax and talked to the tech support over the phone. They said the USB adaptor will work fine with the SX-Key and that it should easily keep up with my encoder signal. I bought the SX Tool Kit Plus along with a few extra SX chips and resonators. I am very happy to see that some of the components that were external on the Basic Stamp are internal to the SX. I don't feel like I wasted my time with the Basic Stamp because it looks like a lot of what I've learned will carry over. Hopefully I made the right decision.
Sign In or Register to comment.