Catch the GIANT Caplico! / Mbed 2 deprecated PID_AutoTuning

Dependencies:   EC mbed

Fork of PID by Catch the GIANT Caplico!

main.cpp

Committer:
kageyuta
Date:
2018-07-23
Revision:
2:18a1325ac2f2
Parent:
1:b38405009bb4
Child:
3:5d7dca0ca819

File content as of revision 2:18a1325ac2f2:

#include "mbed.h"
#include "EC.h"
#define K 0.01
#define stride 0.01
#define count 20
Serial pc(USBTX,USBRX);

PwmOut motorF(PA_3); //モータードライバの接続ピン
PwmOut motorR(PA_4); //モータードライバの接続ピン
Ec Ec1(PA_6,PA_7,NC,2048,0.01); //エンコーダの設定(A相、B相、分解能、計算間隔[s])
Ticker ticker;
Timer timer;

void out (double duty)
{
    double duty_max = 0.3;
    if(duty > 0) { //入力duty比が正の場合
        if( abs(duty) < duty_max ) { //制限値を超えていない場合
            motorF = abs(duty);
            motorR = 0;
        } else { //制限値を超えている場合
            motorF = duty_max;
            motorR = 0;
        }
    } else {//入力duty比が負の場合
        if( abs(duty) < duty_max) { //制限値を超えていない場合
            motorF = 0;
            motorR = abs(duty);
        } else { //制限値を超えている場合
            motorF = 0;
            motorR = duty_max;
        }
    }
}

void calOmega()
{
    Ec1.CalOmega();
}

int main()
{
    ticker.attach(&calOmega,0.01);
    timer.start();
    double omega_old;
    int i,j;
    while(1) {
        pc.printf("Kp = %f : ",K+j*stride);
        timer.reset();
        i=0;
        while(i<count) {
            if(omega_old == 0 && Ec1.getOmega() == 0) break;
            else if(omega_old * Ec1.getOmega() <= 0) i++;
            omega_old = Ec1.getOmega();
            out((10*(j%2) - Ec1.getCount()*(-1)*30*5/6*3.14159265359/2048) * (K + j*stride));
        }
        if(i==count) break;
        pc.printf("TimeOut\r\n");
        j++;
    }
    double period = timer.read() / count;
    pc.printf("K = %f, T = %fs\r\n",K+j*stride,period);
    pc.printf("Kp = %f, Ki = %f, Kd = %f",0.6*(K+j*stride),0.5*period,0.125*period);
}