eive-obsw/mission/controller/acs/ActuatorCmd.cpp

78 lines
3.1 KiB
C++
Raw Normal View History

2022-09-27 11:06:11 +02:00
#include "ActuatorCmd.h"
#include <fsfw/globalfunctions/constants.h>
#include <fsfw/globalfunctions/math/MatrixOperations.h>
#include <fsfw/globalfunctions/math/QuaternionOperations.h>
#include <fsfw/globalfunctions/math/VectorOperations.h>
2022-09-19 15:03:25 +02:00
ActuatorCmd::ActuatorCmd() {}
2022-09-19 15:03:25 +02:00
ActuatorCmd::~ActuatorCmd() {}
2022-09-19 15:03:25 +02:00
2023-03-10 17:31:12 +01:00
void ActuatorCmd::scalingTorqueRws(double *rwTrq, double maxTorque) {
uint8_t maxIdx = 0;
VectorOperations<double>::maxAbsValue(rwTrq, 4, &maxIdx);
double maxValue = rwTrq[maxIdx];
2022-10-12 15:06:24 +02:00
2023-02-27 17:36:33 +01:00
if (maxValue > maxTorque) {
double scalingFactor = maxTorque / maxValue;
2023-03-10 17:31:12 +01:00
VectorOperations<double>::mulScalar(rwTrq, scalingFactor, rwTrq, 4);
2022-10-12 15:06:24 +02:00
}
}
2023-04-27 10:19:07 +02:00
void ActuatorCmd::cmdSpeedToRws(const int32_t speedRw0, const int32_t speedRw1,
const int32_t speedRw2, const int32_t speedRw3,
const double sampleTime, const double inertiaWheel,
const int32_t maxRwSpeed, const double *rwTorque,
int32_t *rwCmdSpeed) {
// group RW speed values (in 0.1 [RPM]) in vector
int32_t speedRws[4] = {speedRw0, speedRw1, speedRw2, speedRw3};
2023-04-27 10:19:07 +02:00
// calculate required RW speed as sum of current RW speed and RW speed delta
// delta w_rw = delta t / I_RW * torque_RW [rad/s]
2022-10-12 15:06:24 +02:00
double deltaSpeed[4] = {0, 0, 0, 0};
2023-04-27 10:19:07 +02:00
const double factor = sampleTime / inertiaWheel * RAD_PER_SEC_TO_RPM * 10;
VectorOperations<double>::mulScalar(rwTorque, factor, deltaSpeed, 4);
2023-04-27 10:19:07 +02:00
// convert double to int32
int32_t deltaSpeedInt[4] = {0, 0, 0, 0};
for (int i = 0; i < 4; i++) {
deltaSpeedInt[i] = std::round(deltaSpeed[i]);
}
2023-04-27 10:19:07 +02:00
// sum of current RW speed and RW speed delta
VectorOperations<int32_t>::add(speedRws, deltaSpeedInt, rwCmdSpeed, 4);
2023-04-27 10:19:07 +02:00
// crop values which would exceed the maximum possible RPM
for (uint8_t i = 0; i < 4; i++) {
if (rwCmdSpeed[i] > maxRwSpeed) {
rwCmdSpeed[i] = maxRwSpeed;
} else if (rwCmdSpeed[i] < -maxRwSpeed) {
rwCmdSpeed[i] = -maxRwSpeed;
}
2023-03-13 11:05:21 +01:00
}
2022-09-19 15:03:25 +02:00
}
2023-04-27 10:33:43 +02:00
void ActuatorCmd::cmdDipoleMtq(const double *inverseAlignment, const double maxDipole,
const double *dipoleMoment, int16_t *dipoleMomentActuator) {
2023-04-27 10:19:07 +02:00
// convert to actuator frame
2023-04-27 10:33:43 +02:00
double dipoleMomentActuatorDouble[3] = {0, 0, 0};
2023-06-01 09:36:50 +02:00
MatrixOperations<double>::multiply(inverseAlignment, dipoleMoment, dipoleMomentActuatorDouble, 3,
3, 1);
2023-04-27 10:19:07 +02:00
// scaling along largest element if dipole exceeds maximum
2023-03-07 13:53:46 +01:00
uint8_t maxIdx = 0;
2023-04-27 10:33:43 +02:00
VectorOperations<double>::maxAbsValue(dipoleMomentActuatorDouble, 3, &maxIdx);
double maxAbsValue = std::abs(dipoleMomentActuatorDouble[maxIdx]);
2023-04-27 10:33:43 +02:00
if (maxAbsValue > maxDipole) {
double scalingFactor = maxDipole / maxAbsValue;
VectorOperations<double>::mulScalar(dipoleMomentActuatorDouble, scalingFactor,
dipoleMomentActuatorDouble, 3);
}
// scale dipole from 1 Am^2 to 1e^-4 Am^2
2023-06-01 09:36:50 +02:00
VectorOperations<double>::mulScalar(dipoleMomentActuatorDouble, 1e4, dipoleMomentActuatorDouble,
3);
for (int i = 0; i < 3; i++) {
2023-04-27 10:33:43 +02:00
dipoleMomentActuator[i] = std::round(dipoleMomentActuatorDouble[i]);
}
2022-09-19 15:03:25 +02:00
}