Stormworks: Build and Rescue

Stormworks: Build and Rescue

40 ratings
C4V's guide on pids
By C4V Engineering and 1 collaborators
Well this is going to be fun. PID or proportional integral derivative or three-term controller. It's a fun little equation that can be an absolute headache but it's going to be your best friend. If a PID does not work, Add more PID. Ok jokes aside let's get into the headac… I mean PID.
4
   
Award
Favorite
Favorited
Unfavorite
introduction
This is how i'll be organizing it

Basics of a PID
What people have used the setting for
What can you do with a PID and how to set them up?
Trapdoors info on PIDs
Example of PIDs and Links
Final word
Basics of a PID
So a PID has 4 mods and 4 settings. What a PID does is you give it a number and it tries to get to that number with the variable it has. These guys can be a pain to tune and sometimes they won't work for what you're doing but 90% of the time these guys will work and if you understand then they will be your best friend so let's get into it.

The nodes it has is

Start (it has a different name in game but it's basically what it does) - bool, input

Setpoint - number, input
Process variable - number, input

Output - number,output.

Now the setpoint is what you want. So for an engine you would set this to 10 for 10 rps or a alt hold, you would set this to 200 to be 200m in the air.

Process variable is what changes. For a engine this would be the engine rps, for a alt hold this would be your altimeter.

Now the output is where the PID tries to get to that number. For an engine this would be the throttle to the engine. If the engine rps is 5 then it will increase the throttle, if it's 15 rps then it will decrease the throttle. For a alt hold this would be the pitch. If your alt is 100 it will pitch up (ok it will actually just keep doing spins if you don't have a clamp like a gyro) until it reaches 200, if your alt is 300 you get the idea. If it needs to go up it will go up, if it needs to go down it will go down.

Now let's get to the fun part. What is a PID and its 4 settings? Well you got the

P - proportional
I - integral
D - derivative

Or as I call then to help new players

P - sensitivity
I - change over time
D - dampening

Now the last setting is max value or something like that. I'll be honest. I don't know what it does because I haven't tested it. I have a feeling it's a clamp of some kind but i'll have to test that. Yeah it's not a clamp and now i don't know what it does.

info from louiser on max value

The Max Value is kind of like a clamp but not really. What it does it prevent the number from ever going over 1 (if 1) and will better for inputs when the maximum value is 1 (i.e. engine throttle).
From what I know, if the max value is 2, half of it is 1, which is usually the maximum value of a component. This can cause the process variable to be very shaky or never get achieved. If the maximum value is 1 however, half of that is 0.5 so it will eventually stablize.

Now lets go over how to actually use a PID. I'll be using the velocity pivots for this test.

This is what you need:

3x2x3 velocity pivot not the compact one
Throttle lever
Constant on
Battery (if playing with infinite off)
PID (of course)

First make a platform for you to test
Next place down everything from the list
After that go to the logic screen. This is where the fun begins
Hook the const on to the PID, PID output to the pivot
Hook the throttle lever to the PID setpoint and the node of the pivot to the process variable
Hook battery to everything if playing with inf electric off

Ok so now we might want to go over all the PID settings.

P - sensitivity more or less. This is basically just how fast the PID responds
I - change over time. For now we won't be using this
D - dampening. When you get close to your set point this guy will slow down the PID so it hits it on target.

Ok so we are going to be using a PD controller aka we just keep I at zero.

Go into the select tool and set the PID to

P - 0.1
I - 0
D - 0.1

And the throttle lever to a max of 1 and a min of -1 with a sensitivity of 60%. Ok now we have a test. Spawn it in and have fun. So what is meant to happen is the velocity is meant to act like a robot.

Here is the setup if you did something wrong https://steamcommunity.com/sharedfiles/filedetails/?id=2648703862
PID might have different settings but it will work like normal.
What can you do with the settings?
The settings on a PID can be used in different ways. For example if you make P 0 you get a ID controller or if you make I 0 you get a PD controller as I said earlier. So let's get into what I have seen and what types of controllers you can make.

So I usually use all the numbers. They usually end up looking like this.

P - 0.5
I - 0.00001
D - 0.1

When the PID gets close to its number and stops, I will still try to keep moving so over time it will get to whatever your set point is. Now you can also do PD controllers which is what I recommend to new players. As I said they can look like this.

P - 0.1
I - 0
D - 0.1

I do this just to keep things simple for new people just because I can be a bit confusing. Now I have also seen ID controllers. These are.. Umm… well I don't know how they work but I have seen them. The ones I have seen look like this.

P - 0
I - 5
D - 10

Now this is my theory on the controller. Sense if you have I at 1 with a normal PID it just goes crazy but if you have P set to 0 I can't go crazy.. Actually it wont do anything so you have to crank it up a fair bit but with no D it will just go nuts as usual so you have to crank D even higher then I so you don't get a pissed off I.

If you're confused by what I said then I don't blame you. That was even confusing for me to write out and I probably missed the nail entirely.

What can you do with a PID and how to set them up?
So what can you do with a PID? Thanks to TDF for asking in the official since I didn't have my phone on me.
So these are just a few things you can do with the PID.

Alt holds
Turning a velocity pivot to a robotic
Rps holders
stabilizers
Firebox temp holder
Turrets
Guidance systems
Cruise control systems

Everything - cake

Now there are probably even more things you can do but that's just a few things. Now lets go over how to make them. I already went over how to turn a velocity to a robot but let's go over RPS holders and stabilizers.

Ok to get a rps holder your going to need:

Toggle button
Throttle lever
AND gate
Less then
PID
Function 1
Constant number

Ok so get a engine and hook it all up
If you don't know how to make an engine then read this https://steamcommunity.com/sharedfiles/filedetails/?id=2733574790

Place down everything and this is where the fun begins.

toggle button to the AND gate
less then to AND gate
Toggle to PID on
Set the constant number to 3
Constant number to less then B
Engine RPS to less then A
Set function to clamp((x*10),5,10)
Function to PID setpoint
Engine RPS to process variable
PID output to engine throttle
Throttle to function

Now set the PID to

P - 0.5
I - 0
D - 0.5

Ok so this is what should happen. When you press the toggle button the engine starts and idles at 5 rps. When you throttle up it should stop at 10 rps.

The function works like this:
clamp((throttle*maxRPS),idle,maxRPS)

Ok, time for a stabilization system. Were going to do pitch for this. You might want to get a boat or plane for this.

You're going to need:

Tilt sensor
PID
Constand on

Ok for this we are just going to be using a pivot to show how it works. So add a velocity pivot onto your boat or plane preferably on the top sideways for pitch. Hook the PID to the pivot and add the tilt sensor onto the subgrid of the pivot facing forward.

How the tilt should be hooked up
< - - [tilt] vehicle

Next set the PID to the same settings as before

P - 0.5
I - 0
D - 0.5

Ok hook the constant ON to the PID start.

In theory it should keep that pivot pointing straight up all the time. If it just falls one way just invert the pivot with either a function, logic, or just flip the pivot.
Trapdoors info on PIDs
This is all the info Trapdoor has on PIDS if you want to learn how they actually work. I did this to have another resource then me for this stuff since what I could be saying could be a mile off its target.

From Trapdoor: “The P (proportional) is just the error (difference between target and current value) multiplied by the number you used for P (Proportional) gain Say you're making an altitude hold and you set the target altitude at 200m and you're currently at 50m The difference (error) is 200-50 = 150 Assuming your P gain is 1, that's just 150 * 1 = 150, so your output would be 150 If your P gain is 0.1, that's 150 * 0.1 = 15, so your output would be 15

Yea it's for the I and D, but it's not strictly necessary When you're in the real world you can't be 100% sure that the PID runs at exactly the same interval, i.e it runs every second, or 10 times per second etc That's why you include how long ago the PID was last updated, so the values scale correctly with time, You don't have to worry about that in Stormworks though, since we know the PID runs at the same time as everything else, 60 times per second

So if your error (difference in altitude from target to current) is still 150m, and the PID runs. With an I gain factor of say.. 0.1, that will do 150 (error) multiplied by 0.1 (I factor) and add that to a sum So that's 150 * 0.1 = 15 Assume that the error is 140m on the next tick. It will then do 140 * 0.1 = 14 and add that to the 15 from the last tick so you now have 15+14 = 29 Again, next tick we're at 130m difference, so 130 * 0.1 = 13 and the sum is 29 + 13 = 42 Since the PID runs 60 times a second you can probably see how the numbers add up pretty quickly. That's why you see people use a very low value for the I factor like 0.000001 You would use the l factor to slowly move towards the target, if the Proportional term isn't able to get there completely. Lastly we have the D, or Derivative term which takes the difference between the last error, and the current error, and multiplies it by the D (derivative) factor So if your last error was 150m and the error on this tick is 140m, that's 150-140 = 10 and then multiplies it by the D factor You would use the D factor to make sure the error doesn't change too quickly Finally it just adds up all the values from the three terms Output = P +I+D

Sorta understand the math behind it helps with tuning PIDs so you don't overdo it It doesn't make any sense to start with a P of 1000 if you're making an 'altitude hold for example

If the difference between target and current is only 1m, that's 1* 1000 = 1000 and you would be outputting 1000 to your elevators 3 You can find out when it would be -1 to 1 by just doing 1/1000 = 0.001 So when you're 1mm over or under your target altitude, your elevators would still be fully engaged 0.5mm would be half engaged, 0.25mm would be quarter engaged etc”

Now all of that was just parts I took from when he went over it all. If you have discord you can find the conversation here.

(if your in the official discord) Start : https://discord.com/channels/357480372084408322/357480372809891840/869169654147469323

Thanks Trap for letting me stea.. I mean use your info even though I probably forgot some paragraphs and info.

no wait. theres more

Basically all the terms of the PID (proportional, integral, derivative) are totally independent, but they use the same number in their calculation. That number is the difference between the Setpoint and the Process Variable, and it's called the "error". The PID does the calculations and adds them together, and that's your output.
The proportional bit is easiest to understand. It just takes the error and multiplies it by the P gain factor, which in your case is 1.

So a P gain factor of 1 means that the output of the P term is just the error. So for example if your target altitude is 100, and you're at 90m, it'll do (Setpoint - Process Variable) * Gain factor to calculate its output which would be (100 - 90) * 1 = 10
Example of PIDs and Links
These are some links and images of PIDs and what they are since I have probably gotten something wrong so I just want to provide as many resources as possible to help you understand… or get a headache.

Wikipedia on PIDS
https://en.wikipedia.org/wiki/PID_controller

Concept of PIDS
https://www.csimn.com/CSI_pages/PIDforDummies.html

A PID gyro set up
https://steamcommunity.com/sharedfiles/filedetails/?id=2767914353

The velocity to robotic set up
https://steamcommunity.com/sharedfiles/filedetails/?id=2648703862

Other pid guides

Simple - https://steamcommunity.com/sharedfiles/filedetails/?id=1366955561

Advanced - https://steamcommunity.com/sharedfiles/filedetails/?id=1640501833

A vehicle to help you understand PIDS
https://steamcommunity.com/sharedfiles/filedetails/?id=2804873858
Final word
Ok so i'm not the best at PIDs but I know just enough on how to use them and try to teach others. This idea came from a very bad joke and since I knew Trapdoor wouldn't write a guide I thought to myself “hey why don't I write a guide and have a section with all of Trap’s info on them like I did with my engine guide just with bunglist reactors.” and that's what I did. Yes, I know these guides aren't grammatically correct but shut and read. If you want to have these guides be grammatically correct then do it yourself. All I care about is if people get the info they need. Ok that's it so yall have fun and don't get a headache mid class.
6 Comments
Sir_James Feb 9 @ 4:48am 
Very well written. First part explains the PID very well. I had heard of a PID but had no clue what it did until I came across this. Congrats to both of the authors on a job well done.
bongokat Jul 25, 2023 @ 9:18am 
Thanks so much!
IcyPandora69450 Jan 6, 2023 @ 5:11pm 
Thanks, I asked around on the discord and got limited stuff. You were there but busy. This helped me a lot.
C4V Engineering  [author] Jun 28, 2022 @ 8:18am 
i see. makes sense. when i was testing it, it wasnt really working for me.

and yeah patrick its a bit hard to read. reason why i add so many examples so people can get a different approach from it.
ClarityFailure Jun 27, 2022 @ 6:21pm 
The Max Value is kind of like a clamp but not really. What it does it prevent the number from ever going over 1 (if 1) and will better for inputs when the maximum value is 1 (i.e. engine throttle).
From what I know, if the max value is 2, half of it is 1, which is usually the maximum value of a component. This can cause the process variable to be very shaky or never get achieved. If the maximum value is 1 however, half of that is 0.5 so it will eventually stablize.
Patrick Jun 27, 2022 @ 7:46am 
Am I reading a transcript? I did eventually get what's being taught but it's really hard to read.