lhlabc 发表于 2006-4-19 09:36:30

求关于数字PID程序增量式或位置式都行

求关于数字PID程序增量式或位置式都行,本人使用ATmega128单片机控制晶闸管的触发角其中

PID三个参数中有浮点数,由于单片机的浮点运算能力很次,看哪位大侠有关于浮点运算的PID程序的例子。

archeng504 发表于 2006-4-19 10:43:26

Atmel的494应用笔记有一个数字PI,参考一下吧!我没有用过这个函数,如你成功了告诉一声!!!



/**

* @file mc_control.c

*

* Copyright (c) 2004 Atmel.

*

* @brief This module provide services to control speed for AT90PWM3 Only

* Type of control : PID means proportionnal, integral and derivative.

*

* @version 1.0 (CVS revision : 1.3)

* @date 2006/02/13 12:32:54

* @author raubree

*****************************************************************************/

/* Speed control variables */

signed int speed_error=0;      //!<Error calculation

signed int speed_integral = 0;

signed int speed_integ = 0;

signed int speed_proportional = 0;



/**************************************************************************************/

/*                        Speed Control                                             */

/**************************************************************************************/

/**

* @briefspeed controller

* @return value of speed (duty cycle on 16 bits)

*         speed_measure has 10 bits resolution

*/

signed int mc_control_speed_16b(signed int speed_ref , signed int speed_measure)

{

signed int Duty = 0;

signed int increment = 0;



// 误差计算

speed_error = speed_ref - speed_measure ;



// 比例项计算 : Kp= 7/64=0.1

speed_proportional = ( speed_error/8 - speed_error/64 );



// 积分项计算

speed_integral = speed_integral + speed_error;



// 速度积分饱和

if(speed_integral >32000) speed_integral =32000;

if(speed_integral < -32000) speed_integral = -32000;



// speed_integ = Ki_speed*speed_integral, with Ki_speed = 29/8192=3e-3

speed_integ = (speed_integral - speed_integral/8 + speed_integral/32) / 256 ;



// 占空比计算

increment = speed_proportional + speed_integ;



increment = (increment/2 + increment/4) ; // PI output normalization



// saturation of the PI output饱和 PI输出

if( increment > (signed int)(0) ) {

    if(increment <= (signed int)(192)) Duty = (signed int)increment ;

   else Duty = 192 ;

}

else {

    if(increment < (signed int)(-192)) Duty = -192 ;

   else Duty = (signed int)increment ;

}



// return Duty Cycle

return Duty;

}

JAMESKING 发表于 2006-4-19 10:50:41

浮点问题多多,建议32bit定点。

archeng504 发表于 2006-4-19 11:22:27

在《电动机的DSP控制》中介绍浮点转定点的方法是使用Q格式,也就是将一个浮点数乘以2的n次方的到一个16位整数,以后就用这个16位整数代替浮点数进行运算,运算完毕后,将最终结果除以2的n次方得到一个整数结果。是用Q格式可以最大限度减少运算结果精度丢失,可以根据需要选择n。使用Q格式还有一个好处,你可以用左移操作实现乘法运算,如a*2n(2的n次方)等价于a<<n,用右移操作实现除法运算,如a/2n(2的n次方)等价于a>>n 。



Q表示 十进制数表示范围

Q15-1≤X≤0.9999695

Q14-2≤X≤1.9999390

Q13-4≤X≤3.9998779

Q12-8≤X≤7.9997559

Q11-16≤X≤15.9995117

Q10-32≤X≤31.9990234

Q9-64≤X≤63.9980469

Q8-128≤X≤127.9960938

Q7-256≤X≤255.9921875

Q6-512≤X≤511.9804375

Q5-1024≤X≤1023.96875

Q4-2048≤X≤2047.9375

Q3-4096≤X≤4095.875

Q2-8192≤X≤8191.75

Q1-16384≤X≤16383.5

Q0-32768≤X≤32767

archeng504 发表于 2006-4-19 11:27:23

运算时要注意数据溢出问题!!!

下面是《DSP芯片的定点运算》

点击此处打开 DSP芯片的定点运算.pdf

JAMESKING 发表于 2006-4-19 15:47:18

谢谢阿成兄弟。

lhlabc 发表于 2006-8-10 08:07:26

非常感谢阿成的热心帮助。

xad74 发表于 2008-6-27 21:06:29

多谢了,正为浮点数发愁呢

tsb0574 发表于 2008-6-27 22:01:18

阿城兄弟能不能把整本传上来啊??

liguangqang 发表于 2008-6-28 13:38:17

点击此处下载 ourdev_329862.rar(文件大小:617K) (原文件名:AN_SPMC75_0012.rar)

szy494468597 发表于 2011-6-15 13:59:08

看看

ABCDELF 发表于 2013-4-21 15:50:18

好东西,谢谢了
页: [1]
查看完整版本: 求关于数字PID程序增量式或位置式都行