solaESKF_EIGEN
Dependencies: mbed LPS25HB_I2C LSM9DS1 PIDcontroller LoopTicker GPSUBX_UART_Eigen SBUS_without_mainfile MedianFilter Eigen UsaPack solaESKF_Eigen Vector3 CalibrateMagneto FastPWM
servo.cpp@57:923e3df16159, 2021-06-01 (annotated)
- Committer:
- NaotoMorita
- Date:
- Tue Jun 01 07:30:06 2021 +0000
- Revision:
- 57:923e3df16159
- Parent:
- 56:888379912f81
- Child:
- 61:c05353850017
change by tail_pos
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cocorlow | 56:888379912f81 | 1 | #include "global.hpp" |
cocorlow | 56:888379912f81 | 2 | |
cocorlow | 56:888379912f81 | 3 | // 割り込まれた時点での出力(computeの結果)を返す関数 |
cocorlow | 56:888379912f81 | 4 | void calcServoOut() |
cocorlow | 56:888379912f81 | 5 | { |
cocorlow | 56:888379912f81 | 6 | if(sbus.failSafe == false) |
cocorlow | 56:888379912f81 | 7 | { |
cocorlow | 56:888379912f81 | 8 | // sbusデータの読み込み |
cocorlow | 56:888379912f81 | 9 | for (int i = 0; i < 16;i ++) |
cocorlow | 56:888379912f81 | 10 | { |
cocorlow | 56:888379912f81 | 11 | rc[i] = 0.65f * mapfloat(float(sbus.getData(i)),368,1680,-1,1) + (1.0f - 0.65f) * rc[i]; // mapped input |
cocorlow | 56:888379912f81 | 12 | } |
cocorlow | 56:888379912f81 | 13 | } |
NaotoMorita | 57:923e3df16159 | 14 | |
NaotoMorita | 57:923e3df16159 | 15 | // 自身の位置に応じてエレベータ舵角を決定する |
NaotoMorita | 57:923e3df16159 | 16 | float deobj = 0.0f; |
NaotoMorita | 57:923e3df16159 | 17 | switch(pos_tail) |
NaotoMorita | 57:923e3df16159 | 18 | { |
NaotoMorita | 57:923e3df16159 | 19 | case 0: //left |
NaotoMorita | 57:923e3df16159 | 20 | deobj = -rc[1]; |
NaotoMorita | 57:923e3df16159 | 21 | break; |
NaotoMorita | 57:923e3df16159 | 22 | case 1: //center |
NaotoMorita | 57:923e3df16159 | 23 | deobj = -rc[1]; |
NaotoMorita | 57:923e3df16159 | 24 | break; |
NaotoMorita | 57:923e3df16159 | 25 | case 2: //right |
NaotoMorita | 57:923e3df16159 | 26 | deobj = -rc[1]; |
NaotoMorita | 57:923e3df16159 | 27 | break; |
NaotoMorita | 57:923e3df16159 | 28 | default: // error situation |
NaotoMorita | 57:923e3df16159 | 29 | deobj = 0.0f; |
NaotoMorita | 57:923e3df16159 | 30 | break; |
NaotoMorita | 57:923e3df16159 | 31 | } |
NaotoMorita | 57:923e3df16159 | 32 | //指令角の最大最小値をrc7で変更 |
NaotoMorita | 57:923e3df16159 | 33 | float objgain = ((rc[6]+1.0f)/2*10.0f+1.0f)*M_PI/180.0f; |
NaotoMorita | 57:923e3df16159 | 34 | float pitchobj = objgain * deobj; |
NaotoMorita | 57:923e3df16159 | 35 | |
NaotoMorita | 57:923e3df16159 | 36 | //ゲインの係数をrc8で変更 |
NaotoMorita | 57:923e3df16159 | 37 | float gaincoef = ((rc[7]+1.0f)/2*0.9)+0.1; |
NaotoMorita | 57:923e3df16159 | 38 | pitchPID.setGain(6.36*gaincoef, 10.6*gaincoef,0.0); |
NaotoMorita | 57:923e3df16159 | 39 | pitchratePID.setGain(0.9540*gaincoef,0.0,0.0); |
cocorlow | 56:888379912f81 | 40 | pitchPID.setProcessValue(rpy.y); |
cocorlow | 56:888379912f81 | 41 | pitchratePID.setProcessValue(gyro.y); |
NaotoMorita | 57:923e3df16159 | 42 | //目標値のセット |
NaotoMorita | 57:923e3df16159 | 43 | pitchPID.setSetPoint(pitchobj); //目標の設定 |
NaotoMorita | 57:923e3df16159 | 44 | float de = pitchPID.compute()+pitchratePID.compute(); |
cocorlow | 56:888379912f81 | 45 | |
cocorlow | 56:888379912f81 | 46 | scaledServoOut[0]=de; |
cocorlow | 56:888379912f81 | 47 | |
cocorlow | 56:888379912f81 | 48 | float LP_servo = 0.2; |
cocorlow | 56:888379912f81 | 49 | for(int i = 0; i < sizeof(servoOut)/sizeof(servoOut[0]); i++) |
cocorlow | 56:888379912f81 | 50 | { |
cocorlow | 56:888379912f81 | 51 | servoOut[i] = LP_servo*(mapfloat(scaledServoOut[i],-1,1,servoPwmMin,servoPwmMax))+(1.0-LP_servo)*servoOut[i]; |
cocorlow | 56:888379912f81 | 52 | if(servoOut[i]<servoPwmMin) |
cocorlow | 56:888379912f81 | 53 | { |
cocorlow | 56:888379912f81 | 54 | servoOut[i] = servoPwmMin; |
cocorlow | 56:888379912f81 | 55 | } |
cocorlow | 56:888379912f81 | 56 | if(servoOut[i]>servoPwmMax) |
cocorlow | 56:888379912f81 | 57 | { |
cocorlow | 56:888379912f81 | 58 | servoOut[i] = servoPwmMax; |
cocorlow | 56:888379912f81 | 59 | } |
cocorlow | 56:888379912f81 | 60 | } |
cocorlow | 56:888379912f81 | 61 | |
cocorlow | 56:888379912f81 | 62 | servo.pulsewidth_us(servoOut[0]); |
cocorlow | 56:888379912f81 | 63 | |
cocorlow | 56:888379912f81 | 64 | //観測アップデート |
cocorlow | 56:888379912f81 | 65 | dynacc = ekf.calcDynAcc(LPacc, accref); |
cocorlow | 56:888379912f81 | 66 | th_mg = abs(acos(LPmag/LPmag.Norm() % LPacc/LPacc.Norm())-val_thmg); |
cocorlow | 56:888379912f81 | 67 | |
cocorlow | 56:888379912f81 | 68 | accnormerr = abs(LPacc.Norm()-accref.Norm()); |
cocorlow | 56:888379912f81 | 69 | //静止時100個の平均 0.01877522 0.00514146 0.00477393 |
cocorlow | 56:888379912f81 | 70 | |
cocorlow | 56:888379912f81 | 71 | //int ang_th = th_mg < 0.01877522; |
cocorlow | 56:888379912f81 | 72 | //int dyn_th = dynaccnorm < 0.00514146; |
cocorlow | 56:888379912f81 | 73 | //int norm_th = accnormerr< 0.00477393; |
cocorlow | 56:888379912f81 | 74 | int ang_th = th_mg < 0.01877522/5.0; |
cocorlow | 56:888379912f81 | 75 | int dyn_th = dynacc.Norm() < 0.00514146/5.0; |
cocorlow | 56:888379912f81 | 76 | int norm_th = accnormerr< 0.00477393/5.0; |
cocorlow | 56:888379912f81 | 77 | if(dyn_th+ang_th+norm_th>0) |
cocorlow | 56:888379912f81 | 78 | { |
cocorlow | 56:888379912f81 | 79 | //if(dyn_th+ang_th+norm_th>-1){ |
cocorlow | 56:888379912f81 | 80 | obs_count += 1; |
cocorlow | 56:888379912f81 | 81 | ekf.updateAcrossMeasures(LPmag/LPmag.Norm(), magref/magref.Norm(), ekf.Rmag); |
cocorlow | 56:888379912f81 | 82 | ekf.updateAcrossMeasures(LPacc/LPacc.Norm(), accref/accref.Norm(), ekf.Racc); |
cocorlow | 56:888379912f81 | 83 | } |
cocorlow | 56:888379912f81 | 84 | |
cocorlow | 56:888379912f81 | 85 | if(loop_count >= 10) |
cocorlow | 56:888379912f81 | 86 | { |
cocorlow | 56:888379912f81 | 87 | writeSdcard(); |
cocorlow | 56:888379912f81 | 88 | loop_count = 1; |
cocorlow | 56:888379912f81 | 89 | obs_count = 0; |
cocorlow | 56:888379912f81 | 90 | } |
cocorlow | 56:888379912f81 | 91 | else |
cocorlow | 56:888379912f81 | 92 | { |
cocorlow | 56:888379912f81 | 93 | loop_count +=1; |
cocorlow | 56:888379912f81 | 94 | } |
cocorlow | 56:888379912f81 | 95 | } |