Voltage compensation vs. just controlling voltage

I’m reading a bit on voltage compensation and I don’t think I fully get the point of it.
If I set the saturation to 10 volts, from now on every time I use percent output it will calculate off of that? And will it matter whether it’s enabled in my shooter, for example, if my PIDF loop is controlling voltage anyway?

A 10V compensation means that when you set the motor in percent output mode, it will run as a percentage of 10V rather than 12; so if you commanded 50% VBus then it would output 5V.

Correct me if I’m wrong but it will affect PIDV control. Most likely, the only gain you’ll need to change is your feed-forward–you can either manually tune it or use SysId to get good gains, both of which should expect voltage compensation to be enabled.

You have generally the right idea. But the PIDF will respect the voltage compensation.

Think of it this way. You never have 12 volts. Even with a brand new battery, the first time you run even a single motor you’re going to see sags in the voltage below 12 volts. Run an entire drivetrain and you’ll be bouncing around 9 or 10 volts.

As a result, anything you’re trying to control using feedback becomes non-linear (unless it has a voltage term built in).

Voltage compensation helps to moderate that.

1 Like

But if I have a control loop running, and its output is already in volts, how would voltage saturation affect the output? Even if I set it to 5v, I would still be using the setVoltage function…

I understand your concern that voltage is voltage so what’s the difference? Well, it depends … on whose definition of voltage you are using. Sure voltage in the motor controller seems pretty straight-forward, but, for example, the Talons don’t do voltage PID(F).

If you are using the Talon internal PID and you have a voltage that you want, then you get to and must define the conversion from volts to velocity or current. And how did you do that conversion? You made some assumptions to accomplish that.

If you are using say the WPILib motor controllers with or without WPILib PID then the situation is still the same in a conversion has to be done. I know one function in WPILib that you might be using does access the battery voltage from the power distribution hub. That comes fairly close to what the Talon sees as battery voltage but it lags a bit and isn’t exactly the same as the Talon since the Talon knows exactly what it’s putting to the motor.

Other functions that you might be using are just guessing what the battery voltage is based on other things like what velocity you want and how you characterized the robot.

Does that help or still too vague of an answer?

Thanks for the detailed answer, but what if I use the method setVoltage with the output from the PID loop?

It’s hard to generalize, given multiple vendors of smart motor controllers that can run closed-loop, the ability to run closed-loop on the RIO, etc. It’s also the case that the motor controllers are really just turning power on and off very quickly to produce an average voltage, but that level of detail isn’t important here. In effect, the motor controller always winds up producing an output that is between -100% and +100% of the supply voltage – which can and does vary.

But, in general, when you are running some form of closed-loop control (such as PIDF), the motor controller is using whatever process parameter you’ve picked for feedback (position, speed, etc.), doing some math with the PIDF constants (there are other options, such as profiled PIDF which involve other/additional constants), and applying the resulting value. It is sometimes the case that a measured input voltage is also used in the calculation, but it is not always done this way.

So you are probably going to have to either look into specifics including the motor controller you are using and where you are running PIDF, or provide this information here, in order to narrow things down to your specific scenario. Even with this, code and/or config parameters sometimes allow things to be set up to work either way.

Also, there is sometimes the option of having output voltage be the process parameter that is used in the PIDF calculations. This is an area where there are lots of options/permutations.

is a good answer and explains why. You haven’t picked, or told us what you picked, to control the motor - Talon, Spark, WPILib; PID or not; profiled or not; whose profile - Talon, Spark, WPILib or I think there are threads about other controls like bang-bang on CD and elsewhere.

Other tips - you can mix where parts of the control functions are run (WPILib roboRIO, Talon, Spark, etc.) but don’t run the same control protocol in two places at once. Don’t run two PID controllers at the same time one using WPILib and one using Talon, for example.

Similarly, don’t run two different voltage compensators at the same time. Don’t use both the WPILib and the Talon voltage compensation at once. You could get some odd jitter behavior.

I believe the CTRE library at least prints warnings if you try to use setVoltage while voltage compensation is enabled (it may also have additional safeguards).

The SparkMax implementation actually overrides the WPI one entirely and delegates instead to its internal compensation, so it’s entirely safe. I hope the CTRE one eventually does this, but it’ll require some substantial architectural changes on their end so I understand the reluctance.