eive-obsw/mission/devices/MGMHandlerLIS3MDL.cpp

425 lines
12 KiB
C++
Raw Normal View History

2020-09-30 21:49:00 +02:00
#include "MGMHandlerLIS3MDL.h"
2020-12-03 12:49:14 +01:00
2020-09-30 21:49:00 +02:00
MGMHandlerLIS3MDL::MGMHandlerLIS3MDL(object_id_t objectId,
object_id_t deviceCommunication, CookieIF* comCookie):
DeviceHandlerBase(objectId, deviceCommunication, comCookie) {
2020-12-03 12:49:14 +01:00
#if OBSW_ENHANCED_PRINTOUT == 1
debugDivider = new PeriodicOperationDivider(10);
#endif
// Set to default values right away.
registers[0] = MGMLIS3MDL::CTRL_REG1_DEFAULT;
registers[1] = MGMLIS3MDL::CTRL_REG2_DEFAULT;
registers[2] = MGMLIS3MDL::CTRL_REG3_DEFAULT;
registers[3] = MGMLIS3MDL::CTRL_REG4_DEFAULT;
registers[4] = MGMLIS3MDL::CTRL_REG5_DEFAULT;
2020-09-30 21:49:00 +02:00
}
MGMHandlerLIS3MDL::~MGMHandlerLIS3MDL() {
}
2020-10-01 02:06:39 +02:00
void MGMHandlerLIS3MDL::doStartUp() {
switch (internalState) {
case STATE_NONE:
internalState = STATE_FIRST_CONTACT;
break;
case STATE_FIRST_CONTACT:
internalState = STATE_SETUP;
break;
case STATE_SETUP:
internalState = STATE_CHECK_REGISTERS;
break;
2020-12-03 12:49:14 +01:00
case STATE_CHECK_REGISTERS: {
// Set up cached registers which will be used to configure the MGM.
if(commandExecuted) {
commandExecuted = false;
setMode(MODE_NORMAL);
2020-10-01 02:06:39 +02:00
}
break;
2020-12-03 12:49:14 +01:00
}
2020-10-01 02:06:39 +02:00
default:
break;
}
}
void MGMHandlerLIS3MDL::doShutDown() {
setMode(_MODE_POWER_DOWN);
}
ReturnValue_t MGMHandlerLIS3MDL::buildTransitionDeviceCommand(
DeviceCommandId_t *id) {
switch (internalState) {
case STATE_FIRST_CONTACT:
*id = MGMLIS3MDL::IDENTIFY_DEVICE;
break;
case STATE_SETUP:
*id = MGMLIS3MDL::SETUP_MGM;
break;
case STATE_CHECK_REGISTERS:
2020-12-03 12:49:14 +01:00
*id = MGMLIS3MDL::READ_CONFIG_AND_DATA;
2020-10-01 02:06:39 +02:00
break;
default:
break;
}
return buildCommandFromCommand(*id, NULL, 0);
}
2020-09-30 21:49:00 +02:00
uint8_t MGMHandlerLIS3MDL::readCommand(uint8_t command, bool continuousCom) {
2020-10-01 02:06:39 +02:00
command |= (1 << MGMLIS3MDL::RW_BIT);
2020-09-30 21:49:00 +02:00
if (continuousCom == true) {
2020-10-01 02:06:39 +02:00
command |= (1 << MGMLIS3MDL::MS_BIT);
2020-09-30 21:49:00 +02:00
}
return command;
}
uint8_t MGMHandlerLIS3MDL::writeCommand(uint8_t command, bool continuousCom) {
2020-10-01 02:06:39 +02:00
command &= ~(1 << MGMLIS3MDL::RW_BIT);
2020-09-30 21:49:00 +02:00
if (continuousCom == true) {
2020-10-01 02:06:39 +02:00
command |= (1 << MGMLIS3MDL::MS_BIT);
2020-09-30 21:49:00 +02:00
}
return command;
}
2020-12-03 12:49:14 +01:00
void MGMHandlerLIS3MDL::setupMgm() {
2020-09-30 21:49:00 +02:00
2020-12-03 12:49:14 +01:00
registers[0] = MGMLIS3MDL::CTRL_REG1_DEFAULT;
registers[1] = MGMLIS3MDL::CTRL_REG2_DEFAULT;
registers[2] = MGMLIS3MDL::CTRL_REG3_DEFAULT;
registers[3] = MGMLIS3MDL::CTRL_REG4_DEFAULT;
registers[4] = MGMLIS3MDL::CTRL_REG5_DEFAULT;
2020-09-30 21:49:00 +02:00
2020-12-03 12:49:14 +01:00
prepareCtrlRegisterWrite();
2020-09-30 21:49:00 +02:00
}
ReturnValue_t MGMHandlerLIS3MDL::buildNormalDeviceCommand(
2020-09-30 22:00:37 +02:00
DeviceCommandId_t *id) {
2020-12-03 12:49:14 +01:00
// Data/config register will be read in an alternating manner.
if(communicationStep == CommunicationStep::DATA) {
lastSentCommand = MGMLIS3MDL::READ_CONFIG_AND_DATA;
*id = MGMLIS3MDL::READ_CONFIG_AND_DATA;
communicationStep = CommunicationStep::TEMPERATURE;
return buildCommandFromCommand(*id, NULL, 0);
}
else {
lastSentCommand = MGMLIS3MDL::READ_TEMPERATURE;
*id = MGMLIS3MDL::READ_TEMPERATURE;
communicationStep = CommunicationStep::DATA;
return buildCommandFromCommand(*id, NULL, 0);
}
2020-09-30 21:49:00 +02:00
}
ReturnValue_t MGMHandlerLIS3MDL::buildCommandFromCommand(
2020-09-30 22:00:37 +02:00
DeviceCommandId_t deviceCommand, const uint8_t *commandData,
2020-09-30 21:49:00 +02:00
size_t commandDataLen) {
2020-10-21 20:13:27 +02:00
lastSentCommand = deviceCommand;
switch(deviceCommand) {
2020-12-03 12:49:14 +01:00
case(MGMLIS3MDL::READ_CONFIG_AND_DATA): {
2020-10-21 20:13:27 +02:00
std::memset(commandBuffer, 0, sizeof(commandBuffer));
2020-12-03 12:49:14 +01:00
commandBuffer[0] = readCommand(MGMLIS3MDL::CTRL_REG1, true);
rawPacket = commandBuffer;
rawPacketLen = MGMLIS3MDL::NR_OF_DATA_AND_CFG_REGISTERS + 1;
return RETURN_OK;
}
case(MGMLIS3MDL::READ_TEMPERATURE): {
std::memset(commandBuffer, 0, 3);
commandBuffer[0] = readCommand(MGMLIS3MDL::TEMP_LOWBYTE, true);
2020-10-21 20:13:27 +02:00
rawPacket = commandBuffer;
2020-12-03 12:49:14 +01:00
rawPacketLen = 3;
return RETURN_OK;
}
2020-10-01 02:06:39 +02:00
case(MGMLIS3MDL::IDENTIFY_DEVICE): {
2020-09-30 21:49:00 +02:00
return identifyDevice();
}
2020-10-01 02:06:39 +02:00
case(MGMLIS3MDL::TEMP_SENSOR_ENABLE): {
2020-09-30 21:49:00 +02:00
return enableTemperatureSensor(commandData, commandDataLen);
}
2020-10-01 02:06:39 +02:00
case(MGMLIS3MDL::SETUP_MGM): {
2020-12-03 12:49:14 +01:00
setupMgm();
return HasReturnvaluesIF::RETURN_OK;
2020-09-30 21:49:00 +02:00
}
2020-10-01 02:06:39 +02:00
case(MGMLIS3MDL::ACCURACY_OP_MODE_SET): {
2020-09-30 21:49:00 +02:00
return setOperatingMode(commandData, commandDataLen);
}
default:
2020-09-30 22:00:37 +02:00
lastSentCommand = DeviceHandlerIF::NO_COMMAND;
2020-09-30 21:49:00 +02:00
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
}
return HasReturnvaluesIF::RETURN_FAILED;
}
2020-10-01 02:06:39 +02:00
ReturnValue_t MGMHandlerLIS3MDL::identifyDevice() {
uint32_t size = 2;
commandBuffer[0] = readCommand(MGMLIS3MDL::IDENTIFY_DEVICE_REG_ADDR);
commandBuffer[1] = 0x00;
2020-09-30 21:49:00 +02:00
2020-10-01 02:06:39 +02:00
rawPacket = commandBuffer;
rawPacketLen = size;
2020-09-30 21:49:00 +02:00
2020-10-01 02:06:39 +02:00
return RETURN_OK;
2020-09-30 21:49:00 +02:00
}
ReturnValue_t MGMHandlerLIS3MDL::scanForReply(const uint8_t *start,
2020-09-30 22:00:37 +02:00
size_t len, DeviceCommandId_t *foundId, size_t *foundLen) {
2020-09-30 21:49:00 +02:00
*foundLen = len;
2020-12-03 12:49:14 +01:00
if (len == MGMLIS3MDL::NR_OF_DATA_AND_CFG_REGISTERS + 1) {
2020-09-30 21:49:00 +02:00
*foundLen = len;
2020-12-03 12:49:14 +01:00
*foundId = MGMLIS3MDL::READ_CONFIG_AND_DATA;
// Check validity by checking config registers
if (start[1] != registers[0] or start[2] != registers[1] or
start[3] != registers[2] or start[4] != registers[3] or
start[5] != registers[4]) {
2020-09-30 21:49:00 +02:00
return DeviceHandlerIF::INVALID_DATA;
}
2020-12-03 12:49:14 +01:00
if(mode == _MODE_START_UP) {
commandExecuted = true;
}
2020-09-30 21:49:00 +02:00
2020-12-03 12:49:14 +01:00
}
else if(len == MGMLIS3MDL::TEMPERATURE_REPLY_LEN) {
*foundLen = len;
*foundId = MGMLIS3MDL::READ_TEMPERATURE;
}
else if (len == MGMLIS3MDL::SETUP_REPLY_LEN) {
2020-09-30 21:49:00 +02:00
*foundLen = len;
2020-10-01 02:06:39 +02:00
*foundId = MGMLIS3MDL::SETUP_MGM;
2020-12-03 12:49:14 +01:00
}
else if (len == SINGLE_COMMAND_ANSWER_LEN) {
2020-09-30 21:49:00 +02:00
*foundLen = len;
*foundId = lastSentCommand;
2020-12-03 12:49:14 +01:00
}
else {
2020-09-30 21:49:00 +02:00
return DeviceHandlerIF::INVALID_DATA;
}
2020-10-01 02:06:39 +02:00
// Data with SPI Interface has always this answer
if (start[0] == 0b11111111) {
2020-09-30 21:49:00 +02:00
return RETURN_OK;
2020-10-01 02:06:39 +02:00
}
else {
2020-09-30 21:49:00 +02:00
return DeviceHandlerIF::INVALID_DATA;
}
}
ReturnValue_t MGMHandlerLIS3MDL::interpretDeviceReply(DeviceCommandId_t id,
const uint8_t *packet) {
switch (id) {
2020-10-01 02:06:39 +02:00
case MGMLIS3MDL::IDENTIFY_DEVICE: {
2020-09-30 21:49:00 +02:00
break;
}
2020-10-01 02:06:39 +02:00
case MGMLIS3MDL::SETUP_MGM: {
2020-09-30 21:49:00 +02:00
break;
}
2020-12-03 12:49:14 +01:00
case MGMLIS3MDL::READ_CONFIG_AND_DATA: {
2020-10-01 02:06:39 +02:00
// TODO: Store configuration and sensor values in new local datasets.
2020-09-30 21:49:00 +02:00
2020-12-03 12:49:14 +01:00
uint8_t scale = getFullScale(registers[2]);
float sensitivityFactor = getSensitivityFactor(scale);
2020-09-30 21:49:00 +02:00
2020-12-03 12:49:14 +01:00
int16_t mgmMeasurementRawX = packet[MGMLIS3MDL::X_HIGHBYTE_IDX] << 8
| packet[MGMLIS3MDL::X_LOWBYTE_IDX] ;
int16_t mgmMeasurementRawY = packet[MGMLIS3MDL::Y_HIGHBYTE_IDX] << 8
| packet[MGMLIS3MDL::Y_LOWBYTE_IDX] ;
int16_t mgmMeasurementRawZ = packet[MGMLIS3MDL::Z_HIGHBYTE_IDX] << 8
| packet[MGMLIS3MDL::Z_LOWBYTE_IDX] ;
// Target value in microtesla
float mgmX = static_cast<float>(mgmMeasurementRawX) * sensitivityFactor
* MGMLIS3MDL::GAUSS_TO_MICROTESLA_FACTOR;
float mgmY = static_cast<float>(mgmMeasurementRawY) * sensitivityFactor
* MGMLIS3MDL::GAUSS_TO_MICROTESLA_FACTOR;
float mgmZ = static_cast<float>(mgmMeasurementRawZ) * sensitivityFactor
* MGMLIS3MDL::GAUSS_TO_MICROTESLA_FACTOR;
#if OBSW_ENHANCED_PRINTOUT == 1
if(debugDivider->checkAndIncrement()) {
sif::info << "MGMHandlerLIS3: Magnetic field strength in"
" microtesla:" << std::endl;
// Set terminal to utf-8 if there is an issue with micro printout.
sif::info << "X: " << mgmX << " \xC2\xB5T" << std::endl;
sif::info << "Y: " << mgmY << " \xC2\xB5T" << std::endl;
sif::info << "Z: " << mgmZ << " \xC2\xB5T" << std::endl;
}
#endif
2020-09-30 21:49:00 +02:00
break;
}
2020-12-03 12:49:14 +01:00
case MGMLIS3MDL::READ_TEMPERATURE: {
int16_t tempValueRaw = packet[2] << 8 | packet[1];
float tempValue = 25.0 + ((static_cast<float>(tempValueRaw)) / 8.0);
#if OBSW_ENHANCED_PRINTOUT == 1
if(debugDivider->check()) {
// Set terminal to utf-8 if there is an issue with micro printout.
sif::info << "MGMHandlerLIS3: Temperature: " << tempValue<< " °C"
<< std::endl;
}
#endif
break;
}
2020-09-30 21:49:00 +02:00
default: {
return DeviceHandlerIF::UNKNOW_DEVICE_REPLY;
}
}
return RETURN_OK;
}
2020-12-03 12:49:14 +01:00
uint8_t MGMHandlerLIS3MDL::getFullScale(uint8_t ctrlRegister2) {
2020-09-30 21:49:00 +02:00
bool FS0 = false;
bool FS1 = false;
2020-12-03 12:49:14 +01:00
if ((ctrlRegister2 >> 5) == 1)
2020-09-30 21:49:00 +02:00
FS0 = true;
2020-12-03 12:49:14 +01:00
if ((ctrlRegister2 >> 6) == 1)
2020-09-30 21:49:00 +02:00
FS1 = true;
if ((FS0 == true) && (FS1 == true))
return 16;
else if ((FS0 == false) && (FS1 == true))
return 12;
else if ((FS0 == true) && (FS1 == false))
return 8;
else
return 4;
}
2020-10-01 02:06:39 +02:00
float MGMHandlerLIS3MDL::getSensitivityFactor(uint8_t scale) {
2020-09-30 21:49:00 +02:00
return (float) scale / (INT16_MAX);
}
ReturnValue_t MGMHandlerLIS3MDL::enableTemperatureSensor(
const uint8_t *commandData, size_t commandDataLen) {
triggerEvent(CHANGE_OF_SETUP_PARAMETER);
uint32_t size = 2;
2020-10-01 02:06:39 +02:00
commandBuffer[0] = writeCommand(MGMLIS3MDL::CTRL_REG1);
2020-09-30 21:49:00 +02:00
if (commandDataLen > 1) {
return INVALID_NUMBER_OR_LENGTH_OF_PARAMETERS;
}
switch (*commandData) {
2020-10-01 02:06:39 +02:00
case (MGMLIS3MDL::ON):
2020-09-30 21:49:00 +02:00
commandBuffer[1] = registers[0] | (1 << 7);
break;
2020-10-01 02:06:39 +02:00
case (MGMLIS3MDL::OFF):
2020-09-30 21:49:00 +02:00
commandBuffer[1] = registers[0] & ~(1 << 7);
break;
default:
return INVALID_COMMAND_PARAMETER;
break;
}
registers[0] = commandBuffer[1];
rawPacket = commandBuffer;
rawPacketLen = size;
return RETURN_OK;
}
ReturnValue_t MGMHandlerLIS3MDL::setOperatingMode(const uint8_t *commandData,
size_t commandDataLen) {
triggerEvent(CHANGE_OF_SETUP_PARAMETER);
if (commandDataLen != 1) {
return INVALID_NUMBER_OR_LENGTH_OF_PARAMETERS;
}
switch (commandData[0]) {
2020-10-01 02:06:39 +02:00
case MGMLIS3MDL::LOW:
registers[0] = (registers[0] & (~(1 << MGMLIS3MDL::OM1))) & (~(1 << MGMLIS3MDL::OM0));
registers[3] = (registers[3] & (~(1 << MGMLIS3MDL::OMZ1))) & (~(1 << MGMLIS3MDL::OMZ0));
2020-09-30 21:49:00 +02:00
break;
2020-10-01 02:06:39 +02:00
case MGMLIS3MDL::MEDIUM:
registers[0] = (registers[0] & (~(1 << MGMLIS3MDL::OM1))) | (1 << MGMLIS3MDL::OM0);
registers[3] = (registers[3] & (~(1 << MGMLIS3MDL::OMZ1))) | (1 << MGMLIS3MDL::OMZ0);
2020-09-30 21:49:00 +02:00
break;
2020-10-01 02:06:39 +02:00
case MGMLIS3MDL::HIGH:
registers[0] = (registers[0] | (1 << MGMLIS3MDL::OM1)) & (~(1 << MGMLIS3MDL::OM0));
registers[3] = (registers[3] | (1 << MGMLIS3MDL::OMZ1)) & (~(1 << MGMLIS3MDL::OMZ0));
2020-09-30 21:49:00 +02:00
break;
2020-10-01 02:06:39 +02:00
case MGMLIS3MDL::ULTRA:
registers[0] = (registers[0] | (1 << MGMLIS3MDL::OM1)) | (1 << MGMLIS3MDL::OM0);
registers[3] = (registers[3] | (1 << MGMLIS3MDL::OMZ1)) | (1 << MGMLIS3MDL::OMZ0);
2020-09-30 21:49:00 +02:00
break;
default:
break;
}
2020-10-30 13:02:31 +01:00
return prepareCtrlRegisterWrite();
2020-09-30 21:49:00 +02:00
}
void MGMHandlerLIS3MDL::fillCommandAndReplyMap() {
2020-09-30 22:00:37 +02:00
/*
* Regarding ArduinoBoard:
* Actually SPI answers directly, but as commanding ArduinoBoard the
* communication could be delayed
* SPI always has to be triggered, so there could be no periodic answer of
* the device, the device has to asked with a command, so periodic is zero.
2020-09-30 21:49:00 +02:00
*
* We dont read single registers, we just expect special
* reply from he Readall_MGM
*/
2020-12-03 12:49:14 +01:00
insertInCommandAndReplyMap(MGMLIS3MDL::READ_CONFIG_AND_DATA, 1);
insertInCommandAndReplyMap(MGMLIS3MDL::READ_TEMPERATURE, 1);
2020-10-01 02:06:39 +02:00
insertInCommandAndReplyMap(MGMLIS3MDL::SETUP_MGM, 1);
insertInCommandAndReplyMap(MGMLIS3MDL::IDENTIFY_DEVICE, 1);
insertInCommandAndReplyMap(MGMLIS3MDL::TEMP_SENSOR_ENABLE, 1);
insertInCommandAndReplyMap(MGMLIS3MDL::ACCURACY_OP_MODE_SET, 1);
2020-09-30 21:49:00 +02:00
}
2020-10-30 13:02:31 +01:00
ReturnValue_t MGMHandlerLIS3MDL::prepareCtrlRegisterWrite() {
2020-09-30 21:49:00 +02:00
2020-10-01 02:06:39 +02:00
commandBuffer[0] = writeCommand(MGMLIS3MDL::CTRL_REG1, true);
2020-09-30 21:49:00 +02:00
2020-10-21 20:13:27 +02:00
for (size_t i = 0; i < MGMLIS3MDL::NR_OF_CTRL_REGISTERS; i++) {
commandBuffer[i + 1] = registers[i];
2020-09-30 21:49:00 +02:00
}
rawPacket = commandBuffer;
2020-12-03 12:49:14 +01:00
rawPacketLen = MGMLIS3MDL::NR_OF_CTRL_REGISTERS + 1;
2020-09-30 21:49:00 +02:00
// We dont have to check if this is working because we just did it
return RETURN_OK;
}
void MGMHandlerLIS3MDL::setNormalDatapoolEntriesInvalid() {
2020-09-30 22:00:37 +02:00
// TODO: use new distributed datapools here.
2020-09-30 21:49:00 +02:00
}
void MGMHandlerLIS3MDL::doTransition(Mode_t modeFrom, Submode_t subModeFrom) {
}
uint32_t MGMHandlerLIS3MDL::getTransitionDelayMs(Mode_t from, Mode_t to) {
return 5000;
}
void MGMHandlerLIS3MDL::modeChanged(void) {
internalState = STATE_NONE;
}
2020-12-03 12:49:14 +01:00
ReturnValue_t MGMHandlerLIS3MDL::initializeLocalDataPool(
LocalDataPool &localDataPoolMap, LocalDataPoolManager &poolManager) {
localDataPoolMap.emplace(MGMLIS3MDL::FIELD_STRENGTH_X,
new PoolEntry<float>({0.0}));
localDataPoolMap.emplace(MGMLIS3MDL::FIELD_STRENGTH_Y,
new PoolEntry<float>({0.0}));
localDataPoolMap.emplace(MGMLIS3MDL::FIELD_STRENGTH_Z,
new PoolEntry<float>({0.0}));
localDataPoolMap.emplace(MGMLIS3MDL::TEMPERATURE_CELCIUS,
new PoolEntry<float>({0.0}));
return HasReturnvaluesIF::RETURN_OK;
}