Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed MatrixMath LPS25HB_I2C LSM9DS1 Matrix2 PIDcontroller LoopTicker SBUS_without_mainfile UsaPack solaESKF_wind Vector3 CalibrateMagneto FastPWM
servo.cpp@73:be7a8b8188de, 2021-08-25 (annotated)
- Committer:
- NaotoMorita
- Date:
- Wed Aug 25 07:01:12 2021 +0000
- Revision:
- 73:be7a8b8188de
- Parent:
- 71:62eb45ecffe9
- Child:
- 74:af3b7cedff56
centered correction
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 | 62:ef10fd919f7b | 14 | //pc.printf("rc:%f\r\n",rc[1]); |
| NaotoMorita | 57:923e3df16159 | 15 | |
| NaotoMorita | 57:923e3df16159 | 16 | // 自身の位置に応じてエレベータ舵角を決定する |
| NaotoMorita | 71:62eb45ecffe9 | 17 | float derc = rc[1]; |
| NaotoMorita | 71:62eb45ecffe9 | 18 | float darc = rc[0]-rc[1]; |
| cocorlow | 63:851e96f54a86 | 19 | deobj = 0.0f; |
| NaotoMorita | 57:923e3df16159 | 20 | switch(pos_tail) |
| NaotoMorita | 57:923e3df16159 | 21 | { |
| NaotoMorita | 57:923e3df16159 | 22 | case 0: //left |
| NaotoMorita | 71:62eb45ecffe9 | 23 | deobj = derc-darc; |
| NaotoMorita | 57:923e3df16159 | 24 | break; |
| NaotoMorita | 57:923e3df16159 | 25 | case 1: //center |
| NaotoMorita | 71:62eb45ecffe9 | 26 | deobj = derc; |
| NaotoMorita | 57:923e3df16159 | 27 | break; |
| NaotoMorita | 57:923e3df16159 | 28 | case 2: //right |
| NaotoMorita | 71:62eb45ecffe9 | 29 | deobj = derc+darc; |
| NaotoMorita | 57:923e3df16159 | 30 | break; |
| NaotoMorita | 57:923e3df16159 | 31 | default: // error situation |
| NaotoMorita | 57:923e3df16159 | 32 | deobj = 0.0f; |
| NaotoMorita | 57:923e3df16159 | 33 | break; |
| NaotoMorita | 57:923e3df16159 | 34 | } |
| NaotoMorita | 57:923e3df16159 | 35 | //指令角の最大最小値をrc7で変更 |
| NaotoMorita | 57:923e3df16159 | 36 | float objgain = ((rc[6]+1.0f)/2*10.0f+1.0f)*M_PI/180.0f; |
| NaotoMorita | 57:923e3df16159 | 37 | float pitchobj = objgain * deobj; |
| NaotoMorita | 57:923e3df16159 | 38 | |
| NaotoMorita | 57:923e3df16159 | 39 | //ゲインの係数をrc8で変更 |
| NaotoMorita | 62:ef10fd919f7b | 40 | float gaincoef = 1.0; |
| NaotoMorita | 62:ef10fd919f7b | 41 | pitchPID.setGain(6.36*gaincoef, 10.6*gaincoef*0.0f,0.0); |
| NaotoMorita | 57:923e3df16159 | 42 | pitchratePID.setGain(0.9540*gaincoef,0.0,0.0); |
| NaotoMorita | 71:62eb45ecffe9 | 43 | pitchPID.setProcessValue(rpy.x); |
| NaotoMorita | 71:62eb45ecffe9 | 44 | pitchratePID.setProcessValue(gyro.x); |
| NaotoMorita | 57:923e3df16159 | 45 | //目標値のセット |
| NaotoMorita | 57:923e3df16159 | 46 | pitchPID.setSetPoint(pitchobj); //目標の設定 |
| NaotoMorita | 71:62eb45ecffe9 | 47 | |
| cocorlow | 63:851e96f54a86 | 48 | de = pitchPID.compute()+pitchratePID.compute(); |
| cocorlow | 56:888379912f81 | 49 | |
| osaka | 67:1da4e7ccdd2b | 50 | if (pos_tail == 0) |
| osaka | 67:1da4e7ccdd2b | 51 | { |
| osaka | 67:1da4e7ccdd2b | 52 | |
| NaotoMorita | 71:62eb45ecffe9 | 53 | scaledServoOut[0]=-de; |
| osaka | 67:1da4e7ccdd2b | 54 | } |
| osaka | 67:1da4e7ccdd2b | 55 | else |
| osaka | 67:1da4e7ccdd2b | 56 | { |
| NaotoMorita | 71:62eb45ecffe9 | 57 | scaledServoOut[0]=de; |
| osaka | 67:1da4e7ccdd2b | 58 | } |
| cocorlow | 56:888379912f81 | 59 | |
| cocorlow | 56:888379912f81 | 60 | float LP_servo = 0.2; |
| cocorlow | 56:888379912f81 | 61 | for(int i = 0; i < sizeof(servoOut)/sizeof(servoOut[0]); i++) |
| cocorlow | 56:888379912f81 | 62 | { |
| cocorlow | 56:888379912f81 | 63 | servoOut[i] = LP_servo*(mapfloat(scaledServoOut[i],-1,1,servoPwmMin,servoPwmMax))+(1.0-LP_servo)*servoOut[i]; |
| cocorlow | 56:888379912f81 | 64 | if(servoOut[i]<servoPwmMin) |
| cocorlow | 56:888379912f81 | 65 | { |
| cocorlow | 56:888379912f81 | 66 | servoOut[i] = servoPwmMin; |
| cocorlow | 56:888379912f81 | 67 | } |
| cocorlow | 56:888379912f81 | 68 | if(servoOut[i]>servoPwmMax) |
| cocorlow | 56:888379912f81 | 69 | { |
| cocorlow | 56:888379912f81 | 70 | servoOut[i] = servoPwmMax; |
| cocorlow | 56:888379912f81 | 71 | } |
| cocorlow | 56:888379912f81 | 72 | } |
| cocorlow | 56:888379912f81 | 73 | |
| cocorlow | 56:888379912f81 | 74 | servo.pulsewidth_us(servoOut[0]); |
| NaotoMorita | 73:be7a8b8188de | 75 | send2center(); |
| cocorlow | 56:888379912f81 | 76 | } |