PDA

View Full Version : Calculate Distance Using Rotary Encoder



guanerrr
- 15th February 2006, 07:26
Hi, I need help how to calculate distance using rotary encoder. I wanna show pulse in LCD.

The rotary encoder spesification as follows:-

Pulse/Revolution: 200 p/r
output: 2 phase, A & B
Micro C: PIC 16F877A, 4 MHz

I try the code below, but I cannot see the pulse of rotation exactly.

Code :


Define LCD_DREG PORTD
Define LCD_DBIT 4
Define LCD_RSREG PORTE
Define LCD_RSBIT 0
Define LCD_EREG PORTE
Define LCD_EBIT 1
define PULSIN_MAX 200
ADCON1 = 4 ' Set PortA 0, 1, 3 to A/D inputs

pulse VAR BYTE

loop:
pulsin portd.2, 1, pulse
Lcdout $fe, 1
Lcdout #pulse
Pause 1000

GOTO loop

sougata
- 15th February 2006, 11:01
Hi,

You are using two time consuming statements , LCDOUT and pause. If your encoder generates pulse in this phase you would loose count. So it is best to set an interrupt and increment the value in ASM. I do this for my projects. (A particular application is a depth indicator on a old mine tub line). In fact you can also use instant basic interrupts (See Darrel's post on instant interrupts) to handle this in basic itself. It gives you fair amount of speed without the pain of asm.

Good Luck

Wishing the speedy recovery of our forum member steve who is hurt in a car accident.
Best Regards
Sougata

guanerrr
- 15th February 2006, 13:54
Sorry, I'm still not clear about using interupt. Please give me more details. TQ

sougata
- 15th February 2006, 14:14
Hi,

What is the maximum expected rotation per second. And with your current setup what are you exactly experiencing.

Regards

Sougata

Wishing a speedy recovery of our forum member Steve who is hurt in a car accident.

guanerrr
- 16th February 2006, 00:24
maximum rev / sec is 20 rps. I'm currently running a line tracer robot. I wanna know how far the robot can go. TQ

sougata
- 16th February 2006, 05:06
Hi,

I understand that you are new to using interrupts but everybody has a first time so go ahead. This forum will always be beside you. Here http://www.picbasic.co.uk/forum/showthread.php?t=3251&highlight=Instant+Interrupts you will find more information. To learn more about how to setup the interrupts, read the datasheet carefully. Enable global interrupt and portb.0 interrupts (your encoder should be connected here), also set the Interrupt edge as per your encoder. Now when you are in the ISR just increment the counter variable. You should check overflow of your variable or use a comparison to increment a pulse/meter (another variable). It is sure you will find problems but do write some code and test.

Good Luck

Sougata

guanerrr
- 17th February 2006, 04:11
I try using this code. But only get forward and backward only. The value of pulse is totaly depend on rotation per second. How I need to maintain pulse / rotation regardless how fast I rotate the encoder. So the distance are varies depend on speed, although the distance is same. Please help



DEFINE LOADER_USED 1

' Define LCD pins
Define LCD_DREG PORTD
Define LCD_DBIT 4
Define LCD_RSREG PORTE
Define LCD_RSBIT 0
Define LCD_EREG PORTE
Define LCD_EBIT 1
define PULSIN_MAX 200
ADCON1 = 4 ' Set PortA 0, 1, 3 to A/D inputs

old var word
new var word
dir var byte
cnt var word ' pulse/rotation

Encoder: ' Routine to read rotary encoder
old = PortB & %00000011 ' Read PortB.0 and PortB.1

pause 2 ' Add some time between reads
new = PortB & %00000011 ' Read PortB.0 and PortB.1


if old = new then goto Encoder ' If old and new equal, read again

old = old & %00000001 ' Only need PortB.0

new = new >> 1 ' Only need PortB.1

dir = old ^ new ' Dir equals PortB.0 XOR PortB.1

If dir = 1 then ' If Dir = 1 then...
cnt = cnt + 1 ' ...Count up
else ' Otherwise
cnt = cnt - 1 ' ...Count down
endif ' End If statement
Lcdout $fe, 1 ' Clear LCD screen
Lcdout #cnt ' Display Hello
Lcdout $fe,$C0, 1 ' Clear LCD screen
Lcdout #dir
pause 10
goto encoder ' Do it forever (I hope)

End

Luciano
- 17th February 2006, 09:36
Hi,

See this thread.
http://www.picbasic.co.uk/forum/showthread.php?t=1552

Read all the posts first.

Best regards,

Luciano

guanerrr
- 17th February 2006, 23:28
I was so wonder by counter result is deferent. When I turn the encoder slower I get more count and when I turn the encoder faster I get less count. Count refers to variable cnt. Can someone explain?

------code--------------

old var word
new var word
dir var byte
cnt var word ' pulse/rotation

Encoder: ' Routine to read rotary encoder
old = PortB & %00000011 ' Read PortB.0 and PortB.1

pause 2 ' Add some time between reads
new = PortB & %00000011 ' Read PortB.0 and PortB.1


if old = new then goto Encoder ' If old and new equal, read again

old = old & %00000001 ' Only need PortB.0

new = new >> 1 ' Only need PortB.1

dir = old ^ new ' Dir equals PortB.0 XOR PortB.1

If dir = 1 then ' If Dir = 1 then...
cnt = cnt + 1 ' ...Count up
else ' Otherwise
cnt = cnt - 1 ' ...Count down
endif ' End If statement
Lcdout $fe, 1 ' Clear LCD screen
Lcdout #cnt ' Display Hello
Lcdout $fe,$C0, 1 ' Clear LCD screen
Lcdout #dir
pause 10
goto encoder ' Do it forever (I hope)

End

sougata
- 18th February 2006, 04:16
Hi there,

When you are turning the rotary encoder slower you get good results:

Reason : Your loop has enough time to "PAUSE" or "LCDOUT" and does not miss pulse.

When you are turning the encoder faster:

It is missing pulses while you are doing other tasks. It is always reliable to use an asm interrupt. No other way out perhaps.

Regards

Sougata

guanerrr
- 21st February 2006, 00:56
Hi there,

When you are turning the rotary encoder slower you get good results:

Reason : Your loop has enough time to "PAUSE" or "LCDOUT" and does not miss pulse.

When you are turning the encoder faster:

It is missing pulses while you are doing other tasks. It is always reliable to use an asm interrupt. No other way out perhaps.

Regards

Sougata


Can anybody tell me how to use ASM interupt. What are the advantages? TQ

sougata
- 22nd February 2006, 01:19
Hi,

I do not have a 16F877A lying around still I would try to one. I basically design with PIC18F452. The memory bank is different than 16F877A. At least I will try to simulate it.

Regards

Sougata

guanerrr
- 27th February 2006, 01:54
I think you can just post the code because I just wanna some idea....TQ

picster
- 8th March 2006, 17:19
You need to UP your sample rate or use interrupts (on change) to prevent aliasing.

If you MUST display the current values on an LCD, I believe you'll need to use interrupts as others have said.

I'm currently working with an encoder with 50 slots, rotating at 1rps (200 positions per second in grey code) and need to sample at <1mS to ensure no errors. This is because there are no absolute guarantees that each of the 4 phases (codes) are equal in length, due to quality control issues with soldering and placement of the LEDs and Photodiodes. Thus, although technically you expect 200 codes per second in this scenario, "normal" oversampling rules dictate 2x that sampling rate, and the error factor that I'm encountering with component placement suggests 5x instead. If you cut your sampling time back, or miss one of these, it can result in an incorrect count.

Thus you're pretty much bound to use interrupts: either on change, or on timer to sample (change is more efficient for obvious reasons).

-------------Picster-----------------------

rishabh62
- 4th May 2012, 16:01
Please help me....
I have a encoder of 1024 PPR and i want to have a Calculation of RPM.... Gear Ration is 150:1
Please help me with

Acetronics2
- 4th May 2012, 16:28
Hi, Rishab - ( Bashir :applause: ! )

It's quite simple : distance is number of pulses, multiplied by perimeter of the wheel, multiplied by 150 and divided by 1024 ... :excitement:

no, no ... no need any thanks .

any other Homework to give us ???

Alain

rishabh62
- 4th May 2012, 16:32
Thanks a Lot but m Using Modicon PLC m218 and need to configure HSC counter for that also i need to convert it to MM for the movement of motor please help if you can!!!

Acetronics2
- 4th May 2012, 16:40
Are you very sure you are on the right forum ???

Here it is the Picbasic Pro for Microchip PICs forum ... not "abcelectronique" with the great and unique " Doudi" san ... :rolleyes:

Alain