PID控制c程序

至于参数的整定,根据响应的情况调,比如,响应慢了,我就增大kp,或者减小kd,超调大了,就减小kp或增大点kd,这个规律你可以看看PID三个参数的作用:)
// 定义PID参数结构体
typedef struct PID { //结构体定义
int SetPoint //设定值
int Proportion; // Proportion 比例系数
int Integral; // Integral 积分系数
int Derivative; // Derivative 微分系数
int LastError; // Error[-1] 前一拍误差
int PreError; // Error[-2] 前两拍误差
} PID;
main()
{
PID vPID; //定义结构变量名PIDInit ( &vPID ); //Initialize Structure vPID.Proportion = 10; //Set PID Coefficients vPID.Integral = 10; // Set PID Integral
vPID.Derivative = 10; // Set PID Derivative
vPID. SetPoint = //根据实际情况设定
while(1)
{
Verror=Measure(); //得到AD的输出值
Error =vPID. SetPoint- Verror; //与设定值比较,得到误差值tempi=PIDCal(&vPID, Error;
laser.Value+=tempi; // Value与Num[2]为共同体,共同体名laser
LASERH=laser.Num[0];
LASERL=laser.Num[1];
}
}
///////////////////////////////////////////////////////////////////////
//Title:PID参数初始化
//Description: Proportion="0"
// Integral=0
// LastError=0
//Input: PID的P、I控制常数和之前的误差量(PID *pp)
//Return:
void PIDInit (PID *pp) //PID参数初始化,都置0
{
memset ( pp,0,sizeof(PID));
//memset()的函数,它可以一字节一字节地把整个数组设置为一个指定的值。

// memset()函数在mem.h头文件中声明,它把数组的起始地址作为其第一个参数,
//第二个参数是设置数组每个字节的值,第三个参数是数组的长度(字节数,不是元素个数)。

//其函数原型为:void *memset(void*,int,unsigned);
//头文件<string.h>
}
///////////////////////////////////////////////////////////////////////
//Title:增量式PID算法程序
//Description:给出一个误差增量
//Input: PID的P、I控制常数和之前的误差量(PID *pp)& 当前误差量(ThisError)
//Return: 误差增量templ
//////////////////////////////////////////////////////////////////////
int PIDCal( PID *pp, int ThisError ){
//增量式PID算法(需要控制的不是控制量的绝对值,而是控制量的增量)
int pError,dError,iError;
long templ;
pError = ThisError-pp->LastError;
iError = ThisError;
dError = ThisError-2*(pp->LastError)+pp->PreError;
//增量计算
templ=pp->Proportion*pError + pp->Integral*iError+pp->Derivative*dError; //增量
//存储误差用于下次运算
pp->PreError = pp->LastError;
pp->LastError = ThisError;
return ((int)(templ>>8));
}。

合集下载

模糊PID控制温控系统设计C语言程序代码

模糊PID控制温控系统设计C语言程序代码

模糊PID控制温控系统设计C语言程序代码介绍本文介绍了使用模糊PID控制方法来设计温控系统的C语言程序代码。

本温控系统使用传感器读取室内温度,然后根据读取的数值对应调整冷风机的风速和加热器的加热时间,从而控制室内温度达到一个设定值。

系统设计本温控系统采用模糊PID控制方法,具体实现流程如下:1.根据设定温度和当前室内温度计算出误差值2.使用模糊控制方法将误差值转化为温度调节量3.根据模糊控制输出的温度调节量计算出PID控制器的输出4.根据PID控制器的输出调节冷风机的风速和加热器的加热时间系统设计中需要使用的传感器,冷风机和加热器的具体型号及参数需要根据实际情况进行选择。

此处不做详细说明。

程序代码实现以下代码实现了上述系统设计,包括模糊控制和PID控制。

// 温控系统C语言程序代码#include<stdio.h>#include<stdlib.h>// 模糊控制double GetTemperatureByFuzzy(double error){double delta = 0.5; // 设定的温度调节步长double result = 0;if (error <= -5){result = 1;}else if (error > -5 && error < 0){result = (error + 5) / 5.0;}else if (error >= 0 && error < 5){result = (5 - error) / 5.0;}else{result = 0;}return result * delta;}// PID控制double GetTemperatureByPID(double error, double lastError, double integ ral){double Kp = 0.5; // 比例系数double Ki = 0.01; // 积分系数double Kd = 0.1; // 微分系数double deltaT = 0.1; // 采样时间double derivate = (error - lastError) / deltaT;double result = Kp * error + Ki * integral + Kd * derivate;return result;}// 主函数int main(){double setTemp = 25; // 设定温度double curTemp = 24; // 当前温度,需要从传感器读取double lastError = 0; // 上一次的误差值double integral = 0; // 积分项while (1){double error = setTemp - curTemp; // 计算当前误差值double fuzzyTemp = GetTemperatureByFuzzy(error); // 模糊控制integral += error; // 更新积分项double pidTemp = GetTemperatureByPID(error, lastError, integra l); // PID控制lastError = error; // 更新上一次误差值// 根据pidTemp和fuzzyTemp调节冷风机的风速和加热器的加热时间,省略// 读取传感器更新当前温度,省略// curTemp = GetCurTemp();// 采样时间,省略// sleep(1);}}本文介绍了使用模糊PID控制方法来设计温控系统的C语言程序代码。

PID控制算法的C语言实现(完整版)

PID控制算法的C语言实现(完整版)

PID控制算法的C语言实现(完整版) 在现代工业生产中,为了实现对生产过程的精确控制,我们需要采用一种能够根据实际需求自动调整参数的控制算法。

PID(Proportional-Integral-Derivative)控制算法就是这样一种广泛应用于工业控制系统的算法。

本文将详细介绍PID控制算法的C语言实现,包括算法的基本原理、实现方法以及注意事项。

我们来了解一下PID控制算法的基本原理。

PID控制器由三个部分组成:比例(P)、积分(I)和微分(D)。

这三个部分分别对误差信号进行处理,然后将处理后的信号相加得到控制输出。

具体来说,比例部分根据误差信号的大小产生相应的控制作用;积分部分对误差信号进行累积,以消除系统的静差;微分部分对误差信号的变化趋势进行预测,以便及时调整控制策略。

通过这三个部分的综合作用,PID控制器能够实现对生产过程的精确控制。

接下来,我们来看一下如何用C语言实现PID控制算法。

我们需要定义一些变量来存储所需的参数和状态信息。

例如,我们需要定义比例系数Kp、积分系数Ki、微分系数Kd以及误差信号e等。

我们还需要定义一些变量来存储上一次的误差信号和积分项等。

这些变量的定义如下:```cdouble Kp, Ki, Kd; // 比例、积分、微分系数double e; // 当前误差信号double de; // 当前误差信号的导数double last_e; // 上一次的误差信号double integral; // 积分项有了这些变量之后,我们就可以开始实现PID控制器的计算过程了。

PID控制器的计算过程主要包括以下几个步骤:1. 计算误差信号:当前误差信号等于期望值与实际值之差。

2. 计算比例项:比例项等于当前误差信号乘以比例系数Kp;3. 计算积分项:积分项等于当前误差信号乘以积分系数Ki加上累积误差信号乘以积分系数Ki;4. 计算微分项:微分项等于当前误差信号的导数乘以微分系数Kd;5. 计算控制输出:控制输出等于比例项、积分项和微分项之和。

pidc语言程序

pidc语言程序

pidc语言程序
PIDC语言程序是一种用于控制工业过程的编程语言。

PIDC的全称为Proportional-Integral-DerivativeController,即比例积分微分控制器。

PIDC语言程序在工业自动化领域中应用广泛,特别是在控制温度、湿度、压力、流量等过程方面。

PIDC语言程序由多个功能块组成,每个功能块都有特定的输入和输出。

常用的功能块包括:比例调节器(P调节器)、积分调节器(I调节器)、微分调节器(D调节器)、开关控制器、计时器、计数器等。

这些功能块可以根据需要进行组合,形成一个完整的PIDC控制系统。

PIDC语言程序的编写需要掌握基本的编程概念和语法。

其中,需要了解如何定义变量、使用运算符、编写条件语句、循环语句以及调用函数等。

此外,还需要了解PIDC语言的特殊语法规则,如时间常数、采样周期、目标值等。

PIDC语言程序的优点在于可以实现高精度的控制,以及对过程的自适应调整。

PIDC控制器可以根据实时反馈的过程数据,自动调整控制参数,使得控制系统能够更加稳定、可靠地运行。

总之,PIDC语言程序是一种重要的工业自动化编程语言,其应用广泛、功能强大,对于提高工业生产效率和产品质量具有重要意义。

- 1 -。

稳定边界法整定pid参数的c程序

稳定边界法整定pid参数的c程序

稳定边界法整定PID参数的C程序一、概述在控制系统中,PID控制器是一种广泛应用的控制器,其参数的整定对控制系统的性能有着重要的影响。

稳定边界法是一种常用的PID参数整定方法,通过该方法可以通过实验测定系统的临界参数从而确定PID控制器的参数值。

本文将介绍如何使用C语言编写稳定边界法整定PID参数的程序。

二、PID控制器概述PID控制器是由比例(P),积分(I),微分(D)三个部分组成的控制器。

其控制输出由当前误差、历史误差以及误差变化率的线性组合得到。

PID控制器适用于许多工业控制系统,并且具有简单、稳定、可靠等特点。

三、稳定边界法原理稳定边界法是一种通过实验来确定控制系统参数的方法。

在使用该方法时,我们需要对系统进行一系列的步进输入,然后测量系统的输出响应。

通过分析系统输出响应的频率特性,我们可以得到系统的临界参数,从而确定PID控制器的参数值。

四、C程序编写在C程序中,我们需要完成以下几个步骤来实现稳定边界法整定PID参数:1. 初始化控制系统。

2. 设定一系列的步进输入信号,并且获取系统的输出响应。

3. 对系统的输出响应进行频率特性分析,得到系统的临界参数。

4. 根据临界参数计算PID控制器的参数值。

5. 输出PID控制器的参数值。

以下是C程序稳定边界法整定PID参数的代码示例:```c#include <stdio.h>#include <math.h>// 初始化控制系统void initControlSystem() {// TODO: 初始化控制系统}// 设定步进输入信号void setStepInput() {// TODO: 设定步进输入信号}// 获取系统输出响应void getOutputResponse() {// TODO: 获取系统输出响应}// 频率特性分析void frequencyAnalysis() {// TODO: 频率特性分析}// 计算PID参数void calculatePIDParameters() { // TODO: 计算PID参数}// 输出PID参数void outputPIDParameters() { // TODO: 输出PID参数}int m本人n() {initControlSystem();setStepInput();getOutputResponse();frequencyAnalysis();calculatePIDParameters();outputPIDParameters();return 0;}```五、总结通过上述C程序示例,我们可以实现使用稳定边界法来整定PID参数。

(完整word版)增量式PID控制C语言代码

(完整word版)增量式PID控制C语言代码
// Integral=0
// LastError=0
//Input: PID的P、I控制常数和之前的误差量(PID *pp)
//Return:
//////////////////////////////////////////////////////////////////////
void PIDInit (PID *pp) //PID参数初始化,都置0
//增量式PID算法(需要控制的不是控制量的绝对值,而是控制量的增量)
int pError,dError,iError;
long templ;
pError = ThisError-pp->LastError;
iError = ThisError;
dError = ThisError-2*(pp->LastError)+pp->PreError;
增量式PID控制C语言代码
增量式PID控制公式:
上面△u(k)是控制量增量,“增量式PID”就是直接以这个增量进行控制。
至于参数的整定,根据响应的情况调,比如,响应慢了,我就增大kp,或者减小kd,超调大了就减小kp或增大点kd,这个规律你可以看看PID三个参数的作用:)
////////////////////////////////////////////////////////////////
{
memset ( pp,0,sizeof(PID));
//memset()的函数,它可以一字节一字节地把整个数组设置为一个指定的值。
// memset()函数在mem.h头文件中声明,它把数组的起始地址作为其第一个参数,
//第二个参数是设置数组每个字节的值,第三个参数是数组的长度(字节数,不是元素个数)。

温度控制的PID算法的C语言程序

温度控制的PID算法的C语言程序

我的题目是:基于PID算法的温度控制系统89C51单片机,通过键盘输入预设值,与DS18B20测得的实际值做比较,然后驱动制冷或加热电路。

用keil C语言来实现PID的控制。

最佳答案//PID算法温控C语言2008-08-17 18:58#include<reg51.h>#include<intrins.h>#include<math.h>#include<string.h>struct PID {unsigned int SetPoint; // 设定目标 Desired Valueunsigned int Proportion; // 比例常数 Proportional Constunsigned int Integral; // 积分常数 Integral Constunsigned int Derivative; // 微分常数 Derivative Constunsigned int LastError; // Error[-1]unsigned int PrevError; // Error[-2]unsigned int SumError; // Sums of Errors};struct PID spid; // PID Control Structureunsigned int rout; // PID Response (Output)unsigned int rin; // PID Feedback (Input)sbit data1=P1^0;sbit clk=P1^1;sbit plus=P2^0;sbit subs=P2^1;sbit stop=P2^2;sbit output=P3^4;sbit DQ=P3^3;unsigned char flag,flag_1=0;unsigned char high_time,low_time,count=0;//占空比调节参数unsigned char set_temper=35;unsigned char temper;unsigned char i;unsigned char j=0;unsigned int s;/*********************************************************** 延时子程序,延时时间以12M晶振为准,延时时间为30us×time***********************************************************/ void delay(unsigned char time){unsigned char m,n;for(n=0;n<time;n++)for(m=0;m<2;m++){}}/*********************************************************** 写一位数据子程序***********************************************************/ void write_bit(unsigned char bitval){EA=0;DQ=0; /*拉低DQ以开始一个写时序*/if(bitval==1){_nop_();DQ=1; /*如要写1,则将总线置高*/}delay(5); /*延时90us供DA18B20采样*/DQ=1; /*释放DQ总线*/_nop_();_nop_();EA=1;}/*********************************************************** 写一字节数据子程序***********************************************************/ void write_byte(unsigned char val){unsigned char i;unsigned char temp;EA=0; /*关中断*/TR0=0;for(i=0;i<8;i++) /*写一字节数据,一次写一位*/{temp=val>>i; /*移位操作,将本次要写的位移到最低位*/temp=temp&1;write_bit(temp); /*向总线写该位*/}delay(7); /*延时120us后*/// TR0=1;EA=1; /*开中断*/}/*********************************************************** 读一位数据子程序***********************************************************/ unsigned char read_bit(){unsigned char i,value_bit;EA=0;DQ=0; /*拉低DQ,开始读时序*/_nop_();_nop_();DQ=1; /*释放总线*/for(i=0;i<2;i++){}value_bit=DQ;EA=1;return(value_bit);}/*********************************************************** 读一字节数据子程序***********************************************************/ unsigned char read_byte(){unsigned char i,value=0;EA=0;for(i=0;i<8;i++){if(read_bit()) /*读一字节数据,一个时序中读一次,并作移位处理*/ value|=0x01<<i;delay(4); /*延时80us以完成此次都时序,之后再读下一数据*/}EA=1;return(value);}/***********************************************************复位子程序***********************************************************/ unsigned char reset(){unsigned char presence;EA=0;DQ=0; /*拉低DQ总线开始复位*/delay(30); /*保持低电平480us*/DQ=1; /*释放总线*/delay(3);presence=DQ; /*获取应答信号*/delay(28); /*延时以完成整个时序*/EA=1;return(presence); /*返回应答信号,有芯片应答返回0,无芯片则返回1*/ }/***********************************************************获取温度子程序***********************************************************/void get_temper(){unsigned char i,j;do{i=reset(); /*复位*/}while(i!=0); /*1为无反馈信号*/ i=0xcc; /*发送设备定位命令*/ write_byte(i);i=0x44; /*发送开始转换命令*/ write_byte(i);delay(180); /*延时*/do{i=reset(); /*复位*/}while(i!=0);i=0xcc; /*设备定位*/write_byte(i);i=0xbe; /*读出缓冲区内容*/write_byte(i);j=read_byte();i=read_byte();i=(i<<4)&0x7f;s=(unsigned int)(j&0x0f);s=(s*100)/16;j=j>>4;temper=i|j; /*获取的温度放在temper中*/}/*================================================================= ===================================Initialize PID Structure=================================================================== ==================================*/void PIDInit (struct PID *pp){memset ( pp,0,sizeof(struct PID));}/*================================================================= ===================================PID计算部分=================================================================== ==================================*/unsigned int PIDCalc( struct PID *pp, unsigned int NextPoint ){unsigned int dError,Error;Error = pp->SetPoint - NextPoint; // 偏差pp->SumError += Error; // 积分dError = pp->LastError - pp->PrevError; // 当前微分pp->PrevError = pp->LastError;pp->LastError = Error;return (pp->Proportion * Error//比例+ pp->Integral * pp->SumError //积分项+ pp->Derivative * dError); // 微分项}/***********************************************************温度比较处理子程序***********************************************************/ compare_temper(){unsigned char i;if(set_temper>temper){if(set_temper-temper>1){high_time=100;low_time=0;}else{for(i=0;i<10;i++){ get_temper();rin = s; // Read Inputrout = PIDCalc ( &spid,rin ); // Perform PID Interation}if (high_time<=100)high_time=(unsigned char)(rout/800); elsehigh_time=100;low_time= (100-high_time);}}else if(set_temper<=temper){if(temper-set_temper>0){high_time=0;low_time=100;}else{for(i=0;i<10;i++){ get_temper();rin = s; // Read Inputrout = PIDCalc ( &spid,rin ); // Perform PID Interation }if (high_time<100)high_time=(unsigned char)(rout/10000);elsehigh_time=0;low_time= (100-high_time);}}// else// {}}/***************************************************** T0中断服务子程序,用于控制电平的翻转 ,40us*100=4ms周期******************************************************/ void serve_T0() interrupt 1 using 1{if(++count<=(high_time))output=1;else if(count<=100){output=0;}elsecount=0;TH0=0x2f;TL0=0xe0;}/***************************************************** 串行口中断服务程序,用于上位机通讯******************************************************/void serve_sio() interrupt 4 using 2 {/* EA=0;RI=0;i=SBUF;if(i==2){while(RI==0){}RI=0;set_temper=SBUF;SBUF=0x02;while(TI==0){}TI=0;}else if(i==3){TI=0;SBUF=temper;while(TI==0){}TI=0;}EA=1; */}void disp_1(unsigned char disp_num1[6]) {unsigned char n,a,m;for(n=0;n<6;n++){// k=disp_num1[n];for(a=0;a<8;a++){clk=0;m=(disp_num1[n]&1);disp_num1[n]=disp_num1[n]>>1;if(m==1)data1=1;elsedata1=0;_nop_();clk=1;_nop_();}}}/***************************************************** 显示子程序功能:将占空比温度转化为单个字符,显示占空比和测得到的温度******************************************************/ void display(){unsigned char codenumber[]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6};unsigned char disp_num[6];unsigned int k,k1;k=high_time;k=k%1000;k1=k/100;if(k1==0)disp_num[0]=0;elsedisp_num[0]=0x60;k=k%100;disp_num[1]=number[k/10];disp_num[2]=number[k%10];k=temper;k=k%100;disp_num[3]=number[k/10];disp_num[4]=number[k%10]+1;disp_num[5]=number[s/10];disp_1(disp_num);}/*********************************************************** 主程序***********************************************************/ main(){unsigned char z;unsigned char a,b,flag_2=1,count1=0;unsigned char phil[]={2,0xce,0x6e,0x60,0x1c,2};TMOD=0x21;TH0=0x2f;TL0=0x40;SCON=0x50;PCON=0x00;TH1=0xfd;TL1=0xfd;PS=1;EA=1;EX1=0;ET0=1;ES=1;TR0=1;TR1=1;high_time=50;low_time=50;PIDInit ( &spid ); // Initialize Structure spid.Proportion = 10; // Set PID Coefficients spid.Integral = 8;spid.Derivative =6;spid.SetPoint = 100; // Set PID Setpoint while(1){if(plus==0){EA=0;for(a=0;a<5;a++)for(b=0;b<102;b++){} if(plus==0){set_temper++;flag=0;}}else if(subs==0) {for(a=0;a<5;a++)for(b=0;a<102;b++){} if(subs==0){set_temper--;flag=0;}}else if(stop==0) {for(a=0;a<5;a++)for(b=0;b<102;b++){} if(stop==0){flag=0;break;}EA=1;}get_temper();b=temper;if(flag_2==1)a=b;if((abs(a-b))>5) temper=a;elsetemper=b;a=temper;flag_2=0;if(++count1>30) {display();count1=0;}compare_temper(); }TR0=0;z=1;while(1){EA=0;if(stop==0){for(a=0;a<5;a++)for(b=0;b<102;b++){} if(stop==0)disp_1(phil);// break;}EA=1;}}//DS18b20 子程序#include <REG52.H>sbit DQ=P2^1; //定义端口typedef unsigned char byte;typedef unsigned int word;//延时void delay(word useconds){for(;useconds>0;useconds--);}//复位byte ow_reset(void){byte presence;DQ=0; //DQ低电平delay(29); //480us DQ=1; //DQ高电平delay(3); //等待presence=DQ; //presence信号delay(25);return(presence);} //0允许,1禁止//从1-wire 总线上读取一个字节byte read_byte(viod){byte i;byte value=0;for (i=8;i>0;i--){value>>=1;DQ=0;DQ=1;delay(1);if(DQ)value|=0x80;delay(6);}return(value);}//向1-wire总线上写一个字节void write_byte(char val){byte i;for (i=8;i>0;i--) //一次写一个字节{DQ=0;DQ=val&0x01;delay(5);DQ=1;val=val/2;}delay(5);}//读取温度char Read_Temperature(void) {union{byte c[2];int x;}temp;ow_reset();write_byte(0xcc);write_byte(0xBE);temp.c[1]=read_byte(); temp.c[0]=read_byte();ow_reset();write_byte(0xCC);write_byte(0x44);return temp.x/2;}参考资料:你把这两个程序组合就可以了图1 模拟PID 控制系统原理图PID 控制器的控制规律可以描述为:(1)比例(P)控制能迅速反应误差,从而减小稳态误差。

C语言编写PID温度控制器程序

C语言编写PID温度控制器程序姓名:况武(07421236)班级:自二系别:通控系#include <stdio.h>#include<math.h>struct _pid {int pv; /*integer that contains the process value*/int sp; /*integer that contains the set point*/float integral;float pgain;float igain;float dgain;int deadband;int last_error;};struct _pid warm,*pid;int process_point, set_point,dead_band;float p_gain, i_gain, d_gain, integral_val,new_integ;;/*------------------------------------------------------------------------pid_initDESCRIPTION This function initializes the pointers in the _pid structureto the process variable and the setpoint. *pv and *sp areinteger pointers.------------------------------------------------------------------------*/void pid_init(struct _pid *warm, int process_point, int set_point){struct _pid *pid;pid = warm;pid->pv = process_point;pid->sp = set_point;}/*------------------------------------------------------------------------pid_tuneDESCRIPTION Sets the proportional gain (p_gain), integral gain (i_gain),derivitive gain (d_gain), and the dead band (dead_band) ofa pid control structure _pid.------------------------------------------------------------------------*/void pid_tune(struct _pid *pid, float p_gain, float i_gain, float d_gain, int dead_band){pid->pgain = p_gain;pid->igain = i_gain;pid->dgain = d_gain;pid->deadband = dead_band;pid->integral= integral_val;pid->last_error=0;}/*------------------------------------------------------------------------pid_setintegDESCRIPTION Set a new value for the integral term of the pid equation.This is useful for setting the initial output of thepid controller at start up.------------------------------------------------------------------------*/void pid_setinteg(struct _pid *pid,float new_integ){pid->integral = new_integ;pid->last_error = 0;}/*------------------------------------------------------------------------pid_bumplessDESCRIPTION Bumpless transfer algorithim. When suddenly changingsetpoints, or when restarting the PID equation after anextended pause, the derivative of the equation can causea bump in the controller output. This function will helpsmooth out that bump. The process value in *pv shouldbe the updated just before this function is used.温度PID控制的C语言程序?------------------------------------------------------------------------*/void pid_bumpless(struct _pid *pid){pid->last_error = (pid->sp)-(pid->pv);}/*------------------------------------------------------------------------pid_calcDESCRIPTION Performs PID calculations for the _pid structure *a. This function uses the positional form of the pid equation, and incorporates an integral windup prevention algorithim. Rectangular integration is used, so this function must be repeated on a consistent time basis for accurate control.RETURN V ALUE The new output value for the pid loop.USAGE #include "control.h"*/float pid_calc(struct _pid *pid){int err;float pterm, dterm, result, ferror;err = (pid->sp) - (pid->pv);if (abs(err) > pid->deadband){ferror = (float) err; /*do integer to float conversion only once*/pterm = pid->pgain * ferror;if (pterm > 100 || pterm < -100){pid->integral = 0.0;}else{pid->integral += pid->igain * ferror;if (pid->integral > 100.0){pid->integral = 100.0;}else if (pid->integral < 0.0) pid->integral = 0.0;}dterm = ((float)(err - pid->last_error)) * pid->dgain;result = pterm + pid->integral + dterm;}else result = pid->integral;pid->last_error = err;return (result);}void main(void){float display_value;int count=0;pid = &warm;// printf("Enter the values of Process point, Set point, P gain, I gain, D gain \n");// scanf("%d%d%f%f%f", &process_point, &set_point, &p_gain, &i_gain, &d_gain);process_point = 30;set_point = 40;p_gain = (float)(5.2);i_gain = (float)(0.77);d_gain = (float)(0.18);dead_band = 2;integral_val =(float)(0.01);printf("The values of Process point, Set point, P gain, I gain, D gain \n");printf(" %6d %6d %4f %4f %4f\n", process_point, set_point, p_gain, i_gain, d_gain);printf("Enter the values of Process point\n");while(count<=20){scanf("%d",&process_point);pid_init(&warm, process_point, set_point);pid_tune(&warm, p_gain,i_gain,d_gain,dead_band);pid_setinteg(&warm,0.0); //pid_setinteg(&warm,30.0);//Get input value for process pointpid_bumpless(&warm);// how to display outputdisplay_value = pid_calc(&warm);printf("%f\n", display_value);//printf("\n%f%f%f%f",warm.pv,warm.sp,warm.igain,warm.dgain); count++;}}。

pid算法温度控制c语言程序

pid算法温度控制c语言程序PID算法是一种常用的温度控制算法,广泛应用于各种温度控制系统中。

在C语言中,我们可以通过编写程序来实现PID算法的温度控制功能。

我们需要了解PID算法的基本原理。

PID算法是通过对系统的反馈信号进行不断调整,使得系统的输出达到期望值。

PID算法由三个部分组成:比例控制、积分控制和微分控制。

比例控制根据反馈信号与期望值的差异来调整输出;积分控制根据反馈信号与期望值的累积差异来调整输出;微分控制根据反馈信号的变化率来调整输出。

在C语言中,我们可以使用变量来表示系统的输入、输出和期望值。

以下是一个简单的示例代码:```c#include <stdio.h>// 定义PID参数float Kp = 1.0; // 比例系数float Ki = 0.5; // 积分系数float Kd = 0.2; // 微分系数// 定义系统变量float setpoint = 25.0; // 期望值float input = 0.0; // 输入值float output = 0.0; // 输出值// 定义误差变量float error = 0.0; // 当前误差float last_error = 0.0; // 上一次误差float integral = 0.0; // 累积误差// PID算法函数float pid_algorithm(float setpoint, float input) {// 计算误差error = setpoint - input;// 计算比例控制float proportional = Kp * error;// 计算积分控制integral += error;float integral_control = Ki * integral;// 计算微分控制float derivative = Kd * (error - last_error); // 计算输出output = proportional + integral_control + derivative;// 更新误差last_error = error;return output;}int main(){// 模拟温度传感器的输入input = 23.5;// 调用PID算法函数output = pid_algorithm(setpoint, input);// 打印输出结果printf("Output: %.2f\n", output);return 0;}```在上述代码中,我们首先定义了PID算法的参数和系统变量。

PID控制C代码

PID控制当今的自动控制技术都是基于反馈的概念。

反馈理论的要素包括三个部分:测量、比较和执行。

测量关心的变量,与期望值相比较,用这个误差纠正调节控制系统的响应。

这个理论和应用自动控制的关键是,做出正确的测量和比较后,如何才能更好地纠正系统。

比例控制(P):比例控制是最常用的控制手段之一,比方说我们控制一个加热器的恒温100度,当开始加热时,离目标温度相差比较远,这时我们通常会加大加热,使温度快速上升,当温度超过100度时,我们则关闭输出,通常我们会使用这样一个函数e(t) = SP –y(t);u(t) = e(t)*PSP——设定值、e(t)——误差值、y(t)——反馈值、u(t)——输出值、P——比例系数滞后性不是很大的控制对象使用比例控制方式就可以满足控制要求,但很多被控对象中因为有滞后性。

也就是如果设定温度是200度,当采用比例方式控制时,如果P选择比较大,则会出现当温度达到200度输出为0后,温度仍然会止不住的向上爬升,比方说升至230度,当温度超过200度太多后又开始回落,尽管这时输出开始出力加热,但温度仍然会向下跌落一定的温度才会止跌回升,比方说降至170度,最后整个系统会稳定在一定的范围内进行振荡。

如果这个振荡的幅度是允许的比方说家用电器的控制,那则可以选用比例控制.比例积分控制(PI):积分的存在是针对比例控制要不就是有差值要不就是振荡的这种特点提出的改进,它常与比例一块进行控制,也就是PI控制。

其公式有很多种,但大多差别不大,标准公式如下:u(t) = Kp*e(t) + Ki∑e(t) +u0u(t)——输出、Kp——比例放大系数、Ki——积分放大系数、e(t)——误差、u0——控制量基准值(基础偏差)大家可以看到积分项是一个历史误差的累积值,如果光用比例控制时,我们知道要不就是达不到设定值要不就是振荡,在使用了积分项后就可以解决达不到设定值的静态误差问题,比方说一个控制中使用了PI控制后,如果存在静态误差,输出始终达不到设定值,这时积分项的误差累积值会越来越大,这个累积值乘上Ki后会在输出的比重中越占越多,使输出u(t)越来越大,最终达到消除静态误差的目的。

温度控制的PID算法与C程序实现

温度控制的PID算法与C程序实现PID (Proportional-Integral-Derivative) 是一种经典的反馈控制算法,被广泛应用于温度控制中。

在温度控制中,PID算法可以根据实际温度与设定温度之间的差异来调整控制器的输出,以达到稳定的温度控制。

下面将介绍PID算法的原理及其在C程序中的实现。

PID算法是通过对三个控制参数的不同组合调整,来实现对控制系统的精确控制。

这三个参数分别是比例项(P),积分项(I)和微分项(D)。

比例项(P)是根据实际温度与设定温度的差异计算出来的,并且与这个差异成比例。

比例项主要用于对系统的快速响应进行调整。

如果比例项过大,可能会导致系统产生震荡,如果比例项过小,则可能导致控制系统响应迟缓。

积分项(I)用于校正持续的误差,通过对误差的积分来进行控制系统的调整。

积分项主要用于对系统的稳定性进行调整。

如果积分项过大,可能会导致系统产生超调和振荡,如果积分项过小,则可能导致系统无法快速地消除误差。

微分项(D)用于预测系统未来变化的趋势,并根据这个趋势来进行控制系统的调整。

微分项主要用于对系统的响应速度进行调整。

如果微分项过大,可能会导致系统产生过度的抖动,如果微分项过小,则可能导致系统响应迟缓。

PID算法的输出是三个控制参数的加权和,即 control = P * error + I * integral + D * derivative。

其中,error为实际温度与设定温度的差异,integral为误差的累积和,derivative为误差的变化率。

下面是一个使用PID算法实现温度控制的C程序的示例:```c#include <stdio.h>//PID参数float Kp = 0.5;float Ki = 0.2;float Kd = 0.1;//温度设定值float setpoint = 50.0;//初始化float errorSum = 0;float lastError = 0;//计算PID控制量float calculateOutput(float currentTemperature) float error = setpoint - currentTemperature; errorSum += error;//计算PID控制量float proportional = Kp * error;float integral = Ki * errorSum;float derivative = Kd * (error - lastError);float output = proportional + integral + derivative; lastError = error;return output;int mai//模拟当前温度float currentTemperature = 40.0;//模拟控制循环while (1)//获取温度传感器读数// float currentTemperature = readTemperature(;//计算PID控制量float controlOutput = calculateOutput(currentTemperature); //执行控制动作,例如根据控制量控制加热器或冷却器// executeControlAction(controlOutput);//模拟温度变化currentTemperature += 0.5;//输出当前温度和控制量printf("Current temperature: %.2f, Control output: %.2f\n", currentTemperature, controlOutput);}return 0;```上述C程序中,首先定义了PID参数Kp、Ki和Kd,以及温度设定值setpoint。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档