fsfw/misc/archive/GyroL3GD20Handler.cpp

282 lines
9.5 KiB
C++
Raw Permalink Normal View History

#include "GyroL3GD20Handler.h"
2021-07-13 19:19:25 +02:00
#include <cmath>
2022-02-02 10:29:30 +01:00
#include "fsfw/datapool/PoolReadGuard.h"
2021-07-13 19:19:25 +02:00
GyroHandlerL3GD20H::GyroHandlerL3GD20H(object_id_t objectId, object_id_t deviceCommunication,
2022-02-02 10:29:30 +01:00
CookieIF *comCookie, uint32_t transitionDelayMs)
: DeviceHandlerBase(objectId, deviceCommunication, comCookie),
transitionDelayMs(transitionDelayMs),
sharedPool(DeviceHandlerBase::getObjectId()),
dataset(sharedPool) {}
2021-07-13 19:19:25 +02:00
GyroHandlerL3GD20H::~GyroHandlerL3GD20H() {}
void GyroHandlerL3GD20H::doStartUp() {
2022-02-02 10:29:30 +01:00
if (internalState == InternalState::NONE) {
internalState = InternalState::CONFIGURE;
}
if (internalState == InternalState::CONFIGURE) {
if (commandExecuted) {
internalState = InternalState::CHECK_REGS;
commandExecuted = false;
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
}
if (internalState == InternalState::CHECK_REGS) {
if (commandExecuted) {
internalState = InternalState::NORMAL;
if (goNormalModeImmediately) {
setMode(MODE_NORMAL);
} else {
setMode(_MODE_TO_ON);
}
commandExecuted = false;
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
}
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
void GyroHandlerL3GD20H::doShutDown() { setMode(_MODE_POWER_DOWN); }
2021-07-13 19:19:25 +02:00
ReturnValue_t GyroHandlerL3GD20H::buildTransitionDeviceCommand(DeviceCommandId_t *id) {
2022-02-02 10:29:30 +01:00
switch (internalState) {
case (InternalState::NONE):
case (InternalState::NORMAL): {
return NOTHING_TO_SEND;
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
case (InternalState::CONFIGURE): {
2023-02-26 14:54:35 +01:00
*id = l3gd20h::CONFIGURE_CTRL_REGS;
2022-02-02 10:29:30 +01:00
uint8_t command[5];
2023-02-26 14:54:35 +01:00
command[0] = l3gd20h::CTRL_REG_1_VAL;
command[1] = l3gd20h::CTRL_REG_2_VAL;
command[2] = l3gd20h::CTRL_REG_3_VAL;
command[3] = l3gd20h::CTRL_REG_4_VAL;
command[4] = l3gd20h::CTRL_REG_5_VAL;
2022-02-02 10:29:30 +01:00
return buildCommandFromCommand(*id, command, 5);
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
case (InternalState::CHECK_REGS): {
2023-02-26 14:54:35 +01:00
*id = l3gd20h::READ_REGS;
2022-02-02 10:29:30 +01:00
return buildCommandFromCommand(*id, nullptr, 0);
2021-07-13 19:19:25 +02:00
}
default:
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
/* Might be a configuration error. */
sif::warning << "GyroL3GD20Handler::buildTransitionDeviceCommand: "
"Unknown internal state!"
<< std::endl;
2021-07-13 19:19:25 +02:00
#else
2022-02-02 10:29:30 +01:00
sif::printDebug(
"GyroL3GD20Handler::buildTransitionDeviceCommand: "
"Unknown internal state!\n");
2021-07-13 19:19:25 +02:00
#endif
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2022-02-02 10:29:30 +01:00
}
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2021-07-13 19:19:25 +02:00
}
ReturnValue_t GyroHandlerL3GD20H::buildNormalDeviceCommand(DeviceCommandId_t *id) {
2023-02-26 14:54:35 +01:00
*id = l3gd20h::READ_REGS;
2022-02-02 10:29:30 +01:00
return buildCommandFromCommand(*id, nullptr, 0);
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
ReturnValue_t GyroHandlerL3GD20H::buildCommandFromCommand(DeviceCommandId_t deviceCommand,
const uint8_t *commandData,
size_t commandDataLen) {
switch (deviceCommand) {
2023-02-26 14:54:35 +01:00
case (l3gd20h::READ_REGS): {
commandBuffer[0] = l3gd20h::READ_START | l3gd20h::AUTO_INCREMENT_MASK | l3gd20h::READ_MASK;
std::memset(commandBuffer + 1, 0, l3gd20h::READ_LEN);
2022-02-02 10:29:30 +01:00
rawPacket = commandBuffer;
2023-02-26 14:54:35 +01:00
rawPacketLen = l3gd20h::READ_LEN + 1;
2022-02-02 10:29:30 +01:00
break;
2021-07-13 19:19:25 +02:00
}
2023-02-26 14:54:35 +01:00
case (l3gd20h::CONFIGURE_CTRL_REGS): {
commandBuffer[0] = l3gd20h::CTRL_REG_1 | l3gd20h::AUTO_INCREMENT_MASK;
2022-02-02 10:29:30 +01:00
if (commandData == nullptr or commandDataLen != 5) {
return DeviceHandlerIF::INVALID_COMMAND_PARAMETER;
}
ctrlReg1Value = commandData[0];
ctrlReg2Value = commandData[1];
ctrlReg3Value = commandData[2];
ctrlReg4Value = commandData[3];
ctrlReg5Value = commandData[4];
2023-02-26 14:54:35 +01:00
bool fsH = ctrlReg4Value & l3gd20h::SET_FS_1;
bool fsL = ctrlReg4Value & l3gd20h::SET_FS_0;
2022-02-02 10:29:30 +01:00
if (not fsH and not fsL) {
2023-02-26 14:54:35 +01:00
sensitivity = l3gd20h::SENSITIVITY_00;
2022-02-02 10:29:30 +01:00
} else if (not fsH and fsL) {
2023-02-26 14:54:35 +01:00
sensitivity = l3gd20h::SENSITIVITY_01;
2022-02-02 10:29:30 +01:00
} else {
2023-02-26 14:54:35 +01:00
sensitivity = l3gd20h::SENSITIVITY_11;
2022-02-02 10:29:30 +01:00
}
commandBuffer[1] = ctrlReg1Value;
commandBuffer[2] = ctrlReg2Value;
commandBuffer[3] = ctrlReg3Value;
commandBuffer[4] = ctrlReg4Value;
commandBuffer[5] = ctrlReg5Value;
rawPacket = commandBuffer;
rawPacketLen = 6;
break;
2021-07-13 19:19:25 +02:00
}
2023-02-26 14:54:35 +01:00
case (l3gd20h::READ_CTRL_REGS): {
commandBuffer[0] = l3gd20h::READ_START | l3gd20h::AUTO_INCREMENT_MASK | l3gd20h::READ_MASK;
2022-02-02 10:29:30 +01:00
std::memset(commandBuffer + 1, 0, 5);
rawPacket = commandBuffer;
rawPacketLen = 6;
break;
2021-07-13 19:19:25 +02:00
}
default:
2022-02-02 10:29:30 +01:00
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
}
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2021-07-13 19:19:25 +02:00
}
ReturnValue_t GyroHandlerL3GD20H::scanForReply(const uint8_t *start, size_t len,
2022-02-02 10:29:30 +01:00
DeviceCommandId_t *foundId, size_t *foundLen) {
// For SPI, the ID will always be the one of the last sent command
*foundId = this->getPendingCommand();
*foundLen = this->rawPacketLen;
2021-07-13 19:19:25 +02:00
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2021-07-13 19:19:25 +02:00
}
ReturnValue_t GyroHandlerL3GD20H::interpretDeviceReply(DeviceCommandId_t id,
2022-02-02 10:29:30 +01:00
const uint8_t *packet) {
2022-08-16 01:08:26 +02:00
ReturnValue_t result = returnvalue::OK;
2022-02-02 10:29:30 +01:00
switch (id) {
2023-02-26 14:54:35 +01:00
case (l3gd20h::CONFIGURE_CTRL_REGS): {
2022-02-02 10:29:30 +01:00
commandExecuted = true;
break;
2021-07-13 19:19:25 +02:00
}
2023-02-26 14:54:35 +01:00
case (l3gd20h::READ_CTRL_REGS): {
2022-02-02 10:29:30 +01:00
if (packet[1] == ctrlReg1Value and packet[2] == ctrlReg2Value and
packet[3] == ctrlReg3Value and packet[4] == ctrlReg4Value and
packet[5] == ctrlReg5Value) {
commandExecuted = true;
} else {
// Attempt reconfiguration
internalState = InternalState::CONFIGURE;
return DeviceHandlerIF::DEVICE_REPLY_INVALID;
}
break;
2021-07-13 19:19:25 +02:00
}
2023-02-26 14:54:35 +01:00
case (l3gd20h::READ_REGS): {
2022-02-02 10:29:30 +01:00
if (packet[1] != ctrlReg1Value and packet[2] != ctrlReg2Value and
packet[3] != ctrlReg3Value and packet[4] != ctrlReg4Value and
packet[5] != ctrlReg5Value) {
return DeviceHandlerIF::DEVICE_REPLY_INVALID;
} else {
if (internalState == InternalState::CHECK_REGS) {
commandExecuted = true;
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
}
2021-07-13 19:19:25 +02:00
2023-02-26 14:54:35 +01:00
statusReg = packet[l3gd20h::STATUS_IDX];
2021-07-13 19:19:25 +02:00
2023-02-26 14:54:35 +01:00
int16_t angVelocXRaw = packet[l3gd20h::OUT_X_H] << 8 | packet[l3gd20h::OUT_X_L];
int16_t angVelocYRaw = packet[l3gd20h::OUT_Y_H] << 8 | packet[l3gd20h::OUT_Y_L];
int16_t angVelocZRaw = packet[l3gd20h::OUT_Z_H] << 8 | packet[l3gd20h::OUT_Z_L];
2022-02-02 10:29:30 +01:00
float angVelocX = angVelocXRaw * sensitivity;
float angVelocY = angVelocYRaw * sensitivity;
float angVelocZ = angVelocZRaw * sensitivity;
2021-07-13 19:19:25 +02:00
2023-02-26 14:54:35 +01:00
int8_t temperaturOffset = (-1) * packet[l3gd20h::TEMPERATURE_IDX];
2022-02-02 10:29:30 +01:00
float temperature = 25.0 + temperaturOffset;
2022-02-22 10:17:56 +01:00
if (periodicPrintout) {
if (debugDivider.checkAndIncrement()) {
/* Set terminal to utf-8 if there is an issue with micro printout. */
2022-02-22 10:17:56 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << "GyroHandlerL3GD20H: Angular velocities (deg/s):" << std::endl;
sif::info << "X: " << angVelocX << std::endl;
sif::info << "Y: " << angVelocY << std::endl;
sif::info << "Z: " << angVelocZ << std::endl;
2022-02-22 10:17:56 +01:00
#else
sif::printInfo("GyroHandlerL3GD20H: Angular velocities (deg/s):\n");
sif::printInfo("X: %f\n", angVelocX);
sif::printInfo("Y: %f\n", angVelocY);
sif::printInfo("Z: %f\n", angVelocZ);
2022-02-22 10:17:56 +01:00
#endif
}
2022-02-02 10:29:30 +01:00
}
2022-02-02 10:29:30 +01:00
PoolReadGuard readSet(&dataset);
2022-08-16 01:08:26 +02:00
if (readSet.getReadResult() == returnvalue::OK) {
2022-02-02 10:29:30 +01:00
if (std::abs(angVelocX) < this->absLimitX) {
dataset.angVelocX = angVelocX;
// dataset.angVelocX.setValid(true);
2022-02-02 10:29:30 +01:00
} else {
// dataset.angVelocX.setValid(false);
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
if (std::abs(angVelocY) < this->absLimitY) {
dataset.angVelocY = angVelocY;
// dataset.angVelocY.setValid(true);
2022-02-02 10:29:30 +01:00
} else {
// dataset.angVelocY.setValid(false);
2022-02-02 10:29:30 +01:00
}
if (std::abs(angVelocZ) < this->absLimitZ) {
dataset.angVelocZ = angVelocZ;
// dataset.angVelocZ.setValid(true);
2022-02-02 10:29:30 +01:00
} else {
// dataset.angVelocZ.setValid(false);
2022-02-02 10:29:30 +01:00
}
dataset.temperature = temperature;
// dataset.temperature.setValid(true);
2022-02-02 10:29:30 +01:00
}
break;
2021-07-13 19:19:25 +02:00
}
default:
2022-02-02 10:29:30 +01:00
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
}
return result;
2021-07-13 19:19:25 +02:00
}
uint32_t GyroHandlerL3GD20H::getTransitionDelayMs(Mode_t from, Mode_t to) {
2022-02-02 10:29:30 +01:00
return this->transitionDelayMs;
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
void GyroHandlerL3GD20H::setToGoToNormalMode(bool enable) { this->goNormalModeImmediately = true; }
2021-07-13 19:19:25 +02:00
// TODO
/*
2022-02-02 10:29:30 +01:00
ReturnValue_t GyroHandlerL3GD20H::initializeLocalDataPool(localpool::DataPool &localDataPoolMap,
PeriodicHkGenerationHelper &hkGenHelper) {
2023-02-26 14:54:35 +01:00
localDataPoolMap.emplace(l3gd20h::ANG_VELOC_X, new PoolEntry<float>({0.0}));
localDataPoolMap.emplace(l3gd20h::ANG_VELOC_Y, new PoolEntry<float>({0.0}));
localDataPoolMap.emplace(l3gd20h::ANG_VELOC_Z, new PoolEntry<float>({0.0}));
localDataPoolMap.emplace(l3gd20h::TEMPERATURE, new PoolEntry<float>({0.0}));
hkGenHelper.enableRegularPeriodicPacket(
2022-08-15 11:38:52 +02:00
subdp::RegularHkPeriodicParams(dataset.getSid(), false, 10.0));
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2021-07-13 19:19:25 +02:00
}
*/
2021-07-13 19:19:25 +02:00
void GyroHandlerL3GD20H::fillCommandAndReplyMap() {
2023-02-26 14:54:35 +01:00
insertInCommandAndReplyMap(l3gd20h::READ_REGS, 1, &dataset);
insertInCommandAndReplyMap(l3gd20h::CONFIGURE_CTRL_REGS, 1);
insertInCommandAndReplyMap(l3gd20h::READ_CTRL_REGS, 1);
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
void GyroHandlerL3GD20H::modeChanged() { internalState = InternalState::NONE; }
2021-09-23 18:12:59 +02:00
void GyroHandlerL3GD20H::setAbsoluteLimits(float limitX, float limitY, float limitZ) {
2022-02-02 10:29:30 +01:00
this->absLimitX = limitX;
this->absLimitY = limitY;
this->absLimitZ = limitZ;
2021-09-23 18:12:59 +02:00
}
void GyroHandlerL3GD20H::enablePeriodicPrintouts(bool enable, uint8_t divider) {
periodicPrintout = enable;
debugDivider.setDivider(divider);
}