Help required using optical Quadrature Enocoders

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hello I have 2 US digital E4P quad encoders.I have read some tutorials on using them but not clear in mind.can someone explain a little bit about reading the pulses and how i am going to do it in c language

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've used these parts. They're pretty solid, though they were fairly sensitive to the encoder disc's height about the sensor.

As for reading from them - what are you looking to get from them? Speed? Speed and direction? Absolute position?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
//encoder b,a on pd 3,2
//   ------    ------
//A  |    |    |    |        ->cw    either edge intr enabled pd2
//----    ------    -------
//      ------    ------
//B     |    |    |    |            polled in handler pd3
//------     ------    -------

//---------------------------------------
#pragma interrupt_handler int0_isr:iv_INT0
void int0_isr(void){
//pd2 int0 rising edge   mode 1
char b;
int lppr; //local ppr

  b=(PIND & 0x08)==0x08; //pd3 hi or lo
  if(!b){
    pos++;
  }	
  if(b){	
    pos--;
  }
  lppr=ppr;
  if(pos > lppr) pos -= lppr;
  if(pos <    0) pos += lppr;	
//	putc('.');	
}

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

nleahcim wrote:
I've used these parts. They're pretty solid, though they were fairly sensitive to the encoder disc's height about the sensor.

As for reading from them - what are you looking to get from them? Speed? Speed and direction? Absolute position?

I will be measuring the rotation of shaft and will try to run the robot straigh by removing the difference between both tyres rotations.So Speed basically.

You have some kind of sample code or advice in using ?

huge Thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

bobgardner wrote:

//encoder b,a on pd 3,2
//   ------    ------
//A  |    |    |    |        ->cw    either edge intr enabled pd2
//----    ------    -------
//      ------    ------
//B     |    |    |    |            polled in handler pd3
//------     ------    -------

//---------------------------------------
#pragma interrupt_handler int0_isr:iv_INT0
void int0_isr(void){
//pd2 int0 rising edge   mode 1
char b;
int lppr; //local ppr

  b=(PIND & 0x08)==0x08; //pd3 hi or lo
  if(!b){
    pos++;
  }	
  if(b){	
    pos--;
  }
  lppr=ppr;
  if(pos > lppr) pos -= lppr;
  if(pos <    0) pos += lppr;	
//	putc('.');	
}

hello.thanks for trying to help.Can you elaborate a little please about whats going on ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quadrature encoders can have 1,2 or 4 counts per pulse. That program is the simplest example: interrupt on the rising edge of channel A, increment or decrement depending on state of channel B. Clear now? Also, remember to add one space after a period.

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A quadrature encoder provides two outputs, A and B. As the shaft rotates, these outputs provide two square waves, 90 degrees out of phase. Which one is leading is determined by the direction of rotation.
If you connect the putouts to two input pins on the CPU, then read the pins any time either one changes, you'll see the following sequence:
00 10 11 01
If the shaft rotates the other way, the sequence will also reverse:
01 11 10 00

The easiest way to use this is is to wait for a rising edge on A. then look at B if B=0 increment your step counter, else decrement it. It's simple, and it works, but it only gives you 1/4 of the possible position resolution.
A more complex way of looking at it would be to use the A and B values as a two bit state. Every time the state changes, compare the new state with the old state, and it will be possible to figure out which direction the shaft has moved. For each possible state value, there are only two possible new state values, one for each direction of rotation. For example, a state value of 10 can only change to 11 or 00, depending on which direction the shaft rotated.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The XMega's event system can track encoder positions directly; its got a quadrature decoder built-in. You just connect the wires and configure the CPU. One of the counters tracks the encoder position, counting up and down. I have sample code if you decide to go this way (I used the xmega 192a3).

Rick

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

RickSharpe wrote:
The XMega's event system can track encoder positions directly; its got a quadrature decoder built-in. You just connect the wires and configure the CPU. One of the counters tracks the encoder position, counting up and down. I have sample code if you decide to go this way (I used the xmega 192a3).

Rick

thanks for your offer.Actually I will be using PIC18F so..anyways thanks :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Chris-Mouse wrote:
A quadrature encoder provides two outputs, A and B. As the shaft rotates, these outputs provide two square waves, 90 degrees out of phase. Which one is leading is determined by the direction of rotation.
If you connect the putouts to two input pins on the CPU, then read the pins any time either one changes, you'll see the following sequence:
00 10 11 01
If the shaft rotates the other way, the sequence will also reverse:
01 11 10 00

The easiest way to use this is is to wait for a rising edge on A. then look at B if B=0 increment your step counter, else decrement it. It's simple, and it works, but it only gives you 1/4 of the possible position resolution.
A more complex way of looking at it would be to use the A and B values as a two bit state. Every time the state changes, compare the new state with the old state, and it will be possible to figure out which direction the shaft has moved. For each possible state value, there are only two possible new state values, one for each direction of rotation. For example, a state value of 10 can only change to 11 or 00, depending on which direction the shaft rotated.

Good info my my friend.Will start my work in a couple of days and let you know.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
For example, a state value of 10 can only change to 11 or 00, depending on which direction the shaft rotated.

On (cheap) non-optical encoders it is possible to have erroneous transitions so you have to filter those out. One cheapish encoder I once used had the nasty habit of rapidly toggling just one line when it was (held) close to a detent and that happens when turning it slowly. Using Bob's code it would result in rapidly counting in one direction.

Luckily this encoder went through the whole quadrature cycle for each detent, so I used a little state machine that did not count up/down unless it detected the full cycle, ignoring the 'impossible' transitions.

Though that little FSM with load of if's could be replaced with some clever bit fiddling magic; it didn't need debouncing though and could be used with the encoder inputs connected to interrupt lines so no polling was needed.