2022-09-20 13:46:42 +02:00
|
|
|
#include "SafeCtrl.h"
|
2022-10-20 11:07:45 +02:00
|
|
|
|
2022-09-27 11:06:11 +02:00
|
|
|
#include <fsfw/globalfunctions/constants.h>
|
|
|
|
#include <fsfw/globalfunctions/math/MatrixOperations.h>
|
|
|
|
#include <fsfw/globalfunctions/math/QuaternionOperations.h>
|
|
|
|
#include <fsfw/globalfunctions/math/VectorOperations.h>
|
2023-03-21 17:19:27 +01:00
|
|
|
#include <fsfw/globalfunctions/sign.h>
|
2022-10-20 11:07:45 +02:00
|
|
|
#include <math.h>
|
2022-09-27 11:06:11 +02:00
|
|
|
|
2022-10-20 11:07:45 +02:00
|
|
|
#include "../util/MathOperations.h"
|
2022-09-20 13:46:42 +02:00
|
|
|
|
2023-02-28 09:18:44 +01:00
|
|
|
SafeCtrl::SafeCtrl(AcsParameters *acsParameters_) { acsParameters = acsParameters_; }
|
2022-09-20 13:46:42 +02:00
|
|
|
|
2022-10-20 11:07:45 +02:00
|
|
|
SafeCtrl::~SafeCtrl() {}
|
2022-09-20 13:46:42 +02:00
|
|
|
|
2023-03-24 11:35:46 +01:00
|
|
|
ReturnValue_t SafeCtrl::safeCtrlStrategy(const bool magFieldValid, const ReturnValue_t mekfValid,
|
|
|
|
const bool satRotRateValid, const bool sunDirValid) {
|
|
|
|
if (not magFieldValid) {
|
|
|
|
return SAFECTRL_NO_MAG_FIELD_FOR_CONTROL;
|
|
|
|
} else if (mekfValid) {
|
|
|
|
return SAFECTRL_USE_MEKF;
|
|
|
|
} else if (satRotRateValid and sunDirValid) {
|
|
|
|
return SAFECTRL_USE_NONMEKF;
|
|
|
|
} else if (satRotRateValid and not sunDirValid) {
|
|
|
|
return SAFECTRL_USE_DAMPING;
|
|
|
|
} else {
|
|
|
|
return SAFECTRL_NO_SENSORS_FOR_CONTROL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-24 14:51:33 +01:00
|
|
|
void SafeCtrl::safeMekf(const double *magFieldB, const double *satRotRateB,
|
|
|
|
const double *sunDirModelI, const double *quatBI, const double *sunDirRefB,
|
|
|
|
const double *satRotRateRefB, double *magMomB, double &errorAngle) {
|
|
|
|
// convert magFieldB from uT to T
|
|
|
|
double magFieldBT[3] = {0, 0, 0};
|
|
|
|
VectorOperations<double>::mulScalar(magFieldB, 1e-6, magFieldBT, 3);
|
2022-10-20 11:07:45 +02:00
|
|
|
|
2023-03-24 14:51:33 +01:00
|
|
|
// convert sunDirModel to body rf
|
|
|
|
double sunDirB[3] = {0, 0, 0};
|
|
|
|
QuaternionOperations::multiplyVector(quatBI, sunDirModelI, sunDirB);
|
2022-10-20 11:07:45 +02:00
|
|
|
|
2023-03-24 14:51:33 +01:00
|
|
|
// calculate angle alpha between sunDirRef and sunDir
|
|
|
|
double dotSun = VectorOperations<double>::dot(sunDirRefB, sunDirB);
|
|
|
|
errorAngle = acos(dotSun);
|
2022-10-20 11:07:45 +02:00
|
|
|
|
2023-03-24 14:51:33 +01:00
|
|
|
// split rotational rate into parallel and orthogonal parts
|
|
|
|
double satRotRateParallelB[3] = {0, 0, 0}, satRotRateOrthogonalB[3] = {0, 0, 0};
|
|
|
|
double parallelLength = VectorOperations<double>::dot(satRotRateB, sunDirB) *
|
|
|
|
pow(VectorOperations<double>::norm(sunDirB, 3), -2);
|
|
|
|
VectorOperations<double>::mulScalar(sunDirB, parallelLength, satRotRateParallelB, 3);
|
|
|
|
VectorOperations<double>::subtract(satRotRateB, satRotRateParallelB, satRotRateOrthogonalB, 3);
|
2022-10-20 11:07:45 +02:00
|
|
|
|
2023-03-24 14:51:33 +01:00
|
|
|
// calculate torque for parallel rotational rate
|
|
|
|
double cmdParallel[3] = {0, 0, 0};
|
|
|
|
if (errorAngle < (double)acsParameters->safeModeControllerParameters.angleStartSpin) {
|
|
|
|
VectorOperations<double>::subtract(satRotRateRefB, satRotRateParallelB, cmdParallel, 3);
|
|
|
|
VectorOperations<double>::mulScalar(
|
|
|
|
cmdParallel, acsParameters->safeModeControllerParameters.k_parallelMekf, cmdParallel, 3);
|
|
|
|
}
|
2022-10-20 11:07:45 +02:00
|
|
|
|
2023-03-24 14:51:33 +01:00
|
|
|
// calculate torque for orthogonal rotational rate
|
|
|
|
double cmdOrtho[3] = {0, 0, 0};
|
|
|
|
VectorOperations<double>::mulScalar(satRotRateOrthogonalB,
|
|
|
|
-acsParameters->safeModeControllerParameters.k_orthoMekf,
|
|
|
|
cmdOrtho, 3);
|
|
|
|
// calculate torque for alignment
|
|
|
|
double cmdAlign[3] = {0, 0, 0}, crossAlign[3] = {0, 0, 0},
|
|
|
|
alignFactor[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
|
|
|
|
MatrixOperations<double>::multiplyScalar(*acsParameters->inertiaEIVE.inertiaMatrix,
|
|
|
|
acsParameters->safeModeControllerParameters.k_alignMekf,
|
|
|
|
*alignFactor, 3, 3);
|
|
|
|
VectorOperations<double>::cross(sunDirRefB, sunDirB, crossAlign);
|
|
|
|
MatrixOperations<double>::multiply(*alignFactor, crossAlign, cmdAlign, 3, 3, 1);
|
2022-10-20 11:07:45 +02:00
|
|
|
|
2023-03-24 14:51:33 +01:00
|
|
|
// sum of all torques
|
|
|
|
double cmdTorque[3] = {0, 0, 0};
|
|
|
|
for (uint8_t i = 0; i < 3; i++) {
|
|
|
|
cmdTorque[i] = cmdAlign[i] + cmdOrtho[i] + cmdParallel[i];
|
|
|
|
}
|
2022-10-20 11:07:45 +02:00
|
|
|
|
2023-03-24 14:51:33 +01:00
|
|
|
// calculate magnetic moment to command
|
2022-10-20 11:07:45 +02:00
|
|
|
double torqueMgt[3] = {0, 0, 0};
|
2023-03-24 14:51:33 +01:00
|
|
|
VectorOperations<double>::cross(magFieldBT, cmdTorque, torqueMgt);
|
2022-10-20 11:07:45 +02:00
|
|
|
double normMag = VectorOperations<double>::norm(magFieldB, 3);
|
2023-03-24 14:51:33 +01:00
|
|
|
VectorOperations<double>::mulScalar(torqueMgt, pow(normMag, -2), magMomB, 3);
|
2022-09-20 13:46:42 +02:00
|
|
|
}
|
|
|
|
|
2023-03-24 14:51:33 +01:00
|
|
|
void SafeCtrl::safeNonMekf(const double *magFieldB, const double *satRotRateB,
|
|
|
|
const double *sunDirB, const double *sunDirRefB,
|
|
|
|
const double *satRotRateRefB, double *magMomB, double &errorAngle) {
|
2023-03-24 11:35:46 +01:00
|
|
|
// convert magFieldB from uT to T
|
2023-03-22 08:59:29 +01:00
|
|
|
double magFieldBT[3] = {0, 0, 0};
|
|
|
|
VectorOperations<double>::mulScalar(magFieldB, 1e-6, magFieldBT, 3);
|
2023-03-21 17:19:27 +01:00
|
|
|
|
2023-03-24 11:35:46 +01:00
|
|
|
// calculate angle alpha between sunDirRef and sunDir
|
|
|
|
double dotSun = VectorOperations<double>::dot(sunDirRefB, sunDirB);
|
|
|
|
errorAngle = acos(dotSun);
|
|
|
|
|
|
|
|
// split rotational rate into parallel and orthogonal parts
|
|
|
|
double satRotRateParallelB[3] = {0, 0, 0}, satRotRateOrthogonalB[3] = {0, 0, 0};
|
|
|
|
double parallelLength = VectorOperations<double>::dot(satRotRateB, sunDirB) *
|
|
|
|
pow(VectorOperations<double>::norm(sunDirB, 3), -2);
|
|
|
|
VectorOperations<double>::mulScalar(sunDirB, parallelLength, satRotRateParallelB, 3);
|
|
|
|
VectorOperations<double>::subtract(satRotRateB, satRotRateParallelB, satRotRateOrthogonalB, 3);
|
|
|
|
|
|
|
|
// calculate torque for parallel rotational rate
|
|
|
|
double cmdParallel[3] = {0, 0, 0};
|
|
|
|
if (errorAngle < (double)acsParameters->safeModeControllerParameters.angleStartSpin) {
|
|
|
|
VectorOperations<double>::subtract(satRotRateRefB, satRotRateParallelB, cmdParallel, 3);
|
2023-03-21 17:19:27 +01:00
|
|
|
VectorOperations<double>::mulScalar(
|
2023-03-24 14:51:33 +01:00
|
|
|
cmdParallel, acsParameters->safeModeControllerParameters.k_parallelMekf, cmdParallel, 3);
|
2022-10-20 11:07:45 +02:00
|
|
|
}
|
|
|
|
|
2023-03-24 11:35:46 +01:00
|
|
|
// calculate torque for orthogonal rotational rate
|
|
|
|
double cmdOrtho[3] = {0, 0, 0};
|
|
|
|
VectorOperations<double>::mulScalar(satRotRateOrthogonalB,
|
2023-03-24 14:51:33 +01:00
|
|
|
-acsParameters->safeModeControllerParameters.k_orthoMekf,
|
2023-03-24 11:35:46 +01:00
|
|
|
cmdOrtho, 3);
|
|
|
|
|
|
|
|
// calculate torque for alignment
|
|
|
|
double cmdAlign[3] = {0, 0, 0}, crossAlign[3] = {0, 0, 0},
|
|
|
|
alignFactor[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
|
|
|
|
MatrixOperations<double>::multiplyScalar(*acsParameters->inertiaEIVE.inertiaMatrix,
|
2023-03-24 14:51:33 +01:00
|
|
|
acsParameters->safeModeControllerParameters.k_alignMekf,
|
2023-03-24 11:35:46 +01:00
|
|
|
*alignFactor, 3, 3);
|
|
|
|
VectorOperations<double>::cross(sunDirRefB, sunDirB, crossAlign);
|
2023-03-24 14:51:33 +01:00
|
|
|
MatrixOperations<double>::multiply(*alignFactor, crossAlign, cmdAlign, 3, 3, 1);
|
2023-03-24 11:35:46 +01:00
|
|
|
|
|
|
|
// sum of all torques
|
|
|
|
double cmdTorque[3] = {0, 0, 0};
|
2023-03-21 17:19:27 +01:00
|
|
|
for (uint8_t i = 0; i < 3; i++) {
|
2023-03-24 11:35:46 +01:00
|
|
|
cmdTorque[i] = cmdAlign[i] + cmdOrtho[i] + cmdParallel[i];
|
2023-03-21 17:19:27 +01:00
|
|
|
}
|
2023-03-22 08:59:29 +01:00
|
|
|
|
2023-03-24 14:51:33 +01:00
|
|
|
// calculate magnetic moment to command
|
2023-03-22 08:59:29 +01:00
|
|
|
double torqueMgt[3] = {0, 0, 0};
|
2023-03-24 11:35:46 +01:00
|
|
|
VectorOperations<double>::cross(magFieldBT, cmdTorque, torqueMgt);
|
2023-03-22 08:59:29 +01:00
|
|
|
double normMag = VectorOperations<double>::norm(magFieldB, 3);
|
2023-03-24 11:35:46 +01:00
|
|
|
VectorOperations<double>::mulScalar(torqueMgt, pow(normMag, -2), magMomB, 3);
|
2023-03-21 17:19:27 +01:00
|
|
|
}
|
2023-03-24 14:51:33 +01:00
|
|
|
|
|
|
|
void SafeCtrl::safeRateDamping(const double *magFieldB, const double *satRotRateB,
|
|
|
|
const double *satRotRateRefB, double *magMomB, double &errorAngle) {
|
|
|
|
// convert magFieldB from uT to T
|
|
|
|
double magFieldBT[3] = {0, 0, 0};
|
|
|
|
VectorOperations<double>::mulScalar(magFieldB, 1e-6, magFieldBT, 3);
|
|
|
|
|
|
|
|
// calculate torque for rate damping
|
|
|
|
double cmdTorque[3] = {0, 0, 0}, diffSatRotRate[3] = {0, 0, 0};
|
|
|
|
VectorOperations<double>::subtract(satRotRateRefB, satRotRateB, diffSatRotRate, 3);
|
|
|
|
VectorOperations<double>::mulScalar(
|
|
|
|
satRotRateB, acsParameters->safeModeControllerParameters.k_rateDamping, cmdTorque, 3);
|
|
|
|
|
|
|
|
// calculate magnetic moment to command
|
|
|
|
double torqueMgt[3] = {0, 0, 0};
|
|
|
|
VectorOperations<double>::cross(magFieldBT, cmdTorque, torqueMgt);
|
|
|
|
double normMag = VectorOperations<double>::norm(magFieldB, 3);
|
|
|
|
VectorOperations<double>::mulScalar(torqueMgt, pow(normMag, -2), magMomB, 3);
|
|
|
|
|
|
|
|
errorAngle = NAN;
|
|
|
|
}
|