PID is a bit complex. You can find code from all over the forum and basically just copy and paste it, but I would recommend against that, as they are difficult and time-consuming to tune as well. If you feel up to it, go ahead, but I would recommend starting off with something simpler. As @JoseAvendano said, a bang-band controller is the easiest to code, but is also probably the least effective (and itâs not super healthy for your motors). If the intent of your PID is to hold your chainbar where the driver lets go, a proportional control is the best balance between simplicity and performance. The code is pretty simple:
motor[m] = (target - current) * kp;
It is the âPâ in PID, and functions under the same principle.
target
is often called setpoint as well, that is simply where you want the chainbar to be.
current
is the current value that the potentiometer reads, and
kp
is your tuning parameter. For a simple holding proportional control (and a potentiometer), this will probably be around 0.06, but you should increase that number as much as you can before it starts to oscillate. If you use an encoder for some reason, start with kp at 1. Below is some sample code for controlling the chainbar:
int chainbar_target = SensorValue[chainbar_pot];
while (true) {
// other code
if (vexRT[Btn6U] || vexRT[Btn6D]) {
motor[m] = (vexRT[Btn6U] - vexRT[Btn6D]) * 127; // use buttons 6U and 6D to control the chainbar
chainbar_target = SensorValue[chainbar_pot]; // update target to the current position of the chainbar
}
else {
motor[m] = (chainbar_target - SensorValue[chainbar_pot]) * 0.06; // run proportional control
}
// other code
}
However, if your intention with a PID is to move the chainbar during autonomous (or with auto-stack or something), proportional will probably not be enough. By definition, proportional controls have something called âsteady-state error.â This is the difference between the current and target in a static system. As the proportional control approaches the target, it lowers the power supplied to the motor. At some point, this power will not be enough to move the motor, and the chainbar will stop moving, locked in position. This will happen before it reaches the target, and therefore a proportional control will always have some steady-state error, so it may not be accurate enough for autonomous. One way to somewhat reduce this issue is to add a constant component, so your proportional code would change from
motor[m] = (target - current) * kp;
to
motor[m] = (target - current) * kp + c;
where
c
is the constant. Again, you will have to tune this value, but a good start would be around 15-20. The constant should be as low as possible while still holding the chainbar up at its maximum horizontal extension. This will help, but not totally alleviate the issue (nothing really can).
If this isnât good enough, youâll have to either make a full PID (check this out), or replace the constant with an âangularâ component, which Iâll describe in another post, because Iâm worried about the character limit in this one.