I have a PID drive function that calculates a power variable that is the sum of the P, I, and D terms. I’ve capped the variable so that it is between [-127, 127] and I later convert that into volts using this function:
int convertPowerToVolts (int power)
{
int maxVolts = 12;
int maxPower = 127;
int volts = (power * maxVolts) / maxPower;
return volts;
}
The volts value is then inputted into motor.spin command:
if(!isGoingFwd){
DriveLF.spin(directionType::rev, volts, voltageUnits::volt);
// Other motors ignored for this example.
}
else{
DriveLF.spin(directionType::fwd, volts, voltageUnits::volt);
}
// isGoingFwd is a bool from the PID drive function's parameters
However, since my power value could be negative, the resulting volts value could also be negative. Would that be a problem? If so, should I cap my power value such that it is [0, 127], thereby not resulting in any negative voltages? Thank you.
Also, is this how to properly insert code into a VEX Forum post or is there some other formatting?
Do you use cortex or v5? V5 does not need PID. If you are using legacy, that is a whole different story… could you post your PID code, so I could take inspiration from it?
I’m using V5. It has the built in PID, but that generally isn’t very good. I’m not really doing anything special with my PID function, and it’s mostly the same as found in this guide. The main difference is that my while loop runs until a certain time is reached. However, I can share my code with you via message if you like.
So I could have the motor.spin command set to fwd by default and the inputted voltage would determine the direction the motors spin, thereby not needing an if/else statement?
So if I wanted my robot to move backwards, I would have to change the distance argument in my drive function so that it is negative, rather than having a positive distance and then specifying the direction?
That should work… We do something similar, just instead of specifying volts, we specify speed in percent, and negative numbers just make us go the other way. Also please PM me the code you have