Incremental PID control C-language code

Source: Internet
Author: User
Tags function prototype

Incremental PID Control C-Language code

Incremental-PID Control formula:



The above U (k) is the amount of control increment, and "incremental pid" is controlled directly by this increment.

As for the parameters of the adjustment, according to the situation of the response, for example, slow response, I will increase KP, or reduce KD, overshoot, on the reduction of KP or increase point KD, this rule you can see the function of PID three parameters:

////////////////////////////////////////////////////////////////

Define PID parameter Structure body

///////////////////////////////////////////////////////////////

typedef struct PID {//struct body definition

int SetPoint//Set Value

int proportion; Proportion proportional coefficient

int Integral; Integral Integral coefficient

int derivative; Derivative differential coefficient

int lasterror; ERROR[-1] Previous beat error

int preerror; Error[-2] first two beat error

} PID;

Main ()

{

PID Vpid; Define structure variable name

Pidinit (&VPID); Initialize Structure

Vpid.proportion = 10; Set PID coefficients

Vpid.integral = 10; Set PID Integral

Vpid.derivative = 10; Set PID derivative

Vpid. SetPoint =//Set According to the actual situation

while (1)

{

Verror=measure (); Get the output value of the ad

Error =vpid.   Setpoint-verror; Compared with the set value, the error value is obtained

Tempi=pidcal (&vpid, Error;

Laser.          Value+=tempi; Value and num[2] for community, community name Laser

Laserh=laser. NUM[0];

Laserl=laser. NUM[1];

}

}

///////////////////////////////////////////////////////////////////////

Title:pid parameter Initialization

description:proportion= "0"

Integral=0

Lasterror=0

Input:pid p, I control constants and previous error quantities (PID *pp)

return:

//////////////////////////////////////////////////////////////////////

void Pidinit (PID *pp)//pid parameter initialization, all 0

{

memset (pp,0,sizeof (PID));

A function of memset () that can set the entire array to a specified value in bytes and bytes.

The memset () function is declared in the mem.h header file, which takes the starting address of the array as its first argument,

The second argument is to set the value of each byte of the array, and the third argument is the length of the array (the number of bytes, not the number of elements).

Its function prototype is: void *memset (void*,int,unsigned);

Header file <string.h>

}

///////////////////////////////////////////////////////////////////////

Title: Incremental PID algorithm Program

Description: Gives an error increment

Input:pid p, I control constant and previous error amount (PID *pp) & Current error amount (THISERROR)

Return: Error increment Templ

//////////////////////////////////////////////////////////////////////

int pidcal (PID *pp, int thiserror) {

Incremental PID algorithm (need to control not the absolute value of control quantity, but the increment of control quantity)

int perror,derror,ierror;

Long Templ;

Perror = thiserror-pp->lasterror;

Ierror = Thiserror;

Derror = thiserror-2* (pp->lasterror) +pp->preerror;

Incremental calculation

Templ=pp->proportion*perror + pp->integral*ierror+pp->derivative*derror; Incremental

Storage error For next operation

Pp->preerror = pp->lasterror;

Pp->lasterror = Thiserror;

return ((int) (templ>>8));

}

Reference from: http://blog.sina.com.cn/s/blog_408540af0100asu3.html

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.