Where should PID live - subsystem or command? (Command-based)

I’m curious on what’s considered the best practice for coding the PID.

Does it make sense to create several PIDs for a subsystem:

PIDController forwardController = new PIDController(P,I,D);
PIDController turnController = new PIDController(P,I,D);
etc…

and create getters for commands to retrieve and calculate in their execute block?

OR…

Should the PID Controllers be created in each command? If so does that mean every time a new command is created/scheduled that we’d be creating a new PID? My fear is that if for example I have a command bound to, say the A button, does that mean everytime I hold and release A I’m creating a new PID loop. Would that bog down the rio?

Thanks in advance for any help!

Generally all PID setup and configuration is done in the Subsystem, with the Commands using getters/setters to set setpoints, get current position, turn the PID off, etc.

3 Likes

Yep, this is a good practice. If defining commands inline as in-subsystem factory methods, you don’t even need the verbosity of all the intermediary methods.

Another option is PIDSubsystem/PIDCommand: PID Control through PIDSubsystems and PIDCommands — FIRST Robotics Competition documentation

2 Likes

Wow that was so helpful! That read on in-subsystem factory methods sent me down a rabbit hole - awesome read!

This might be a misunderstanding of PID in general, but with a PID command do I need to end anything aside from the command to stop the PID loop. Does a PID loop only take up resources when the calculate method is called?

The PID runs from the command itself (as opposed to pre-2020 PID).

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.