Can I write PID if I use the function of drivetrain in VEX IDE

As you guys know, PID requires each motor of the drivetrain. BUT, How can I know the position of each motor if I use the drivetrain function in VEXcode pro v5?
Need some help!
Also, do running an PID requires any sensor?

If you look in the robot-config.cpp file, you can see that vexcode internally creates motor objects for the drive motors. However, these motors are not included in robot-config.h and therefore not accesible in main.cpp. To fix this, add extern motor LeftDriveSmart; and extern motor RightDriveSmart; in main.cpp. This lets the program know that these motor objects are defined elsewhere, and it will know to use them when you try to get its position.


Thanks!! I get it, and another question. Have you watched this video before?

I am wondering what are the units of - desireValue and desireTurnValue.
I think it decides the distance that the robot will move in autonomous.

I believe that the units are in degrees of the motor’s encoder. This video was created by @Connor and he can provide confirmation on this.


Yes they are based upon degrees of the motor encoders and not the robot’s heading in degrees itself. If you want to translate it into degrees of the robot’s orientation you will have to do some math in the code to translate it. But honestly, all teams should consider using the inertial sensor with the turning PID. This was merely just for show that you can write a PID without any IMU, but it is highly recommended to make your turning PID based upon an IMU (Inertial sensor preferred).


Thanks for replying!! Do you have any other videos of PID? Like, how does an inertial sensor interact with PID?

Yes, PID is a classic example of what people call a “closed-loop controller” in control theory. Essentially, there is a “target” defined, which is your desired value to reach, as well as an “error”, which is essentially how far away you are from the target. Your current position is found using sensors, whether it is encoders, inertial, global coordinate (gps, odometry) etc. It then calculates your output based on the error in order to try and make error reach zero.

Closed loop controllers, especially PID, are great autonomous programming in VEX. The hardest challenge about programming a routine is generally to make your robot move and turn accurately and precisely. Making your robot turn a certain angle is essentially analogous to saying, “I have a target angle, move the robot to a certain place so that the inertial reading ( = robot heading) is the same as the target”, which is exactly what a closed loop controller does.

Here you go. This is the turn PID I am currently using, which uses inertial as feedback.

PID is essentially a mathematical function f(x) (a set of calculations) that tries to bring the input closer to the setpoint. In fact, all PID functions will look essentially like this below.

void pid(double target){
    // stop having these as global variables ffs, the less global variable you have the better
    double kP = 0, kI = 0, kD = 0; 
    double error = target, derivative = 0, integral = 0, prevError = error;
    double input, output; 

        double input = // sensor input goes here

        error = target - input;
        derivative = error - prevError;
        integral += error;
        double output = error * kP + integral * kI + derivative * kD;

        // do something with the output. For example, feed the output to the motor

All the math will look essentially identical. The only differences between each PID loop are the input (what sensor to use), output (what to do to reach target) and the gains (how much is each of the P, I and D weighted). The inertial sensor acts as the input of PID, because it tells you something about the current state of the robot.

If you would want to learn more about PID, I will strongly suggest you read this document so you can understand how each of the three components work, and how to tune a PID loop. Fully understanding how PID works, rather than copying some code from youtube, will allow you to better understand how your robot is controlled, and it will help you debug your code in the future if you understand what is going on.

Lastly, Keehan told me to send you some regards


Yo, Keehan’s teammate! Thank you for teaching me that much. I saw your video about the Async S Curve. That is so cool. I am wondering if you are willing to teach me more about that.