#include <stdio.h>
#include<math.h> //定义PID 的结构体
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_init --- This function initializes the
pointers in the _pid structure to the process variable
and the setpoint. *pv and *sp are integer 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_tune --- Sets the proportional gain (p_gain), integral gain (i_gain),
derivitive gain (d_gain), and the dead band (dead_band)
of a pid control structure _pid. 设定PID参数 ---- P,I,D,死区
*/ 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 = ;
} /*
pid_setinteg --- Set a new value for the integral term of the pid equation.
This is useful for setting the initial output of the pid controller at start up. 设定输出初始值
*/ void pid_setinteg( struct _pid *pid, float new_integ )
{
pid->integral = new_integ;
pid->last_error = ;
} /* pid_bumpless --- Bumpless transfer algorithim.
When suddenly changing setpoints, or when restarting the PID equation
after an extended pause, the derivative of the equation can cause a bump
in the controller output. This function will help smooth out that bump.
The process value in *pv should be the updated just before this function is used. pid_bumpless 实现无扰切换
当突然改变设定值时,或重新启动后,将引起扰动输出。这
个函数将能实现平顺扰动, 在调用该函数之前需要先更新 PV值
*/ void pid_bumpless( struct _pid *pid )
{
pid->last_error = ( pid->sp ) - ( pid->pv ); //设定值与反馈值偏差
} /*
pid_calc --- 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 VALUE The new output value for the pid loop. USAGE #include "control.h"
本函数使用位置式PID计算方式,并且采取了积分饱和限制运算
PID计算
*/ 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 > || pterm < - )
{
pid->integral = 0.0;
}
else
{
// 积分项
pid->integral += pid->igain * ferror; // 输出为0--100%
// 如果计算结果大于100,则等于100
if ( pid->integral > 100.0 )
{
pid->integral = 100.0;
}
// 如果计算结果小于0.0,则等于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; // 输出PID值(0-100)
return ( result );
} void main( void )
{
float display_value;
int count = ;
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 = ;
set_point = ;
p_gain = (float) ( 5.2 );
i_gain = (float) ( 0.77 );
d_gain = (float) ( 0.18 );
dead_band = ;
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 <= )
{
scanf( "%d", &process_point ); // 设定PV,SP 值
pid_init( &warm, process_point, set_point ); // 初始化PID 参数值
pid_tune( &warm, p_gain, i_gain, d_gain, dead_band ); // 初始化PID 输出值
pid_setinteg( &warm, 0.0 ); //pid_setinteg(&warm,30.0);
//Get input value for process point
pid_bumpless( &warm ); // how to display output
display_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 语言)(来自老外)的更多相关文章

  1. 【转】位置式、增量式PID算法C语言实现

    位置式.增量式PID算法C语言实现 芯片:STM32F107VC 编译器:KEIL4 作者:SY 日期:2017-9-21 15:29:19 概述 PID 算法是一种工控领域常见的控制算法,用于闭环反 ...

  2. PID算法(C语言)

    /************ PID算法(C语言) ************/ #include <stdio.h> #include<math.h> struct _pid { ...

  3. PID算法(c 语言)(转)

    PID算法(c 语言)(来自老外) #include <stdio.h> #include<math.h> //定义PID 的结构体 struct _pid { int pv; ...

  4. PID控制算法的C语言实现二 PID算法的离散化

    上一节中,我论述了PID算法的基本形式,并对其控制过程的实现有了一个简要的说明,通过上一节的总结,基本已经可以明白PID控制的过程.这一节中先继续上一节内容补充说明一下. 1.说明一下反馈控制的原理, ...

  5. PID控制算法的C语言实现一 PID算法原理

    本系列是转载............. 全部的程序有一个共同点:就是我没认真去调pid的参数 在工业应用中PID及其衍生算法是应用最广泛的算法之一,是当之无愧的万能算法,如果能够熟练掌握PID算法的设 ...

  6. PID算法的C语言实现

    1.根据我控制算法类文章中关于PID的理论的一些描述,同时也根据网络上一些其他的PID文章,以及自己最近一个项目的实践后,总结了几套基于C语言的PID算法,由于网络中很少有人进行分享完整的PID算法实 ...

  7. PID控制器开发笔记之一:PID算法原理及基本实现

    在自动控制中,PID及其衍生出来的算法是应用最广的算法之一.各个做自动控制的厂家基本都有会实现这一经典算法.我们在做项目的过程中,也时常会遇到类似的需求,所以就想实现这一算法以适用于更多的应用场景. ...

  8. 单片机之PID算法

    说到PID算法,想必大部人并不陌生,PID算法在很多方面都有重要应用,比如电机的速度控制,恒温槽的温度控制,四轴飞行器的平衡控制等等,作为闭环控制系统中的一种重要算法,其优点和可实现性都成为人们的首选 ...

  9. PID控制算法的C语言实现十 专家PID与模糊PID的C语言实现

    本节是PID控制算法的C语言实现系列的最后一节,前面8节中,已经分别从PID的实现到深入的过程进行了一个简要的讲解,从前面的讲解中不难看出,PID的控制思想非常简单,其主要问题点和难点在于比例.积分. ...

  10. PID控制算法的C语言实现三 位置型PID的C语言实现

    上一节中已经抽象出了位置性PID和增量型PID的数学表达式,这一节,重点讲解C语言代码的实现过程,算法的C语言实现过程具有一般性,通过PID算法的C语言实现,可以以此类推,设计其它算法的C语言实现. ...

随机推荐

  1. Python实现AD域认证

    Python 通过ldap进行ad域账号的校验. 首先需要安装python-ldap的模块 http://www.python-ldap.org/. 在这里用的是windows系统,当然比较容易,下载 ...

  2. Android8 自定义广播接收不到的问题

    最近在用安卓广播的时候,按照流程进行操作,可是不管怎样都没有出现我接受的广播,网上查阅资料以后,发现在Android8中,如果是静态注册广播,需要在action中保留原来的静态广播,加入Compone ...

  3. 高仿QQ、微信效果的图片浏览器(支持原图和缩略图、多种手势、CocoaPods)

    感谢原文作者的分享 本文转载至 http://my.oschina.net/u/2406027/blog/735738 PYPhotoBrowser GitHub地址:https://github.c ...

  4. mysql按位的索引判断值是否为1

    DELIMITER $$ DROP FUNCTION IF EXISTS `value_of_bit_index_is_true`$$/*计算某个数字的某些索引的位的值是否都为1,索引类似1,2,3, ...

  5. 升级 Glide 4.0 版本相关配置

    http://blog.csdn.net/hexingen/article/details/72578066 http://blog.csdn.net/hexingen/article/details ...

  6. 10.30 rest_framework总结

    2018-10-30 20:25:23 终于学完了rest_framework  这个框架! 这个框架有一些基本组件!最重要的就是看源码!要一个类一个类的去找!按顺序! 并且要自己配置类的时候要先看源 ...

  7. K XOR Clique

    BaoBao has a sequence a​1​,a​2,...,a​n. He would like to find a subset S of {1,2,...,n} such that ∀i ...

  8. less的安装与用法

    1. node.js node.js是一个前端的框架 自带一个包管理工具npm node.js 的安装 官网:http://nodejs.cn/ 在命令行检验是否安装成功 打开cmd 切换到项目目录, ...

  9. 【C++ mid-term exerises】

    1. 用掷骰子方式,模拟班级每个学号被随机抽点的概率. (12分) 具体要求如下: (1)设计并实现一个骰子类Dice. ① 数据成员sides表示骰子面数.构造时,指定骰子是6面,8面,还是其它数值 ...

  10. swust oj 956

    约瑟夫问题的实现 2000(ms) 65535(kb) 3266 / 10775 n个人围成一个圈,每个人分别标注为1.2.....n,要求从1号从1开始报数 ,报到k的人出圈,接着下一个人又从1开始 ...