Can anyone explain how a profiled pid works? The wpilib documentation says it changes the set points and constrains velocity and acceleration (or maybe it constrains vel. and acc. through setpoints), but how does this work? Is it controlling the velocity and acceleration of the error? I am also interested about how the trapezoidal constraints affect the system and how to best start tuning them. Any insights are very much appreciated.
Hi, I’d imagine it is similar to normal PID. here is a paper on controls in FRC which has just about everything in it https://file.tavsys.net/control/controls-engineering-in-frc.pdf
The short of it is, it limits how fast you can change the input (setpoint) to the PID controller (which is also inside of it).
The “Profiled” part is attached to the input of the PID controller; you feed it a goal (where you want to go eventually) and it feeds setpoints (where you want to be ASAP) to the PID controller gradually.
Let’s say you have an elevator on your robot – say, it’s 2023. The elevator is sitting at the bottom, the setpoint is at the bottom, and you activate the controller. Then, in one timestep (20 milliseconds), your code changes that setpoint to the top. The motor goes full power and tries to send the elevator to the top as quickly as possible, and it’s not going to make it there in 20ms.
A profile generator takes that single step input and limits the rate of change, and there are two limits: acceleration and velocity. It’s your job to set those to realistic values for your mechanism.
In this video, you can see the goal changes almost instantly, but the setpoint changes slowly.
As far as implementation goes, essentially, the ProfiledPIDController looks at the current velocity and position of your setpoint, looks at the most recent goal position request and goes, “ok, based on this, what’s the best I can do to head towards where you’re asking?” It’s pretty robust; you can edit this simulation code a bit, feed it setpoints from an analog axis, woggle it up and down, and the elevator will follow a smoothed curve.
There’s a normal PID controller under the hood, but it doesn’t instantly set the setpoint to what you set it, it computes a series of position setpoints that respect the velocity and acceleration constraints you give it.
As far as tuning goes, tune position PID control for small position changes, make it pretty aggressive. Then pick low velocity and acceleration limits, command larger position changes, and keep raising the constraints until it’s as fast as you want it without going unstable.
Thanks for the response.
Just to clarify, does the profile feature incrementally increase the set point (which is what the PID controller controls to throughout the loop) and the rate that which the set points increments is defined by your constraints (velocity and acceleration? Does this mean the constraints are not equivalent to the physical constraints of the actuators (the maximum speed and acceleration your elevator can achieve) but rather values you have to test until they align with your physical constraints? Or am I misinterpreting the profiles function?
Yes, you have to set the acceleration and velocity limits in code to be less than or equal to what your mechanism can actually do – which are probably less than the theoretical maximums. Otherwise, it’s the same problem as originally described – the PID loop will tell the mechanism to be somewhere sooner than the mechanism can get there. For example, it’s not a bad idea to set these up to what the mechanism can do at 10V or less, in case you happen to be trying to drive at the same time.
Obviously, if you set the limits lower than what your mechanism can do, it’ll work just fine, but be slower. That’s why kreeve suggested picking lower limits and gradually increasing them.
That said, the units of the constraints are real units…if you set up your mechanism setpoints in real units. For example, if your setpoints are in meters, your velocity will be in meters-per-second and acceleration in meters-per-second-squared.
Yes. You can use the theoretical maximum acceleration and velocity of your mechanism, but one of the reasons to use motion profiling is to limit acceleration/velocity, so you probably want to set those limits below the theoretical maximums.
Do we find practical maximums to use as constraints through characterization? Or would those be the theoretical maximums you’re referencing?
Use the practical limits when you construct the controller object.
One way to think about what is going on is to go through what is happening each iteration of the main robot loop – many times each second. Let’s say you are using profiled PID for position control of some sort.
Each time the “periodic” code runs, you obtain a position reading from a sensor (say a rotation sensor on a swerve module steering axis). This gives you position, and you can derive velocity from a series of positions taken at known points in time. So, you have the current position and velocity of the system you are trying to control.
You also have the desired end position, which may be unchanged from the last iteration of the loop, or which may have just been updated – it doesn’t matter. Normally, the desired end velocity is zero, because you want to be at rest when you reach the desired position. The difference between your current system state and your desired end state is the “error” you are trying to eliminate – by the end of the movement.
You also have a model of the system – the velocity and acceleration constants. Now, with all of this information, you can create a motion plan to get you to the desired end state. And, this motion plan tells you the velocity and acceleration needed to be on plan right now – and this tells you the error, right now. You then apply PID control (often just the P term) to counter this instantaneous error. This gives you a voltage to apply to the motor.
So, taking a step back, you are reading position, doing some math, and setting a voltage for the motor, many times a second.
Now, for the motion plan. Position, velocity, and acceleration are all related – velocity is the change in position over time, and acceleration is the change in velocity over time. We are dealing with discrete numbers here (taken at fixed points in time and not continuously), but the concepts here are tied to the derivatives and integrals of calculus.
If you graph velocity over time, a trapezoidal motion plan forms a trapezoid. There is a ramp up from the current velocity to maximum velocity (a line segment with a non-zero slope), some amount of time at maximum velocity (a horizontal line segment), and some amount of time deaccelerating from maximum velocity to a stop.
(If there isn’t enough time to reach maximum velocity before it will be time to start decelerating, the trapezoid will look more like a triangle, since it will not spend any time at maximum velocity and may not reach this velocity.)
The maximum velocity and maximum acceleration put constraints on the maximum height for the trapezoid and for the maximum slope of the non-horizontal segments. Any point on this graph can be turned into a voltage, based on these maximum constants. And, then you add in any needed PID correction.
Go look at the code if you want to understand this in more detail or have a concrete explanation. But, this may help to frame what you will see when you do this.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.