Quadrature Encoder: Programming and Setup

We have been trying for a couple days and cannot figure out how to setup and program the quad shaft encoder.

We are using EasyC V4 for Cortex.

We are trying to have our robot drive forward for 3 feet and stop. There are limit switches in Digital Ports 1 and 2 and we plugged our quad encoder in digital ports 3 and 7.

PROBLEM: The autonomous program starts, and our motors never turn off. We have attempted to switch ports, but since we have no interrupt ports, confusion has set in.

PLEASE HELP! any advise would be great.

I forgot to post that we have read the vex page on the encoder and have downloaded the test code to no avail.

If there is a quick fix, we would be very grateful!

Did you plug the quad encoders into the digital ports or the analog ports? I am pretty sure they go in the analog ports… because they send more than just 1 and 0…

There’s not much information to go on, I see you have a print statement, are you sure the encoders are counting in the direction the motors are spinning? i.e. since your example keeps the motors running if they are <= 3 are you sure you are not running the motors backward?

If you read the information on the encoders, yous should have found they output 360 ‘ticks’ per revolution, so 3 counts (in your example) would only equate to 3 degrees of wheel rotation (not very far)

Also one last note the Quad encoders plug into the digital ports. you can quickly change the count direction by swapping the two wires / ports plugged into the cortex.

Cheers Kb

No, digital ports. The encoder counts 360 counts per revolution so perhaps your test would occur at more like 1000 counts. Check to see if they count positive or negative, if negative then swap the two wires around.

My bad… I never used the red ones only the IMES

Probably doesn’t matter, but I’d try using two concurrent digital ports for your quad encoders (like 3 and 4). If nothing else, it should make it easier to keep track of what is plugged in where! :slight_smile:
Also, try commenting out your motor commands in that loop so you can just manually push your robot back and forth, or turn the encoder by hand, and see the results in your terminal window. That way you’ll get a feel for what values you will be getting.

fretless_kb sorry for the lack of information. We werent exactly sure what are problem was, so addressing it with the correct information proved difficult.

The reason we had it at 3 “ticks” was because at first we set it to 360 to see how far one complete rotation would get us, but it ran 25 feet. Which, unbeknownst to us was the full 15 sec autonous. In our counfusion we set it to 3 to see if this would lessen, but ofcourse it didnt.

So this afternoon for trouble shooting we should

  1. move the quad encoder to digital ports 3 and 4
  2. and “try commenting out your motor commands in that loop”.
    (is this meaning move my motor commands?) sorry we are very new to robotics.

Also I found this program this morning, would it work better, or should I stick to how we have been doing it.

I can’t currently remember if easyC will automatically update the ports you’ve selected to the Interrupt and Output setting. You may need to manually do this in the Controller Configuration. You can change it by clicking on the little diagram next the port you’re planning on using. If I remember correctly, the interrupt port will be a squiggly line and the output/input will be arrows pointing their respective ways. (facing away from the output and vice versa). I’m going to try to update this post in a few hours, when I can get into the robotics room with correct pictures.

P.S. You’ll need to look at the quadrature encoder block to find out which should be the interrupt and what the other should be.

To my knowledge we do not have interrupt ports. I know that the PIC had them, but since we are using Cortex we have 8 analog and 12 digital ports. I am not sure if they do change automatically, although I know that we can change them. For a quad encoder are they suppose be inputs or outputs? I was thinking it was an input, am I wrong?

BTW thank you for looking when you get there, we appreciate it.

The terminology for digital ports was changed when the cortex was introduced. Any of the digital ports on the cortex can be used to generate an interrupt to the microprocessor inside. An interrupt routine is a special piece of software that runs when a device (like a quad encoder) needs immediate attention, the microprocessor stops whatever it is doing and “services” the interrupt request.

The quad encoder has two outputs (hence two cables) each of which is an input to the cortex, this shows the basics of how it works.


We only have the A and B signals, the Z signal is used as an index and would need a third connection to the cortex if it was available. Each time the A or B signal changes state, an interrupt is generated and a counter incremented (or decremented) in the cortex’s software.

The cortex should be setup to have two digital inputs to be used with a quad encoder.

No worries, I was just expressing the difficulty of determining the root cause of the program with little to go on.

This doesn’t really matter for Easy C, All of the Digital ports can be Interrupt Ports on the Cortex, and Easy C allows you to define any set of ports for use with the Quadrature encoders. Robot C ensures they are side by side, but I’ve tried both and had great success.

I don’t recognize comment #2

I can’t really read the second screen picture, you should really consider attaching the program file instead of a picture of the screen.

But back to your problem,

I would suggest your original code is fine to test with. Change 3 to a bigger number say greater than 360 for one revolution.

Watch the output at the print statement, is the encoder value increasing or decreasing? or is it not changing?

Good Luck, Kb

< added - Wow what a great post by jpearman as I was eating dinner! >

Ok so we figured it out. Thank you to anyone who assisted us in getting the quad encoders to work in our autonomous.

The Fix:
1st we moved the quad encoder to digital ports 3 and 4 (which i realize wasnt key, but it did make things a little easier).

2nd we put in a get quad encoder before the while statement and in the statement.

3rd we removed the if/then statement and replaced it with a while loop. I am not sure if this is sound programming, but it did the trick for us so we are pleased with it.

4th we actually programmed the while statement correctly encoder1 >= -165 to recieve just under a half rotation; reason being that we have it geared 5:1, so it is actually rotating just under 2.5 revolutions (about 66 inches).

I posted the picture of what we have so far (also with the addition of our second encoder). It looks odd, but look at the second pic and how the wheels are placed…might shed some light on the oddity of our program.
xdrive robot.jpg

Congratulations, and thanks for the status update!

Best wishes in your competitions.