Eigen Revision

Dependencies:   mbed LPS25HB_I2C LSM9DS1 PIDcontroller Autopilot_Eigen LoopTicker GPSUBX_UART_Eigen SBUS_without_mainfile MedianFilter Eigen UsaPack solaESKF_Eigen Vector3 CalibrateMagneto FastPWM

servo.cpp

Committer:
NaotoMorita
Date:
2021-09-16
Revision:
82:c183c29d2427
Parent:
77:2bf856e3eca4
Child:
83:e69ab831031c

File content as of revision 82:c183c29d2427:

#include "global.hpp"

// 割り込まれた時点での出力(computeの結果)を返す関数
void calcServoOut()
{
    if(serialControlSource){
        for(int i = 0;i<4;i++){
            rc[i] = float(vp.actData[i])/1000.0f;
        }
    }else{
        // sbusデータの読み込み
        for (int i =0 ; i < 16;i ++){
            rc[i] = 0.65f * mapfloat(float(sbus.getData(i)),368,1680,-1,1) + (1.0f - 0.65f) * rc[i]; // mapped input
        }
    }
    
    //rc[1] = joystick.getX();
    //rc[0] = joystick.getY();
    
    // 自身の位置に応じてエレベータ舵角を決定する
    
    float rollObj = 20.0f*M_PI/180.0f*rc[0];
    float gaincoef = 1.0f;
    rollPID.setGain(5.0*gaincoef,0.0f,0.0);
    rollratePID.setGain(0.5*gaincoef,0.0f,0.0);
    pitchPID.setProcessValue(rpy.x);
    pitchratePID.setProcessValue(gyro.x);
    rollPID.setProcessValue(rpy.y);
    rollPID.setSetPoint(rollObj);
    rollratePID.setProcessValue(gyro.y);
    //de = -rc[1];
    //da = +rc[0];
    de = -(pitchPID.compute()+pitchratePID.compute()-rc[1]);
    da = rollPID.compute()+rollratePID.compute();
    //float de = pitchPID.compute()+pitchratePID.compute()-rc[1];
    //float da = rollPID.compute()+rollratePID.compute()+rc[0];
    dT = rc[2];
    
    scaledServoOut[0]=-de+da;
    scaledServoOut[1]=de+da;
    scaledMotorOut[0]= dT;
    
    float LP_servo = 0.2;
    float LP_motor = 0.2;
    for(int i = 0; i < sizeof(servoOut)/sizeof(servoOut[0]); i++)
    {
        servoOut[i] = LP_servo*(mapfloat(scaledServoOut[i],-1,1,servoPwmMin,servoPwmMax))+(1.0-LP_servo)*servoOut[i];
        if(servoOut[i]<servoPwmMin)
        {
            servoOut[i] = servoPwmMin;
        }
        if(servoOut[i]>servoPwmMax)
        {
            servoOut[i] = servoPwmMax;
        }
    }
    
    for(int i = 0;i<sizeof(motorOut)/sizeof(motorOut[0]) ;i++){
        motorOut[i] = LP_motor*(mapfloat(scaledMotorOut[i],-1,1,motorPwmMin,motorPwmMax))+(1.0-LP_motor)*motorOut[i];
        if(motorOut[i]<motorPwmMin) {
            motorOut[i] = motorPwmMin;
        };
        if(motorOut[i]>motorPwmMax) {
            motorOut[i] = motorPwmMax;
        };
    }
    servoRight.pulsewidth_us(servoOut[0]);
    servoLeft.pulsewidth_us(servoOut[1]); 
    servoThrust.pulsewidth_us(motorOut[0]);

    if(loop_count >= 10)
    {
        writeSdcard();
        loop_count = 1;

    }
    else
    {
        loop_count +=1;
    }
}