搜索
bottom↓
回复: 0

[求助]MWC 2.2中PID代码问题

[复制链接]

出0入0汤圆

发表于 2014-3-25 21:30:08 | 显示全部楼层 |阅读模式
  1.   //**** PITCH & ROLL & YAW PID ****
  2.   int16_t prop;
  3.   prop = min(max(abs(rcCommand[PITCH]),abs(rcCommand[ROLL])),500); // range [0;500]

  4.   for(axis=0;axis<3;axis++) {
  5.     if ((f.ANGLE_MODE || f.HORIZON_MODE) && axis<2 ) { // MODE relying on ACC
  6.       // 50 degrees max inclination,倾角限定在50度,对应下面语句的【-500,500】,angle角度也是数值乘0.1后的结果。
  7.       //rcCommand [-500;+500] for ROLL/PITCH/YAW ,‘<<1’扩大到[-1000,1000],与gps角度求和
  8.       errorAngle = constrain((rcCommand[axis]<<1) + GPS_angle[axis],-500,+500) - angle[axis] + conf.angleTrim[axis]; //16 bits is ok here
  9.       PTermACC = ((int32_t)errorAngle*conf.P8[PIDLEVEL])>>7;                          // 32 bits is needed for calculation: errorAngle*P8[PIDLEVEL] could exceed 32768   16 bits is ok for result
  10.       PTermACC = constrain(PTermACC,-conf.D8[PIDLEVEL]*5,+conf.D8[PIDLEVEL]*5);

  11.       errorAngleI[axis]     = constrain(errorAngleI[axis]+errorAngle,-10000,+10000);    // WindUp     //16 bits is ok here
  12.       ITermACC              = ((int32_t)errorAngleI[axis]*conf.I8[PIDLEVEL])>>12;            // 32 bits is needed for calculation:10000*I8 could exceed 32768   16 bits is ok for result
  13.     }
  14.     if ( !f.ANGLE_MODE || f.HORIZON_MODE || axis == 2 ) { // MODE relying on GYRO or YAW axis
  15.       if (abs(rcCommand[axis])<500) error =          (rcCommand[axis]<<6)/conf.P8[axis] ; // 16 bits is needed for calculation: 500*64 = 32000      16 bits is ok for result if P8>5 (P>0.5)
  16.                                else error = ((int32_t)rcCommand[axis]<<6)/conf.P8[axis] ; // 32 bits is needed for calculation

  17.       error -= gyroData[axis];

  18.       PTermGYRO = rcCommand[axis];
  19.       
  20.       errorGyroI[axis]  = constrain(errorGyroI[axis]+error,-16000,+16000);         // WindUp   16 bits is ok here
  21.       if (abs(gyroData[axis])>640) errorGyroI[axis] = 0;
  22.       ITermGYRO = ((errorGyroI[axis]>>7)*conf.I8[axis])>>6;                        // 16 bits is ok here 16000/125 = 128 ; 128*250 = 32000
  23.     }
  24.     if ( f.HORIZON_MODE && axis<2) {
  25.       PTerm = ((int32_t)PTermACC*(512-prop) + (int32_t)PTermGYRO*prop)>>9;         // the real factor should be 500, but 512 is ok
  26.       ITerm = ((int32_t)ITermACC*(512-prop) + (int32_t)ITermGYRO*prop)>>9;
  27.     } else {
  28.       if ( f.ANGLE_MODE && axis<2) {
  29.         PTerm = PTermACC;
  30.         ITerm = ITermACC;
  31.       } else {
  32.         PTerm = PTermGYRO;
  33.         ITerm = ITermGYRO;
  34.       }
  35.     }

  36.     PTerm -= ((int32_t)gyroData[axis]*dynP8[axis])>>6; // 32 bits is needed for calculation   

  37.     delta          = gyroData[axis] - lastGyro[axis];  // 16 bits is ok here, the dif between 2 consecutive gyro reads is limited to 800
  38.     lastGyro[axis] = gyroData[axis];
  39.     deltaSum       = delta1[axis]+delta2[axis]+delta;
  40.     delta2[axis]   = delta1[axis];
  41.     delta1[axis]   = delta;

  42.     DTerm = ((int32_t)deltaSum*dynD8[axis])>>5;        // 32 bits is needed for calculation
  43.                      
  44.     axisPID[axis] =  PTerm + ITerm - DTerm;
  45.   }
复制代码


    上面代码是MWC 2.2中的PID相关代码,有几个问题,麻烦大家指点下~
1.像“ PTermACC = ((int32_t)errorAngle*conf.P8[PIDLEVEL])>>7; ”这条语句里面的移位操作,代码里的位移操作的依据是什么呢呢?
2.“PTerm -= ((int32_t)gyroData[axis]*dynP8[axis])>>6; // 32 bits is needed for calculation”,PTermACC减去这部分的依据是什么呢?

阿莫论坛20周年了!感谢大家的支持与爱护!!

曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-8-26 15:13

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

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