Author Topic: Reading Encoders  (Read 2096 times)

0 Members and 1 Guest are viewing this topic.

Offline arixroboticsTopic starter

  • Full Member
  • ***
  • Posts: 119
  • Helpful? 3
Reading Encoders
« on: July 03, 2009, 08:28:36 PM »
Hi all,

I'm working on something that will need to read rotary quadrature encoders. I'm just wondering what are the usual interface people use to connect encoders to MCUs.

Here are the possible interfaces I could think of:

1. Use normal digital inputs, just keep reading the pin and count every encoder clicks. This is probably easiest, but will use up the whole MCU time. Then I'll probably need another MCU to do the processing.

2. Similar to 1, but using interrupts instead. So every encoder clicks will cause an interrupt on the MCU.

3. Use the built-in encoder module in dsPIC33F series. I've done this before, really easy since I don't need to worry about counting the clicks. But I'm trying to keep the cost to a minimum, and dsPICs cost a lot more compared to the others.

4. Use an encoder-reader IC. Does this thing even exists? I think I've seen it somewhere, but I can't really remember. Its an IC that is made specially for reading encoders.

Do you know any other ways? What technique have you used?

Offline SmAsH

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: Reading Encoders
« Reply #1 on: July 04, 2009, 01:16:30 AM »
you could use a smaller mcu that counts the clicks and outputs a corresponding analogue value to the master mcu?

Offline TrickyNekro

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,207
  • Helpful? 15
  • 1.6L Peugeot 307 tuner
Re: Reading Encoders
« Reply #2 on: July 04, 2009, 01:49:18 AM »
4. Use an encoder-reader IC. Does this thing even exists? I think I've seen it somewhere, but I can't really remember. Its an IC that is made specially for reading encoders.

It exists but I don't remember name....

Anyway, encoding is a matter of code.... You really need only one MCU for a quadrature encoder or maybe too....
How to do it....

Let's say you have a global clock that ticks it's own way on your system... It's 2am in the MCU 2am at the encoder (realtime system)...
In order to do parallel processing you need to set time as small small small fragments...
The MCU will read the ports and do all the math in that fragment.
You set that fragments with Timer interrupts, and let's say that our fragment is 1ms for example.
Take a look at the code below

Code: [Select]
' Timer interrupt is set to 1ms


 IF a = 1 THEN
  a = 0





 a = 1


' Note that this code is executed in less time than the 1ms clock circle.

But if you want to count pulses then what???
Well..... in that case....
We need some more theory...

A signal from an encoder is typically a wave, to detect fronts of this wave you need the transition from low voltage to high
or reverse...To do that you simply need to read the port state. Once every tick of the interrupt.
Measure some ticks for some time then change the speed to the motors respectively.

So the code will look something like this

Code: [Select]
' Timer interrupt every 1ms

P0 = Pin(from one encoder)


 IF a = 1 THEN
 a = 0

  IF b < 1000 THEN
   P1 = Pin(from encoder)
   IF P1 <> P0 THEN
    C = C + 1
  b = 0   ' 1 second has passed
  P0 = P1
  GOTO/GOSUB Motor_corrections

And... well I hope you get the rest of the concept :-P
For whom the interrupts toll...

P.S. I've been inactive for almost a year... Don't give promises but I'll try to complete my tutorials. I'll let you know when..


Offline Soeren

  • Supreme Robot
  • *****
  • Posts: 4,672
  • Helpful? 227
  • Mind Reading: 0.0
Re: Reading Encoders
« Reply #3 on: July 05, 2009, 01:52:51 AM »

Cheapest way (if you're afraid of any overhead on your primary controller) would be to use a PIC10Fxxxx and make a separate encoder that can be polled, whenever you have spare ticks, and then read its present count and direction and clear it.

But you have to have a very demanding application or some very sloppy coding to NOT have time for reading it directly on your primary controller.

A rather fast and fairly heavy robot with quite large wheels needs what? A lot of power?
Please remember...
Engineering is based on numbers - not adjectives


Get Your Ad Here