Hi all,
First a little project background info (perhaps TMI?):
I have undertaken a project to add a digital scale to my Wood-Mizer portable sawmill. Others have done this using DRO's (digital read-outs) but I want to go a different route because the DRO's invariably fail after just a few months due to the harsh environmental conditions inherent with a sawmill...vibration, dust, moisture. Besides, where's the challenge in buying something off of eBay? My particular mill already uses a rotary encoder to provide vertical movement information to a rather basic PIC-based setworks device that is used to make the drops for the desired board thickness, but it does not provide any absolute blade height information. A physical scale and pointer are used for that. My plan is to read the signal from the existing rotary encoder using a PICAXE to tally the total count and use another PICAXE to convert the count to a measurement and output it to an LCD.
I was surprised to find that it is an absolute encoder...I was expecting an incremental encoder. Having read through at least a dozen threads on this forum about incremental encoders, I already had a plan in mind for accomplishing the task . I found the technical info for the encoder here:
http://usdigital.com/products/encoders/absolute/rotary/kit/MAE3
The one I have is the 10-bit, PWM (1KHz) output version. At first I had no idea how to track the direction and multiple rotations of the encoder, but after giving it some thought I came up with a scheme that seems to work:
Here's my first attempt at coding this, which works when turning encoder slowly by hand:
FYI:
The shaft is turning at 16 RPS, which by my math translates to 62500uS per revolution.
The PWM duty cycle is 1025uS, which gives a max pulsin count of 820 running at 32Mhz
Questions:
Thanks for your help with this.
First a little project background info (perhaps TMI?):
I have undertaken a project to add a digital scale to my Wood-Mizer portable sawmill. Others have done this using DRO's (digital read-outs) but I want to go a different route because the DRO's invariably fail after just a few months due to the harsh environmental conditions inherent with a sawmill...vibration, dust, moisture. Besides, where's the challenge in buying something off of eBay? My particular mill already uses a rotary encoder to provide vertical movement information to a rather basic PIC-based setworks device that is used to make the drops for the desired board thickness, but it does not provide any absolute blade height information. A physical scale and pointer are used for that. My plan is to read the signal from the existing rotary encoder using a PICAXE to tally the total count and use another PICAXE to convert the count to a measurement and output it to an LCD.
I was surprised to find that it is an absolute encoder...I was expecting an incremental encoder. Having read through at least a dozen threads on this forum about incremental encoders, I already had a plan in mind for accomplishing the task . I found the technical info for the encoder here:
http://usdigital.com/products/encoders/absolute/rotary/kit/MAE3
The one I have is the 10-bit, PWM (1KHz) output version. At first I had no idea how to track the direction and multiple rotations of the encoder, but after giving it some thought I came up with a scheme that seems to work:
read encoder (NewPos) and compare with previous position (PrevPos)
if NewPos > PrevPos and the difference is small(TBD), then rotation is CCW (anti-CW)
if NewPos > PrevPos and the difference is large(TBD), then rotation is CW through the zero position
repeat for NewPos < PrevPos...
if NewPos > PrevPos and the difference is small(TBD), then rotation is CCW (anti-CW)
if NewPos > PrevPos and the difference is large(TBD), then rotation is CW through the zero position
repeat for NewPos < PrevPos...
Here's my first attempt at coding this, which works when turning encoder slowly by hand:
Code:
[color=Green];PICAXE-18M2[/color]
[color=Blue]setfreq m32
symbol [/color][color=Purple]NewPos [/color][color=DarkCyan]= [/color][color=Purple]w2[/color]
[color=Blue]symbol [/color][color=Purple]PrevPos [/color][color=DarkCyan]= [/color][color=Purple]w3[/color]
[color=Blue]symbol [/color][color=Purple]Offset [/color][color=DarkCyan]= [/color][color=Purple]w5 [/color][color=Green];holds the difference between PrevPos and NewPos[/color]
[color=Blue]symbol [/color][color=Purple]Cnt [/color][color=DarkCyan]= [/color][color=Purple]w4
Cnt [/color][color=DarkCyan]= [/color][color=Navy]8750 [/color][color=Green];set measurement count initially to 1 inch[/color]
[color=Black]main:[/color]
[color=Blue]pulsin c.1[/color][color=Black],[/color][color=Navy]1[/color][color=Black],[/color][color=Purple]NewPos[/color]
[color=Blue]if [/color][color=Purple]NewPos [/color][color=DarkCyan]> [/color][color=Navy]820 [/color][color=Blue]then [/color][color=Green];prevent erroneous readings that can occur at 0-position
[/color][color=Purple]NewPos [/color][color=DarkCyan]= [/color][color=Navy]1[/color]
[color=Blue]endif[/color]
[color=Green];debug:pause 4000:goto main[/color]
[color=Blue]select case [/color][color=Purple]NewPos
[/color]
[color=Blue]case [/color][color=DarkCyan]= [/color][color=Purple]PrevPos [/color][color=Green];if no position change do nothing
[/color]
[color=Blue]case [/color][color=DarkCyan]> [/color][color=Purple]PrevPos
Offset [/color][color=DarkCyan]= [/color][color=Purple]NewPos [/color][color=DarkCyan]- [/color][color=Purple]PrevPos
[/color][color=Blue]if [/color][color=Purple]Offset [/color][color=DarkCyan]< [/color][color=Navy]410 [/color][color=Blue]then
[/color][color=Purple]Cnt [/color][color=DarkCyan]= [/color][color=Purple]Cnt [/color][color=DarkCyan]+ [/color][color=Purple]NewPos [/color][color=DarkCyan]- [/color][color=Purple]PrevPos [/color][color=DarkCyan]MIN [/color][color=Navy]0 [/color][color=Green];CCW not thru 0
[/color][color=Blue]else
[/color][color=Purple]Cnt [/color][color=DarkCyan]= [/color][color=Purple]Cnt [/color][color=DarkCyan]- [/color][color=Navy]820 [/color][color=DarkCyan]+ [/color][color=Purple]NewPos [/color][color=DarkCyan]- [/color][color=Purple]PrevPos [/color][color=DarkCyan]MIN [/color][color=Navy]0 [/color][color=Green];CW thru 0
[/color][color=Blue]endif
case [/color][color=DarkCyan]< [/color][color=Purple]PrevPos
Offset [/color][color=DarkCyan]= [/color][color=Purple]PrevPos [/color][color=DarkCyan]- [/color][color=Purple]NewPos
[/color][color=Blue]if [/color][color=Purple]Offset [/color][color=DarkCyan]< [/color][color=Navy]410 [/color][color=Blue]then
[/color][color=Purple]Cnt [/color][color=DarkCyan]= [/color][color=Purple]Cnt [/color][color=DarkCyan]+ [/color][color=Purple]NewPos [/color][color=DarkCyan]- [/color][color=Purple]PrevPos [/color][color=DarkCyan]MIN [/color][color=Navy]0 [/color][color=Green];CCW thru 0
[/color][color=Blue]else
[/color][color=Purple]Cnt [/color][color=DarkCyan]= [/color][color=Purple]Cnt [/color][color=DarkCyan]+ [/color][color=Navy]820 [/color][color=DarkCyan]-[/color][color=Purple]PrevPos [/color][color=DarkCyan]+ [/color][color=Purple]NewPos [/color][color=DarkCyan]MIN [/color][color=Navy]0 [/color][color=Green];CW not thru 0
[/color][color=Blue]endif
endselect
debug[/color][color=Black]:[/color][color=Blue]pause [/color][color=Navy]4000[/color]
[color=Purple]PrevPos [/color][color=DarkCyan]= [/color][color=Purple]NewPos[/color]
[color=Blue]goto [/color][color=Black]main[/color]
The shaft is turning at 16 RPS, which by my math translates to 62500uS per revolution.
The PWM duty cycle is 1025uS, which gives a max pulsin count of 820 running at 32Mhz
Questions:
- On line 21 of the code I would like to test NewPos against (PrevPos +/- 2). How can I do that?
- How can I calculate the sampling rate per revolution (PWM duty cycle plus code execution time)?
- Is there a better, faster way to to code this?
- Using this scheme, should I still plan on using a second PIC for the LCD?
Thanks for your help with this.