PID Loop for DR4B

My team needs a PID Loop for a double reverse four bar, if you could provide this it would be of great help

There are dozens of threads here on PID and even more resources outside of VexForum. I’m sure you can find some PID code that will work for you, but remember that you will need to modify it accordingly and tune the constants to your lift.

Actually, a simple google search of “Vex PID Code” provided the following as the first result: https://vexforum.com/t/a-pid-controller-in-robotc/20105/1

which is a full program in RobotC courtesy of @jpearman . You will also find many team’s github repos with PID examples. Here is a PROS example of a PID loop (integral component is not shown)
Please do thorough research in the future.

(Edited to fix a typo.)

A PID loop is a complex feedback loop, and no PID loop for someone else’s robot will work on your robot. As such, you need to learn how to write a PID loop yourself.

If you’ve never used PID before, this can be a bit overwhelming, so I’ll teach you here how to write a basic P loop. A P loop is not quite as precise as a PID loop, but the P is by far the most important part of the loop. Below is the code I use in my P loop.


		task liftPID()
{
   while(true)
     {
	errorLift = targetLift - SensorValue(liftPot); //find error between target and actual
	motor(leftLift) = motor(rightLift) = errorLift * kpLift; //and set motors accordingly
        delay(25);
     }
}

As you can tell, this code is extremely simple. To break down the logic, the robot knows know where the lift is thanks to the lift potentiometer, and it knows where the lift want to be because you tell the robot in the code. Thus, the robot needs to figure out the distance between the target and the actual, and set the motors to something accordingly. If the distance between the target and the actual is 1000 tics, the motors will probably need to be set to something very high to move quickly, but if the error is just 10 tics, they will be set to something very low. In the specific case of my code, I calculate the error by subtracting the target from the sensor value of the lift. So, if the target is 1000, and the sensor value is 500, the error becomes 500. Next, I set the motors to that error times the “kp,” or constant of proportionality. You will need to tune your own kp based on your specific lift. If the kp value is too low, the lift will move too slowly, and if the kp value is too high, the lift will oscillate or spiral out of control once it hits the target. Fortunately, tuning the kp only takes a few minutes. If the kp is 0.1, the motors will be set to 500 x 0.1 = speed 50. This will adjust depending on the error every 25 milliseconds.

If you want the lift to go to position 2500 on the potentiometer, you would simply say (in a different task)


targetLift = 2500;

If you are interested in developing more advanced PID loops using the integral and derivative functions, or if you don’t understand what I just explained, MartinMaVexForever has an excellent youtube series you can find with a quick google search. Best of luck, and let us know if you have any issues with your loop!

1 Like

If the goal is to keep the lift level P is probably sufficient though I would recommend having sensor on both sides and comparing them/ adjusting motor values between the sides as well. If the object is to have preset heights then P is likely fine unless you are lifting both cones and MGs the you may need I as well. “I” was more critical in SS where the weight would vary more dramatically. D just lets you use higher P values (get to the target faster) without overshooting/ossilation. Short answer start with @Anomaly code if you have issues with lift level then sensor second side and drive second motor at same value +/- a % of the difference.

I’ve used code like this before, and I honestly wouldn’t recommend it unless you can’t physically reinforce your lift. My team’s DR4B racks under 2" when the lift is fully extended, so we don’t need anything like this, but for a lift that truly can’t be reinforced, this is good advice. It all depends on your lift setup and build quality.

PID loops work especially awesome to get to specific heights as efficiently as possible. In Skyrise, teams had big old DR4B’s and this worked extremely well. They even optimized and tuned the way up and down separately as a DR4B can have a lot of momentum on the way down (thanks gravity!). In that game there was more weight. You may be able to do just fine having only one cone in your grasp on the arm this game.

But don’t just stop your work when the way up and holds position, keep an eye on the full flow.

Thank you all for the helpful replies, I really appreciate it