Miscellaneous PID control algorithms-final article: using C language to implement PID Algorithms in 51 Single-Chip Microcomputer

Source: Internet
Author: User

Unfortunately, the second chapter cannot be published on the homepage. Hit the ground. Send the final article-code.

For code design ideas, see the first two articles.

// Pid. h # ifndef _ PID __# define _ PID _/* PID = Uk + KP * [E (k)-E (k-1)] + KI * E (k) + KD * [E (k)-2E (k-1) + E (K-2)]; (incremental PID formula) function entry: RK (set value ), CK (actual value), KP, KI, KD function outlet: U (K) */typedef struct PIDValue {int8 KP; int8 KI; int8 KD; int8 F; int8 BITMOV; int EK [3]; int UK; int RK; int CK; int UK_REAL;} pid_str; // PIDValueStr PID; void pid_exe (pid_str * PID); # endif/pid. c/* PID = PID-> UK_REAL + PID-> KP * [E (k)-E (k-1)] + PID-> KI * E (k) + PID-> KD * [E (k)-2E (k-1) + E (K-2)]; (incremental PID formula) function entry: PID-> RK (set value), PID-> CK (actual value), PID-> KP, PID-> KI, PID-> KD function outlet: U (K) */# include "defines. h "# include" pid. h "# define MAXOUT 0xff // # define MAXGAP 100 void pid_exe (pid_str * PID) {PID-> EK [2] = PID-> EK [1]; PID-> EK [1] = PID-> EK [0]; PID-> EK [0] = PID-> RK-PID-> CK; PID-> UK_REAL = PID-> UK_REAL + PID-> KP * (PID-> EK [0]-PID-> EK [1]) // after one differentiation, the integral is the original number + (float) PID-> KI * PID-> EK [0]/PID-> F // direct integral + (float) PID-> KD * (PID-> EK [0]-2 * PID-> EK [1] + PID-> EK [2]) * PID-> F; // second-order differential credits are first-order differential if (PID-> UK_REAL> PID-> BITMOV)> = MAXOUT) {PID-> UK = MAXOUT ;} else if (PID-> UK_REAL> PID-> BITMOV <=-MAXOUT) {PID-> UK =-MAXOUT ;} else {PID-> UK = PID-> UK_REAL> PID-> BITMOV ;}}

Here the code I wrote uses the incremental PID (I .e. UK_REAL + PID-> KP * [E (k)-E (k-1)] + PID-> KI * E (k) + PID-> KD * [E (k)-2E (k-1) + E (K-2)]; this statement corresponds to the value added by the pid control on the basis of the previous pid control, which is equivalent to a single guide ). The final output result will accumulate the values of each operation.

The difference between the extracted positional pid and incremental pid is attached ..

(1) the output of the positional PID control is related to the entire past state, and the accumulated error value is used. The output of the incremental PID is only related to the error of the current beat and the first two beats, therefore, the cumulative error of positional PID control is relatively greater;

(2) The incremental PID controls the increment of the control amount and has no integral effect. Therefore, this method is applicable to the objects with integral components, such as stepper motors, of the execution mechanism, the Positional PID is suitable for the objects without integral components of the actuator, such as electro-hydraulic servo valve.

(3) because the incremental PID outputs the increment of the control amount, if the computer fails, the impact of false operations is small, and the execution mechanism itself has the memory function, it can still be kept in the original position, it does not seriously affect the work of the system, but the positional output directly corresponds to the output of the object, which has a great impact on the system.

PS: The Aesthetic Fatigue of the previous template .. Change the template to change your mood ..

PPS: Please make a little effort .. After...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.