Vex v5 PID shaft encoders

Hi, I was trying to use shaft encoders for autonomous with the PID loop. I can not seem to figure out how to do it.

I’m assuming you mean how to use the encoders to calculate error and target, and you know how the rest of the loop works. For your target, the easiest measurement to write auton with is generally considered to be distance, and then the function will convert the distance to degrees for the encoder. I would recommend using inches, since basically everything else in vex uses them. Take the distance you pass as a parameter and multiply it by 360/(2πr), 2*r being the given size of the wheel from vex (i.e. 4" or 3.25"). If your wheels are on a gear ratio, you will need to adjust for this. Say your wheels are on a 3:1 gear ratio for torque, and the encoder is on the shaft in the motor. You would then have to divide your target by 3 along with the degree conversion to account for the fact that the wheels are travelling 3 times slower. After this, set error to target, and your loop should be good to go.

3 Likes
Use motor.rotateFor(for, distance in revolution, rev, true or false to make the command blocking or unblocking);

Hope this helps

1 Like

If possible, could you please paste your code? :slight_smile:
@anthonym3713

I was trying to do this:
#include “robot-config.h”

int main() {
while(1) {
Motor.spin(vex::directionType::fwd, 30, vex::velocityUnits::pct);
Motor2.spin(vex::directionType::rev, 30, vex::velocityUnits::pct);
Brain.Screen.clearScreen();

// display the encoder value on the screen
Brain.Screen.printAt(1, 20, "Encoder value: %f degrees", Encoder.velocity(vex::velocityUnits::pct));

// display the encoder velocity on the screen
Brain.Screen.printAt(1, 40, "Encoder2 speed: %f degrees/sec", Encoder2.velocity(vex::velocityUnits::pct));

//Sleep the task for a short amount of time to prevent wasted resources.
vex::task::sleep(500);
if(Encoder.velocity(vex::velocityUnits::pct) > Encoder2.velocity(vex::velocityUnits::pct)){
    Motor.spin(vex::directionType::fwd, 10, vex::velocityUnits::pct);
}

}

}

the if(Encoder.velocity(vex::velocityUnits::pct) > Encoder2.velocity(vex::velocityUnits::pct)){
Motor.spin(vex::directionType::fwd, 10, vex::velocityUnits::pct);
was not working for me