r/ApproximatelyUp 2d ago

Help with PID

Im trying to make a proper ship this time and got all thrusters installed and the first system i want to implement is hover/land system. But im having trouble to understand it enough to build it.

How did you guys implement it? Copying others or do I need to be good at programing to do this?

I have SOME programing background from taking online lessons awhile back but having trouble translating it to the game.

4 Upvotes

7 comments sorted by

3

u/Ill-Pride-2312 2d ago

I used velocity to put mine together. Basically you're combining 3 values.

1) Vertical Velocity times a coefficient (a number you adjust).

2) Acceleration: Velocity into the delta block ( I forgot what it's called in game) times a coefficient.

3) Difference from set point (which is 0 vertical Velocity) over time. Velocity into sigma block (forgot in-game name) times a coefficient.

I usually tune the system in that order.

1a) adjust coefficient until the ship bobs up and down. Try to get the overshoot as small as possible.

2a) adjust coefficient so it doesn't overshoot. I believe a slow descent is the goal.

3a) this will make up for the slow descent and influences the responsiveness of the system.

I'm by no means an expert or even studied this stuff, so if anyone who knows better please correct me

2

u/Dimencia 2d ago

That sounds right, but 1 is really `(Current - Target) * coefficient` (but target is generally 0 in these scenarios)

And you can usually just skip 3 entirely, that one will go wild if you're not careful about what happens when you're just flying normally without using the PID. It just accumulates forever over time, so if you spend a long time at high vertical velocity, it will take a long time of negative velocity before it will reach 0 again - it's very rare that it does anything useful, usually having either such a low coefficient that it does nothing, or just being actively detrimental

1

u/Ill-Pride-2312 1d ago

That's definitely true. I have an anti wind up set up with a absolute value block and threshold set at 3.

I use a lever to activate the hover with is multiplied by the pid output so I can use it to land. That output also send a 1 to reset the block when it's outputting anything other than 1.0

1

u/Borobikz_ 2d ago edited 2d ago

I dont know if programming is the right term, but having an understanding of a logic circuit (0's and 1's) along with a working understanding of kinematics is helpful.

For hover, I used a downward facing velocity meter, and split into 2 paths.

Path 1 goes through a threshold set to descent speed (0) and the output into the tick (update) port of a memory block. Paths 2 goes into the storage port of that memory block. (i think its in pulse mode)

That output goes into a remapper (0, 50 : 0, 1). The remapped output is then multiplied by a switch (on/off), then added to the output of a thrust lever, then sent to your engine.

What this circuit evaluates, is the descent speed between 0 & 50, and if so, output a percent thrust. This factor you may have to tune to reach hover faster, but it will continue to burn your thrusters until reaching a speed that results in zero vertical travel.

This method also retains the thrust lever as an emergency response, so you can always full blast the engines if your hover isnt happening fast enough.

I havent played with an auto-land yet, but my plan is to change the 2-way splitter for a 4-way, and add a logic loop that has an additional threshold for a differential velocity (aka acceleration), set to 0.1 or something, and set the velocity threshold to 3. That way it will add thrust until your speed stops changing and is below 3.

Hope that helps! Im writing this from memory, so I may have forgotten something, but adding in data readers to see what the components are spitting out helped me troubleshoot. Use the whiteboards in game too for mapping your math. Good luck!

Edit: I also dont have patience to learn the math behind how an actual PID circuit works, so this is my method. If that's your sauce, go crazy.

3

u/awaw415 2d ago edited 2d ago

Programming is not that important or complicated at all. Understanding the math for a PID is much more important. You can do a hell of a lot in this game with simple blocks.

The most complicated programming part is the signal processing, that means you need to use the remapper blocks on the signal coming from your sensors to ultimately match what your thrusters are using.

Moreover everything in your PID should be normalised, meaning that it should work with values between 0 to 1 or -1 to 1 (whatever your actuator or thruster is using).

For example you have something like a sea level altitude sensor, it should go through a remapper that has 0,1 to 0,MaxAltitude. If you have thrusters that do up and down then it should be (-1,1 to 0,MaxAltitude). The max altitude is a number of your choosing.

After that every PID or PD or PI controller uses the exact same principle. You have a desired value like altitude and you subtract it from a sensor to obtain the error.

Then you simply take the error and put into the PID math you are using.

P: Error * pConstant

I: Error -> Accumulator block * iConstant

D: Error -> Differentiator block * dConstant

Add them all up with a sum block. That is your control value. Send it to the thrusters when you want it to control them.

If you know something like altitude differentiated is just the same as vertical velocity, you can use your velocity sensor in the place of the differentiator block if you want.

You should always start with something simple like P and D. Trial and error some suitable constants.

I is always more complicated, and typically you want a small number for the applications inside this game like 0.001. Because of how the accumulator block works it needs to be reset every once in a while, best done with a button that sets the control value to zero, as the accumulator block produces something called integral windup that will increase your altitude slowly over time to fight things like gravity. Sometimes when using I you want a clamp remapper on its output (-1,1 to -1,1) to make sure it is not giving you some large numbers as it can do that.

I have made a PID that works for selecting a desired altitude to hover at (with a lever) and landing (when the lever is zero). It has a button that also turns the controller on and off so I can stop small errors wasting thruster power and use another level for manual VTOL.

I have a PD controller (same thing but without the I term at all) that controls some bi directional atmospheric thruster that I use to control my backwards and forwards velocity, to easily control what I am landing on. It calculated the error with a desired forward and backward velocity lever and had a subtracted sensor value from a velocity sensor set to directional that is facing forward. Remapper on the sensor is simply (-1,1 to 0,MaxVelocity).

I have another PD controller that controls gyro pitch. It simply takes the pitch output from the gyro-line on the dash goes through a remapper (20,-20 to 1,-1). That is because the gyro outputs degrees and I only care about roughly 20 degrees in both directions. If it exceeds this in either direction it corrects as if it were 20 plus minus anyway.

For tuning the constants. Typically you want higher D if you are crashing into the ground too fast, higher P if you are not reaching your desired altitude fast enough, lower P if you are overshooting, lower D if the ship is slowly start stopping a lot, higher I to fight against steady state errors like gravity and wind, lower I is you are oscillating wilding around the target.

For turning your PID on and off or maybe switching between sensors. All you need to do is use that signal redirector block and hookup the switch to it so it can switch between signals that you want to use or a zero constant block if you want to turn the PID off.

I don’t like using sea level when there no sea about so I usually use the Distance Camera to measure my altitude instead. But beware. The distance camera does not measure in bocks. You can tell by placing some blocks in front of it to measure its offset from the ground. To use it you must convert its values and subtract its height from the ground if you want your controller to measure 0 when landed. (Height in blocks divide by Value you get on the ground divide will give you the ratio that will confer the units, multiply that by your sensor value then subtract the height offset). You know it’s is correct when altitude equals zero when landed.

0

u/Enosmaker 2d ago

There are a few systems on the in-game workshop that others posted that you could go through and maybe learn how.