2022-09-27 11:06:11 +02:00
|
|
|
#include "ActuatorCmd.h"
|
2022-11-03 10:43:27 +01:00
|
|
|
|
2022-09-20 15:06:05 +02:00
|
|
|
#include <fsfw/globalfunctions/constants.h>
|
|
|
|
#include <fsfw/globalfunctions/math/MatrixOperations.h>
|
|
|
|
#include <fsfw/globalfunctions/math/QuaternionOperations.h>
|
2022-11-03 10:43:27 +01:00
|
|
|
#include <fsfw/globalfunctions/math/VectorOperations.h>
|
2022-09-19 15:03:25 +02:00
|
|
|
|
2022-11-03 10:43:27 +01:00
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
#include "util/CholeskyDecomposition.h"
|
|
|
|
#include "util/MathOperations.h"
|
2022-09-19 15:03:25 +02:00
|
|
|
|
2022-11-03 10:43:27 +01:00
|
|
|
ActuatorCmd::ActuatorCmd(AcsParameters *acsParameters_) { acsParameters = *acsParameters_; }
|
2022-09-19 15:03:25 +02:00
|
|
|
|
2022-11-03 10:43:27 +01:00
|
|
|
ActuatorCmd::~ActuatorCmd() {}
|
2022-09-19 15:03:25 +02:00
|
|
|
|
2022-11-08 13:48:50 +01:00
|
|
|
void ActuatorCmd::scalingTorqueRws(const double *rwTrq, double *rwTrqScaled) {
|
2022-10-12 15:06:24 +02:00
|
|
|
// Scaling the commanded torque to a maximum value
|
|
|
|
double maxTrq = acsParameters.rwHandlingParameters.maxTrq;
|
|
|
|
|
|
|
|
double maxValue = 0;
|
|
|
|
for (int i = 0; i < 4; i++) { // size of torque, always 4 ?
|
2022-11-08 13:48:50 +01:00
|
|
|
if (abs(rwTrq[i]) > maxValue) {
|
|
|
|
maxValue = abs(rwTrq[i]);
|
2022-10-12 15:06:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (maxValue > maxTrq) {
|
2022-12-13 11:51:03 +01:00
|
|
|
double scalingFactor = maxTrq / maxValue;
|
|
|
|
VectorOperations<double>::mulScalar(rwTrq, scalingFactor, rwTrqScaled, 4);
|
2022-10-12 15:06:24 +02:00
|
|
|
}
|
2022-11-08 13:48:50 +01:00
|
|
|
}
|
|
|
|
|
2023-02-10 10:56:50 +01:00
|
|
|
void ActuatorCmd::cmdSpeedToRws(const int32_t speedRw0, const int32_t speedRw1,
|
|
|
|
const int32_t speedRw2, const int32_t speedRw3,
|
|
|
|
const double *rwTorque, int32_t *rwCmdSpeed) {
|
2022-11-08 13:48:50 +01:00
|
|
|
using namespace Math;
|
2022-10-12 15:06:24 +02:00
|
|
|
|
|
|
|
// Calculating the commanded speed in RPM for every reaction wheel
|
2023-02-10 10:56:50 +01:00
|
|
|
int32_t speedRws[4] = {speedRw0, speedRw1, speedRw2, speedRw3};
|
2022-10-12 15:06:24 +02:00
|
|
|
double deltaSpeed[4] = {0, 0, 0, 0};
|
|
|
|
double commandTime = acsParameters.onBoardParams.sampleTime,
|
|
|
|
inertiaWheel = acsParameters.rwHandlingParameters.inertiaWheel;
|
|
|
|
double radToRpm = 60 / (2 * PI); // factor for conversion to RPM
|
|
|
|
// W_RW = Torque_RW / I_RW * delta t [rad/s]
|
|
|
|
double factor = commandTime / inertiaWheel * radToRpm;
|
2023-02-10 10:56:50 +01:00
|
|
|
int32_t deltaSpeedInt[4] = {0, 0, 0, 0};
|
2022-11-08 13:48:50 +01:00
|
|
|
VectorOperations<double>::mulScalar(rwTorque, factor, deltaSpeed, 4);
|
2023-02-10 10:56:50 +01:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
deltaSpeedInt[i] = std::round(deltaSpeed[i]);
|
|
|
|
}
|
|
|
|
VectorOperations<int32_t>::add(speedRws, deltaSpeedInt, rwCmdSpeed, 4);
|
2023-02-13 10:58:12 +01:00
|
|
|
VectorOperations<int32_t>::mulScalar(rwCmdSpeed, 10, rwCmdSpeed, 4);
|
2022-09-19 15:03:25 +02:00
|
|
|
}
|
|
|
|
|
2023-02-10 10:56:50 +01:00
|
|
|
void ActuatorCmd::cmdDipolMtq(const double *dipolMoment, int16_t *dipolMomentActuator) {
|
2023-02-02 10:47:58 +01:00
|
|
|
// Convert to actuator frame
|
2023-02-10 10:56:50 +01:00
|
|
|
double dipolMomentActuatorDouble[3] = {0, 0, 0};
|
2022-11-03 10:43:27 +01:00
|
|
|
MatrixOperations<double>::multiply(*acsParameters.magnetorquesParameter.inverseAlignment,
|
2023-02-10 10:56:50 +01:00
|
|
|
dipolMoment, dipolMomentActuatorDouble, 3, 3, 1);
|
2023-02-02 10:47:58 +01:00
|
|
|
// Scaling along largest element if dipol exceeds maximum
|
2022-11-03 10:43:27 +01:00
|
|
|
double maxDipol = acsParameters.magnetorquesParameter.DipolMax;
|
|
|
|
double maxValue = 0;
|
|
|
|
for (int i = 0; i < 3; i++) {
|
2023-02-02 10:47:58 +01:00
|
|
|
if (abs(dipolMomentActuator[i]) > maxDipol) {
|
|
|
|
maxValue = abs(dipolMomentActuator[i]);
|
2022-11-03 10:43:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (maxValue > maxDipol) {
|
|
|
|
double scalingFactor = maxDipol / maxValue;
|
2023-02-10 10:56:50 +01:00
|
|
|
VectorOperations<double>::mulScalar(dipolMomentActuatorDouble, scalingFactor,
|
|
|
|
dipolMomentActuatorDouble, 3);
|
2022-11-03 10:43:27 +01:00
|
|
|
}
|
2023-02-02 10:47:58 +01:00
|
|
|
// scale dipole from 1 Am^2 to 1e^-4 Am^2
|
2023-02-10 10:56:50 +01:00
|
|
|
VectorOperations<double>::mulScalar(dipolMomentActuatorDouble, 1e4, dipolMomentActuatorDouble, 3);
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
dipolMomentActuator[i] = std::round(dipolMomentActuatorDouble[i]);
|
|
|
|
}
|
2022-09-19 15:03:25 +02:00
|
|
|
}
|