syntax for shaft encoder?

Hi, I recently started using sensors on my robot and i’m having some problems on programing the shaft encoder

Here are my questions: (ps. I am using easy C for programing (V2))

  1. I only know that 1 wire goes to interrupts, but how about the other one?

  2. please correct me if i misunderstand the commands for shaft encoder.
    “start” means to start the sensor
    “get” means to get value from the sensor
    “preset” means to set a specific value for the sensor
    “stop” means to stop it

  3. last question, I have no idea how to use those commands.I don’t really understand the samples on the vex official website. can some1 show me some other samples of using all 4 commands?


Yes, 1 wire goes into the Interrupt (Int) port, and the other goes into the Analog/Digital (A/D) Port. Sorry that I cannot help you with much else, because I do not use EasyC, instead I use RobotC.

This is a good place to ask questions… :wink:

You have the newer Quad Optical Encoders with the Two Connectors. That means that you can use one or the other, or Both, if you want to be able to determine the Direction of the Rotation. Ideally, if you are using both they should both go the the Interrupts. EasyC v2 supports the Quad Encoders with the [FONT=Courier New]StartQuadEncoder()[/FONT] function, which use One Interrupt and One Digital Input.

Since the Vex Controller is related to the FRC Controller, take a look at this document about using the Timers, -driven Interrupt**Timers which are closely related to Sensors that use Interrupts verses Digital Input…

Yes, that is correct. The reason for Start and Stop is so that Processor time is only dedicated when you want to use the Optical Encoder.

Sure… But the examples are quite straight forward… Maybe I should start with, what do you understand about the Optical Encoder and its Intended Purpose??

Optical Encoders are used to detect Rotation, and Quad Encoders to detect Rotation and Direction. Indirectly, this can be used to Calculate Rotational Speed (RPM), and/or a Distance Traveled, and with the Quad Encoder, which Direction was Traveled.

Umm… so its a Quad Encoder
I only know that this sensor can measure the distance that a robot travels and also limit the distance.
But how can this quad encoder tell u the direction/rotation?
besides, which port in the interrupts does it go to?

Yes, see the Incremental Rotary Encoder section of Encoders on Wikipedia.

Actually, you calculate the Distance, and then you decide if you want to limit the distance.

For Example, if the Optical Encoder is on the same metal shaft as the Wheel, every revolution of the Wheel is a revolution for the Optical Encoder. The Optical Encoder is 90 Counts per Revolution, so 90 Counts is also One Revolution of the Wheel, a 2" Wheel is 6.2831852" around (2" * Pi [3.1415926]). So, 90 Counts of the Optical Encoder is 6.2831852" of Travel, if there is no slippage.

It does not mater if you go slow or fast, the counter only counts revolutions…

So lets say you drive your robot above, 790 Counts. How far did you go??

790 Counts / 90 Counts per Revolution = 8.778 Revolutions.
8.778 Revolutions Times 6.2831852" = 55.152", or 140.086 centimeters

If you decide that 55.152" is enough, you stop moving, and have just limited your distance…

If you have a different size wheel, or if the Encoder is on a Gear that is not a 1:1 Ratio with the Wheel, you will need to perform additional calculations to get the proper conversion of Optical Encoder counts to Wheel Revolutions, and thus Distance Traveled.

A lot of this is also documented in the Inventor’s Guide - Optical Shaft Encoder download. But, sometimes that can be hard to follow, so keep asking questions…

Two different Optical Encoders that are Out of Phase. I would think that the A Encoder goes in the Interrupt and the B Encoder goes to the Digital Input, because depending on the Direction, the Encoders will Trigger in a different order. Get them backwards, the Direction will report backwards. (I must note that my Optical Encoders are mis-placed, and I can not plug them in and try them this minute, so I am replying of the Top of My Head)

thanks, MarkO, ur explanation really helps me understanding how encoders work :slight_smile:
but i still have some questions on how to programing it.

  1. what is the purpose of presetting the encoder? so it doesn’t start at negative value (or any other unknown value?)

2.when u preset the encoder, can u preset the same encoder later on in the same program flow under the same condition? ex: while (1==1), if (a==0)…etc?

  1. when u preset the value for an encoder and then start it, does it mean the encoder starts to count distance from the set value? (preset it to 0 and start it will that mean start counting from 0?)

4.when using the syntax, “start”, what does that mean to invert the direction? so it can measure it in counter-clockwise or clockwise? Does it always measure in clockwise?

OK… I am quite limited on trying things these next few days… I have a Chemistry Final on Monday, my Optical Quad Encoders and Programming Dongal is misplaced…

Note that you are doing nothing to the Encoder itself, you are Setting the Value of the Variable that holds the number of Clicks of the Encoder.

It saves you some calculations…

If you want to travel 450 Clicks, Start the Encoder at Zero, and then as you move, you check the current value on the Encoder until you reach or exceed 450.

If the Encoder is not Zero, you need to Save the Value of the Encoder in another Variable (maybe called [FONT=Courier New]Encoder_Start_Value[/FONT], since I prefer descriptive variable names), then as you move, you check the current value on the Encoder, subtract the [FONT=Courier New]Encoder_Start_Value[/FONT] and see if it is 450 Clicks or more.

AFAIK, you can reset the Preset any time. A limit might be that you need to Stop the Encoder, Preset the Encoder, then Start the Encoder, but that can be done any time…

Yes, or set it to 1000, and it will count from 1000.

I think it will depend on which Input is A and which Input is B to determine Clockwise and Counter-Clockwise…

This line is from the EasyC v2 Help.
“Use Invert Direction reverse the positive direction of rotation.”

Read through the Help in EasyC. When you have a Quad Encoder Dialog Box up, click the Help Button to bring up the Help on the subject.

Note the EasyC v2 document states that Vex Labs does not provide a Quad Encoder, which was originally true, but is no longer.

I would suggest that you “Try it and find out.” (A line from a former College Instructor)

There is Four Basic things to test.
Inverted and Non-Inverted in the Code, and Input A and Input B. If the Encode Wires are not labeled (I think mine are), use a Sharpe ™ or Fingernail Polish and Mark one ‘A’ and the other ‘B’.

Make a little table.

Setup a simple program that displays the Value of the Counter.

For each Basic Test:
Turn the Quad Encoder Clockwise, then Counter-Clockwise.
Set the Invert Flag.
Turn the Quad Encoder Clockwise, then Counter-Clockwise.

Switch the A and B Encoder lines.
Turn the Quad Encoder Clockwise, then Counter-Clockwise.
Set the Invert Flag.
Turn the Quad Encoder Clockwise, then Counter-Clockwise.

Note which way the Counter Increments and Decrements. Record your results in the Table.
20100314-002.pdf (64.2 KB)
20100314-002.doc (11.5 KB)

thanks MarkO, now i can program it w/o any problems

but, i still have one more question though, can I just plug in one of wires of the Quad Encoder and so maybe it will work like a normal shaft encoder? or it doesn’t matter?

Did the Table I uploaded help with the Programming??? I was curious to see what you (or anyone else) though of it…

Yes, you can plug in just one of the Wires, and Program just like a Single Optical Encoder.