极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11366|回复: 1

請問有關ARDUINO四軸PID的問題(程式碼)

[复制链接]
发表于 2013-5-16 21:38:40 | 显示全部楼层 |阅读模式
本帖最后由 TTTTTTT33 于 2013-5-16 21:52 编辑

我最近在做四軸,想請問大家

我參照論壇上找到的互補濾波和PID演算自行修改,但是都無法平衡!!

互補濾波:

angleA= atan2(compass.a.y , compass.a.z) * 180 / 3.14159;
   omega= 0.068 * (gyro.g.x );
   
   angleA2= atan2(compass.a.x , compass.a.z) * 180 / 3.14159;
   omega= 0.068 * (gyro.g.y );
   
   unsigned long now = millis();                           // 当前时间(ms)
   float dt = (now - preTime)/* / 1000.0*/;                    // 微分时间(ms)
   preTime = now;  
   float K = 0.8;                    
   float A = K / (K + dt);                    
   f_angle = A * (f_angle + omega * dt) + (1-A) * angleA;
   f_angle2=A * (f_angle2 + omega2 * dt) + (1-A) * angleA2;

PID:

    now = millis(); // 當前時間(ms)
     float TimeCh = (now - lastTime) / 1000.0; // 採樣時間(s) 這裡怪怪的,採樣是S,因為已經是
                                             millis(),應該是要乘1000吧??
      // 比例係數、積分係數、微分係數
    //float SampleTime = 0.1; // 採樣時間(s)
     float Setpoint = 0; // 設定目標值(degree)
     float outMin = -42.5, outMax = 42.5; // 輸出上限、輸出下限
     
   
   // if(TimeCh >= SampleTime) // 到達預定採樣時間時
     //{
        float Input = f_angle, Input2 = f_angle2; // 輸入賦值
         float error = Setpoint - Input, error2 = Setpoint - Input2; // 偏差值
         ITerm+= (error * TimeCh); // 計算積分項
         ITerm = constrain(ITerm, outMin, outMax); // 限定值域
         float DTerm = (error - lastError) / TimeCh; // 計算微分項
         Output = Kp * error + Ki * ITerm + Kd *  DTerm; // 計算輸出值
         Output = constrain(Output, outMin, outMax); // 限定值域
         
      
          // 控制左驅
       //if(lastError>Input)  
         B = 117.5 - Output;   
         B = B+Bin;     
         B = constrain(B, 95, 180);
         myservo2.write(B);  
               
         // 控制右驅
      D = 119.5 + Output;
         D = D+Din;
         D = constrain(D, 95, 180);
         myservo4.write(D);

馬達控制是使用Servo.h函式,能夠反應的範圍是95~180,但是我看很多人好像不用Servo.h函式控制電調,而是用analogWrite,請問這兩者有差別嗎??

謝謝
回复

使用道具 举报

发表于 2013-11-7 20:58:22 | 显示全部楼层
我也是前者,后者我反正不会用,我做六轴呢,算法还在研究中
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-14 07:57 , Processed in 0.035748 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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