Can you help with us program a shaft encoder on vexcode?

My teams are trying to get better at programming sensors and, we can’t figure out how to program the shaft encoder. Can someone give me a basic code letting me measure distance? We just need a basic understanding so we can make a reliable autonomous.

Well, the shaft encoder can return the degrees that it has rotated, using commands like the rotation or position commands. The formula for circumference is 2 * PI * radius of wheel. Multiplying the circumference of the wheel by rotation/360 would probably give you the distance the wheel has traveled that the shaft encoder is connected to.

2 Likes

Also, VEX Knowledge Base article on shaft encoder:

https://kb.vex.com/hc/en-us/articles/360039512851-Optical-Shaft-Encoder-Sensors-for-VEX-V5

And more info on using/coding legacy Optical Shaft Encoder connected to V5 lgacy 3-wire port:

2 Likes

Yeah, I messed up when I wrote my question, I need to figure out how to write a code that can measure distance.

Like distance in a single direction? If so, my answer still stands, you can use the circumference and rotation to measure the distance the wheel has gone.

Pseudo code:

while (distance traveled is less than target)
{
get rotation;
convert rotation distance to inches using circumference;

drive forward
}
stop driving

If you are talking about odometry, also known as position tracking, that is a whole other slice of a whole other pie.

Odometry links

Where to start with Position Tracking

https://nickmertin.ca/Tracking.pdf

4 Likes

Originally I was just looking for the distance in a single direction but, now we’re getting inertial sensors so I guess I need odometry now.

Can you get me a code to input my calculated distance?

Sorry? I don’t get your meaning.

I’ve got the circumference of my wheel. I’m trying to make an if statement where once the encoder travels a distance the wheels stop

You wouldn’t really want to use an if statement for this (at least in my opinion). If statements are swell for checking things, but not for waiting to do something.

If you look at post 5 (it was my post), I made a little pseudo code that will drive forward until the distance is traveled (it wasn’t that good, but it probably would have worked).

Copied from post 5:

while (distance traveled is less than target)
{
get rotation;
convert rotation distance to inches using circumference;

drive forward
}
stop driving

I’ll make it a lot closer to operable code but you’ll still need to create some variables, put in motor commands, etc.

encoder.resetRotation();

while ((circumference * (encoder.rotation() / 360)) < target_distance)
{
drive forward
}
stop driving

Hopefully that is enough to get you started!

4 Likes

Just for the record I’m trying to use this in my auton. Will that change anything?

Nope! I was writing that with auton in mind, so it should be good.

1 Like

That’s good, that means what I have might work

So in your the code you gave me it says “target_distance” what would I put their if I need multiple target distances?

Your formula confuses me. Multiplying the circumference by the rotations then dividing by 360 doesn’t make sense, it gives me an answer of less than one which, is not close to how far my wheel moves in one rotation. Can you clarify the formula and help me understand it?

We cover how to use encoders and create odometers in our Robot Autonomy and Control Workshop. The recording is not available yet, but the files are below and there are VEXCode programs showing how to do both. You can also go through the included PDF of the presentation for a brief explanation.

Hope this helps!

Best,

Jose
msra_logo

5 Likes

PS. Here are the relevant VEX Code files in case anybody doesn’t have a MathWorks account.Encoder_Odometry.zip (101.2 KB)

3 Likes

I’ve made a code but for some reason the robot won’t start moving do you have any idea why that is?

Hardware issue

Do the motors work, do the ports work, do the motors spin when not powered, does other code spin the motors?

Or

Software issue

Sending a value that is too small so that the motors don’t spin, program ends immediately after telling the motors to move, running driver instead of auton.

These are some possibilities, but there are a much greater number than I have noted. It would greatly enhance the forum’s ability to help if you posted your code.

3 Likes

Here’s my code

#include “vex.h”

using namespace vex;

int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();

EncoderA.resetRotation();
while ((10.205 * (EncoderA.rotation(rotationUnits::deg) / 360)) < 5){

LeftFront.spin(directionType::rev, 100, velocityUnits(pct));
LeftBack.spin(directionType::rev, 100, velocityUnits(pct));
RightFront.spin(directionType::fwd, 100, velocityUnits(pct));
RightBack.spin(directionType::fwd, 100, velocityUnits(pct));

LeftFront.stop(brakeType::brake);
LeftBack.stop(brakeType::brake);
RightFront.stop(brakeType::brake);
RightBack.stop(brakeType::brake);
}
}