Control PID

Hello good morning, I’m new to programming I ask for ccontrol And I would like to see if any of you can help me code a control pid for autonomous, that starts the robot fast and as it gets closer to its target it reduces its speed to zero.

Please someone who can help me :pleading_face::pray:

1 Like

Search YouTube, Google, and VEX Forums. It’s out there. Nobody’s going to give you the code.

Many people here have helped me with small pieces of code or examples to support me and be able to create my code, and I really think those are the people who are worthwhile and make the world Be better.

Really, if you did not want to contribute anything to this topic, you would have better saved your words.

4 Likes

I’ve found the document here to be extremely helpful towards helping me understand how PID controllers work and how to implement them. It explains the concepts in a simple and easy to understand way and even has pseudocode at the end that you can use as a guide for writing your own PID loop. Additionally it explains the process for tuning your PID constants.

4 Likes

I’d like to start out by saying that Giving out code doesn’t help you in the long run, understanding the code is what will help you, people that help with parts of code are helping you understand how it works, but people who just give the code with no explanation don’t.

What you are looking for (a PID controller) can be simplified to a less accurate but easier for beginners to program that is just a P loop (one piece of the PID), and if you are looking for a great place to start I would start with just a P loop.

A P loop, which means a proportional loop, is made when you set the motor power to be the difference between the position the motor is at and the position where you want the motor to be at (or the error).

My first P loop program I made a function to call whenever you want to use the loop where you put in the distance you want to go

void drive(float dist) {

}

that function just has a while loop that loops while the absolute value of the error is greater than whatever accuracy works for you (or whatever accuracy gives you the most amount of accuracy without making the while loop never stop)


void drive(float dist) {
   while (dist-abs(motor.position(degrees))>5){

   }
}

(Abs is a command that takes the absolute value)

Then In that loop I set the motor power to be the error


void drive(float dist) {
   while (dist-abs(motor.position(degrees))>5){

 motor.spin(fwd, dist-motor.position(degrees), pct);
   }
}

(Abs is a command that takes the absolute value, fwd means forward, and pct means percent )

I tested this, and it didn’t quite give the desired results, the optimal motor power was not quite proportional to the error, so I multiplied it by a constant (Kp)

double Kp = 0.75;

void drive(float dist) {
   while (dist-abs(motor.position(degrees))>5){

 motor.spin (fwd, ( dist - motor.position(degrees)) * Kp, pct);
   }
}

(Abs is a command that takes the absolute value, fwd means forward, and pct means percent )

When Kp is calibrated, the robot should give the desired result, a more accurate auton than just wait commands.

To call the function just do drive(what ever distance you want in degrees);

This is not the exact code I made, I am just typing this from memory so it might be a bit off, and if you want to add more motors make the error equal the average motor position. I also later made the distance equal inches instead of degrees by using the circumference of the wheel

When I wanted more accuracy, I learned how to make a PID and improve this code, but this worked for the first couple competitions.

I hope this helps, and good luck with programming!

(Edit: you can just add the D and I to this loop, but what I ended up doing was implementing multithreading to make it run with other stuff, but if you are newer to coding I wouldnt suggest that)

13 Likes

Hello, I appreciate very much that you have made this explanation, but I still have a doubt. Why is there a minus sign between “Dist” and “abs”?

it is a calculation for the error, the loop will exit if the error is less than 5.

1 Like

I’m pretty sure it’s supposed to say abs( dist-motor.position(degrees))

This is to check if the magnitude of the error is at least 5

1 Like

Good catch, that was a typo, sorry about that, I was trying to type it from memory as I didn’t have the code right in front of me
(Edit: what I had written only works properly when dist is positive)

2 Likes

Thank you, now, why is 5 chosen as the number?

it was just a random number