2020-12-22 13:41:01 +01:00
|
|
|
#include <mission/devices/GyroL3GD20Handler.h>
|
2021-01-14 12:17:05 +01:00
|
|
|
#include <fsfw/datapoollocal/PoolReadHelper.h>
|
2020-12-22 00:30:50 +01:00
|
|
|
|
|
|
|
GyroHandler::GyroHandler(object_id_t objectId, object_id_t deviceCommunication,
|
|
|
|
CookieIF *comCookie):
|
2020-12-23 20:17:39 +01:00
|
|
|
DeviceHandlerBase(objectId, deviceCommunication, comCookie),
|
|
|
|
dataset(this) {
|
2020-12-22 00:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
GyroHandler::~GyroHandler() {}
|
|
|
|
|
|
|
|
void GyroHandler::doStartUp() {
|
2020-12-22 13:41:01 +01:00
|
|
|
if(internalState == InternalState::STATE_NONE) {
|
|
|
|
internalState = InternalState::STATE_CONFIGURE;
|
|
|
|
}
|
2020-12-22 00:30:50 +01:00
|
|
|
|
2020-12-22 13:41:01 +01:00
|
|
|
if(internalState == InternalState::STATE_CONFIGURE) {
|
|
|
|
if(commandExecuted) {
|
|
|
|
internalState = InternalState::STATE_NORMAL;
|
|
|
|
commandExecuted = false;
|
|
|
|
}
|
|
|
|
}
|
2020-12-22 00:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GyroHandler::doShutDown() {
|
2020-12-22 13:41:01 +01:00
|
|
|
setMode(_MODE_POWER_DOWN);
|
2020-12-22 00:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t GyroHandler::buildTransitionDeviceCommand(DeviceCommandId_t *id) {
|
2020-12-23 20:17:39 +01:00
|
|
|
switch(internalState) {
|
|
|
|
case(InternalState::STATE_NONE):
|
|
|
|
case(InternalState::STATE_NORMAL): {
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
case(InternalState::STATE_CONFIGURE): {
|
|
|
|
*id = L3GD20H::CONFIGURE_CTRL_REGS;
|
|
|
|
uint8_t command [5];
|
|
|
|
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;
|
|
|
|
return buildCommandFromCommand(*id, command, 5);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// might be a configuration error.
|
|
|
|
sif::debug << "GyroHandler::buildTransitionDeviceCommand: Unknown "
|
|
|
|
<< "internal state!" << std::endl;
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
2020-12-22 00:30:50 +01:00
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t GyroHandler::buildNormalDeviceCommand(DeviceCommandId_t *id) {
|
2020-12-23 20:17:39 +01:00
|
|
|
*id = L3GD20H::READ_REGS;
|
|
|
|
return buildCommandFromCommand(*id, nullptr, 0);
|
2020-12-22 00:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t GyroHandler::buildCommandFromCommand(
|
|
|
|
DeviceCommandId_t deviceCommand, const uint8_t *commandData,
|
|
|
|
size_t commandDataLen) {
|
2020-12-23 20:17:39 +01:00
|
|
|
switch(deviceCommand) {
|
|
|
|
case(L3GD20H::READ_REGS): {
|
|
|
|
commandBuffer[0] = L3GD20H::READ_START | L3GD20H::AUTO_INCREMENT_MASK |
|
|
|
|
L3GD20H::READ_MASK;
|
|
|
|
|
|
|
|
std::memset(commandBuffer + 1, 0, L3GD20H::READ_LEN);
|
|
|
|
rawPacket = commandBuffer;
|
|
|
|
rawPacketLen = L3GD20H::READ_LEN + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(L3GD20H::CONFIGURE_CTRL_REGS): {
|
|
|
|
commandBuffer[0] = L3GD20H::CTRL_REG_1 | L3GD20H::AUTO_INCREMENT_MASK;
|
|
|
|
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];
|
|
|
|
|
|
|
|
bool fsH = ctrlReg4Value & L3GD20H::SET_FS_1;
|
|
|
|
bool fsL = ctrlReg4Value & L3GD20H::SET_FS_0;
|
|
|
|
|
|
|
|
if(not fsH and not fsL) {
|
|
|
|
scaleFactor = static_cast<float>(L3GD20H::RANGE_DPS_00) / INT16_MAX;
|
|
|
|
}
|
|
|
|
else if(not fsH and fsL) {
|
|
|
|
scaleFactor = static_cast<float>(L3GD20H::RANGE_DPS_01) / INT16_MAX;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
scaleFactor = static_cast<float>(L3GD20H::RANGE_DPS_11) / INT16_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
commandBuffer[1] = ctrlReg1Value;
|
|
|
|
commandBuffer[2] = ctrlReg2Value;
|
|
|
|
commandBuffer[3] = ctrlReg3Value;
|
|
|
|
commandBuffer[4] = ctrlReg4Value;
|
|
|
|
commandBuffer[5] = ctrlReg5Value;
|
|
|
|
|
|
|
|
rawPacket = commandBuffer;
|
|
|
|
rawPacketLen = 6;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(L3GD20H::READ_CTRL_REGS): {
|
|
|
|
commandBuffer[0] = L3GD20H::READ_START | L3GD20H::AUTO_INCREMENT_MASK |
|
|
|
|
L3GD20H::READ_MASK;
|
|
|
|
|
|
|
|
std::memset(commandBuffer + 1, 0, 5);
|
|
|
|
rawPacket = commandBuffer;
|
|
|
|
rawPacketLen = 6;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
|
|
|
|
}
|
2020-12-22 00:30:50 +01:00
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t GyroHandler::scanForReply(const uint8_t *start, size_t len,
|
|
|
|
DeviceCommandId_t *foundId, size_t *foundLen) {
|
2020-12-23 20:17:39 +01:00
|
|
|
// SPI, ID will always be the one of the last sent command.
|
|
|
|
*foundId = this->getPendingCommand();
|
|
|
|
*foundLen = this->rawPacketLen;
|
|
|
|
|
|
|
|
// Data with SPI Interface has always this answer
|
|
|
|
if (start[0] == 0b11111111) {
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
return DeviceHandlerIF::INVALID_DATA;
|
2020-12-22 00:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t GyroHandler::interpretDeviceReply(DeviceCommandId_t id,
|
|
|
|
const uint8_t *packet) {
|
2020-12-23 20:17:39 +01:00
|
|
|
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
|
|
|
|
switch(id) {
|
|
|
|
case(L3GD20H::CONFIGURE_CTRL_REGS): {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(L3GD20H::READ_CTRL_REGS): {
|
|
|
|
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::STATE_CONFIGURE;
|
|
|
|
return DeviceHandlerIF::DEVICE_REPLY_INVALID;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(L3GD20H::READ_START): {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
statusReg = packet[L3GD20H::STATUS_IDX];
|
|
|
|
|
|
|
|
float angVelocX = (packet[L3GD20H::OUT_X_H] << 8 |
|
|
|
|
packet[L3GD20H::OUT_X_L]) * scaleFactor;
|
|
|
|
float angVelocY = (packet[L3GD20H::OUT_Y_H] << 8 |
|
|
|
|
packet[L3GD20H::OUT_Y_L]) * scaleFactor;
|
|
|
|
float angVelocZ = (packet[L3GD20H::OUT_Z_H] << 8 |
|
|
|
|
packet[L3GD20H::OUT_Z_L]) * scaleFactor;
|
|
|
|
|
|
|
|
int8_t temperaturOffset = (-1) * packet[L3GD20H::TEMPERATURE_IDX];
|
|
|
|
float temperature = 25.0 + temperaturOffset;
|
|
|
|
|
2021-01-14 12:17:05 +01:00
|
|
|
PoolReadHelper readSet(&dataset);
|
2020-12-23 20:17:39 +01:00
|
|
|
if(result == HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
dataset.angVelocX = angVelocX;
|
2020-12-29 01:15:35 +01:00
|
|
|
dataset.angVelocY = angVelocY;
|
|
|
|
dataset.angVelocZ = angVelocZ;
|
2020-12-23 20:17:39 +01:00
|
|
|
dataset.temperature = temperature;
|
|
|
|
dataset.setValidity(true, true);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
return result;
|
2020-12-22 00:30:50 +01:00
|
|
|
}
|
|
|
|
|
2020-12-23 20:17:39 +01:00
|
|
|
|
2020-12-22 00:30:50 +01:00
|
|
|
uint32_t GyroHandler::getTransitionDelayMs(Mode_t from, Mode_t to) {
|
|
|
|
return 5000;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t GyroHandler::initializeLocalDataPool(
|
2021-01-14 11:37:32 +01:00
|
|
|
localpool::DataPool &localDataPoolMap, LocalDataPoolManager &poolManager) {
|
2020-12-23 20:17:39 +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}));
|
2020-12-22 00:30:50 +01:00
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GyroHandler::fillCommandAndReplyMap() {
|
2020-12-23 20:17:39 +01:00
|
|
|
insertInCommandAndReplyMap(L3GD20H::READ_REGS, 1, &dataset);
|
|
|
|
insertInCommandAndReplyMap(L3GD20H::CONFIGURE_CTRL_REGS, 1);
|
|
|
|
insertInCommandAndReplyMap(L3GD20H::READ_CTRL_REGS, 1);
|
2020-12-22 00:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GyroHandler::modeChanged() {
|
2020-12-23 20:17:39 +01:00
|
|
|
internalState = InternalState::STATE_NONE;
|
2020-12-22 00:30:50 +01:00
|
|
|
}
|