Position-based PID control algorithm

Source: Internet
Author: User
Tags constant
Just before soon had a PID, part of the procedure is as follows, for reference only

/*==============================================================================
In the use of single-chip microcomputer as the control of the CPU, please simplify, the specific PID parameters must be determined by the concrete object through the experiment.
Because of the processing speed and the limitation of RAM resource, the floating-point arithmetic is not used, but all the parameters are all integers.
Computation to the last divided by a 2 of the n-th square data (equivalent to shift), for similar fixed-point number operations, can greatly improve the speed of operation,
According to the different requirements of control precision, when the precision requirement is high, pay attention to retain the "remainder" caused by the shift, and do well the remainder compensation.
This program is only the basic structure of commonly used PID algorithm, does not include the input and output processing part.
==============================================================================*/

#include <string.h>
#include <stdio.h>


/*===============================================================================
PID Function
The PID function is used in mainly
Control applications. PID Calc performs one iteration of the PID
Algorithm.
While the PID function works, main was just a dummy program showing
A typical usage.

PID function
The PID function is mainly used for control applications. The PID calculator performs a PID iterative algorithm. Although the PID function works,
Mostly just a virtual program that shows a typical use.
================================================================================*/
typedef struct PID {
Double setpoint; Set Target desired Value
Double proportion; Proportional constant Proportional Const
Double Integral; Integral constant Integral Const
Double derivative; Differential constant derivative Const
Double LastError; ERROR[-1]
Double Preverror; Error[-2]
Double Sumerror; Sums of Errors
} PID;


/*================================ PID Calculation Part ===============================*/
Double Pidcalc (PID *pp, double nextpoint)
{
Double Derror, Error;

Error = pp->setpoint-nextpoint; Deviation
Pp->sumerror + = Error; Integral
Derror = pp->lasterror-pp->preverror; Current differential
Pp->preverror = pp->lasterror;
Pp->lasterror = Error;
Return (Pp->proportion * Error//scale item
+ pp->integral * Pp->sumerror//Points
+ pp->derivative * Derror//Differential term
);
}

/*======================= initialized PID structure Initialize PID structure===========================*/
void Pidinit (PID *pp)
{
memset (pp,0,sizeof (PID));
}


/*======================= Main program Main program=======================================*/
Double sensor (void)//virtual sensor functions Dummy sensor function{return 100.0;}
void Actuator (double rdelta)//virtual drive function Dummy Actuator function{}
void Main (void)
{
PID SPID; PID control structure PID controlling Structure
Double ROut; PID response (output) PID Response (outputs)
Double rIn; PID feedback (inputs) PID Feedback (input)
Pidinit (&AMP;SPID); Initialize Structure Initialize Structure
Spid.proportion = 0.5; Setting PID coefficients set PID coefficients
Spid.integral = 0.5;
spid.derivative = 0.0;
Spid.setpoint = 100.0; Set PID setting set PID SetPoint
for (;;)
{//analog up to PID processing mock up of PID processing
RIn = sensor (); Read input
ROut = Pidcalc (&spid,rin); PID iterative Perform pid Interation performed
Actuator (ROut); The effect of the required changes Effect Needed changes

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.