PBASIC program for reading a quadrature encoder
Clint
Posts: 95
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
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
Comments
·
·· 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
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 Savage
Parallax Tech Support
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 -->
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
·
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
www.emesys.com/BS2fsm.htm#twobit]
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
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
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?
http://www.parallax.com/dl/docs/cols/nv/vol1/col/nv8.pdf
·· 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
·
I am I/O pin limited right now. Is there any way to read something like this using only two pins on the STAMP?
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
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
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
Now what the heck am I going to do with all the BS2 components I bought?
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
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 -->
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.
Post Edited (Clint) : 2/1/2007 5:12:15 PM GMT