PID Controller Implementation in Software - Phil's Lab #6

preview_player
Показать описание


How to implement a PID controller in software using C, discussing theory and practical considerations. Demonstration of PID controller code using a custom flight simulator.

[NOTE] Something I forgot to mention in the video! Note on 'derivative-on-measurement': Since the 'error signal' effectively going into the differentiator does not depend on the setpoint: e[n] = 0 - measurement, and therefore (e[n] - e[n - 1]) = (0 - measurement) - (0 - prevMeasurement) = -Kd * (measurement - prevMeasurement). Note the minus sign compared to derivative-on-error!
I've made the change in the Git repo - before you would have had to use a negative Kd gain to get the same result. Now you can, as normal with derivative-on-error, use a positive Kd gain as usual.

Additional note: The derivative low-pass filter can be controlled by the constant 'tau', which is the time constant of the filter (-3dB frequency in Hz, fc = 1 / (2*pi*tau)). A larger value of tau means the signal is filtered more heavily. As tau approaches zero, the differentiator approaches a 'pure differentiator' with no filtering.

[TIMESTAMPS]
00:00 Introduction
00:39 Control system basics

02:40 PID representation in continuous domain
04:57 Converting from the continuous to the discrete domain
06:11 PID controller difference equation

07:35 Practical considerations
10:48 Basic software structure

11:53 Implementation in C

18:46 Example: Flight simulator using PID controller code

ID: QIBvbJtYjWuHiTG0uCoK
Рекомендации по теме
Комментарии
Автор

New hardware and PCB design course on mixed-signal embedded systems just released!

Something I forgot to mention in the video! Note on 'derivative-on-measurement': Since the 'error signal' effectively going into the differentiator does not depend on the setpoint: e[n] = 0 - measurement, and therefore (e[n] - e[n - 1]) = (0 - measurement) - (0 - prevMeasurement) = -Kd * (measurement - prevMeasurement). Note the minus sign compared to derivative-on-error!
I've made the change in the Git repo - before you would have had to use a negative Kd gain to get the same result. Now you can, as normal with derivative-on-error, use a positive Kd gain as usual.

PhilsLab
Автор

This is the first time I find a direct C application from a theoretical control explanation. I've been searching for this for some time on my university's library and no luck at all.
Thank you so much for this, you got a fan right here.

patrickhochleitner
Автор

I’ve learned this in school only from a theoretical standpoint that glosses over the very important details of “how do we ACTUALLY make this work”. Thank you for increasing my knowledge!

jasonbotzas
Автор

This channel is slowly becoming one of my favorites, thank you so much!!

bastianacevedobustos
Автор

Just finished my ECE undergrad last month. Practical implementations of control systems in digital hardware is something that I never learned and wished was part of my curriculum. Excellent, thanks.

ObsidianJunkie
Автор

I've learned this PID knowledge in the university ~ 6 years ago. But it was definitely not as clear as your illustration and demonstration. Bravo.

coorfang
Автор

Nice Work, one of the best PID explanations because its not in Matlab or Simulink. You actually see the anti-windup code

hvinstrumentsandparts
Автор

As a fellow lover of microcontrollers and Signal Processing I absolutely love the content on this channel... I have to say that Phil does a great job of getting through the rather advanced mathematics and focuses on only the essentials. Great work.

shaungovender
Автор

I'm a mechatronics engineering student, I've covered a lot of theory about advanced control stategies, but we always skipped PID control and, most of all, its implementation in code. This video is fantastic, you have a very good skill in explaining clearly concepts and this is exactly what I was looking for to have a more practical grasp about such theoretical concepts. Well done!

GiancarloAllasia
Автор

By far one of the best YouTubers at explaining both hardware and software. Clearly very knowledgeable, I am currently consuming your content as fast as possible

DownTownDowns
Автор

Brilliant video! I've forgotten some (most?) of my differential equations concepts, but you gave a very nice review--and I love your PID algorithm. The flight sim was most impressive as well. Thanks for making this video!

bluehornet
Автор

About time a video like this exists. I'm taking control systems right now as an undergrad and I've been waiting to use that theory in practice. Your videos have been perfect for that transition. Perfectly timed :)

georgetroulis
Автор

Fantastic video Phil!
Suggestion: Increase font size so we can see your code a bit easier.
Keep the great content coming!

kylemilner
Автор

You saved my day man! I finally understand how to implement basically any continuous time controller onto a arduino or something like that. One can have ALL the understanding of the theory, but if you cannot apply it, you are basically worth nothing for a company. Thank you so much!

kersgames
Автор

It is by far the best video about PID, specially about practical implementation of the controller.

sergeyzolotykh
Автор

Great video thanks! Helped enormously with my 3rd year project. Nice to see the theory and practical being connected as this is often neglected and causes much difficulty

bouipozz
Автор

Very well done! You are an excellent teacher. Concise and to the point, good flow with no fumbles. That is hard to do. I have been doing this for over 45 years and you are in the top five for good teachers.

leecaraway
Автор

It might be worth considering adding a separate variable to integrate the errors before multiplying the integral gain. The float (essentially being scientific notation in base 2) will stop integrating if the value you are adding (0.5 * Ki * (error + prevError)) drops below the LSB of the fractional portion of the float for a given exponent of the integrator. In other words, you can introduce a steady-state error that won't be resolved for sufficiently small errors. The effect is exacerbated for low values of Ki and T.

benjaminpaik
Автор

Hey Phil,

Great video. Information is not to extent but the fundamental concepts are there, plus you actually give an example of a real world application, which at the end of the day is what really matters.

Thank you.

martinestorninhoblocher
Автор

14:48 - As the guy who reviews the other people code for living I do really appreciate making the code easier to read rather than super fancy, short and 'sophisticated'. Especially in C# or JavaScript where you have at least 40 ways to achieve same thing and new syntactic sugars every two years.

darkstar