Rotary Encoder Question (Counter)
TrickyDick
Posts: 9
Hello All:
I just started to learn the Basic Stamp. To date I have completed approximately 1/3 of "What is a Microcontroller?", which I am enjoying immensely. The reason I started down the road of learning Basic Stamp, was to assist me in completion of a DIY project, and to become "better rounded" professionally.
The question I have pertains primarily to my DIY project, where I am attempting to tune a L/C network, utilizing a DC Motor, and 512 CPR Rotary Encoder. The basic requirement of my circuit is to count the turns of the motor, and eventually display the values on a LCD display, so I can quickly retune the circuit to resonance as I change operating frequency. The item being moved by the motor is a large Jennings Vacuum Variable Capacitor with limit switches.
I found the following article, which was written for BS1 (I am using BS2), and thought it would be a great fit. I especially liked the band-spread display format.
Link: http://www.parallax.com/dl/docs/cols/nv/vol1/col/nv8.pdf
Unfortunately, this was written for the bs1, and I am not competent enough yet to modify the code.
I then found a similar piece of code, here on the forum: http://forums.parallax.com/showthread.php?t=94836&highlight=Rotary+Encoder
While the program listed in the above link does show sign of life, the debug terminal is giving me very odd results. I was hoping that I could get the encoder to count upwards from 00000, but that is not the case. Instead is seem to be getting a value which is either 00000 or 65535. I believe this problem may be my encoder which has a rather high CPS when compared to other encoders I have seen discussed on this forum. The encoder which I am currently using is a AVAGO Technologies/HP HEDS 5540. The datasheet for this encoder can be found at the following link:
Link: http://www.poeticmonkey.com/ebay/misc-p/heds-5540-encoder/heds-5540-i06.pdf
I currently am not utilizing the pull-up resistor shown on page 7, because I believe they are not needed. During the final installation, I will use them, but my understanding from the Applications Engineer, is these pull-up resistors are more relevant for leads that exceed 4' length.
Any input into this matter would be appreciated, thanks in advance!
TrickyDick
I just started to learn the Basic Stamp. To date I have completed approximately 1/3 of "What is a Microcontroller?", which I am enjoying immensely. The reason I started down the road of learning Basic Stamp, was to assist me in completion of a DIY project, and to become "better rounded" professionally.
The question I have pertains primarily to my DIY project, where I am attempting to tune a L/C network, utilizing a DC Motor, and 512 CPR Rotary Encoder. The basic requirement of my circuit is to count the turns of the motor, and eventually display the values on a LCD display, so I can quickly retune the circuit to resonance as I change operating frequency. The item being moved by the motor is a large Jennings Vacuum Variable Capacitor with limit switches.
I found the following article, which was written for BS1 (I am using BS2), and thought it would be a great fit. I especially liked the band-spread display format.
Link: http://www.parallax.com/dl/docs/cols/nv/vol1/col/nv8.pdf
Unfortunately, this was written for the bs1, and I am not competent enough yet to modify the code.
I then found a similar piece of code, here on the forum: http://forums.parallax.com/showthread.php?t=94836&highlight=Rotary+Encoder
While the program listed in the above link does show sign of life, the debug terminal is giving me very odd results. I was hoping that I could get the encoder to count upwards from 00000, but that is not the case. Instead is seem to be getting a value which is either 00000 or 65535. I believe this problem may be my encoder which has a rather high CPS when compared to other encoders I have seen discussed on this forum. The encoder which I am currently using is a AVAGO Technologies/HP HEDS 5540. The datasheet for this encoder can be found at the following link:
Link: http://www.poeticmonkey.com/ebay/misc-p/heds-5540-encoder/heds-5540-i06.pdf
I currently am not utilizing the pull-up resistor shown on page 7, because I believe they are not needed. During the final installation, I will use them, but my understanding from the Applications Engineer, is these pull-up resistors are more relevant for leads that exceed 4' length.
Any input into this matter would be appreciated, thanks in advance!
TrickyDick
Comments
There is one outstanding issue, the counter tends to "trend" either up or down. What I mean by trending, is when the encoder is turned C.W. the count does on average rise. The count is not stable, and will drop a few digits, and then continue to count upward, and then stall. The same is true for C.C.W. rotation, except it seems worse than in clockwise.
One item I did notice, is if I short the P0/P1 input, while the encoder is running, the count works wonderfully, but counts upward. I am not sure if this is a noise issue, but the scope on my DMM shows a reasonable square wave at 2.5 V average. The peaks are around VCC, and the frequency is 13 Khz.
TrickyDick
In general terms, go with the schematic on page 7 of the docs, with the pull-ups.
The issue with Stamps and encoders is efficiently using the pulse streams when things get fast. This means either high pulses per revolution or high revs or both. Thirteen KHz is very fast in terms of the Stamp's high speed quadrature counting ability.
You don't show your code, but assuming the code is made to deal with quadrature counting, there are a couple of tests to try to pin down where things are going pear-shaped.
While watching the debug screen, very slowly turn the encoder while watching the count. If the pulses are coming too fast, you will see the numbers increment erratically.
When you get a feel for how fast the program will count, note the number on the screen. Turn the encoder some known number of revolutions, say two or three. Now, turn it the same amount in the opposite direction. The number shown should be the same as the starting number.
Cheers,
Thanks for the quick response!
The code that I am using is one which I plagerized from the forum. I believe it is designed to handle quadrature counting, as it appears to be a spin off of the code written in this article: http://www.parallax.com/dl/docs/cols/nv/vol1/col/nv8.pdf
The code is as follows:
[ Program Description ]
' This code demonstrates reading a rotary/optical encoder using a BASIC
' Stamp 2 Module. This code will work on the BS2e, BS2sx, BS2p24, BS2p40,
' BS2pe and BS2px24.
'
[ I/O Definitions ]
' A Output Connects To P0
' B Output Connects To P1
'
[ Variables ]
oldBits VAR Word 'Nib ' Previous State Of I/O Pins 0 - 3
newBits VAR Word 'Nib ' Current State Of I/O Pins 0 - 3
direction VAR Word 'Bit ' Direction Encoder Is Turned
counter VAR Word 'byte ' Counter (0-255)
'
[ Initialization ]
OUTS = %0000000000000000 ' Set All Output Pins Low
DIRS = %1111111111111100 ' I/O Pins 0 - 1 Inputs
newBits = INA & %0011
'
[ Program Code ]
Main:
DO
newBits = INA & %0011 ' Get State Of I/O Pins 0, 1
IF newBits <> oldBits THEN ' Have Bits Changed?
direction = newBits.BIT0 ^ oldBits.BIT1
counter = counter - 1 + (2 * direction)
DEBUG HOME, DEC5 counter ' Show New Counter On DEBUG Screen
oldBits = newBits ' Update oldBits
ENDIF
LOOP
STOP
I will try the expirement you recommend, and record the result. I did manage to look at the output of the encoder utilizing a Tektronix O'scope, and the output of the encoder had quit a bit of noise. I wonder of a line driver would not help fix the problem?
TrickyDick
Double check your encoder sensor output and signal into the stamp.
I just checked the inputs with a fine tooth comb. It looks to be hooked up correctly, and I have traced the output of the encoder all the way to the input of the BS2 with my oscilloscope.
The link you provided is helpful. It appears as though I need to utilize the SX chip, which oddly enough is already en route to me, as I bought a second hand Parallax Professional Development Board, which the seller included those DIP's as part of the buy.
I also tried slowing the motor down by hand turning it, and that seemed to stabilize the counter, but its still no where near were I need it. The Motor I am using has a 60.5:1 gear ratio, and is almost impossible to turn by hand, without hurting yourself. I have been able to slow it way down by turning down the voltage from 19VDC to 5VDC, but it still turns too fast, but if you add friction to the shaft, you can see the counter begin to work "better" the slower you go.
I guess I had better research what I will need to implement the SX chip, as it appears as though they wont be around for long!
TrickyDick
I've interfaced Red Lion counters and small PLC's to Stamps several times in order to get around this kind of problem.
After further analysis, the pulses will be running at almost 30 - 50 KHz when the motor is running at full speed, so I am now convinced this cannot be done with a BS2 alone.
I will do some more research into this approach, thanks for the suggestion.
TrickyDick
512 CPR?
So you're spinning something (probably the motor, surely not the varicap) at 100 revolutions/sec, or 6000 RPM?
Something seems amiss here. Too many counts or too much precision. You'll need to ramp up & down from high speed precisely unless you want to rip those limit switches off. How many turns are you doing, and much precision is actually needed here?
I am certain the motor is NOT spinning at 6000 RPM. The measurement of 50 KHz was taken when the motor was running, unloaded, at 19.1Vdc. I suspect the when the motor is installed, and the drive shaft is attached to the Vacuum Variable capacitor, it will slow substantially.
From what I understand, the quadrature encoder I am using is designed for high precision applications (i.e. CNC Machines, Copiers, etc). While I am pretty sure I do not require this level of precision, my design is constrained to this encoder, as it is now a permanent fixture to my 60.5:1 DC motor. I would imagine the more precision the better, as I am aware that I will have to slow the motor down prior to reaching the limit switches.
I have found a PLC which may help. It is the HCTL-2017 Quadrature Decoder/Counter Interface IC. These are designed to work in conjunction with the AVAGO quadrature encoder I am using. Here is a link to the data sheet: http://www1.futureelectronics.com/doc/AVAGO%20TECHNOLOGIES/HCTL-2021-A00.pdf
The only down side to this chip is I will not be able to clock it at 14 MHz, which may be a problem. Instead I plan on clocking it at 13 MHz and hope for the best.
TrickyDick
I'll check the Red Lion model when I get back to the shop. It was about 3" square, red led digits and had up to 6 transistor outputs which could be set to come on and off as the count progressed.
Besides the usual inputs for reset, etc, it had a serial communication link that the Stamp could use to talk to the display to set and retrieve values.
Will be back to you.
That would be great, thanks for the effort on this. My family is leaving for the Holiday weekend, so no rush on getting the data, as I will not have internet access for a few days.
TrickyDick
If you're stuck, I could make one of my old ones available.
Cheers,
But that's just me...
Let me try the AVAGO PLC, which is en-route to me from Future Electronics. If that fails to yield results, then I would like to re-engage you on the Red Lion. If you prefer, I could send you a PM to negotiate a deal, if that time comes.
Erco:
While I agree, I may not need a qudrature encoder, and 512 CPR, I am pretty sure 1 or 2 CPR will not get the job done. One item, I forgot to mention, is the shaft must turn 30 times to go from limit, to limit. At 1 to 2 CPR, I would probably miss resonance do to the High Q of the L/C circuit I am utilizing. I also don't think I would have the resolution required to effectively slow the motor down when the shaft is nearing a stop.
I think 75 to 100 CPR would work nicely, but since I have this hardware on hand, and its already attached to the motor, I am going to give it try. Should that fail, then I am willing to re-evaluate my situation.
TrickyDick
Your Alexandria/NoVa locale resonated with me. I grew up in Woodbridge and went to Virginia Tech. Go Hokies!
It's funny you should mention NOVA, as I lived in Los Angeles for two years. Worked at JPL on a project, and when it was over, I came back to Virginia.
TrickyDick