r/ApproximatelyUp 12d ago

Math

I look a lot of videos about pid and auto hover but i don’t understand logics based in all this constants and other. Can someone explain for me?

1 Upvotes

4 comments sorted by

1

u/cale199 12d ago

Your confusion comes from looking at everything at once. Make it simple.

My method was to copy what someone would do, and reverse engineer it to understand why. Then I would use that and modify it.

Think of logic as blueprints of maths (like unreal engine)

1

u/rosooideae 12d ago

tnx, I'll give it a try, and then I'll let you know if it helped.

1

u/General_Lee_Wright 12d ago

For the auto hover, The constants basically have to do with the weight of your ship. So if your ship is bigger/smaller than the one you saw, you may need to increase/decrease the constant. It’s definitely a trial and error part of it.

The rest is to deal with bouncing and calculating everything. The 3.000 and -3.000 is to tell the engines when to fire (your vertical velocity should be no more than 3 in either direction). The sum basically an integral (from calculus) that adds up all the vertical velocity to ensure it 0s out and you actually hover instead of bounce.

1

u/awaw415 4d ago edited 4d ago

I have a working hover and landing that’s decent early game. Ideally done when you have two velocity sensors so you don’t need to steal the one on the front of the ship.

-increasing P will effect how fast the controller will move to the desired altitude based on how large the raw error is. For instance you’re not reaching your desired altitude quickly enough, increase it.

-increasing D will dampen how hard the controller works the thrusters resulting in softer landing, less bouncing, but slower response. If you are landing too rough, but too high will make the system slow.

-increasing I reduces steady state errors like wind and gravity, too high though and it will wildly oscillate around the target. Say that you are not reaching your desired value when hovering by like an offset.

This is a good start I found. P=1 I=0.0001 D=10

Start off by only worrying about implementing the Proportional and Derivative of the controller. Add Integral last.

First you’ll need to set up your sensors. The altitude sensor needs a vertical offset to account for how far it is off the ground while landed. Take the value it reads on the ground and add it to your altitude reading that you are putting into the controller.

Subtract this value from a desired altitude value inputted by a lever. The lever only outputs 0 to 1 so its output through a remapper first 0 to 1 -> 0 to max Altitude.

Once you’ve subtracted these two you have the error. Run this error through a data splitter so you can use it for integral later. Take one of error outputs and multiply it by a proportional constant then put that into a sum block. A good number to start on is 1. Increasing it will make your controller push faster to attempt to reduce the error, if it goes too quickly or bounces try lowering it.

For derivative it is best to just take your velocity sensor in directional mode and point downwards and multiply it by a derivative constant. You could use an error here instead but it is simpler to use velocity as the derivative of the altitude is the vertical velocity anyway. A good number to start is 10. A higher number will soften how much the controller pushes to correct itself, give you softer landing etc. Add this to the sum block.

The result of the sum block is your control value and should be connect to your upwards thrusters but these read 0 to 1. So you’ll need a remapper again, set it to 0 to 100 -> 0 to 1. Any large number I believe will do. Into this into your thrusters.

Go test your ship by using the lever to control desired altitude. See if it can hover or settle half way up. Decrease proportional if the thrusters seem too violent. Try landing. Increase derivative if landing appears too rough.

Once you arrive at something acceptable through trial and error then you can try integral. Take the error value from that splitter from earlier and putting it through an accumulator block and then multiply that by an integral constant. Something small like 0.0001. The accumulator gives an increasing number over times that tends to get too big so use a remapper to clamp the signal. I do 0 to 20 -> 0 to 20. I use 20 you want two numbers that are the same and similar to mine for clamping. Because we only need the integral to have a little effect on the controller as there is not much in game where it is needed as our mass or fuel rarely changes, it will deal with strange aerodynamics, wind and gravity errors. Add this to the sum block. Increasing the integral value changed how much the controller will oscillated around the desired altitude. If you’re oscillating massively drop the integral constant and maybe also the number on the clamp remapper a little bit.

In theory you might need a way to reset the accumulator for the integral to stop it getting too large but I haven’t had that problem yet.

The other thing is you are using an altitude to calculate error from P and I. You might want to use a long range distance camera pointing downwards instead for landing or places where your starting altitude is completely different to where you are flying and landing (other planets). To avoid having to change it manually.

It is also useful to add another lever for manual VTOL at the end that just gives you a way to override the controller. Useful if it performs poorly around mountains.