# A rich source of expert knowledge

Learn from experts in the world of embedded systems

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

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.
Chapters:
• 00:00Introduction
• 00:39Control system basics
• 02:40PID representation in continuous domain
• 04:57Converting from the continuous to the discrete domain
• 06:11PID controller difference equation
• 07:35Practical considerations
• 10:48Basic software structure
• 11:53Implementation in C
• 18:46Example: Flight simulator using PID controller code
{"ft_lang_label":"__label__en","ft_lang_prob":0.90365106,"math_prob":0.767277,"size":2421,"snap":"2023-40-2023-50","text_gpt3_token_len":576,"char_repetition_ratio":0.122465864,"word_repetition_ratio":0.78125,"special_character_ratio":0.2503098,"punctuation_ratio":0.11136891,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.983873,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-11-29T05:33:03Z\",\"WARC-Record-ID\":\"<urn:uuid:dd81b7c4-3b9a-45f9-9fd1-1b89b237b397>\",\"Content-Length\":\"81533\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:77f72528-8862-4af5-b1aa-49d75bb3cbe4>\",\"WARC-Concurrent-To\":\"<urn:uuid:4b1eea1d-ce0d-4bb6-908f-a0f4d6e785f2>\",\"WARC-IP-Address\":\"18.165.98.126\",\"WARC-Target-URI\":\"https://fedevel.com/blog/pid-controller-implementation-in-software-phils-lab-6\",\"WARC-Payload-Digest\":\"sha1:MS2HCDXD3HQS2FHHSUXEJEHSLXMCTBDF\",\"WARC-Block-Digest\":\"sha1:SG5F5AQLFSXPL4XAFMCYCS2I2ECAQIIV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100056.38_warc_CC-MAIN-20231129041834-20231129071834-00213.warc.gz\"}"}