R88 发表于 2015-1-12 17:13:59

STC的版主来讲讲PID的整定过程吧

之前看到版主上传了一个PID的DCDC程序。请版主讲讲一下参数的整定过程。还有版主能否给出瞬态测试的波形情况。看到楼主的程序跟PIC官网的差不多,是不是参考了PIC的呢?

对于过流过压的程序版主上传的程序中没有看到,想问下过流的响应时间如何?版主是怎么测试一个周期就完成了一个PID运算呢?

我了解PID的传统整定方法都是对于被控系统的传函比较模糊的情况下。对于常用的电源拓扑Gvd(s)的传函是已经推导出来的,版主的程序的参数是否是计算出来的呢?如果只用PI是否理论上对于电压控制模式不够用呢?

最后感谢楼主上传的程序。

liyang121316 发表于 2015-1-12 19:19:41

不之所云,路过学习。

NM2012 发表于 2015-1-12 19:45:30

{:funk:}            

R88 发表于 2015-1-13 08:55:31

liyang121316 发表于 2015-1-12 19:19
不之所云,路过学习。

我是菜鸟哈,说的可能不是太清楚,见谅。

liyang121316 发表于 2015-1-13 09:45:16

R88 发表于 2015-1-13 08:55
我是菜鸟哈,说的可能不是太清楚,见谅。

    我对pid确实是一点都不了解。

fengyunyu 发表于 2015-1-13 10:43:04

版主上传了一个PID的DCDC程序,是哪个帖子?

R88 发表于 2015-1-13 11:14:39

fengyunyu 发表于 2015-1-13 10:43
版主上传了一个PID的DCDC程序,是哪个帖子?

http://www.amobbs.com/thread-5609262-1-1.html

iop 发表于 2015-1-13 13:23:34

大致看了下代码,是不是打包成了LIB的啊

R88 发表于 2015-1-13 21:52:00

居然看不见版主出现;贴个文章:

A brief description on PID Controllers:
----------------------------------------
A PID controller responds to an error signal in a closed control loop and attempts to adjust the controlled quantity in order to achieve the desired system response. The controlled parameter can be any measurable system quantity, such as speed, voltage, current, or stock price.The output of the PID controller can control one or more system parameters that will affect the controlled system quantity.For example, the speed control loop in this application can control the PWM duty cycle directly or it can set the current demand for an inner control loop that regulates the motor currents.The benefit of the PID controller is that it can be adjusted empirically by adjusting one or more gain values and observing the changein system response.

A digital PID controller is executed at a periodic sampling interval and it is assumed that the controller is executed frequently enough so that the system can be properly controlled.For example, the current controller in this application is executed every PWM cycle, since the motor can change very rapidly.The speed controller in this application is executed at the medium event rate (100 Hz), because motor speed changes will occur relatively slowly due to mechanical time constants.

The error signal is formed by subtracting the desired setting of the parameter to be controlled from the actual measured value of that parameter.This sign of the error indicates the direction of change required by the control input.

The Proportional (P) term of the controller is formed by multiplying the error signal by a P gain.This will cause the PID controller to produce a control response that is a function of the error magnitude.As the error signal becomes larger, the P term of the controller becomes larger to provide more correction.

The effect of the P term will tend to reduce the overall error as time elapses.However, the effect of the P term will reduce as the error approaches zero.In most systems, the error of the controlled parameter will get very close to zero, but will not converge.The result is a small remaining steady state error.The Integral (I) term of the controller is used to fix small steady state errors.The I term takes a continuous running total of the error signal.Therefore, a small steady state error will accumulate into a large error value over time.This accumulated error signal is multiplied by an I gain factor and becomes the I output term of the PID controller.

The Differential (D) term of the PID controller is used to enhance the speed of the controller and responds to the rate of change of the error signal.The D term input is calculated by subtracting the present error value from a prior value.This delta error value is multiplied by a D gain factor that becomes the D output term of the PID controller.The D term of the controller produces more control output the faster the system error is changing.

It should be noted that not all PID controllers will implement the D or, less commonly, the I terms.For example, the speed controller in this application does not have a D term due to the relatively slow response time of motor speed changes.In this case, the D term could cause excessive changes in PWM duty cycle that could affect the operation of the sensorless algorithm and produce over current trips.

Adjusting the PID Gains

The P gain of a PID controller will set the overall system response.When first tuning a controller, the I and D gains should be set to zero.The P gain can then be increased until the system responds well to set-point changes without excessive overshoot or oscillations.Using lower values of P gain will 'loosly' control the system, while higher values will give 'tighter' control.At this point, the system will probably not converge to the set-point.

After a reasonable P gain is selected, the I gain can be slowly increased to force the system error to zero.Only a small amount of I gain is required in most systems.Note that the effect of the I gain, if large enough, can overcome the action of the P term, slow the overall control response,and cause the system to oscillate around the set-point.If this occurs, reducing the I gain and increasing the P gain will usually solve the problem.

After the P and I gains are set, the D gain can be set.   The D term will speed up the response of control changes, but it should be used sparingly because it can cause very rapid changes in the controller output.This behavior is called 'set-point kick'.The set-point kick occurs because the difference in system error becomes instantaneously very large when the control set-point is changed. In some cases, damage to system hardware can occur.If the system response is acceptable with the D gain set to zero, you can probably omit the D term.

STCunio 发表于 2015-1-14 01:32:49

现在STC15W4K系列单个PWM周期内跑不了整个PID(即使是PIC官网的程序,那还是一个汇编的),就差那么一点点哦(说不定用汇编也能跑起来),再快点就可以了,所以做了个PI库(跟PIC官网的程序不一样),去掉D环节。过流保护可以使用片内的比较器,这个是预留给大家来做的{:titter:},过压保护在程序AD中断里,检测输出电压大小的,超过了就让controlOutput=0,pwm自然也就为0。还可以用硬件的方法来触发PWMFLT信号来关闭ePWM(增强型PWM)输出。

PID手调整定
测试方法:将示波器触发到AC模式,从空载加载(满载)产生阶跃扰动,捕捉输出波形,板子上有测试点,不用担心探头没地方夹。如图所示,即输出波形。

1、先调Kp,将Ki设成一个很小的数(不要为0),Kd可以为0,增大Kp的值,这时,图中的第1个波峰会增大(快速调节误差),先让波峰调到(稳定值-第一个波谷)/2处就可以了,Kp再大些话可能在增大Ki 时波形会超调。
2、调Ki,先前调好的Kp保持不变,增大Ki,这时从第1个波峰到输出稳定的时间就缩短(负载响应变好),如果继续增大会导致第一个波峰高于稳定值(出现了较大的超调)或是积分饱和,所以在调Ki 的时候第1个波峰到稳定值这个过程总体上是上升的就可以了。
3、调Kd,增大Kd,可以在第1个波峰之后平滑上升到稳定波形(减小超调),若是没有Kd上升过程中会有几个震荡波形。

xinfa190 发表于 2015-1-14 09:06:45

楼上说的好

R88 发表于 2015-1-14 09:29:10

STCunio 发表于 2015-1-14 01:32
现在STC15W4K系列单个PWM周期内跑不了整个PID(即使是PIC官网的程序,那还是一个汇编的),就差那么一点点 ...

你应该是版主吧?怎么还整个小号呢?PIC的程序是400KHZ,而且程序注释的意思貌似是一个周期就执行了一次:In this code example, the first buck stage is being controlled using voltage mode control. The first buck stage is controlled at 5V. The PWM frequency is 400kHz and the ADC routine is triggered every PWM cycles 2.5us.汇编和C的速度差别这么大呢?
你这个瞬态测试是5V输出的么?这接近800mV的过冲是不是还没有整定好呢?你是怎么验证PI是一个周期完成的?整个IO取反之类的?
对于像电源这种控制系统,传递函数已经明确的条件下,用传统的PID整定是否合适呢?
版主有测过STC过流过压的响应速度如何?过压用AD检测貌似比较慢。

STCunio 发表于 2015-1-14 11:43:06

R88 发表于 2015-1-14 09:29
你应该是版主吧?怎么还整个小号呢?PIC的程序是400KHZ,而且程序注释的意思貌似是一个周期就执行了一次 ...

那个号是老号,这个是新号,我的开关频率没那么快,在50K~60KHz,AD的采样速率才300K,采样速率必须大于2倍的开关频率,实际上必须是5倍以上才好。然后运算能力也受限了,也只能在50K~60KHz范围内才能在单PWM周期内完成1次PI调节(零阶保持器),用示波器观察P32的IO电平,在中断程序的开头P32=1,退出中断之前P32=0,比对PWM开关频率就知道是不是单周期处理1次PI。
我输出的是9V,瞬间加2A的负载。过流保护可以使用片内的比较器;过压保护觉得采样慢的话可以外加个比较器,输出接PWMFLT。比较器的速度够快的吧。至于电源的传递函数,必须根据实际情况算出功率级的Gp(S),然后离散化求出Gp(Z),再考虑AD或其他环节的传递函数,最后算出总的传递函数,然后把零点、极点求出来,画出伯德图,根据需要选择补偿器,多数情况选择IIR(P2Z2)进行补偿,其实PID就是IIR的一个特例(TI的实例很多用IIR来做PID),IIR的参数是要用MATLAB来完成,求出a1、a2、b0、b1、b2,最后得出差分方程。
最后要注意的是库中Q15格式是小于1的数,Kp、Ki、Kd只能都是小于1的,这样就不会溢出。

R88 发表于 2015-1-14 12:02:37

STCunio 发表于 2015-1-14 11:43
那个号是老号,这个是新号,我的开关频率没那么快,在50K~60KHz,AD的采样速率才300K,采样速率必须大于2 ...

最后一个问题:既然可以用2p2z计算出参数,为何不用这个。也就是用传统pid整定方法和用2p2z理论计算,选哪个根据什么依据?

zzjjhh250 发表于 2015-1-16 09:06:02

经典控制理论的控制器调整,必须依赖于 被控对象才能够设计。所以,依然推荐对被控对象进行建模才是根本之道。
当然,现实当中的被控对象模型 一般以一阶 或者二阶居多。
环路调试,如果没有兴趣建模,可以通过气阶跃响应观察,就和上面的坛友一样。
当然,第一步是要使得环路能够稳定。一般的被控对象,纯PI一般均可以稳定,不过得把穿越早一点,然后提高增益。
本质上还是推荐,被控对象建模。
不像现代控制理论,通过观测器可以不依赖于被控对象模型。

R88 发表于 2015-1-16 11:15:49

zzjjhh250 发表于 2015-1-16 09:06
经典控制理论的控制器调整,必须依赖于 被控对象才能够设计。所以,依然推荐对被控对象进行建模才是根本之 ...

您说的不错,其实我最后就是问的楼主既然被控的模型传函已知,为何不用理论计算的2p2z.
楼主用的电压控制模式,LC形成的双极点很小,PI补偿显示不够,估计带宽小的可怜。

zzjjhh250 发表于 2015-1-16 12:09:19

带宽多少,是根据客户spec需求来的,主要体现在动态上面,如果在整个动态范围中,被控对象的模型是变化的,eg CCM DCM这种情形,带宽再宽 动态一样不好。
2p2z 数字补偿,计算出的系数相差较大(使用常用的离散方式 ZOH TUSTIN DIFF)
主要还是要看计算频率。电源有个1-2k 的带宽已经不错了。

STCunio 发表于 2015-1-16 12:28:07

R88 发表于 2015-1-16 11:15
您说的不错,其实我最后就是问的楼主既然被控的模型传函已知,为何不用理论计算的2p2z.
楼主用的电压控制 ...

单片机被发挥到了极限,单个PWM周期内带3个参数的PID我都跑不了(汇编或许可以),而况是5个参数2P2Z,现在只能跑PI,这个实例是为了让大家入门数字电源,用51单片机要比用DSP入门要简单的多。

R88 发表于 2015-1-16 13:38:55

STCunio 发表于 2015-1-16 12:28
单片机被发挥到了极限,单个PWM周期内带3个参数的PID我都跑不了(汇编或许可以),而况是5个参数2P2Z,现 ...

感谢版主,期待版主的四轴

R88 发表于 2015-1-16 13:43:03

zzjjhh250 发表于 2015-1-16 12:09
带宽多少,是根据客户spec需求来的,主要体现在动态上面,如果在整个动态范围中,被控对象的模型是变化的, ...

“电源有个1-2k 的带宽已经不错了”

是指版主的?还是纯数字电源?还是包括模拟的所有?

zzjjhh250 发表于 2015-1-16 13:45:08

R88 发表于 2015-1-16 13:43
“电源有个1-2k 的带宽已经不错了”

是指版主的?还是纯数字电源?还是包括模拟的所有? ...

单纯谈带宽没有意义

R88 发表于 2015-1-16 13:48:35

在获得更高带宽的条件下,为了获得相同的瞬态响应,输出电容就可以很小。1-2K对于buck太小了,反激还差不多。

levinxia 发表于 2015-1-18 19:02:42

讨论的多了,就学习到的多了。顶

Chash 发表于 2015-5-25 22:07:25

好帖子!值得学习。。

xihua13104 发表于 2015-7-25 10:04:49

坐看大神们的讨论,学习到很多,谢谢!

739819067 发表于 2018-5-28 16:17:21

STCunio 发表于 2015-1-14 01:32
现在STC15W4K系列单个PWM周期内跑不了整个PID(即使是PIC官网的程序,那还是一个汇编的),就差那么一点点 ...

您好,PID对应的输出应该是PWM,那测到的波形不应该是方波么,还是做了DA处理

qm9698 发表于 2018-6-2 20:42:45

学习了,{:smile:}

STCunio 发表于 2018-6-7 08:52:26

739819067 发表于 2018-5-28 16:17
您好,PID对应的输出应该是PWM,那测到的波形不应该是方波么,还是做了DA处理 ...

电路用的是BUCK降压型拓扑,开关过程中电感续流,输出还有个电容,再加上PID稳压控制,所以你看到的是一个输出稳定的波形

leolong03 发表于 2018-6-26 11:29:05

学习{:lol:}{:lol:}{:lol:}{:lol:}

su33691 发表于 2018-6-26 13:38:15

STCunio 发表于 2015-1-16 12:28
单片机被发挥到了极限,单个PWM周期内带3个参数的PID我都跑不了(汇编或许可以),而况是5个参数2P2Z,现 ...

版主有没有试过用STC8跑PID算法?
我感觉STC8比STC15快了50%

skype 发表于 2018-6-27 08:36:23

想问下STC51跑一个PID要多少时间?

STCunio 发表于 2018-6-27 09:19:03

skype 发表于 2018-6-27 08:36
想问下STC51跑一个PID要多少时间?

上面说了50-60kHz是极限,PI调节是单周期执行

skype 发表于 2018-6-27 17:26:00

PID运算时间

STCunio 发表于 2018-6-27 19:09:16

skype 发表于 2018-6-27 17:26
PID运算时间

调节1次不就是运算一次,以50k的频率为例,20us一个周期,基本上是PID运行在18us的样子,不会多出太多的时间,上面有位兄弟说STC8比STC15快了50%,那这个时间会更短

skype 发表于 2018-6-27 23:15:53

STCunio 发表于 2018-6-27 19:09
调节1次不就是运算一次,以50k的频率为例,20us一个周期,基本上是PID运行在18us的样子,不会多出太多的 ...

谢谢回复,刚才翻看了您以前的贴子,很有收获!

skype 发表于 2018-6-28 10:06:12

我晕,两种PID运算,一种浮点,一种用楼主的Q15整数运算,PIC16单片机,16M主频,运行时间完全一样!




50US,{:huffy:}

skype 发表于 2018-6-28 10:41:10

对比16M主频的STM8S105,同样的PID运算,怎么相差那么远呢?

这几天正为这事发愁呢,以为换个PID算法就可以提高运行速度{:cry:}

su33691 发表于 2018-6-28 11:10:10

PIC16单片机的指令太简单,而且4个时钟为一个指令周期。

STCunio 发表于 2018-6-28 11:18:04

可以改进的,1、牺牲一下AD的精度,8位即可,采样值在0~255之间,相应的基准也要在这个范围内。2、去掉PID的D环节,D环节不要参与计算.3、还原Q15格式用指针的指针来取值,不要右移15位。另外pic16的单片机工作频率是要四分频,并且没有乘法器,不适合做复杂的运算,STC15系列的单片机是在28MHz的频率下做出这个的50kHz的数字电源的。
页: [1]
查看完整版本: STC的版主来讲讲PID的整定过程吧