Overview
A quick demonstration of how to set up and tune a PID for a roll-stabalised seat.
The Setup
For a PID system there’s four key parts:
- An actuator: This is just a part that interacts with the world, it could be a robotic hinge, pivot, engine or piston. In this case we are trying to prevent our seat rolling, so a robotic pivot is the obvious choice. Do not worry about the direction of rotation.
- A sensor: For our system to react to the environment, we need something to measure it. We are using a tilt sensor. To measure roll, rotate the sensor so that the arrow faces to the side of the boat. It does not matter which side. Make sure it is attached to the part of the boat which can rotate, and not the main body.
- A setpoint: This is the target for our system. We want our seat to stay dead flat, so we will use the ‘number’ block from logic. This will be set to zero automatically, so no need to change it.
- An On Signal: This turns on our PID gate. If you want to be able to turn on or off your PID consider using a switch instead.
Switch to logic mode. And complete the following steps:
- Connect the ‘Process Variable’ of the PID to the tilt sensor
- Connect the ‘Setpoint’ of the PID to the ‘number’ gate
- Connect the ‘Control Output’ of the PID to the ‘Rotation Target’ of the robotic pivot
- Connect the ‘Active’ of the PID to the ‘On Signal’
Tuning The PID
Now this is the part where most people get lost, despite the scary appearance it’s surprisingly simple. Before we begin lets break down what the three variables of the PID do.
Proportional:
This is simply a measure of how far off we are from our target. If our measured value and our target value are very similar, the output of P will be very small. Likewise, if the difference is very large, P will be very large. In essence, P = target_value – measured_value.
P does the bulk of the work, but it’s not perfect. Imagine we are designing an auto-hover for a helicopter. At first the helicopter is too low, the difference between are target and position are very large and the engine turns to full throttle to bring us to altitude. Once we reach the right altitude, the PID notices the difference is very small and cuts the engines. Whoops! Now our helicopter falls. Eventually as we fall the difference becomes large enough that our throttle increases and we return to altitude. This leaves our helicopter continually jumping up and down, not ideal!
Differential:
Wait a second, did we skip I? That’s right, I isn’t that useful for basic systems so we’re keeping to the important stuff. D is a measure of how fast we are moving towards the target. If we are rocketing towards are targets and likely to overshoot it, D creates an opposing force. Or, if we are drifting away from our target it increases the correction.
Lets return to our helicopter problem. D doesn’t care about how far we are from the target, just how we are moving towards it. So imagine our helicopter has reached altitude. As explained, this will cause P to decrease and our helicopter should start falling. D detects this fall much faster than P and quickly correct the heli. Now we can fly at a nice stable altitude.
Tuning:
- Switch to the ‘select’ tool in the editor and click on the PID. Now set every variable to zero
- We start with P by setting P to a low value, say 0.1. Spawn the ship and observe how it reacts.
- If the system under-reacts (ie. it moves too slowly to reach our target) we need to increase P. Since values can vary wildly, I tend to double my previous value.
- If the system overeacts (ie. it corrects our rotation too much and we start tilting in the wrong direction) we need to decrease P.
- If the system moves in the wrong direction (ie. The boat tilts, and chair tilts even more) we need to invert the output. Make all values negative.
- Eventually after repeating the previous step we will get a seat that reacts well to waves. But you might find it acts a bit drunk, swinging from side to side like a pendulum
- If our system swings slowly, increase D starting with a low value like 0.1.
- If our system vibrates rapidly, D is too high and needs to be decreased.
And that’s literally all there is. Remeber start with P and keep all variables at zero until you have it tuned to perfection. I’ll add a more complex guide that includes I and multiple PIDs at a later date.