智能车制作

标题: "基友一号2.0"的PID,有演示,有代码!!! [打印本页]

作者: znfc2    时间: 2012-3-21 10:37
标题: "基友一号2.0"的PID,有演示,有代码!!!
本帖最后由 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++;
  
   }
  
  }


作者: LJG    时间: 2012-3-21 10:40
很好的资料 一语中的呀
作者: znfc2    时间: 2012-3-21 10:43
LJG 发表于 2012-3-21 10:40
很好的资料 一语中的呀

教科书上把它写的很复杂的样子
作者: jxndlym    时间: 2012-3-21 11:28
是很不错
作者: Goolloo    时间: 2012-3-21 11:59
楼主好厉害。。。有木有QQ哇
作者: fshunj    时间: 2012-3-21 12:24
强啊
作者: znfc2    时间: 2012-3-21 13:42
Goolloo 发表于 2012-3-21 11:59
楼主好厉害。。。有木有QQ哇

1046166424

作者: 恋May    时间: 2012-3-21 13:46
znfc2 发表于 2012-3-21 13:42
1046166424

加你需要身份证上的名字

作者: lcl155702    时间: 2012-3-21 14:46
znfc2 发表于 2012-3-21 10:43
教科书上把它写的很复杂的样子

大神,你就是参考这个做的吗?

作者: 木未    时间: 2012-3-21 15:38
不错不错哦~~~
作者: Crazyfox    时间: 2012-3-21 18:28
开源?不错哦
作者: klain    时间: 2012-3-21 19:41
楼主,拿C语言实现pid是在vc++6.0上实现的吗?
作者: znfc2    时间: 2012-3-21 21:46
klain 发表于 2012-3-21 19:41
楼主,拿C语言实现pid是在vc++6.0上实现的吗?

差不多就是这样
作者: znfc2    时间: 2012-3-21 21:47
lcl155702 发表于 2012-3-21 14:46
大神,你就是参考这个做的吗?

yes(我不是大神)
作者: Goolloo    时间: 2012-3-21 21:54
被楼主坑了。。。要姓名
作者: klain    时间: 2012-3-21 22:01
Goolloo 发表于 2012-3-21 21:54
被楼主坑了。。。要姓名

额···没坑你啊···你没仔细看说明
作者: Goolloo    时间: 2012-3-21 22:06
klain 发表于 2012-3-21 22:01
额···没坑你啊···你没仔细看说明

身份证上的名字啊。。。

作者: lifei092    时间: 2012-3-21 22:07
不错、、、、
作者: klain    时间: 2012-3-21 23:11
Goolloo 发表于 2012-3-21 22:06
身份证上的名字啊。。。

下载文件··里面自有说明···对吧  楼主···
作者: znfc2    时间: 2012-3-22 12:21
klain 发表于 2012-3-21 23:11
下载文件··里面自有说明···对吧  楼主···

居然被你发现了{:soso_e113:}
作者: Ooo    时间: 2012-3-22 12:52
厉害
作者: klain    时间: 2012-3-22 22:21
znfc2 发表于 2012-3-22 12:21
居然被你发现了

想多点向楼主学习哈···基友系列很给力···
作者: linjk    时间: 2012-3-22 22:38
非常谢谢!正需要这个
作者: znfc2    时间: 2012-3-23 11:58
klain 发表于 2012-3-22 22:21
想多点向楼主学习哈···基友系列很给力···

低调

作者: iceboy    时间: 2012-3-23 12:33
路过帮顶
作者: klain    时间: 2012-3-23 15:21
znfc2 发表于 2012-3-23 11:58
低调

楼主,你这是位置式的~~~有没有增量式的,给我来一份  397762270@qq.com   

作者: QianDE1991    时间: 2012-3-23 15:43
这个软件的下位机应该怎么写???那个说明看的不是很懂,希望基友能帮助下~~~
作者: usstwhw    时间: 2012-3-23 17:01
表示加扣扣加不上

作者: ai756036    时间: 2012-3-24 10:45
到处学习
作者: 277538205    时间: 2012-3-24 16:43

作者: znfc2    时间: 2012-3-28 16:11
klain 发表于 2012-3-23 15:21
楼主,你这是位置式的~~~有没有增量式的,给我来一份  397762270@qq.com

那个PPT里两种都有
作者: znfc2    时间: 2012-3-28 16:12
QianDE1991 发表于 2012-3-23 15:43
这个软件的下位机应该怎么写???那个说明看的不是很懂,希望基友能帮助下~~~

你间隔的发AABBCC和BBBBBB看看现象
作者: XJKJames    时间: 2012-3-30 15:54
znfc2 发表于 2012-3-28 16:12
你间隔的发AABBCC和BBBBBB看看现象

还是不明白~~~~
作者: 568581185    时间: 2012-4-2 22:09
留名。。。。
作者: daiyinghua    时间: 2012-4-4 12:09
楼主是用什么软件仿真看波形的?

作者: 摩云金翅    时间: 2012-4-4 14:09

作者: dongpinbo    时间: 2012-4-5 16:02
百度上的程序
作者: 249176669    时间: 2012-4-8 18:54
加不上你QQ。。下载的文件里面也没有说明文档。。只有一个PPT。。。。希望能加QQ交流下 249176669。。。
作者: 249176669    时间: 2012-4-8 18:58
看了另外一个帖子加上QQ了 。。呵呵
作者: 一毛钱~~~~~~    时间: 2012-4-8 20:56
先看看再说。。。
作者: 574350344    时间: 2012-4-9 20:21

作者: 活在九零后    时间: 2012-4-14 17:44
先收藏了
作者: zgl616697107    时间: 2012-4-14 18:49
亲,请问在调pid参数的震荡图像时,那个图像的X轴怎么和时间扯上关系的啊!
作者: znfc2    时间: 2012-4-14 19:12
时间就是X轴
作者: zgl616697107    时间: 2012-4-14 19:28
我觉得是多次执行pid这个函数使实际值靠近目标值....所以我觉得X轴应该是第几次执行(而不是时间),也就是执行程序的次数,不知道我表达清楚没哟....还有,得到的值应该是离散的吧!
作者: znfc2    时间: 2012-4-15 10:02
zgl616697107 发表于 2012-4-14 19:28
我觉得是多次执行pid这个函数使实际值靠近目标值....所以我觉得X轴应该是第几次执行(而不是时间),也就是 ...

次数时间其实都一样

作者: xiexueshi    时间: 2012-4-18 16:44
大神帮忙看看这个波是用卡尔曼滤波后的,红色是加速度的,黄色是融合之后的,帮忙指点一下需要改进的地方,谢谢~
作者: qinlu123    时间: 2012-4-18 18:58
天哪我写的PID代码才几行怪不得不好使
作者: znfc2    时间: 2012-4-18 19:13
qinlu123 发表于 2012-4-18 18:58
天哪我写的PID代码才几行怪不得不好使

我也只用了几行

作者: znfc2    时间: 2012-4-18 19:13
xiexueshi 发表于 2012-4-18 16:44
大神帮忙看看这个波是用卡尔曼滤波后的,红色是加速度的,黄色是融合之后的,帮忙指点一下需要改进的地方, ...

看起来不够平滑
作者: vgjkl    时间: 2012-4-18 22:26
{:soso_e179:}
作者: xiexueshi    时间: 2012-4-18 22:35
znfc2 发表于 2012-4-18 19:13
看起来不够平滑

应该把那一点过冲去点滤掉是吗,指点一下,加速度输出信号直接显示就是这个红色的,需要处理一下吗,谢谢~
作者: znfc2    时间: 2012-4-19 12:22
xiexueshi 发表于 2012-4-18 22:35
应该把那一点过冲去点滤掉是吗,指点一下,加速度输出信号直接显示就是这个红色的,需要处理一下吗,谢谢 ...

要卡尔曼融合啊
作者: 如水似衡    时间: 2012-4-26 20:38
大神,你是怎么做到快速过弯的?我们现在车速只要1M多,再快就会冲出去,速度还没加PID控制
作者: znfc2    时间: 2012-5-1 19:39
恋May 发表于 2012-3-21 13:46
加你需要身份证上的名字

李栋
作者: etoah    时间: 2012-5-5 06:18
好帖, mark
作者: 六月雪    时间: 2012-5-5 08:30
不错的资料
作者: hsx1612727380    时间: 2012-5-5 20:51
znfc2 发表于 2012-3-21 13:42
1046166424

你的真实姓名啊!!!!!!!!!!!!!
作者: znfc2    时间: 2012-5-5 21:48
hsx1612727380 发表于 2012-5-5 20:51
你的真实姓名啊!!!!!!!!!!!!!

李栋
作者: isearching    时间: 2012-5-7 19:52
顶一下!
作者: chengeng0910    时间: 2012-5-7 20:03
看看
作者: 拼搏    时间: 2012-5-13 08:56
学习了
作者: Stroll_S    时间: 2012-5-13 20:02
好东西,一定要收藏!!
作者: 丶枫    时间: 2012-5-21 16:56
我也看看
作者: 华水剑客    时间: 2012-5-23 18:45

作者: Think_p    时间: 2012-5-29 12:50
lz qq没法加呀
作者: 840149319    时间: 2012-5-29 13:22

作者: 逆毛    时间: 2012-6-27 22:21
调节时间会不会有点长?
作者: B组    时间: 2012-6-30 14:17
我的PID设置为低速恒速运行。现象是高速很高,低速几乎要停下来。一直有很明显的速度变化。请问这是哪有问题?
作者: 死神之怒    时间: 2012-7-7 14:27
大神给力啊,就此拜过……
作者: wendaoxyp    时间: 2012-7-13 18:36
谢谢lz了
作者: 602王帅    时间: 2012-7-14 09:16
楼主是一个大好人呀,谢谢了。
作者: LI912683617    时间: 2012-7-19 11:34
楼主~打不开啊~求帮助啊~
作者: 开心果219407    时间: 2013-3-27 19:24
楼主  真牛
作者: Robins    时间: 2013-3-27 20:29
很棒啊,学习了!{:soso_e179:}
作者: 墨羽飞    时间: 2013-4-15 20:13
学习学习~~~
作者: 漫无止尽的八月    时间: 2013-5-21 00:27
本帖最后由 漫无止尽的八月 于 2013-5-21 00:30 编辑

学习啦~
作者: ♂_~遥~♂    时间: 2013-5-22 00:02
mark
作者: xinquan456    时间: 2013-5-26 01:34
3ks




欢迎光临 智能车制作 (http://111.231.132.190/) Powered by Discuz! X3.2