Happy Monday to all!
I had a revelation while sitting in the shower....

We have two mechanical encoders on our bot. They have four output pins each. One is a ground, one is for the voltage or whatever, and the other two are signals. The two signals are offset by either 1/2 or 1/4 of a sine wave. At andy rate, they don't change at the exact same time.

So I was thinking.... Is there a way that I could combine the two signal wires into one and stick each encoder on one of the digital interrrupts (3-6) because they can detect a change in state? If I could it would make coding much much easier and it'd be sweeeeeeeeeet. I just don't know enough about the electric signal coming out of the encoder to be able to judge.

Thanks alot.

By the way.... Did anyone else wake up this morning and hope that Monday wasn't here ALREADY!!!!

Kevin Karan

02-23-2004, 07:22 AM

The reason it has 2, is because it has 2 sensors on it. they are set half a notch apart from one another. The theary behind it is that you get directional ability by seeing the order the things hit. If you dont need direction you could just use one of the signal wires. If you want high res and all the features of the encoder (do you want to know if its going forword or backword) you could just put the second signal wire on its own interupt (excluding the power/ground).

seanwitte

02-23-2004, 07:40 AM

Kevin Watson posted an encoder example on his web site kevin.org/frc. The output of the encoder is called Gray Code and is set up so that only one channel changes at a time. Channel A leads channel B by 90 degrees. Thats just a fancy way of saying that channel B won't see a state change until halfway through a state change on channel A. Here is the order the signals will change as the shaft rotates:

A B
-- --
0 0
1 0
1 1
0 1
0 0

Looking at Channel A, there are two rising edges (transition from 0 to 1). In one direction, from top to bottom, Channel B is 0. In the other direction Channel B is 1. That means that an interrupt triggered on the rising edge of Channel A will give you both distance and direction. You will connect Channel A to an interrupt set up to look for a rising edge and Channel B to a digital input. Inside the ISR, if Channel B is high you're moving in one direction and if its low you're moving in the other.

Kevin Karan

02-23-2004, 07:52 AM

Kevin Watsons example only shows how to set up the interupts. It is alot more involved than that. You must be very acurate with your messurements of wheel diameter or your distance/speed readings will be way off. You also need to know how many times the state will change per turn.

Jeff McCune

02-23-2004, 12:02 PM

Forgive me for possibly asking a dumb question but aren't you making this way harder than it needs to be? We've just got an aluminum disk with 30 holes punched in it and the encoder that first gives you. Our interrupt handler is a whopping shaft_encoder++;

Kevin Karan

02-23-2004, 12:57 PM

30? We got a laser cut piece of polystierene with 16 holes and it works fine. I guess what direction the bot is going by assuming that power over 127 is forward and below 127 is reverse. Good for speed, bad for distance :D

seanwitte

02-23-2004, 01:32 PM

Forgive me for possibly asking a dumb question but aren't you making this way harder than it needs to be? We've just got an aluminum disk with 30 holes punched in it and the encoder that first gives you. Our interrupt handler is a whopping shaft_encoder++;

The wheels on our robot are 12" in diameter. It would be difficult to mount an encoder disk anywhere but on the output shaft, so 30 holes would give us a resolution of 1.25". Not too bad. The down side is that if we get pushed backwards we won't know it. It also takes time to punch 30 holes in a disk, mount the sensors and debug it. We bought a 128 count/rev optical encoder. It gives us a resolution of 3 tenths of an inch, plus direction for free. Its one extra line of code to check the other encoder channel to get the direction. For a tank-drive robot the math is fairly involved to convert the distance measurements to the actual position. We figured that if we're going to do it we should have the best resolution possible.

mightywombat

02-23-2004, 02:07 PM

I'm still struggling with how to figure direction.... Any help?

seanwitte

02-23-2004, 02:14 PM

I'm still struggling with how to figure direction.... Any help?

I'm assuming you have an ISR coded already. Connect the signal pin not connected to the interrupt and ground to the signal and ground pins on digital input number 8. Your ISR will look like this:

QUOTE:
Connect the signal pin not connected to the interrupt and ground to the signal and ground pins on digital input number 8

I think I know what you are getting at but could you clarify this?

deltacoder1020

02-24-2004, 03:13 PM

QUOTE:
Connect the signal pin not connected to the interrupt and ground to the signal and ground pins on digital input number 8

I think I know what you are getting at but could you clarify this?

basically, take whichever sensors isn't hooked up to the interrupt pin, and connect that sensor to digital input 8 - you'll need to hook up SIG and GND, and depending on the sensor, possibly +5V too.

Mark McLeod

02-24-2004, 03:56 PM

QUOTE:
Connect the signal pin not connected to the interrupt and ground to the signal and ground pins on digital input number 8

I think I know what you are getting at but could you clarify this?Just a clarification of why you are using the signals this way. At least I hope this ends up clarifying things.

In a quadrature encoder you have two signals out of phase by 90 degrees from each other. That's why you have two signal lines. (The word quadrature means 90 degrees.)
When the primary signal line goes high it causes the interrupt (in this case in digital input 1). If you check the second signal line (in this case on digital input 8) at the same time you will see it will always be low when the wheels are rotating in one direction and always high when the wheels are rotating in the opposite direction. So you can add counts going forward and subtract from your count when going backwards.

Think of two square waves offset from each other like this:

____----____---- signal A

__----____----____ signal B

If you think of a vertical line moving from left to right you'll see whenever signal A goes high (---) signal B is always high too (---).
But moving your imaginary vertical line from right to left, whenever signal A goes high (---) signal B is always low (___).

[edit] I had to come back and finish up this explanation later, because my car pool was leaving.:ahh:

mightywombat

02-24-2004, 08:43 PM

You're a genious....
I totally understand the concept. I'm just struggling as to how I hook it up.
What I tried today (and miserably failed at) is this:

I found a 4-cable pwm-like Y(splitter)-cable. On an interrupt pin (pin1) I stuck the ground to the ground, one of the two signals (The "A" signal) to the signal pin and the +5 to the +5. Then I stuck the split ground to the ground on digital6 and the split +5 to the split +5 and the other signal (the "B" signal) to the signal pin all on digital 6.

Is this correct?

I'm very/pretty sure of my code, as I have played around with interrupts a good deal and have a general knowledge of how they work and how to implement them...

Mark McLeod

02-24-2004, 09:07 PM

While you don't actually need the split +5v or ground on the digital input, only the signal pin is required, it shouldn't matter.

Is digital input 6 also setup as an interrupt? The code seanwitte posted assumes you are only polling. That's why he specified input 8 rather than one of the potential interrupts.

mightywombat

02-24-2004, 09:18 PM

sweet.
thanks for the quick reply.
While I know I don't have pin6 configured as an interrupt, I will try to move it anyways. Thanks for the info. Wiring will be much simpler now.