2023-11-22 13:36:04 +01:00
|
|
|
#include "AttitudeEstimation.h"
|
|
|
|
|
|
|
|
AttitudeEstimation::AttitudeEstimation(AcsParameters *acsParameters_) {
|
|
|
|
acsParameters = acsParameters_;
|
|
|
|
}
|
|
|
|
|
|
|
|
AttitudeEstimation::~AttitudeEstimation() {}
|
|
|
|
|
2023-11-22 13:56:33 +01:00
|
|
|
void AttitudeEstimation::quest(acsctrl::SusDataProcessed *susData,
|
|
|
|
acsctrl::MgmDataProcessed *mgmData,
|
2023-12-13 10:31:29 +01:00
|
|
|
acsctrl::AttitudeEstimationData *attitudeEstimationData) {
|
2023-12-06 15:52:20 +01:00
|
|
|
if (not(susData->susVecTot.isValid() and susData->sunIjkModel.isValid() and
|
2023-12-13 10:12:29 +01:00
|
|
|
mgmData->mgmVecTot.isValid() and mgmData->magIgrfModel.isValid())) {
|
2023-12-06 15:52:20 +01:00
|
|
|
{
|
2023-12-13 10:31:29 +01:00
|
|
|
PoolReadGuard pg{attitudeEstimationData};
|
2023-12-06 15:52:20 +01:00
|
|
|
if (pg.getReadResult() == returnvalue::OK) {
|
2023-12-13 10:31:29 +01:00
|
|
|
std::memcpy(attitudeEstimationData->quatQuest.value, ZERO_VEC4, 4 * sizeof(double));
|
|
|
|
attitudeEstimationData->quatQuest.setValid(false);
|
2023-12-06 15:52:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-11-22 13:36:04 +01:00
|
|
|
|
2023-12-06 15:52:20 +01:00
|
|
|
// Normalize Data
|
|
|
|
double normMgmB[3] = {0, 0, 0}, normMgmI[3] = {0, 0, 0}, normSusB[3] = {0, 0, 0},
|
|
|
|
normSusI[3] = {0, 0, 0};
|
2023-12-12 16:01:30 +01:00
|
|
|
VectorOperations<double>::normalize(susData->susVecTot.value, normSusB, 3);
|
|
|
|
VectorOperations<double>::normalize(susData->sunIjkModel.value, normSusI, 3);
|
|
|
|
VectorOperations<double>::normalize(mgmData->mgmVecTot.value, normMgmB, 3);
|
|
|
|
VectorOperations<double>::normalize(mgmData->magIgrfModel.value, normMgmI, 3);
|
2023-11-22 13:36:04 +01:00
|
|
|
|
2023-12-06 15:52:20 +01:00
|
|
|
// Create Helper Vectors
|
|
|
|
double normHelperB[3] = {0, 0, 0}, normHelperI[3] = {0, 0, 0}, helperCross[3] = {0, 0, 0},
|
|
|
|
helperSum[3] = {0, 0, 0};
|
|
|
|
VectorOperations<double>::cross(normSusB, normMgmB, normHelperB);
|
|
|
|
VectorOperations<double>::cross(normSusI, normMgmI, normHelperI);
|
|
|
|
VectorOperations<double>::normalize(normHelperB, normHelperB, 3);
|
|
|
|
VectorOperations<double>::normalize(normHelperI, normHelperI, 3);
|
|
|
|
VectorOperations<double>::cross(normHelperB, normHelperI, helperCross);
|
|
|
|
VectorOperations<double>::add(normHelperB, normHelperI, helperSum, 3);
|
2023-11-22 13:36:04 +01:00
|
|
|
|
2023-12-06 15:52:20 +01:00
|
|
|
// Sensor Weights
|
|
|
|
double kSus = 0, kMgm = 0;
|
2024-02-12 14:43:34 +01:00
|
|
|
kSus = std::pow(acsParameters->kalmanFilterParameters.sensorNoiseSus, -2);
|
|
|
|
kMgm = std::pow(acsParameters->kalmanFilterParameters.sensorNoiseMgm, -2);
|
2023-11-22 13:36:04 +01:00
|
|
|
|
2023-12-06 15:52:20 +01:00
|
|
|
// Weighted Vectors
|
|
|
|
double weightedSusB[3] = {0, 0, 0}, weightedMgmB[3] = {0, 0, 0}, kSusVec[3] = {0, 0, 0},
|
|
|
|
kMgmVec[3] = {0, 0, 0}, kSumVec[3] = {0, 0, 0};
|
|
|
|
VectorOperations<double>::mulScalar(normSusB, kSus, weightedSusB, 3);
|
|
|
|
VectorOperations<double>::mulScalar(normMgmB, kMgm, weightedMgmB, 3);
|
|
|
|
VectorOperations<double>::cross(weightedSusB, normSusI, kSusVec);
|
|
|
|
VectorOperations<double>::cross(weightedMgmB, normMgmI, kMgmVec);
|
|
|
|
VectorOperations<double>::add(kSusVec, kMgmVec, kSumVec, 3);
|
2023-11-22 13:36:04 +01:00
|
|
|
|
2023-12-06 15:52:20 +01:00
|
|
|
// Some weird Angles
|
|
|
|
double alpha = (1 + VectorOperations<double>::dot(normHelperB, normHelperI)) *
|
|
|
|
(VectorOperations<double>::dot(weightedSusB, normSusI) +
|
|
|
|
VectorOperations<double>::dot(weightedMgmB, normMgmI)) +
|
|
|
|
VectorOperations<double>::dot(helperCross, kSumVec);
|
|
|
|
double beta = VectorOperations<double>::dot(helperSum, kSumVec);
|
|
|
|
double gamma = std::sqrt(std::pow(alpha, 2) + std::pow(beta, 2));
|
2023-11-22 13:36:04 +01:00
|
|
|
|
2023-12-06 15:52:20 +01:00
|
|
|
// I don't even know what this is supposed to be
|
|
|
|
double constPlus =
|
|
|
|
1. / (2 * std::sqrt(gamma * (gamma + alpha) *
|
|
|
|
(1 + VectorOperations<double>::dot(normHelperB, normHelperI))));
|
|
|
|
double constMinus =
|
|
|
|
1. / (2 * std::sqrt(gamma * (gamma - alpha) *
|
|
|
|
(1 + VectorOperations<double>::dot(normHelperB, normHelperI))));
|
2023-11-22 13:36:04 +01:00
|
|
|
|
2023-12-06 15:52:20 +01:00
|
|
|
// Calculate Quaternion
|
|
|
|
double qBI[4] = {0, 0, 0, 0}, qRotVecTot[3] = {0, 0, 0}, qRotVecPt0[3] = {0, 0, 0},
|
|
|
|
qRotVecPt1[3] = {0, 0, 0};
|
|
|
|
if (alpha >= 0) {
|
|
|
|
// Scalar Part
|
|
|
|
qBI[3] = (gamma + alpha) * (1 + VectorOperations<double>::dot(normHelperB, normHelperI));
|
|
|
|
// Rotational Vector Part
|
|
|
|
VectorOperations<double>::mulScalar(helperCross, gamma + alpha, qRotVecPt0, 3);
|
2023-12-13 16:52:42 +01:00
|
|
|
VectorOperations<double>::mulScalar(helperSum, beta, qRotVecPt1, 3);
|
2023-12-06 15:52:20 +01:00
|
|
|
VectorOperations<double>::add(qRotVecPt0, qRotVecPt1, qRotVecTot, 3);
|
|
|
|
std::memcpy(qBI, qRotVecTot, sizeof(qRotVecTot));
|
2023-11-22 13:36:04 +01:00
|
|
|
|
2023-12-13 16:52:42 +01:00
|
|
|
VectorOperations<double>::mulScalar(qBI, constPlus, qBI, 4);
|
2023-12-06 15:52:20 +01:00
|
|
|
QuaternionOperations::normalize(qBI, qBI);
|
|
|
|
} else {
|
|
|
|
// Scalar Part
|
|
|
|
qBI[3] = (beta) * (1 + VectorOperations<double>::dot(normHelperB, normHelperI));
|
|
|
|
// Rotational Vector Part
|
|
|
|
VectorOperations<double>::mulScalar(helperCross, beta, qRotVecPt0, 3);
|
2023-12-13 16:52:42 +01:00
|
|
|
VectorOperations<double>::mulScalar(helperSum, gamma - alpha, qRotVecPt1, 3);
|
2023-12-06 15:52:20 +01:00
|
|
|
VectorOperations<double>::add(qRotVecPt0, qRotVecPt1, qRotVecTot, 3);
|
|
|
|
std::memcpy(qBI, qRotVecTot, sizeof(qRotVecTot));
|
2023-11-27 10:52:30 +01:00
|
|
|
|
2023-12-13 16:52:42 +01:00
|
|
|
VectorOperations<double>::mulScalar(qBI, constMinus, qBI, 4);
|
2023-12-06 15:52:20 +01:00
|
|
|
QuaternionOperations::normalize(qBI, qBI);
|
2023-12-06 16:02:08 +01:00
|
|
|
}
|
|
|
|
// Low Pass
|
|
|
|
if (VectorOperations<double>::norm(qOld, 4) != 0.0) {
|
|
|
|
QuaternionOperations::slerp(qBI, qOld, acsParameters->onBoardParams.questFilterWeight, qBI);
|
2023-12-06 15:52:20 +01:00
|
|
|
}
|
|
|
|
{
|
2023-12-13 10:31:29 +01:00
|
|
|
PoolReadGuard pg{attitudeEstimationData};
|
2023-12-06 15:52:20 +01:00
|
|
|
if (pg.getReadResult() == returnvalue::OK) {
|
2023-12-13 10:31:29 +01:00
|
|
|
std::memcpy(attitudeEstimationData->quatQuest.value, qBI, 4 * sizeof(double));
|
|
|
|
attitudeEstimationData->quatQuest.setValid(true);
|
2023-11-23 11:50:26 +01:00
|
|
|
}
|
2023-11-22 13:36:04 +01:00
|
|
|
}
|
|
|
|
}
|