New hardware and PCB design course on mixed-signal embedded systems just released! ⏵Course content: www.phils-lab.net/courses ⏵Course sign-up: phils-lab-shop.fedevel.education 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.
Kd should always be positive, so please change it in git repo (and test code again). To avoid the confusion you could always use error to setpoint.But with a flag set the setpoint always to 0 but only for the derivative component. (hope the intention is clear). So one can choose if "kick in" is wanted or not. BTW: What's the practical impact of the kick in on the output response?
@@gerhardwiesinger You might make another derivative term for the setpoint, with a separate Kd(set), and this way you could control how to respond to setpoint change and how to measurement change.
Very good video and one of the best I saw on implementing PID so far. But I don't fully understand how the derivative of the measurement and the derivative of the error can give the same result. This seems to work only if you imply the setpoint is not changing between two measurements, right? I mean, if the setpoint is increasing at the same speed as the measurement, the error is constant and derivative of the error is 0. But the derivative of the measurement would be non-zero in this case. Or am I missing something.
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.
Thank you! Yeah, I had the same problem, as it was quite hard to find complete and documented code online (and in books) that also covers practical issues in implementation - so very glad to hear this has helped.
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!
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.
I agree with you perfectly. Most of the lecturers just teach what they read in a book and not implementers. As the author of AoE, Horowitz, said "Most Engineers of this age are rather applied Mathematicians always hiding behind equations"
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!
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.
By far one of the best UA-camrs at explaining both hardware and software. Clearly very knowledgeable, I am currently consuming your content as fast as possible
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!
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 :)
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!
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.
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.
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.
I'm glad to hear that! Although not primarily a software guy myself, I try my best to make my code as easy-to-read as possible. I've been meaning to read 'Clean Code' for a while but still hasn't happened yet - one day...
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
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.
Immediately subbed. This is probably one of the best videos on PID control out there, and AFAIK, the only one that does a good job of explaining how to implement one in real life, where computation is discrete and numerical noise is inescapable. Loved the idea of dt-on-measurement. Had never seen that before, and now I wish I had thought of that.... time to update my PID code!
Thank you man, really glad to hear that. Yeah, I wish more people would actually show how to implement things in real-life, far too much time spent on theoretical learnings that don't translate 1-to-1.
@@PhilsLab Very true. Oh, and I implemented a new PID controller based on yours just now, and it's a significant improvement over my old one! Many thanks! One thing I was still curious about, if I could ask for an elaboration, was on how you worked out the resulting expressions, especially for D. You mentioned using a bilinear transformation to transform values to discrete space, but your expressions have the transformation already folded in with the integration and diff. That was the one part where I had to just take your word for it, because algebra is really not my thing. In any case, most excellent video! Thanks again!
Very good explanation. I would add that the tracking error states the direction that the gain of the PID control will take. If the error is positive the gain will be positive, and vice versa.
This is a very nice information. I believe that the design can be further improved with a better anti-windup mechanism. Integrator clamping is a rather crude way to prevent the saturation of the control signal. A much better and elegant solution is the back-calculation algorithm which dynamically modifies the integrator. It's a simple yet very efficient solution. You need to impose a software saturation borders of your controller's output. Then you calculate the difference across this saturation. The resulting signal is now a second error in the system which is fed back via a negative feedback loop to the integrator's input. The signal can be further amplified(or attenuated) with it's own coefficient. For example - a microcontroller can be an effective linear controller when the output is PWM. If we assume a control signal implemented over 10-bit PWM the software saturation is 0 to 1023.
[update: whoops, this may already be implemented in the updated anti-wind-up] /* or, more simply: */ p = ...; i = ...; d = ...; /* P, I, D terms */ out = p + i + d; /* sum to output */ out = (out>limMax) ? limMax : ((out
Thank you for the really well explained tutorial especially the implementation in the code. I was searching for something like this. This is exactly what I was looking for! Many thanks on your really awesome video!
Dear Friend , I have no words to Thank You ,, I watched lot of Videos to Understand the PID but its really very hard to understand ,, Your video really makes me to understand the PID Very Clean and Nice Explanation Thanks a lottt ..... Very Very Helpful .. God bless you my dear friend. all other your videos are very very nice :) :)
Friday evening, and an implementation of PID controllers. Perfect! I wish mathematicians wrote out their formulas in C. Translating from meat puppet is tedious.
Phil, an amazing morning watch and followup; excellent use of references to read. I am invested in Arduino IDE now on VSC & PlatformIO so making your code compile directly may be a task but not impossible. So, just what hill am I climbing to get VSC & PlatformIO to be happy. However steep and tall that hill is, what you have theoretically discussed and digitally implemented is understood quite well thanks to your presentation style and knowledge. I will checkout the 6 part video series to learn more.
One important attribute for controlling mechanical actuators is to avoid constant tiny adjustments. If the measurement is noisy and that translates to continuously changing an actuator, the increased wear from the jitter type adjustments leads to premature failure. This can be mitigated right at the controlled device by, for example, using a slotted coupling so that there is a bit of hysteresis. It should really be part of the control system, be it PID or any other.
Yes, that's a great point and something I only broadly hinted at with the 'derivative amplifies HF noise' part, but should have explicitly mentioned that this over-actuation due to noise in a control system will cause actuator wear.
thanks very much for this tutorial. Could you please explain the difference between T and tau. Sampling time vs Time constant for the filter? Where do we get its value from? How do we use this: (-3dB frequency in Hz, fc = 1 / (2*pi*tau))?
Thank you for everything, all the videos and information... every thing is increadible and very helpful. Nonehtless, i have a question, but why your PID has limits of -5 and 5 and for the integration part of -10 tup to 10, it is controlling the motors isn't it? For example if you control an AC motor with a trigger to a DIAC circuit, meaning the trigger should be between 0 up to 10 ms, if it is taken half the sin of the mains at 50 Hz, the PI output never can be negative, meaning that the clamping should be between this 0 ms up to max max max 10 ms, isn t it? Thank you very much
Great video! What I would be interested in is how to derive the derivative part of the controller with the integrated low-pass filter. It looks like this is a very common problem but I haven't found a good video which describes the transformation of the transfer funtion from the laplace domain to the actual implementation in the discrete time domain.
thanks for your detailed tutal. Two question, in your github code: pid->differentiator = -(2.0f * pid->Kd * (measurement - pid->prevMeasurement) /* Note: derivative on measurement, therefore minus sign in front of equation! */ + (2.0f * pid->tau - pid->T) * pid->differentiator) / (2.0f * pid->tau + pid->T); 1.Kd use positive value? 2. why not caculate differentiator part int the way below? pid->differentiator = -(2.0f * pid->Kd * (measurement - pid->prevMeasurement) /* Note: derivative on measurement, therefore minus sign in front of equation! */ - (2.0f * pid->tau - pid->T) * pid->differentiator) / (2.0f * pid->tau + pid->T); thanks very much.
This lecture takes me back to classical control systems and their design. Great presentation with the math and examples. Very important when it comes to controls systems in vehicles of all kinds, especially high speed, high performance and high agility systems: Fighter aircraft, Formula 1 race cars, guided rocket systems and so much more.
Interesting topic, I did’t know about a PID controller until now. From a programming topic, I would create this PID controller in C++ which encapsulates it more like a re-useable PID block. For the float data types I would may use a template type to be flexible for different use cases with uint8_t, uint16_t, etc. as some MCU’s don’t offer float support and the software float simulation eats a lot flash memory. Great video, thanks.
Thank you, Helmut. I like your ideas! Yes, C++ would offer a nicer way to make a class out of the PID controller. I tend to go with C these days, as I'm mainly programming for STM32s and C - to me - is just much better integrated than C++. And yeah, MCUs without a floating point unit will have a bit more work to do running this code. Then again, most PID scenarios will commonly only run at a few hundred Hz or kHz, and the update routine is not particularly math-heavy so I've been able to run this on things like Arduino Nanos. Even though these days all I work on is STM32s with floating point units haha. Thanks again for your comment!
@@PhilsLab today MCUs have sufficient flash and memory resources that C++ works great with them. I work with STM32L0, L4, D21 and ESP32, the entire code written in C++. At present for embedded projects with Mbed-os and Arduino OS. Simple data structures like a dynamic list of objects, a hash map etc. all this is available in C++ with good documentation without re-inventing algorithm in C where basic little support is given by the C library, with C++ all this is included. I started some time ago with Silicon Labs MCUs doing basic low level stuff like an async serial. When I moved to STM, all this was different and it was needed to start over again for basic stuff like a async serial or a simple timers. Basically wasting my engineering efforts without being able to bring my work to a new platform like Atmet D21. With Mbed-os all this is much better because Mbed OS has identical C++ APIs for all major ARM based MCUs, there are about 150 development boards available from different vendors all Mbed-os compatible. For ArduinoI wrote on top of Arduino Mbed-OS compatible IO and timer libraries. This means for larger projects my C++ code is 98% identically for different MCUs even across OS’s like Mbed and Arduino. For a software engineer re-purposing code boosts productivity brings more stable and maintainable code. Focusing on the solution and being flexible to support entirely new MCU vendors is something where clean C++ Code blocks can help. Regards from Hannover, Germany. Helmut
I'm confused by a few things: I see mixing and matching of units in many applications: a velocity in meters per second is the error/input and a PWM byte is the output (0-255). This tells me that the PID variables are both weights and unit conversions at the same time. If unit conversions were eliminated, all PID systems would have much more similar coefficients. Also, what is time? Are they some standard "fractions of a second" unit or do they vary between applications?
I also noticed that but it is not a problem since measured value probably comes from A/D converter which means 0-4095 if it is 12bit. So we are not dealing with units on input and output is also unit less since it is PWM duty cycle in 8 bits. Maybe would be smart to normalize these 12 or 10 bit measured values to resolution of PWM. If A/D resolution is 12bit and PWM duty cycle 8 bit then: measure/4095 * 255
Thank you! Yes, I'll be doing a video series on control system design, which includes software implementation, and also hope to make a video on the extended Kalman filter which runs on the FCS.
Thank you, I am going to use this for my antenna rotor controller that has phi and theta to track satellites. Me and my dad are currently trying to contact the International Space Station using the rotor controller!
I have a question regarding the use of the PID controller. For example, you have to balance a bar at a set angle with two motors on the ends (something like half of a quadcopter). The measurement is the current angle or the angular rate of the system. The input into the plant is some sort of number for example from 125-250 (min to max throttle). My question is - what kind of unit does the PID controller work in and how is the unit selected? On one hand, it's good for it to work with the input from the gyro since that's how the system determines the error between the desired angle and the current angle, but on the other hand, its good for it to work with the throttle value, because that's what the PID is supposed to output.
Hi Phil, maybe a follow up on tuning the PID, how to get to the set point in the quickest time but limiting over and undershoot. tuning and stability are often just off as I think they are harder than getting the loop running. great video and I will be looking at this for my next soldering iron software update.
Thank you! I've actually got a video planned going from mathematical modelling, to controller design, to simulation, and finally through to real-world implementation. As you say, showing how to actually tune a control system and how theory compares with practice.
You did an amazing Job...Thank you sooo much...it really helped me out with my project about underwater drone!! Hope we'll see more content from you...peaaace!!!
Thank you very much for your videos, you are a beast!! Please DO NOT STOP (EVER) and excuse my stupidity but i have to ask. I tested your code and of course it works perfectly, but I would like to understand why (when you simulate the first order system update) the output of the plant is: output = (SAMPLE_TIME_S * inp + output) / (1.0f + alpha * SAMPLE_TIME_S); I'm not grasping that and it's haunting me. Much love from Spain ^^
Thank you very much for your kind words! No worries, basically I start from the continuous time, first-order system: x' = -alpha * x + u. Then we can approximate the derivative to be: x' = (x[n] - x[n-1]) / T - where T is the sample time in seconds, such that: (x[n]-x[n-1])/T = -alpha * x[n] + u[n]. Then, rearranging: x[n] * (1 + alpha * T) = u[n] * T + x[n-1]. And rearranging again: x[n] = (T * u[n] + x[n-1]) / (1 + alpha * T). Which is the expression you see in the code. Let me know if that helps!
@@PhilsLab It absolutely does, thank you master ^^ As you are so kind I'm gonna bother you with a last question. Could you recommend me the best (complete and to the point) book/tutorial in your opinion to review/master control systems theory from a practical point of view? I'm working in a new job with stm32, just testing and validating hw/sw at the moment, but I will need to dive into motion control design problems soon. My background was a very wide industrial engineering and ended working programming PLCs in the automotive industry (no analog control). I finally got out of that hell and found a cool job but I have some gaps and need to refresh this as I never got the chance to implement the things I really loved to study. Sorry for the long post
Hey Phil. Thank you for the video! I just have a question, isn't the term 1/(2tau +T) missing on the first derivative term? I know the minus sign has been fixed, but i think this is another mistake, or am I missing something? I implemented a PID controller without that term, and it worked after I tweaked the parameters, but just in case...
Thank you for sharing. I do have a question. In the code, I see that actually coded the tick time "Tau". I've read in a microcontroller forum that Tau is not needed to be coded because it is taken care of every time the new sensor value is obtained in a tick fashion. It is confusing, since I see some code include the time variable and some code don't. Can you please explain?
How ignorant I was to write a sludge pump controller in assembler for a 6800 chip (not the Motorola one) that emulated a PDP8 mini computer. I tested it on a model and it worked. I was lucky in 1979. I wasn't aware of PID but it's obvious now. At the time there were no digital controllers, they were all analog and there wasn't anything for a pneumatic pump. I managed to increase the percent solids from 1% to 8% which was thought to be impossible. It still flowed and saved a lot of heat energy. I only had a few hundred 12 bit words. How large is the compiled code? I started programming by hand in octal, but found a cross assembler written in Fortran IV that ran on a mainframe. I could print out the code and enter it by hand. Burning a ROM was a major undertaking. The original language assumed magnetic core memory where the return address of a subroutine was written into the first word of the subroutine and the return was a relative jump using the value. Can't do that in a ROM.
Thanks for your comment, Kim! To be honest, what you did with the 'limited' technology at the time seems far more impressive to me than writing this little PID C program I did here! Would have loved to have been around as an engineer back in the 70s/80s. The code here takes up about 4kB of memory (and a couple tens of bytes of RAM).
Thanks for the useful Video, I decided to implement your PID algorithm to Temperature control of 1000 degrees furnace and wondering how to determine TAU and sample time. Note that my microcontroller will generate 2 seconds PWM (slow PWM) and I will control the temperature through Duty Cycle. regards
What is the reason to NOT want the derivative kick? The output is clamped anyway, and I, personally, find it very handy to be able to kick the system into overdrive when it suddenly finds itself too far from the set point.
Where would you suggest putting functionality for limiting the change limit of the output (i.e. no more than 1% per period) without causing max wind-up in the integrator?
Equations in PIDController_Update reduce to output or measurement =.98 pid.out. other factors must be artifacts from past alternatives. I am somewhat familiar with Laplace and Z transform but would appreciate a detailed documentation of each and every step starting with basic differential equations for PID through Z transform. I have started with your code and I am converting it to run in real time on an Arduino mega and will incorporate changes to gains, setpoint, unwinding limits and hopefully also graph in real time but Arduino IDE 2.o3 is new to me having used 1.86 for years. Thanks much for your help.
In the video you say that the material from which the tustin discretization is done should be linked. I cannot find it, so could you link it either in the desciption or as a comment to this comment? Otherwise, really nice video :)
It'll always depend on the system you wish to control. T is the sampling time - which ideally should be minimised (i.e. trying to achieve the greatest sampling rate possible) and tau can cause instabilities if chosen too large.
Suppose I am trying to control the speed of some actuator using a PID controller. Once the speed reaches the set value, the error would be 0, in which case the input to the actuator would be 0 (I guess). If this happens the speed would drop to 0. How does the PID controller handle this so that there is a constant input to the actuator, even when the error is 0.
What does PID stand for? This: A proportional-integral-derivative controller (PID controller or three-term controller) is a control loop mechanism employing feedback that is widely used in industrial control systems and a variety of other applications requiring continuously modulated control. A PID controller continuously calculates an error value e(t) as the difference between a desired setpoint (SP) and a measured process variable (PV) and applies a correction based on proportional, integral, and derivative terms (denoted P, I, and D respectively), hence the name. -wiki
I tried doing the calculations from the functions of S to functions of N. But im stuck on the derivative term. Ik get the same result but in the d[n] term i get 1 ((2tau+T)/(2tau+T)). If i look at the calculatioms the d[n] terms would be (2tau-T)/(2tau+1) if the orignal filter would be s/(tau s-1) not s/(tau s+1). What could i be doimg wrong?
What about the effect of the zero order hold which is typically considered when converting from continues to discrete time? I didn’t find any mention of that in the video.
That isn’t part of the controller itself - strictly speaking. Rather, it needs to be considered in the overall loop for stability when designing the complete control system - that is taking the plant and system into account. The ZOH effect (due to the DAC) effectively introduces a small delay, which may require the controller gains to be adjusted. However, the implementation (code-wise, as shown in the video) stays the same. I go into more detail in my ‘control system design’ series of videos, if you are interested.
@@PhilsLab understood. thanks. Yes I have see your other videos also till part 2/6. They are brilliant! In the above video you have designed only the controller, so I assume that when adding the rest of the system in the simulation/code you will need to add a ZOH between the controller and plant (if you are planning to implement this controller on a real MCU), so as to mimic the effect of the output DAC. Right?
@@yadaKiKhula Thank you! Yes, we'll be looking into that - if we indeed need to model that for our controller design. In some cases you can get away with not including the ZOH effects. But yes, you are right, the ZOH is to model the effects of the DAC!
Hi Phil, could you also make a video on how to tune a PID either for this application or for another specific application like the gyroscope you example you already have?
In fact, the video is very useful and useful I have a question please: How can I use this library to drive a washing machine's AC brushed motor I wrote a simple code to do that (C code) But the motor does not start smoothly, it is jumping The reason is that I did not use PID thank you Mr. Philips
New hardware and PCB design course on mixed-signal embedded systems just released!
⏵Course content: www.phils-lab.net/courses
⏵Course sign-up: phils-lab-shop.fedevel.education
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.
Thanks for sharing i have a question how can we get tau and how we chose sampling time ?
Kd should always be positive, so please change it in git repo (and test code again). To avoid the confusion you could always use error to setpoint.But with a flag set the setpoint always to 0 but only for the derivative component. (hope the intention is clear). So one can choose if "kick in" is wanted or not. BTW: What's the practical impact of the kick in on the output response?
@@gerhardwiesinger Thanks, I've included the minus sign. Code has been checked and works as expected.
@@gerhardwiesinger You might make another derivative term for the setpoint, with a separate Kd(set), and this way you could control how to respond to setpoint change and how to measurement change.
Very good video and one of the best I saw on implementing PID so far. But I don't fully understand how the derivative of the measurement and the derivative of the error can give the same result. This seems to work only if you imply the setpoint is not changing between two measurements, right? I mean, if the setpoint is increasing at the same speed as the measurement, the error is constant and derivative of the error is 0. But the derivative of the measurement would be non-zero in this case. Or am I missing something.
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.
Thank you! Yeah, I had the same problem, as it was quite hard to find complete and documented code online (and in books) that also covers practical issues in implementation - so very glad to hear this has helped.
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!
This channel is slowly becoming one of my favorites, thank you so much!!
I'm very happy to hear that - thank you!
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.
Very glad to hear you found it helpful. Same for me when I was at university, lots of theory but effectively no mention of how to use it in practice.
I agree with you perfectly. Most of the lecturers just teach what they read in a book and not implementers. As the author of AoE, Horowitz, said "Most Engineers of this age are rather applied Mathematicians always hiding behind equations"
Spot on explanation, I swear most people who try and explain this don't understand it themselves. Thanks so much.
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.
Very happy to hear that, thank you very much!
same, i got c+ on control and system..
Nice Work, one of the best PID explanations because its not in Matlab or Simulink. You actually see the anti-windup code
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!
Really glad to hear that, thank you!
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.
Thank you very much, Shaun!
By far one of the best UA-camrs at explaining both hardware and software. Clearly very knowledgeable, I am currently consuming your content as fast as possible
Thank you very much for your kind comment! Definitely more videos to come as well :)
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!
Thank you, Tom! Very glad to hear that - hope you can use the algorithm in one of your projects! :)
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 :)
Awesome, very glad to hear that - hope you can put the code here to use! :)
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!
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.
Thank you, Lee. That's very kind of you to say, very happy to hear that!
Fantastic video Phil!
Suggestion: Increase font size so we can see your code a bit easier.
Keep the great content coming!
Thank you, Kyle! That's a very good suggestion, I'll keep that in mind for future videos.
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.
DR RORPOPOR HERBAL on UA-cam changed my entire life with his herbal medicine. I appreciate you sir, for taken away my PID 🌿🌿🌿🌿
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.
I'm glad to hear that! Although not primarily a software guy myself, I try my best to make my code as easy-to-read as possible. I've been meaning to read 'Clean Code' for a while but still hasn't happened yet - one day...
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
It is by far the best video about PID, specially about practical implementation of the controller.
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.
Immediately subbed. This is probably one of the best videos on PID control out there, and AFAIK, the only one that does a good job of explaining how to implement one in real life, where computation is discrete and numerical noise is inescapable.
Loved the idea of dt-on-measurement. Had never seen that before, and now I wish I had thought of that.... time to update my PID code!
Thank you man, really glad to hear that. Yeah, I wish more people would actually show how to implement things in real-life, far too much time spent on theoretical learnings that don't translate 1-to-1.
@@PhilsLab Very true. Oh, and I implemented a new PID controller based on yours just now, and it's a significant improvement over my old one! Many thanks!
One thing I was still curious about, if I could ask for an elaboration, was on how you worked out the resulting expressions, especially for D. You mentioned using a bilinear transformation to transform values to discrete space, but your expressions have the transformation already folded in with the integration and diff. That was the one part where I had to just take your word for it, because algebra is really not my thing.
In any case, most excellent video! Thanks again!
Very good explanation. I would add that the tracking error states the direction that the gain of the PID control will take. If the error is positive the gain will be positive, and vice versa.
This is a very nice information. I believe that the design can be further improved with a better anti-windup mechanism. Integrator clamping is a rather crude way to prevent the saturation of the control signal. A much better and elegant solution is the back-calculation algorithm which dynamically modifies the integrator.
It's a simple yet very efficient solution. You need to impose a software saturation borders of your controller's output. Then you calculate the difference across this saturation. The resulting signal is now a second error in the system which is fed back via a negative feedback loop to the integrator's input. The signal can be further amplified(or attenuated) with it's own coefficient.
For example - a microcontroller can be an effective linear controller when the output is PWM. If we assume a control signal implemented over 10-bit PWM the software saturation is 0 to 1023.
[update: whoops, this may already be implemented in the updated anti-wind-up]
/* or, more simply: */
p = ...; i = ...; d = ...; /* P, I, D terms */
out = p + i + d; /* sum to output */
out = (out>limMax) ? limMax : ((out
What a nice video! - Informative and straight to the point. No frills. Just results. Big thanks!
Thank you, very glad you liked it!
Thank you for the really well explained tutorial especially the implementation in the code. I was searching for something like this. This is exactly what I was looking for! Many thanks on your really awesome video!
Dear Friend , I have no words to Thank You ,, I watched lot of Videos to Understand the PID but its really very hard to understand ,, Your video really makes me to understand the PID
Very Clean and Nice Explanation Thanks a lottt ..... Very Very Helpful .. God bless you my dear friend. all other your videos are very very nice :) :)
Thank you, Baskar!
Probably my new favorite channel! Thanks for this videos man!
Awesome - thank you!
Friday evening, and an implementation of PID controllers. Perfect! I wish mathematicians wrote out their formulas in C. Translating from meat puppet is tedious.
Thank you! Yeah, I wish more control theory books would have more snippets of actual code..
@@PhilsLab when looking at actual code written by control theorists i am not so sure that would be a good thing... ;)
Very impressed! The best I ever saw, clearly explained it in less than 20 minutes!
That's very kind of you to say - thank you!
A video about tunning those parameters would be cool either. Nice content!
Thank you! Yes, will be doing an extensive video on that in an upcoming video series on controller design!
You are the best Philip, this is by far the best PID video I've ever seen. Please make more such videos. Thanks a lot .!
Thank you very much! Yes, got a larger control theory one lined up - going from theory through to practical implementation.
This is great Thanks ! You've made life a bit easier for many of us semi-boneheads.
Thanks, Don!
Excellent video. Exactly what I was searching for.
Thank you!
Absolutely brilliant video! Many thanks for making this and showing the unity simulation as well, that was a new thing to see.
Thank you very much! My pleasure, I'm glad you liked it!
Thank you for this video! The explanation and example code were exactly what I was looking for.
First Time I am commenting on a video because you blew away me with your explanation/implementation skills. Thank you so much for the explanation
Thank you very much, Ravi!
Great walkthrough of the problems with PID's
Great walkthrough of the implementation in C
Thanks for sharing :-)
DR RORPOPOR HERBAL on UA-cam changed my entire life with his herbal medicine. I appreciate you sir, for taken away my PID 🌿🌿🌿🌿
really useful man, you took me back in time to review system control concepts
Very glad to hear that, thank you!
Phil, an amazing morning watch and followup; excellent use of references to read. I am invested in Arduino IDE now on VSC & PlatformIO so making your code compile directly may be a task but not impossible. So, just what hill am I climbing to get VSC & PlatformIO to be happy. However steep and tall that hill is, what you have theoretically discussed and digitally implemented is understood quite well thanks to your presentation style and knowledge. I will checkout the 6 part video series to learn more.
DR RORPOPOR HERBAL on UA-cam changed my entire life with his herbal medicine. I appreciate you sir, for taken away my PID 🌿🌿🌿🌿
One important attribute for controlling mechanical actuators is to avoid constant tiny adjustments. If the measurement is noisy and that translates to continuously changing an actuator, the increased wear from the jitter type adjustments leads to premature failure. This can be mitigated right at the controlled device by, for example, using a slotted coupling so that there is a bit of hysteresis. It should really be part of the control system, be it PID or any other.
Yes, that's a great point and something I only broadly hinted at with the 'derivative amplifies HF noise' part, but should have explicitly mentioned that this over-actuation due to noise in a control system will cause actuator wear.
Clear and concise. Love the use of engineering paper!
Thank you, Michael!
Well done and very applicable to digital power conversion. Modeling PID systems in a simulator like LTspice would be very useful.
Thank you, Ken! I'll see if I can make a video on (PID) control in circuit simulators.
that's cool. I made one 2 years ago in C for a coolling system(control Fan via PWM) sold by a famous PC part vendor.
15:13 Soooo, where did you get this from? There's nothing in the description
Great video Phil! What is the alpha in TestSystem_Update function used to calculate the output?
thanks very much for this tutorial. Could you please explain the difference between T and tau. Sampling time vs Time constant for the filter? Where do we get its value from? How do we use this: (-3dB frequency in Hz, fc = 1 / (2*pi*tau))?
Good question
Thank you for everything, all the videos and information... every thing is increadible and very helpful. Nonehtless, i have a question, but why your PID has limits of -5 and 5 and for the integration part of -10 tup to 10, it is controlling the motors isn't it? For example if you control an AC motor with a trigger to a DIAC circuit, meaning the trigger should be between 0 up to 10 ms, if it is taken half the sin of the mains at 50 Hz, the PI output never can be negative, meaning that the clamping should be between this 0 ms up to max max max 10 ms, isn t it? Thank you very much
Great video! What I would be interested in is how to derive the derivative part of the controller with the integrated low-pass filter. It looks like this is a very common problem but I haven't found a good video which describes the transformation of the transfer funtion from the laplace domain to the actual implementation in the discrete time domain.
thanks for your detailed tutal. Two question, in your github code:
pid->differentiator = -(2.0f * pid->Kd * (measurement - pid->prevMeasurement) /* Note: derivative on measurement, therefore minus sign in front of equation! */
+ (2.0f * pid->tau - pid->T) * pid->differentiator)
/ (2.0f * pid->tau + pid->T);
1.Kd use positive value?
2. why not caculate differentiator part int the way below?
pid->differentiator = -(2.0f * pid->Kd * (measurement - pid->prevMeasurement) /* Note: derivative on measurement, therefore minus sign in front of equation! */
- (2.0f * pid->tau - pid->T) * pid->differentiator)
/ (2.0f * pid->tau + pid->T);
thanks very much.
This lecture takes me back to classical control systems and their design.
Great presentation with the math and examples. Very important when it comes to controls systems in vehicles of all kinds, especially high speed, high performance and high agility systems: Fighter aircraft, Formula 1 race cars, guided rocket systems and so much more.
Thank you! Yeah, control systems are absolutely everywhere. Incredibly important in almost all areas of engineering.
Especial thanks for the filter in derivative channel!
My pleasure, thank you!
Interesting topic, I did’t know about a PID controller until now.
From a programming topic, I would create this PID controller in C++ which encapsulates it more like a re-useable PID block. For the float data types I would may use a template type to be flexible for different use cases with uint8_t, uint16_t, etc. as some MCU’s don’t offer float support and the software float simulation eats a lot flash memory.
Great video, thanks.
Thank you, Helmut. I like your ideas!
Yes, C++ would offer a nicer way to make a class out of the PID controller. I tend to go with C these days, as I'm mainly programming for STM32s and C - to me - is just much better integrated than C++.
And yeah, MCUs without a floating point unit will have a bit more work to do running this code. Then again, most PID scenarios will commonly only run at a few hundred Hz or kHz, and the update routine is not particularly math-heavy so I've been able to run this on things like Arduino Nanos. Even though these days all I work on is STM32s with floating point units haha.
Thanks again for your comment!
@@PhilsLab today MCUs have sufficient flash and memory resources that C++ works great with them. I work with STM32L0, L4, D21 and ESP32, the entire code written in C++. At present for embedded projects with Mbed-os and Arduino OS. Simple data structures like a dynamic list of objects, a hash map etc. all this is available in C++ with good documentation without re-inventing algorithm in C where basic little support is given by the C library, with C++ all this is included.
I started some time ago with Silicon Labs MCUs doing basic low level stuff like an async serial. When I moved to STM, all this was different and it was needed to start over again for basic stuff like a async serial or a simple timers. Basically wasting my engineering efforts without being able to bring my work to a new platform like Atmet D21.
With Mbed-os all this is much better because Mbed OS has identical C++ APIs for all major ARM based MCUs, there are about 150 development boards available from different vendors all Mbed-os compatible. For ArduinoI wrote on top of Arduino Mbed-OS compatible IO and timer libraries. This means for larger projects my C++ code is 98% identically for different MCUs even across OS’s like Mbed and Arduino.
For a software engineer re-purposing code boosts productivity brings more stable and maintainable code. Focusing on the solution and being flexible to support entirely new MCU vendors is something where clean C++ Code blocks can help.
Regards from Hannover, Germany. Helmut
I'm confused by a few things: I see mixing and matching of units in many applications: a velocity in meters per second is the error/input and a PWM byte is the output (0-255). This tells me that the PID variables are both weights and unit conversions at the same time. If unit conversions were eliminated, all PID systems would have much more similar coefficients. Also, what is time? Are they some standard "fractions of a second" unit or do they vary between applications?
I also noticed that but it is not a problem since measured value probably comes from A/D converter which means 0-4095 if it is 12bit. So we are not dealing with units on input and output is also unit less since it is PWM duty cycle in 8 bits. Maybe would be smart to normalize these 12 or 10 bit measured values to resolution of PWM. If A/D resolution is 12bit and PWM duty cycle 8 bit then: measure/4095 * 255
Great video.
I'd like to see a software-focused follow up from your "Flight Control System Design: Hardware" video.
Thank you! Yes, I'll be doing a video series on control system design, which includes software implementation, and also hope to make a video on the extended Kalman filter which runs on the FCS.
This is pure gold, subscribed :)
Thank you!
Thanks for the video, it's well organized and really easy to follow, you are a great teacher. Subscribed.
That's great to hear - thank you!
Thank you, I am going to use this for my antenna rotor controller that has phi and theta to track satellites. Me and my dad are currently trying to contact the International Space Station using the rotor controller!
Awesome, hope your antenna tracker works out!
@@PhilsLab The antenna tracker works, here it is! ua-cam.com/video/vrlw4QPKMRY/v-deo.html
I have a question regarding the use of the PID controller. For example, you have to balance a bar at a set angle with two motors on the ends (something like half of a quadcopter).
The measurement is the current angle or the angular rate of the system.
The input into the plant is some sort of number for example from 125-250 (min to max throttle).
My question is - what kind of unit does the PID controller work in and how is the unit selected?
On one hand, it's good for it to work with the input from the gyro since that's how the system determines the error between the desired angle and the current angle, but on the other hand, its good for it to work with the throttle value, because that's what the PID is supposed to output.
@6:35, where can I find the derivation for p[n], I[n] ,d[n]. Please the link buddy..
Hi Phil, maybe a follow up on tuning the PID, how to get to the set point in the quickest time but limiting over and undershoot. tuning and stability are often just off as I think they are harder than getting the loop running. great video and I will be looking at this for my next soldering iron software update.
Thank you! I've actually got a video planned going from mathematical modelling, to controller design, to simulation, and finally through to real-world implementation. As you say, showing how to actually tune a control system and how theory compares with practice.
You did an amazing Job...Thank you sooo much...it really helped me out with my project about underwater drone!!
Hope we'll see more content from you...peaaace!!!
Awesome, thank you - very glad to hear that!
Our college is all about theory and no shit given about practical side. I always wanted to know the implementation. Thanks for sharing this!!!!
Was the same for me at uni.. :( Glad you found the video helpful!
Very informative and well presented
Thank you!
a brief explanation of the choice of tau and T would be very much appreciated.
I'm really grateful for this video Phil!. It help me a lot. Thank you!!
Really glad to hear that, thank you!
Thank you very much for your videos, you are a beast!! Please DO NOT STOP (EVER) and excuse my stupidity but i have to ask.
I tested your code and of course it works perfectly, but I would like to understand why (when you simulate the first order system update) the output of the plant is:
output = (SAMPLE_TIME_S * inp + output) / (1.0f + alpha * SAMPLE_TIME_S);
I'm not grasping that and it's haunting me.
Much love from Spain ^^
Thank you very much for your kind words!
No worries, basically I start from the continuous time, first-order system: x' = -alpha * x + u. Then we can approximate the derivative to be: x' = (x[n] - x[n-1]) / T - where T is the sample time in seconds, such that:
(x[n]-x[n-1])/T = -alpha * x[n] + u[n].
Then, rearranging: x[n] * (1 + alpha * T) = u[n] * T + x[n-1].
And rearranging again: x[n] = (T * u[n] + x[n-1]) / (1 + alpha * T). Which is the expression you see in the code.
Let me know if that helps!
@@PhilsLab It absolutely does, thank you master ^^
As you are so kind I'm gonna bother you with a last question. Could you recommend me the best (complete and to the point) book/tutorial in your opinion to review/master control systems theory from a practical point of view?
I'm working in a new job with stm32, just testing and validating hw/sw at the moment, but I will need to dive into motion control design problems soon. My background was a very wide industrial engineering and ended working programming PLCs in the automotive industry (no analog control). I finally got out of that hell and found a cool job but I have some gaps and need to refresh this as I never got the chance to implement the things I really loved to study.
Sorry for the long post
Hey Phil. Thank you for the video!
I just have a question, isn't the term 1/(2tau +T) missing on the first derivative term? I know the minus sign has been fixed, but i think this is another mistake, or am I missing something?
I implemented a PID controller without that term, and it worked after I tweaked the parameters, but just in case...
Thank you for sharing. I do have a question. In the code, I see that actually coded the tick time "Tau". I've read in a microcontroller forum that Tau is not needed to be coded because it is taken care of every time the new sensor value is obtained in a tick fashion. It is confusing, since I see some code include the time variable and some code don't. Can you please explain?
How ignorant I was to write a sludge pump controller in assembler for a 6800 chip (not the Motorola one) that emulated a PDP8 mini computer. I tested it on a model and it worked. I was lucky in 1979. I wasn't aware of PID but it's obvious now. At the time there were no digital controllers, they were all analog and there wasn't anything for a pneumatic pump. I managed to increase the percent solids from 1% to 8% which was thought to be impossible. It still flowed and saved a lot of heat energy. I only had a few hundred 12 bit words. How large is the compiled code? I started programming by hand in octal, but found a cross assembler written in Fortran IV that ran on a mainframe. I could print out the code and enter it by hand. Burning a ROM was a major undertaking. The original language assumed magnetic core memory where the return address of a subroutine was written into the first word of the subroutine and the return was a relative jump using the value. Can't do that in a ROM.
Thanks for your comment, Kim! To be honest, what you did with the 'limited' technology at the time seems far more impressive to me than writing this little PID C program I did here! Would have loved to have been around as an engineer back in the 70s/80s.
The code here takes up about 4kB of memory (and a couple tens of bytes of RAM).
I would like to understand better the dynamic integrator anti-windup. Do you still have the link you mentioned in the video? Thank you.
Thanks for the useful Video, I decided to implement your PID algorithm to Temperature control of 1000 degrees furnace and wondering how to determine TAU and sample time. Note that my microcontroller will generate 2 seconds PWM (slow PWM) and I will control the temperature through Duty Cycle. regards
What is the reason to NOT want the derivative kick?
The output is clamped anyway, and I, personally, find it very handy to be able to kick the system into overdrive when it suddenly finds itself too far from the set point.
Where would you suggest putting functionality for limiting the change limit of the output (i.e. no more than 1% per period) without causing max wind-up in the integrator?
Nice work ! I just want to ask you how can we determine the value of Tau that we will use it in the filter part . Thank you .
*Thanks for such a great explanation and that too on my birthday*
Thank you - happy birthday!
Hi Phil
it is a very helpful tutorial. Wondering how would you select the tau.
thanks
Great video, thank you very much for sharing... looking forward to the next one
Thank you, Carlos - more videos to come soon!
Thank you!!!!!🎉 Needed this
just a great explanation! Thanks for share these videos!
My pleasure, thank you for watching!
Equations in PIDController_Update reduce to output or measurement =.98 pid.out. other factors must be artifacts from past alternatives. I am somewhat familiar with Laplace and Z transform but would appreciate a detailed documentation of each and every step starting with basic differential equations for PID through Z transform. I have started with your code and I am converting it to run in real time on an Arduino mega and will incorporate changes to gains, setpoint, unwinding limits and hopefully also graph in real time but Arduino IDE 2.o3 is new to me having used 1.86 for years. Thanks much for your help.
In the video you say that the material from which the tustin discretization is done should be linked. I cannot find it, so could you link it either in the desciption or as a comment to this comment?
Otherwise, really nice video :)
Awesome tutorial! Everything I needed! :)
Thanks for watching, Paul!
What are some example values for T and tau?
It'll always depend on the system you wish to control. T is the sampling time - which ideally should be minimised (i.e. trying to achieve the greatest sampling rate possible) and tau can cause instabilities if chosen too large.
Great work thanks.
Question. What is difference between tau and sampling Time?
Suppose I am trying to control the speed of some actuator using a PID controller. Once the speed reaches the set value, the error would be 0, in which case the input to the actuator would be 0 (I guess). If this happens the speed would drop to 0. How does the PID controller handle this so that there is a constant input to the actuator, even when the error is 0.
Thank you for explaining that topic!
thank for video, i have a question
how to calculate the "tau" variable?
thank you
I dont understand this low pass filter method for the derivative...can anyone provide a link to an explanation?
is it okay if i use just proportional and integral for the code? or i need to changes some code for running on proportional and integral ?
What does PID stand for? This:
A proportional-integral-derivative controller (PID controller or three-term controller) is a control loop mechanism employing feedback that is widely used in industrial control systems and a variety of other applications requiring continuously modulated control. A PID controller continuously calculates an error value e(t) as the difference between a desired setpoint (SP) and a measured process variable (PV) and applies a correction based on proportional, integral, and derivative terms (denoted P, I, and D respectively), hence the name. -wiki
Thanks for the code. How do I choose the tau and T variables? I will try to use this controller in an Arduino robot car.
I tried doing the calculations from the functions of S to functions of N. But im stuck on the derivative term. Ik get the same result but in the d[n] term i get 1 ((2tau+T)/(2tau+T)). If i look at the calculatioms the d[n] terms would be (2tau-T)/(2tau+1) if the orignal filter would be s/(tau s-1) not s/(tau s+1). What could i be doimg wrong?
What about the effect of the zero order hold which is typically considered when converting from continues to discrete time? I didn’t find any mention of that in the video.
That isn’t part of the controller itself - strictly speaking. Rather, it needs to be considered in the overall loop for stability when designing the complete control system - that is taking the plant and system into account. The ZOH effect (due to the DAC) effectively introduces a small delay, which may require the controller gains to be adjusted. However, the implementation (code-wise, as shown in the video) stays the same. I go into more detail in my ‘control system design’ series of videos, if you are interested.
@@PhilsLab understood. thanks. Yes I have see your other videos also till part 2/6. They are brilliant! In the above video you have designed only the controller, so I assume that when adding the rest of the system in the simulation/code you will need to add a ZOH between the controller and plant (if you are planning to implement this controller on a real MCU), so as to mimic the effect of the output DAC. Right?
@@yadaKiKhula Thank you! Yes, we'll be looking into that - if we indeed need to model that for our controller design. In some cases you can get away with not including the ZOH effects.
But yes, you are right, the ZOH is to model the effects of the DAC!
how can i add the sensor data to the update function. Do I need to replace the "measurement" variable with the sensor data?
Hi Phil, could you also make a video on how to tune a PID either for this application or for another specific application like the gyroscope you example you already have?
Hi Karthik, I'm planning on making a video on PID motor control in the near future. Just waiting on the PCBs to arrive..
Thanks for making this nightmare to become interesting for me
Haha glad it got better!
Would love to also see a PID auto-tune code.
thank sir, but how to find tau and Ts to add in the code for the systems?
In fact, the video is very useful and useful
I have a question please:
How can I use this library to drive a washing machine's AC brushed motor
I wrote a simple code to do that (C code)
But the motor does not start smoothly, it is jumping
The reason is that I did not use PID
thank you Mr. Philips