Eigen
Dependencies: Eigen
Dependents: optWingforHAPS_Eigen hexaTest_Eigen
solaESKF.cpp@54:cd514d9d4b19, 2021-11-07 (annotated)
- Committer:
- NaotoMorita
- Date:
- Sun Nov 07 05:38:04 2021 +0000
- Revision:
- 54:cd514d9d4b19
- Parent:
- 53:8b551358a7e3
- Child:
- 55:21611d4cf7e8
include modify
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
NaotoMorita | 44:7d82e63b6a86 | 1 | #include "solaESKF.hpp" |
NaotoMorita | 54:cd514d9d4b19 | 2 | |
NaotoMorita | 19:3fae66745363 | 3 | |
NaotoMorita | 19:3fae66745363 | 4 | |
NaotoMorita | 44:7d82e63b6a86 | 5 | solaESKF::solaESKF() |
NaotoMorita | 46:15988dc41923 | 6 | :pihat(3,1),vihat(3,1),qhat(4,1),accBias(3,1),gyroBias(3,1),gravity(3,1),errState(18,1),Phat(18,18),Q(18,18) |
NaotoMorita | 19:3fae66745363 | 7 | { |
NaotoMorita | 46:15988dc41923 | 8 | pihat << 0.0f << 0.0f << 0.0f; |
NaotoMorita | 46:15988dc41923 | 9 | vihat << 0.0f << 0.0f << 0.0f; |
NaotoMorita | 46:15988dc41923 | 10 | qhat << 1.0f << 0.0f << 0.0f << 0.0f; |
NaotoMorita | 46:15988dc41923 | 11 | accBias << 0.0f << 0.0f << 0.0f; |
NaotoMorita | 46:15988dc41923 | 12 | gyroBias << 0.0f << 0.0f << 0.0f; |
NaotoMorita | 46:15988dc41923 | 13 | gravity << 0.0f << 0.0f << 9.8f; |
NaotoMorita | 46:15988dc41923 | 14 | |
NaotoMorita | 46:15988dc41923 | 15 | for (int i = 1; i < 19; i++){ |
NaotoMorita | 46:15988dc41923 | 16 | errState(i,1) = 0.0f; |
NaotoMorita | 46:15988dc41923 | 17 | for (int j = 1; j < 19; j++){ |
NaotoMorita | 46:15988dc41923 | 18 | Phat(i,j) = 0.0f; |
NaotoMorita | 46:15988dc41923 | 19 | Q(i,j) = 0.0f; |
NaotoMorita | 46:15988dc41923 | 20 | } |
NaotoMorita | 46:15988dc41923 | 21 | } |
NaotoMorita | 44:7d82e63b6a86 | 22 | |
NaotoMorita | 44:7d82e63b6a86 | 23 | |
NaotoMorita | 21:d6079def0473 | 24 | nState = errState.getRows(); |
NaotoMorita | 47:2467de40951f | 25 | |
NaotoMorita | 46:15988dc41923 | 26 | setBlockDiag(Phat,0.1f,1,3);//position |
NaotoMorita | 46:15988dc41923 | 27 | setBlockDiag(Phat,0.1f,4,6);//velocity |
NaotoMorita | 46:15988dc41923 | 28 | setBlockDiag(Phat,0.1f,7,9);//angle error |
NaotoMorita | 47:2467de40951f | 29 | setBlockDiag(Phat,0.1f,10,12);//acc bias |
NaotoMorita | 47:2467de40951f | 30 | setBlockDiag(Phat,0.1f,13,15);//gyro bias |
NaotoMorita | 47:2467de40951f | 31 | setBlockDiag(Phat,0.00000001f,16,18);//gravity |
NaotoMorita | 47:2467de40951f | 32 | setBlockDiag(Q,0.00025f,4,6);//velocity |
NaotoMorita | 47:2467de40951f | 33 | setBlockDiag(Q,0.005f/57.0,7,9);//angle error |
NaotoMorita | 46:15988dc41923 | 34 | setBlockDiag(Q,0.001f,10,12);//acc bias |
NaotoMorita | 46:15988dc41923 | 35 | setBlockDiag(Q,0.001f,13,15);//gyro bias //positionとgravityはQ項なし |
NaotoMorita | 19:3fae66745363 | 36 | |
NaotoMorita | 19:3fae66745363 | 37 | |
NaotoMorita | 19:3fae66745363 | 38 | } |
NaotoMorita | 19:3fae66745363 | 39 | |
NaotoMorita | 47:2467de40951f | 40 | |
NaotoMorita | 44:7d82e63b6a86 | 41 | void solaESKF::updateNominal(Matrix acc, Matrix gyro,float att_dt) |
NaotoMorita | 19:3fae66745363 | 42 | { |
NaotoMorita | 44:7d82e63b6a86 | 43 | Matrix gyrom = gyro - gyroBias; |
NaotoMorita | 44:7d82e63b6a86 | 44 | Matrix accm = acc - accBias; |
NaotoMorita | 44:7d82e63b6a86 | 45 | |
NaotoMorita | 44:7d82e63b6a86 | 46 | Matrix qint(4,1); |
NaotoMorita | 46:15988dc41923 | 47 | qint << 1.0f << 0.5f*gyrom(1,1)*att_dt << 0.5f*gyrom(2,1)*att_dt << 0.5f*gyrom(3,1)*att_dt; |
NaotoMorita | 44:7d82e63b6a86 | 48 | qhat = quatmultiply(qhat,qint); |
NaotoMorita | 19:3fae66745363 | 49 | float qnorm = sqrt(MatrixMath::dot(MatrixMath::Transpose(qhat),qhat)); |
NaotoMorita | 19:3fae66745363 | 50 | qhat *= (1.0f/ qnorm); |
NaotoMorita | 23:1509648c2318 | 51 | |
NaotoMorita | 23:1509648c2318 | 52 | Matrix dcm(3,3); |
NaotoMorita | 23:1509648c2318 | 53 | computeDcm(dcm, qhat); |
NaotoMorita | 46:15988dc41923 | 54 | Matrix accned = dcm*accm+gravity; |
NaotoMorita | 44:7d82e63b6a86 | 55 | vihat += accned*att_dt; |
NaotoMorita | 44:7d82e63b6a86 | 56 | |
NaotoMorita | 44:7d82e63b6a86 | 57 | pihat += vihat*att_dt+0.5f*accned*att_dt*att_dt; |
NaotoMorita | 19:3fae66745363 | 58 | } |
NaotoMorita | 19:3fae66745363 | 59 | |
NaotoMorita | 44:7d82e63b6a86 | 60 | void solaESKF::updateErrState(Matrix acc,Matrix gyro,float att_dt) |
NaotoMorita | 23:1509648c2318 | 61 | { |
NaotoMorita | 44:7d82e63b6a86 | 62 | Matrix gyrom = gyro - gyroBias; |
NaotoMorita | 44:7d82e63b6a86 | 63 | Matrix accm = acc - accBias; |
NaotoMorita | 23:1509648c2318 | 64 | |
NaotoMorita | 23:1509648c2318 | 65 | Matrix dcm(3,3); |
NaotoMorita | 23:1509648c2318 | 66 | computeDcm(dcm, qhat); |
NaotoMorita | 47:2467de40951f | 67 | Matrix a2v = -dcm*MatrixMath::Matrixcross(accm(1,1),accm(2,1),accm(3,1)); |
NaotoMorita | 44:7d82e63b6a86 | 68 | |
NaotoMorita | 47:2467de40951f | 69 | Matrix A(nState,nState); |
NaotoMorita | 44:7d82e63b6a86 | 70 | //position |
NaotoMorita | 47:2467de40951f | 71 | A(1,4) = 1.0f; |
NaotoMorita | 47:2467de40951f | 72 | A(2,5) = 1.0f; |
NaotoMorita | 47:2467de40951f | 73 | A(3,6) = 1.0f; |
NaotoMorita | 47:2467de40951f | 74 | |
NaotoMorita | 47:2467de40951f | 75 | //velocity |
NaotoMorita | 46:15988dc41923 | 76 | for (int i = 1; i < 4; i++){ |
NaotoMorita | 46:15988dc41923 | 77 | for (int j = 1; j < 4; j++){ |
NaotoMorita | 47:2467de40951f | 78 | A(i+3,j+6) = a2v(i,j); |
NaotoMorita | 47:2467de40951f | 79 | A(i+3,j+9) = -dcm(i,j); |
NaotoMorita | 47:2467de40951f | 80 | |
NaotoMorita | 23:1509648c2318 | 81 | } |
NaotoMorita | 23:1509648c2318 | 82 | } |
NaotoMorita | 47:2467de40951f | 83 | A(4,16) = 1.0f; |
NaotoMorita | 47:2467de40951f | 84 | A(5,17) = 1.0f; |
NaotoMorita | 47:2467de40951f | 85 | A(6,18) = 1.0f; |
NaotoMorita | 23:1509648c2318 | 86 | |
NaotoMorita | 44:7d82e63b6a86 | 87 | //angulat error |
NaotoMorita | 47:2467de40951f | 88 | A(7,8) = gyrom(3,1); |
NaotoMorita | 47:2467de40951f | 89 | A(7,9) = -gyrom(2,1); |
NaotoMorita | 47:2467de40951f | 90 | A(8,7) = -gyrom(3,1); |
NaotoMorita | 47:2467de40951f | 91 | A(8,9) = gyrom(1,1); |
NaotoMorita | 47:2467de40951f | 92 | A(9,7) = gyrom(2,1); |
NaotoMorita | 47:2467de40951f | 93 | A(9,8) = -gyrom(1,1); |
NaotoMorita | 47:2467de40951f | 94 | A(7,13) = -1.0f; |
NaotoMorita | 47:2467de40951f | 95 | A(8,14) = -1.0f; |
NaotoMorita | 47:2467de40951f | 96 | A(9,15) = -1.0f; |
NaotoMorita | 25:07ac5c6cd61c | 97 | |
NaotoMorita | 47:2467de40951f | 98 | |
NaotoMorita | 47:2467de40951f | 99 | Matrix Fx = MatrixMath::Eye(nState) + A * att_dt;// + (0.5f*att_dt*att_dt) * A * A; |
NaotoMorita | 47:2467de40951f | 100 | Matrix Qd = Q * att_dt + 0.5f*A*Q * att_dt * att_dt+ 0.5f*Q*MatrixMath::Transpose(A) * att_dt * att_dt; |
NaotoMorita | 44:7d82e63b6a86 | 101 | |
NaotoMorita | 44:7d82e63b6a86 | 102 | errState = Fx * errState; |
NaotoMorita | 47:2467de40951f | 103 | Phat = Fx*Phat*MatrixMath::Transpose(Fx)+Qd; |
NaotoMorita | 25:07ac5c6cd61c | 104 | } |
NaotoMorita | 25:07ac5c6cd61c | 105 | |
NaotoMorita | 50:dadad0567349 | 106 | void solaESKF::updateAccConstraints(Matrix acc,float palt,Matrix R) |
NaotoMorita | 46:15988dc41923 | 107 | { |
NaotoMorita | 46:15988dc41923 | 108 | Matrix accm = acc - accBias; |
NaotoMorita | 46:15988dc41923 | 109 | Matrix tdcm(3,3); |
NaotoMorita | 46:15988dc41923 | 110 | computeDcm(tdcm, qhat); |
NaotoMorita | 46:15988dc41923 | 111 | tdcm = MatrixMath::Transpose(tdcm); |
NaotoMorita | 46:15988dc41923 | 112 | Matrix tdcm_g = tdcm*gravity; |
NaotoMorita | 46:15988dc41923 | 113 | Matrix a2v = MatrixMath::Matrixcross(tdcm_g(1,1),tdcm_g(2,1),tdcm_g(3,1)); |
NaotoMorita | 46:15988dc41923 | 114 | |
NaotoMorita | 50:dadad0567349 | 115 | Matrix H(4,nState); |
NaotoMorita | 46:15988dc41923 | 116 | for (int i = 1; i < 4; i++){ |
NaotoMorita | 46:15988dc41923 | 117 | for (int j = 1; j < 4; j++){ |
NaotoMorita | 46:15988dc41923 | 118 | H(i,j+6) = a2v(i,j); |
NaotoMorita | 46:15988dc41923 | 119 | H(i,j+15) = tdcm(i,j); |
NaotoMorita | 46:15988dc41923 | 120 | } |
NaotoMorita | 46:15988dc41923 | 121 | } |
NaotoMorita | 46:15988dc41923 | 122 | H(1,10) = -1.0f; |
NaotoMorita | 46:15988dc41923 | 123 | H(2,11) = -1.0f; |
NaotoMorita | 46:15988dc41923 | 124 | H(3,12) = -1.0f; |
NaotoMorita | 50:dadad0567349 | 125 | H(4,3) = 1.0f; |
NaotoMorita | 46:15988dc41923 | 126 | |
NaotoMorita | 47:2467de40951f | 127 | //Matrix A = H*Phat*MatrixMath::Transpose(H)+R; |
NaotoMorita | 47:2467de40951f | 128 | //Matrix K = (Phat*MatrixMath::Transpose(H))*(MatrixMath::Inv(MatrixMath::Transpose(A)*A+10.0f*MatrixMath::Eye(3))*MatrixMath::Transpose(A)); |
NaotoMorita | 47:2467de40951f | 129 | Matrix K = (Phat*MatrixMath::Transpose(H))*MatrixMath::Inv(H*Phat*MatrixMath::Transpose(H)+R); |
NaotoMorita | 50:dadad0567349 | 130 | Matrix zacc = -accm-tdcm*gravity; |
NaotoMorita | 50:dadad0567349 | 131 | Matrix z(4,1); |
NaotoMorita | 52:ab42c0497088 | 132 | z << zacc(1,1) << zacc(2,1) << zacc(3,1) << palt - pihat(3,1); |
NaotoMorita | 46:15988dc41923 | 133 | errState = K * z; |
NaotoMorita | 50:dadad0567349 | 134 | Phat = (MatrixMath::Eye(nState)-K*H)*Phat; |
NaotoMorita | 47:2467de40951f | 135 | //Phat = Phat-K*(H*Phat*MatrixMath::Transpose(H)+R)*MatrixMath::Transpose(K); |
NaotoMorita | 50:dadad0567349 | 136 | //Phat = (MatrixMath::Eye(nState)-K*H)*Phat*MatrixMath::Transpose(MatrixMath::Eye(nState)-K*H)+K*R*MatrixMath::Transpose(K); |
NaotoMorita | 46:15988dc41923 | 137 | } |
NaotoMorita | 46:15988dc41923 | 138 | |
NaotoMorita | 46:15988dc41923 | 139 | void solaESKF::updateGyroConstraints(Matrix gyro,Matrix R) |
NaotoMorita | 46:15988dc41923 | 140 | { |
NaotoMorita | 46:15988dc41923 | 141 | Matrix gyrom = gyro - gyroBias; |
NaotoMorita | 46:15988dc41923 | 142 | Matrix dcm(3,3); |
NaotoMorita | 46:15988dc41923 | 143 | computeDcm(dcm, qhat); |
NaotoMorita | 46:15988dc41923 | 144 | Matrix a2v = -dcm*MatrixMath::Matrixcross(gyrom(1,1),gyrom(2,1),gyrom(3,1)); |
NaotoMorita | 46:15988dc41923 | 145 | |
NaotoMorita | 46:15988dc41923 | 146 | Matrix H(2,nState); |
NaotoMorita | 46:15988dc41923 | 147 | for (int i = 1; i < 3; i++){ |
NaotoMorita | 46:15988dc41923 | 148 | for (int j = 1; j < 4; j++){ |
NaotoMorita | 46:15988dc41923 | 149 | H(i,j+6) = a2v(i,j); |
NaotoMorita | 46:15988dc41923 | 150 | H(i,j+12) = -dcm(i,j); |
NaotoMorita | 46:15988dc41923 | 151 | } |
NaotoMorita | 46:15988dc41923 | 152 | } |
NaotoMorita | 46:15988dc41923 | 153 | |
NaotoMorita | 47:2467de40951f | 154 | //Matrix A = H*Phat*MatrixMath::Transpose(H)+R; |
NaotoMorita | 47:2467de40951f | 155 | //Matrix K = (Phat*MatrixMath::Transpose(H))*(MatrixMath::Inv(MatrixMath::Transpose(A)*A+10.0f*MatrixMath::Eye(2))*MatrixMath::Transpose(A)); |
NaotoMorita | 47:2467de40951f | 156 | Matrix K = (Phat*MatrixMath::Transpose(H))*MatrixMath::Inv(H*Phat*MatrixMath::Transpose(H)+R); |
NaotoMorita | 46:15988dc41923 | 157 | |
NaotoMorita | 46:15988dc41923 | 158 | Matrix z3 = -dcm*gyrom; |
NaotoMorita | 46:15988dc41923 | 159 | Matrix z(2,1); |
NaotoMorita | 46:15988dc41923 | 160 | z << z3(1,1) << z3(2,1); |
NaotoMorita | 46:15988dc41923 | 161 | errState = K * z; |
NaotoMorita | 47:2467de40951f | 162 | //Phat = (MatrixMath::Eye(nState)-K*H)*Phat; |
NaotoMorita | 47:2467de40951f | 163 | //Phat = Phat-K*(H*Phat*MatrixMath::Transpose(H)+R)*MatrixMath::Transpose(K); |
NaotoMorita | 46:15988dc41923 | 164 | Phat = (MatrixMath::Eye(nState)-K*H)*Phat*MatrixMath::Transpose(MatrixMath::Eye(nState)-K*H)+K*R*MatrixMath::Transpose(K); |
NaotoMorita | 46:15988dc41923 | 165 | } |
NaotoMorita | 46:15988dc41923 | 166 | |
NaotoMorita | 46:15988dc41923 | 167 | |
NaotoMorita | 46:15988dc41923 | 168 | void solaESKF::updateGPSVelocity(Matrix velgps,Matrix R) |
NaotoMorita | 19:3fae66745363 | 169 | { |
osaka | 40:119792aa6d3b | 170 | |
NaotoMorita | 43:cbc2c2d65131 | 171 | Matrix H(3,nState); |
NaotoMorita | 46:15988dc41923 | 172 | H(1,4) = 1.0f; |
NaotoMorita | 46:15988dc41923 | 173 | H(2,5) = 1.0f; |
NaotoMorita | 46:15988dc41923 | 174 | H(3,6) = 1.0f; |
NaotoMorita | 46:15988dc41923 | 175 | |
NaotoMorita | 47:2467de40951f | 176 | //Matrix A = H*Phat*MatrixMath::Transpose(H)+R; |
NaotoMorita | 47:2467de40951f | 177 | //Matrix K = (Phat*MatrixMath::Transpose(H))*(MatrixMath::Inv(MatrixMath::Transpose(A)*A+10.0f*MatrixMath::Eye(3))*MatrixMath::Transpose(A)); |
NaotoMorita | 47:2467de40951f | 178 | Matrix K = (Phat*MatrixMath::Transpose(H))*MatrixMath::Inv(H*Phat*MatrixMath::Transpose(H)+R); |
NaotoMorita | 46:15988dc41923 | 179 | Matrix z(3,1); |
NaotoMorita | 46:15988dc41923 | 180 | z << velgps(1,1) - vihat(1,1) << velgps(2,1)-vihat(2,1) << velgps(3,1) - vihat(3,1); |
NaotoMorita | 46:15988dc41923 | 181 | errState = K * z; |
NaotoMorita | 50:dadad0567349 | 182 | Phat = (MatrixMath::Eye(nState)-K*H)*Phat; |
NaotoMorita | 46:15988dc41923 | 183 | //Phat = Phat-K*(H*Phat*MatrixMath::Transpose(H)+R)*MatrixMath::Transpose(K); |
NaotoMorita | 50:dadad0567349 | 184 | //Phat = (MatrixMath::Eye(nState)-K*H)*Phat*MatrixMath::Transpose(MatrixMath::Eye(nState)-K*H)+K*R*MatrixMath::Transpose(K); |
NaotoMorita | 46:15988dc41923 | 185 | } |
NaotoMorita | 43:cbc2c2d65131 | 186 | |
NaotoMorita | 46:15988dc41923 | 187 | void solaESKF::updateGPSPosition(Matrix posgps,Matrix R) |
NaotoMorita | 46:15988dc41923 | 188 | { |
NaotoMorita | 52:ab42c0497088 | 189 | Matrix H(2,nState); |
NaotoMorita | 46:15988dc41923 | 190 | H(1,1) = 1.0f; |
NaotoMorita | 46:15988dc41923 | 191 | H(2,2) = 1.0f; |
NaotoMorita | 52:ab42c0497088 | 192 | //H(3,3) = 1.0f; |
osaka | 40:119792aa6d3b | 193 | |
NaotoMorita | 47:2467de40951f | 194 | //Matrix A = H*Phat*MatrixMath::Transpose(H)+R; |
NaotoMorita | 47:2467de40951f | 195 | //Matrix K = (Phat*MatrixMath::Transpose(H))*(MatrixMath::Inv(MatrixMath::Transpose(A)*A+1000.0f*MatrixMath::Eye(3))*MatrixMath::Transpose(A)); |
NaotoMorita | 47:2467de40951f | 196 | Matrix K = (Phat*MatrixMath::Transpose(H))*MatrixMath::Inv(H*Phat*MatrixMath::Transpose(H)+R); |
NaotoMorita | 52:ab42c0497088 | 197 | Matrix z(2,1); |
NaotoMorita | 52:ab42c0497088 | 198 | z << posgps(1,1) - pihat(1,1) << posgps(2,1)-pihat(2,1); //<< posgps(3,1) - pihat(3,1); |
NaotoMorita | 46:15988dc41923 | 199 | errState = K * z; |
NaotoMorita | 50:dadad0567349 | 200 | Phat = (MatrixMath::Eye(nState)-K*H)*Phat; |
NaotoMorita | 46:15988dc41923 | 201 | //Phat = Phat-K*(H*Phat*MatrixMath::Transpose(H)+R)*MatrixMath::Transpose(K); |
NaotoMorita | 50:dadad0567349 | 202 | //Phat = (MatrixMath::Eye(nState)-K*H)*Phat*MatrixMath::Transpose(MatrixMath::Eye(nState)-K*H)+K*R*MatrixMath::Transpose(K); |
NaotoMorita | 19:3fae66745363 | 203 | } |
NaotoMorita | 46:15988dc41923 | 204 | |
NaotoMorita | 46:15988dc41923 | 205 | void solaESKF::updateGPS(Matrix posgps,Matrix velgps,Matrix R) |
NaotoMorita | 46:15988dc41923 | 206 | { |
NaotoMorita | 46:15988dc41923 | 207 | |
NaotoMorita | 53:8b551358a7e3 | 208 | Matrix H(5,nState); |
NaotoMorita | 46:15988dc41923 | 209 | H(1,1) = 1.0f; |
NaotoMorita | 46:15988dc41923 | 210 | H(2,2) = 1.0f; |
NaotoMorita | 53:8b551358a7e3 | 211 | H(3,4) = 1.0f; |
NaotoMorita | 53:8b551358a7e3 | 212 | H(4,5) = 1.0f; |
NaotoMorita | 53:8b551358a7e3 | 213 | H(5,6) = 1.0f; |
NaotoMorita | 46:15988dc41923 | 214 | |
NaotoMorita | 53:8b551358a7e3 | 215 | //Matrix A = H*Phat*MatrixMath::Transpose(H)+R; |
NaotoMorita | 53:8b551358a7e3 | 216 | //Matrix K = (Phat*MatrixMath::Transpose(H))*(MatrixMath::Inv(MatrixMath::Transpose(A)*A+10.0f*MatrixMath::Eye(3))*MatrixMath::Transpose(A)); |
NaotoMorita | 53:8b551358a7e3 | 217 | Matrix K = (Phat*MatrixMath::Transpose(H))*MatrixMath::Inv(H*Phat*MatrixMath::Transpose(H)+R); |
NaotoMorita | 53:8b551358a7e3 | 218 | Matrix z(5,1); |
NaotoMorita | 53:8b551358a7e3 | 219 | z << posgps(1,1) - pihat(1,1) << posgps(2,1)-pihat(2,1) << velgps(1,1) - vihat(1,1) << velgps(2,1)-vihat(2,1) << velgps(3,1) - vihat(3,1); |
NaotoMorita | 46:15988dc41923 | 220 | errState = K * z; |
NaotoMorita | 53:8b551358a7e3 | 221 | Phat = (MatrixMath::Eye(nState)-K*H)*Phat; |
NaotoMorita | 46:15988dc41923 | 222 | //Phat = Phat-K*(H*Phat*MatrixMath::Transpose(H)+R)*MatrixMath::Transpose(K); |
NaotoMorita | 53:8b551358a7e3 | 223 | //Phat = (MatrixMath::Eye(nState)-K*H)*Phat*MatrixMath::Transpose(MatrixMath::Eye(nState)-K*H)+K*R*MatrixMath::Transpose(K); |
NaotoMorita | 46:15988dc41923 | 224 | } |
NaotoMorita | 46:15988dc41923 | 225 | |
NaotoMorita | 44:7d82e63b6a86 | 226 | Matrix solaESKF::computeAngles() |
NaotoMorita | 25:07ac5c6cd61c | 227 | { |
NaotoMorita | 25:07ac5c6cd61c | 228 | |
NaotoMorita | 44:7d82e63b6a86 | 229 | Matrix euler(3,1); |
NaotoMorita | 44:7d82e63b6a86 | 230 | euler(1,1) = atan2f(qhat(1,1)*qhat(2,1) + qhat(3,1)*qhat(4,1), 0.5f - qhat(2,1)*qhat(2,1) - qhat(3,1)*qhat(3,1)); |
NaotoMorita | 44:7d82e63b6a86 | 231 | euler(2,1) = asinf(-2.0f * (qhat(2,1)*qhat(4,1) - qhat(1,1)*qhat(3,1))); |
NaotoMorita | 44:7d82e63b6a86 | 232 | euler(3,1) = atan2f(qhat(2,1)*qhat(3,1) + qhat(1,1)*qhat(4,1), 0.5f - qhat(3,1)*qhat(3,1) - qhat(4,1)*qhat(4,1)); |
NaotoMorita | 44:7d82e63b6a86 | 233 | return euler; |
NaotoMorita | 19:3fae66745363 | 234 | } |
NaotoMorita | 19:3fae66745363 | 235 | |
NaotoMorita | 21:d6079def0473 | 236 | |
NaotoMorita | 44:7d82e63b6a86 | 237 | void solaESKF::fuseErr2Nominal() |
NaotoMorita | 19:3fae66745363 | 238 | { |
NaotoMorita | 44:7d82e63b6a86 | 239 | //position |
NaotoMorita | 44:7d82e63b6a86 | 240 | pihat(1,1) += errState(1,1); |
NaotoMorita | 44:7d82e63b6a86 | 241 | pihat(2,1) += errState(2,1); |
NaotoMorita | 44:7d82e63b6a86 | 242 | pihat(3,1) += errState(3,1); |
NaotoMorita | 44:7d82e63b6a86 | 243 | |
NaotoMorita | 44:7d82e63b6a86 | 244 | //velocity |
NaotoMorita | 44:7d82e63b6a86 | 245 | vihat(1,1) += errState(4,1); |
NaotoMorita | 44:7d82e63b6a86 | 246 | vihat(2,1) += errState(5,1); |
NaotoMorita | 44:7d82e63b6a86 | 247 | vihat(3,1) += errState(6,1); |
NaotoMorita | 44:7d82e63b6a86 | 248 | |
NaotoMorita | 44:7d82e63b6a86 | 249 | //angle error |
NaotoMorita | 19:3fae66745363 | 250 | Matrix qerr(4,1); |
NaotoMorita | 46:15988dc41923 | 251 | qerr << 1.0f << 0.5f*errState(7,1) << 0.5f*errState(8,1) << 0.5f*errState(9,1); |
NaotoMorita | 19:3fae66745363 | 252 | qhat = quatmultiply(qhat, qerr); |
NaotoMorita | 19:3fae66745363 | 253 | float qnorm = sqrt(MatrixMath::dot(MatrixMath::Transpose(qhat),qhat)); |
NaotoMorita | 19:3fae66745363 | 254 | qhat *= (1.0f/ qnorm); |
NaotoMorita | 23:1509648c2318 | 255 | |
NaotoMorita | 44:7d82e63b6a86 | 256 | //acc bias |
NaotoMorita | 44:7d82e63b6a86 | 257 | accBias(1,1) += errState(10,1); |
NaotoMorita | 44:7d82e63b6a86 | 258 | accBias(2,1) += errState(11,1); |
NaotoMorita | 44:7d82e63b6a86 | 259 | accBias(3,1) += errState(12,1); |
NaotoMorita | 44:7d82e63b6a86 | 260 | |
NaotoMorita | 44:7d82e63b6a86 | 261 | //gyro bias |
NaotoMorita | 44:7d82e63b6a86 | 262 | gyroBias(1,1) += errState(13,1); |
NaotoMorita | 44:7d82e63b6a86 | 263 | gyroBias(2,1) += errState(14,1); |
NaotoMorita | 44:7d82e63b6a86 | 264 | gyroBias(3,1) += errState(15,1); |
NaotoMorita | 44:7d82e63b6a86 | 265 | |
NaotoMorita | 44:7d82e63b6a86 | 266 | //gravity bias |
NaotoMorita | 44:7d82e63b6a86 | 267 | gravity(1,1) += errState(16,1); |
NaotoMorita | 44:7d82e63b6a86 | 268 | gravity(2,1) += errState(17,1); |
NaotoMorita | 44:7d82e63b6a86 | 269 | gravity(3,1) += errState(18,1); |
NaotoMorita | 47:2467de40951f | 270 | |
NaotoMorita | 47:2467de40951f | 271 | for (int i = 1; i < 19; i++){ |
NaotoMorita | 47:2467de40951f | 272 | errState(i,1) = 0.0f; |
NaotoMorita | 47:2467de40951f | 273 | } |
NaotoMorita | 44:7d82e63b6a86 | 274 | |
NaotoMorita | 19:3fae66745363 | 275 | } |
NaotoMorita | 19:3fae66745363 | 276 | |
NaotoMorita | 44:7d82e63b6a86 | 277 | Matrix solaESKF::quatmultiply(Matrix p, Matrix q) |
NaotoMorita | 19:3fae66745363 | 278 | { |
NaotoMorita | 44:7d82e63b6a86 | 279 | |
NaotoMorita | 19:3fae66745363 | 280 | Matrix qout(4,1); |
NaotoMorita | 44:7d82e63b6a86 | 281 | qout(1,1) = p(1,1)*q(1,1) - p(2,1)*q(2,1) - p(3,1)*q(3,1) - p(4,1)*q(4,1); |
NaotoMorita | 44:7d82e63b6a86 | 282 | qout(2,1) = p(1,1)*q(2,1) + p(2,1)*q(1,1) + p(3,1)*q(4,1) - p(4,1)*q(3,1); |
NaotoMorita | 44:7d82e63b6a86 | 283 | qout(3,1) = p(1,1)*q(3,1) - p(2,1)*q(4,1) + p(3,1)*q(1,1) + p(4,1)*q(2,1); |
NaotoMorita | 44:7d82e63b6a86 | 284 | qout(4,1) = p(1,1)*q(4,1) + p(2,1)*q(3,1) - p(3,1)*q(2,1) + p(4,1)*q(1,1); |
NaotoMorita | 19:3fae66745363 | 285 | float qnorm = sqrt(MatrixMath::dot(MatrixMath::Transpose(qout),qout)); |
NaotoMorita | 19:3fae66745363 | 286 | qout *= (1.0f/ qnorm); |
NaotoMorita | 19:3fae66745363 | 287 | return qout; |
NaotoMorita | 19:3fae66745363 | 288 | } |
NaotoMorita | 19:3fae66745363 | 289 | |
NaotoMorita | 44:7d82e63b6a86 | 290 | void solaESKF::computeDcm(Matrix& dcm, Matrix quat) |
NaotoMorita | 19:3fae66745363 | 291 | { |
NaotoMorita | 19:3fae66745363 | 292 | |
NaotoMorita | 44:7d82e63b6a86 | 293 | dcm(1,1) = quat(1,1)*quat(1,1) + quat(2,1)*quat(2,1) - quat(3,1)*quat(3,1) - quat(4,1)*quat(4,1); |
NaotoMorita | 44:7d82e63b6a86 | 294 | dcm(1,2) = 2.0f*(quat(2,1)*quat(3,1) - quat(1,1)*quat(4,1)); |
NaotoMorita | 44:7d82e63b6a86 | 295 | dcm(1,3) = 2.0f*(quat(2,1)*quat(4,1) + quat(1,1)*quat(3,1)); |
NaotoMorita | 44:7d82e63b6a86 | 296 | dcm(2,1) = 2.0f*(quat(2,1)*quat(3,1) + quat(1,1)*quat(4,1)); |
NaotoMorita | 44:7d82e63b6a86 | 297 | dcm(2,2) = quat(1,1)*quat(1,1) - quat(2,1)*quat(2,1) + quat(3,1)*quat(3,1) - quat(4,1)*quat(4,1); |
NaotoMorita | 44:7d82e63b6a86 | 298 | dcm(2,3) = 2.0f*(quat(3,1)*quat(4,1) - quat(1,1)*quat(2,1)); |
NaotoMorita | 44:7d82e63b6a86 | 299 | dcm(3,1) = 2.0f*(quat(2,1)*quat(4,1) - quat(1,1)*quat(3,1)); |
NaotoMorita | 44:7d82e63b6a86 | 300 | dcm(3,2) = 2.0f*(quat(3,1)*quat(4,1) + quat(1,1)*quat(2,1)); |
NaotoMorita | 44:7d82e63b6a86 | 301 | dcm(3,3) = quat(1,1)*quat(1,1) - quat(2,1)*quat(2,1) - quat(3,1)*quat(3,1) + quat(4,1)*quat(4,1); |
NaotoMorita | 19:3fae66745363 | 302 | |
NaotoMorita | 19:3fae66745363 | 303 | } |
NaotoMorita | 19:3fae66745363 | 304 | |
NaotoMorita | 44:7d82e63b6a86 | 305 | void solaESKF::setQhat(float ex,float ey,float ez) |
NaotoMorita | 44:7d82e63b6a86 | 306 | { |
NaotoMorita | 44:7d82e63b6a86 | 307 | float cos_z_2 = cosf(0.5f*ez); |
NaotoMorita | 44:7d82e63b6a86 | 308 | float cos_y_2 = cosf(0.5f*ey); |
NaotoMorita | 44:7d82e63b6a86 | 309 | float cos_x_2 = cosf(0.5f*ex); |
NaotoMorita | 19:3fae66745363 | 310 | |
NaotoMorita | 44:7d82e63b6a86 | 311 | float sin_z_2 = sinf(0.5f*ez); |
NaotoMorita | 44:7d82e63b6a86 | 312 | float sin_y_2 = sinf(0.5f*ey); |
NaotoMorita | 44:7d82e63b6a86 | 313 | float sin_x_2 = sinf(0.5f*ex); |
NaotoMorita | 19:3fae66745363 | 314 | |
NaotoMorita | 19:3fae66745363 | 315 | // and now compute quaternion |
NaotoMorita | 19:3fae66745363 | 316 | qhat(1,1) = cos_z_2*cos_y_2*cos_x_2 + sin_z_2*sin_y_2*sin_x_2; |
NaotoMorita | 19:3fae66745363 | 317 | qhat(2,1) = cos_z_2*cos_y_2*sin_x_2 - sin_z_2*sin_y_2*cos_x_2; |
NaotoMorita | 19:3fae66745363 | 318 | qhat(3,1) = cos_z_2*sin_y_2*cos_x_2 + sin_z_2*cos_y_2*sin_x_2; |
NaotoMorita | 19:3fae66745363 | 319 | qhat(4,1) = sin_z_2*cos_y_2*cos_x_2 - cos_z_2*sin_y_2*sin_x_2; |
NaotoMorita | 19:3fae66745363 | 320 | } |
NaotoMorita | 19:3fae66745363 | 321 | |
NaotoMorita | 47:2467de40951f | 322 | |
NaotoMorita | 47:2467de40951f | 323 | |
NaotoMorita | 44:7d82e63b6a86 | 324 | void solaESKF::setGravity(float gx,float gy,float gz) |
NaotoMorita | 23:1509648c2318 | 325 | { |
NaotoMorita | 44:7d82e63b6a86 | 326 | gravity(1,1) = gx; |
NaotoMorita | 44:7d82e63b6a86 | 327 | gravity(2,1) = gy; |
NaotoMorita | 44:7d82e63b6a86 | 328 | gravity(3,1) = gz; |
NaotoMorita | 23:1509648c2318 | 329 | } |
NaotoMorita | 47:2467de40951f | 330 | Matrix solaESKF::getPihat() |
NaotoMorita | 47:2467de40951f | 331 | { |
NaotoMorita | 47:2467de40951f | 332 | return pihat; |
NaotoMorita | 47:2467de40951f | 333 | } |
NaotoMorita | 47:2467de40951f | 334 | Matrix solaESKF::getVihat() |
NaotoMorita | 47:2467de40951f | 335 | { |
NaotoMorita | 47:2467de40951f | 336 | return vihat; |
NaotoMorita | 47:2467de40951f | 337 | } |
NaotoMorita | 47:2467de40951f | 338 | Matrix solaESKF::getQhat() |
NaotoMorita | 47:2467de40951f | 339 | { |
NaotoMorita | 47:2467de40951f | 340 | return qhat; |
NaotoMorita | 47:2467de40951f | 341 | } |
NaotoMorita | 47:2467de40951f | 342 | Matrix solaESKF::getAccBias() |
NaotoMorita | 47:2467de40951f | 343 | { |
NaotoMorita | 47:2467de40951f | 344 | return accBias; |
NaotoMorita | 47:2467de40951f | 345 | } |
NaotoMorita | 47:2467de40951f | 346 | Matrix solaESKF::getGyroBias() |
NaotoMorita | 47:2467de40951f | 347 | { |
NaotoMorita | 47:2467de40951f | 348 | return gyroBias; |
NaotoMorita | 47:2467de40951f | 349 | } |
NaotoMorita | 47:2467de40951f | 350 | Matrix solaESKF::getGravity() |
NaotoMorita | 47:2467de40951f | 351 | { |
NaotoMorita | 47:2467de40951f | 352 | return gravity; |
NaotoMorita | 47:2467de40951f | 353 | } |
NaotoMorita | 47:2467de40951f | 354 | Matrix solaESKF::getErrState() |
NaotoMorita | 47:2467de40951f | 355 | { |
NaotoMorita | 47:2467de40951f | 356 | return errState; |
NaotoMorita | 47:2467de40951f | 357 | } |
NaotoMorita | 23:1509648c2318 | 358 | |
NaotoMorita | 47:2467de40951f | 359 | void solaESKF::setPhatPosition(float val) |
NaotoMorita | 47:2467de40951f | 360 | { |
NaotoMorita | 47:2467de40951f | 361 | setBlockDiag(Phat,val,1,3); |
NaotoMorita | 47:2467de40951f | 362 | } |
NaotoMorita | 47:2467de40951f | 363 | void solaESKF::setPhatVelocity(float val) |
NaotoMorita | 47:2467de40951f | 364 | { |
NaotoMorita | 47:2467de40951f | 365 | setBlockDiag(Phat,val,4,6); |
NaotoMorita | 47:2467de40951f | 366 | } |
NaotoMorita | 47:2467de40951f | 367 | void solaESKF::setPhatAngleError(float val) |
NaotoMorita | 47:2467de40951f | 368 | { |
NaotoMorita | 47:2467de40951f | 369 | setBlockDiag(Phat,val,7,9); |
NaotoMorita | 47:2467de40951f | 370 | } |
NaotoMorita | 47:2467de40951f | 371 | void solaESKF::setPhatAccBias(float val) |
NaotoMorita | 47:2467de40951f | 372 | { |
NaotoMorita | 47:2467de40951f | 373 | setBlockDiag(Phat,val,10,12); |
NaotoMorita | 47:2467de40951f | 374 | } |
NaotoMorita | 47:2467de40951f | 375 | void solaESKF::setPhatGyroBias(float val) |
NaotoMorita | 47:2467de40951f | 376 | { |
NaotoMorita | 47:2467de40951f | 377 | setBlockDiag(Phat,val,13,15); |
NaotoMorita | 47:2467de40951f | 378 | } |
NaotoMorita | 47:2467de40951f | 379 | void solaESKF::setPhatGravity(float val) |
NaotoMorita | 47:2467de40951f | 380 | { |
NaotoMorita | 47:2467de40951f | 381 | setBlockDiag(Phat,val,16,18); |
NaotoMorita | 47:2467de40951f | 382 | } |
NaotoMorita | 47:2467de40951f | 383 | |
NaotoMorita | 47:2467de40951f | 384 | |
NaotoMorita | 47:2467de40951f | 385 | void solaESKF::setQVelocity(float val) |
NaotoMorita | 47:2467de40951f | 386 | { |
NaotoMorita | 47:2467de40951f | 387 | setBlockDiag(Q,val,4,6); |
NaotoMorita | 47:2467de40951f | 388 | } |
NaotoMorita | 47:2467de40951f | 389 | void solaESKF::setQAngleError(float val) |
NaotoMorita | 47:2467de40951f | 390 | { |
NaotoMorita | 47:2467de40951f | 391 | setBlockDiag(Q,val,7,9); |
NaotoMorita | 47:2467de40951f | 392 | } |
NaotoMorita | 47:2467de40951f | 393 | void solaESKF::setQAccBias(float val) |
NaotoMorita | 47:2467de40951f | 394 | { |
NaotoMorita | 47:2467de40951f | 395 | setBlockDiag(Q,val,10,12); |
NaotoMorita | 47:2467de40951f | 396 | } |
NaotoMorita | 47:2467de40951f | 397 | void solaESKF::setQGyroBias(float val) |
NaotoMorita | 47:2467de40951f | 398 | { |
NaotoMorita | 47:2467de40951f | 399 | setBlockDiag(Q,val,13,15); |
NaotoMorita | 47:2467de40951f | 400 | } |
NaotoMorita | 19:3fae66745363 | 401 | |
NaotoMorita | 19:3fae66745363 | 402 | |
NaotoMorita | 44:7d82e63b6a86 | 403 | void solaESKF::setDiag(Matrix& mat, float val){ |
NaotoMorita | 44:7d82e63b6a86 | 404 | for (int i = 1; i < mat.getCols()+1; i++){ |
NaotoMorita | 44:7d82e63b6a86 | 405 | mat(i,i) = val; |
NaotoMorita | 22:7d84b8bc20b4 | 406 | } |
NaotoMorita | 22:7d84b8bc20b4 | 407 | } |
NaotoMorita | 22:7d84b8bc20b4 | 408 | |
NaotoMorita | 45:df4618814803 | 409 | void solaESKF::setBlockDiag(Matrix& mat, float val,int startIndex, int endIndex){ |
NaotoMorita | 44:7d82e63b6a86 | 410 | for (int i = startIndex; i < endIndex+1; i++){ |
NaotoMorita | 44:7d82e63b6a86 | 411 | mat(i,i) = val; |
NaotoMorita | 22:7d84b8bc20b4 | 412 | } |
NaotoMorita | 22:7d84b8bc20b4 | 413 | } |
osaka | 51:a5af3b280d23 | 414 | |
osaka | 51:a5af3b280d23 | 415 | void solaESKF::setPihat(float pi_x, float pi_y, float pi_z) |
osaka | 51:a5af3b280d23 | 416 | { |
osaka | 51:a5af3b280d23 | 417 | pihat << pi_x << pi_y << pi_z; |
osaka | 51:a5af3b280d23 | 418 | } |