智能车制作

 找回密码
 注册

扫一扫,访问微社区

查看: 20270|回复: 78
打印 上一主题 下一主题

"基友一号2.0"的PID,有演示,有代码!!!

  [复制链接]

184

主题

1972

帖子

0

精华

功勋会员

E=mc^2.0

Rank: 10Rank: 10Rank: 10

积分
15110

论坛骨干奖章活跃会员奖章优秀会员奖章资源大师奖章论坛元老奖章

威望
5404
贡献
8060
兑换币
1555
注册时间
2010-12-8
在线时间
823 小时
跳转到指定楼层
1#
发表于 2012-3-21 10:37:19 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 znfc2 于 2012-3-21 10:42 编辑

经典PID算法
下面的波形神马的,可以看看我的另一个帖子:{:soso_e189:}
http://www.znczz.com/thread-96281-1-1.html

C语言实现PID算法
#include <stdio.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_init
  
  DESCRIPTION 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
  
  DESCRIPTION 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.
  ------------------------------------------------------------------------*/
  
  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_setinteg
  
  DESCRIPTION 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 = 0;
  }
  
  /*------------------------------------------------------------------------
  pid_bumpless
  
  DESCRIPTION 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.
  ------------------------------------------------------------------------*/
  void pid_bumpless(struct _pid *pid)
  {
  
   pid->last_error = (pid->sp)-(pid->pv);
  
  }
  
  /*------------------------------------------------------------------------
  pid_calc
  
  DESCRIPTION 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"*/
  
  
  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 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++;
  
   }
  
  }

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

21

主题

259

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1004
威望
683
贡献
117
兑换币
0
注册时间
2012-2-1
在线时间
102 小时
2#
发表于 2012-3-21 10:40:24 | 只看该作者
很好的资料 一语中的呀
回复 支持 反对

使用道具 举报

184

主题

1972

帖子

0

精华

功勋会员

E=mc^2.0

Rank: 10Rank: 10Rank: 10

积分
15110

论坛骨干奖章活跃会员奖章优秀会员奖章资源大师奖章论坛元老奖章

威望
5404
贡献
8060
兑换币
1555
注册时间
2010-12-8
在线时间
823 小时
3#
 楼主| 发表于 2012-3-21 10:43:31 | 只看该作者
LJG 发表于 2012-3-21 10:40
很好的资料 一语中的呀

教科书上把它写的很复杂的样子
回复 支持 反对

使用道具 举报

6

主题

190

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1849

优秀会员奖章活跃会员奖章

威望
1002
贡献
495
兑换币
30
注册时间
2011-8-26
在线时间
176 小时
4#
发表于 2012-3-21 11:28:39 | 只看该作者
是很不错
回复 支持 反对

使用道具 举报

4

主题

239

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
2113
威望
1263
贡献
242
兑换币
20
注册时间
2011-10-15
在线时间
304 小时
5#
发表于 2012-3-21 11:59:32 | 只看该作者
楼主好厉害。。。有木有QQ哇
回复 支持 反对

使用道具 举报

7

主题

77

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1261
威望
571
贡献
98
兑换币
0
注册时间
2010-3-19
在线时间
296 小时
6#
发表于 2012-3-21 12:24:14 | 只看该作者
强啊
回复 支持 反对

使用道具 举报

184

主题

1972

帖子

0

精华

功勋会员

E=mc^2.0

Rank: 10Rank: 10Rank: 10

积分
15110

论坛骨干奖章活跃会员奖章优秀会员奖章资源大师奖章论坛元老奖章

威望
5404
贡献
8060
兑换币
1555
注册时间
2010-12-8
在线时间
823 小时
7#
 楼主| 发表于 2012-3-21 13:42:35 | 只看该作者
Goolloo 发表于 2012-3-21 11:59
楼主好厉害。。。有木有QQ哇

1046166424
回复 支持 反对

使用道具 举报

165

主题

4119

帖子

2

精华

杰出人士

老司机【呆萌侠】

Rank: 12Rank: 12Rank: 12

积分
16835

活跃会员奖章优秀会员奖章推广达人奖章热心会员奖章论坛元老奖章论坛骨干奖章在线王奖章资源大师奖章

QQ
威望
9701
贡献
4552
兑换币
1629
注册时间
2011-7-23
在线时间
1291 小时
毕业学校
工大
8#
发表于 2012-3-21 13:46:11 | 只看该作者
znfc2 发表于 2012-3-21 13:42
1046166424

加你需要身份证上的名字
回复 支持 反对

使用道具 举报

0

主题

32

帖子

0

精华

高级会员

Rank: 4

积分
862
威望
462
贡献
120
兑换币
0
注册时间
2012-2-4
在线时间
143 小时
9#
发表于 2012-3-21 14:46:53 | 只看该作者
znfc2 发表于 2012-3-21 10:43
教科书上把它写的很复杂的样子

大神,你就是参考这个做的吗?
回复 支持 反对

使用道具 举报

5

主题

243

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1238
威望
722
贡献
276
兑换币
14
注册时间
2012-2-1
在线时间
120 小时
毕业学校
武汉理工
10#
发表于 2012-3-21 15:38:09 | 只看该作者
不错不错哦~~~
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

关于我们|联系我们|小黑屋|智能车制作 ( 黑ICP备2022002344号

GMT+8, 2024-6-27 01:23 , Processed in 0.058874 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表