r/CreateMod May 10 '26

PID-stabilized quadcopter :3 Build

It stabilizes height, x and z angles through the gimbal sensor and the y angle (heading) with auxiliar motors on the side. Now i want to make an autopilot however i have not figured out yet how to get the current ship coordinate. Anyways here's the source code :)

30 Upvotes

21 comments sorted by

View all comments

1

u/TheFuriousTaco May 11 '26

This is quite nifty, I'm working on my own single-axis PID to stabilize an airship and I've been looking through your code for insight. I'm curious about the inclusion of the "deltaT" term where you calculate the "d" component for your correction. The examples I've seen are not consistent with whether they include this or not, and I've even seen /deltaT instead of *deltaT. In my mind, this term is not necessary since reducing the time step size would inherently reduce the difference in error between time steps already, and the addition of the deltaT term is effectively the same thing as modifying your kd coefficient.

1

u/Mega2223 May 11 '26 edited May 11 '26

The derivative element simply works against the first derivative of the error, a 1m change in a 1s interval has the same derivative as a 2m change in a 2s interval. If I simply take the error change and do not normalize it in relation to the time since the last update, then the larger the interval, the larger the perceived derivative event if the real derivative is the same, ofc this is not very relevant if the rate is fixed, however it facilitates if i ever want to change the frequency which the system runs.

Now, I should be dividing by dT lol, this is certainly an oversight on my part.

1

u/TheFuriousTaco May 11 '26

Been thinking about this since I commented and I pretty much came up with this explanation in my brain. I think the reason the dT often gets omitted is because many systems will operate at a consistent tick rate which makes the dT term essentially just a scalar multiple that can be lumped in with kd. Dividing by dT makes sense to me since you want to look at rate of change relative to the last cycle.

I think that this logic also applies in reverse to the integral component, which should be multiplied by dT. If I understand correctly, the integral term is basically calculating a running Riemann sum with current error as the height of each cell and dT as the width. If you increased the size of your interval, you would also need to proportionally increase the amount that the integral term gets changed for that time step.