2015_robocon_bteam / Mbed 2 deprecated 2015robot_main

Dependencies:   PID QEI mbed

Fork of 2015robot_main by Naoto Deguchi

autoMode.h

Committer:
DeguNaoto
Date:
2015-09-28
Revision:
55:db1797ac6cb1
Parent:
52:d9e1629da852
Child:
57:3fbd487e055e

File content as of revision 55:db1797ac6cb1:

#ifndef AUTOMODE_H
#define AUTOMODE_H

/***PID Controller***/
PID velocity_controller(36.0,5274.0 ,0.0,RATE);
PID direction_controller(36.0,3.0,0.0,RATE);

/***IM920 correspondence***/
void autoIM920() {
    if(b==7){  /*mode change*/
        if(edge7){
            edge7=0;
            autoflag=0;
            Indicator4=0;
            IndicatorAuto=1;
        }
    }
    else if(b==6){  /*start*/
        targ_velocity=speed;
        sendData(4,defoR);
        wait(0.1);
        sendData(5,defoL);
        wait(0.1);
        stateR = defoR;
        stateL = defoL;
    }
    else if(b==5){ /*reset*/
        sendData(4,31);
        wait(0.1);
        sendData(5,31);
        wait(0.1);
    }
    else if(b==8){ /*set defo*/
        sendData(4,defoR);
        wait(0.1);
        sendData(5,defoL);
        wait(0.1);
    }
    else if(b==9){  /*stop*/
        Motor_swing=0;
        sita=PI/4.0,x=0.0,y=0.0;
        targ_velocity=0.0;
        targ_sita=PI/4.0;
        velocity_controller.reset();
        direction_controller.reset();
    }
    else if(b==1){  /*L down*/
        if(edge1){
            edge1=0;
            if(stateL!=1) stateL--;
            sendData(5,stateL);
        }
    }
    else if(b==2){  /*L up*/
        if(edge2){
            edge2=0;
            if(stateL!=28) stateL++;
            sendData(5,stateL);
        }
    }
    else if(b==3){  /*R down*/
        if(edge3){
            edge3=0;
            if(stateR!=1) stateR--;
            sendData(4,stateR);
        }
    }
    else if(b==4){  /*R up*/
        if(edge4){
            edge4=0;
            if(stateR!=28) stateR++;
            sendData(4,stateR);
        }
    }
    if(b!=7) edge7=1;
    if(b!=1) edge1=1;
    if(b!=2) edge2=1;
    if(b!=3) edge3=1;
    if(b!=4) edge4=1;
}

/***The function is PID controller initialize.***/
inline void initializeControllers()
{
    velocity_controller.setInputLimits(-200000.0, 200000.0); //x1
    direction_controller.setInputLimits(-10.0, 10.0); //x2

    //Pwm output from MOVE_OUTPUT_LIMIT_BOTTOM to MOVE_OUTPUT_LIMIT_TOP
    velocity_controller.setOutputLimits(-1.0, 1.0);
    direction_controller.setOutputLimits(-1.0, 1.0);

    //set bias. 初期値
    velocity_controller.setBias(0.0);
    direction_controller.setBias(0.0);

    //set mode.
    velocity_controller.setMode(AUTO_MODE);
    direction_controller.setMode(AUTO_MODE);
}

/***The function is following move speed.***/
inline void velocity_following()
{
    velocity_controller.setSetPoint((float)targ_velocity);
    velocity_controller.setProcessValue((float)velocity);
    x1 = (double)velocity_controller.compute();
}

inline void sita_following()
{
    direction_controller.setSetPoint((float)targ_sita);
    direction_controller.setProcessValue((float)sita);
    //direction_controller.setSetPoint(0.0);  /*目標値とのずれをなくす*/
    //direction_controller.setProcessValue(-y);
    x2 = (double)direction_controller.compute();
}

inline void move_following()
{
    velocity_following();
    sita_following();
    Vr                  = ( x1 + x2 ) / 2.0;
    Vl                  = ( x1 - x2 ) / 2.0;
    if( abs(Vr) < 0.05 ) Vr = 0.0;
    if( abs(Vl) < 0.05 ) Vl = 0.0;
    Move_r( ( float ) Vr );
    Move_l( ( float ) Vl );
}

#endif /*autoMode.h*/