taken over source lis3mdl device
This commit is contained in:
parent
f610d6f050
commit
0a4a7de58e
@ -1,13 +1,18 @@
|
||||
#include "MGMHandlerLIS3MDL.h"
|
||||
|
||||
|
||||
MGMHandlerLIS3MDL::MGMHandlerLIS3MDL(object_id_t objectId,
|
||||
object_id_t deviceCommunication, CookieIF* comCookie):
|
||||
DeviceHandlerBase(objectId, deviceCommunication, comCookie) {
|
||||
registers[0] = 0x00;
|
||||
registers[1] = 0x00;
|
||||
registers[2] = 0x00;
|
||||
registers[3] = 0x00;
|
||||
registers[4] = 0x00;
|
||||
#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;
|
||||
|
||||
}
|
||||
|
||||
@ -29,18 +34,14 @@ void MGMHandlerLIS3MDL::doStartUp() {
|
||||
internalState = STATE_CHECK_REGISTERS;
|
||||
break;
|
||||
|
||||
case STATE_CHECK_REGISTERS:
|
||||
if (setupMGM() == RETURN_OK) {
|
||||
for (size_t i = 1; i <= MGMLIS3MDL::NR_OF_CTRL_REGISTERS; i++) {
|
||||
if (registers[i - 1] != commandBuffer[i]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
setMode(_MODE_TO_ON);
|
||||
case STATE_CHECK_REGISTERS: {
|
||||
// Set up cached registers which will be used to configure the MGM.
|
||||
if(commandExecuted) {
|
||||
commandExecuted = false;
|
||||
setMode(MODE_NORMAL);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@ -63,7 +64,7 @@ ReturnValue_t MGMHandlerLIS3MDL::buildTransitionDeviceCommand(
|
||||
break;
|
||||
|
||||
case STATE_CHECK_REGISTERS:
|
||||
*id = MGMLIS3MDL::READALL_MGM;
|
||||
*id = MGMLIS3MDL::READ_CONFIG_AND_DATA;
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -88,25 +89,33 @@ uint8_t MGMHandlerLIS3MDL::writeCommand(uint8_t command, bool continuousCom) {
|
||||
return command;
|
||||
}
|
||||
|
||||
ReturnValue_t MGMHandlerLIS3MDL::setupMGM() {
|
||||
void MGMHandlerLIS3MDL::setupMgm() {
|
||||
|
||||
registers[0] = (1 << MGMLIS3MDL::TEMP_EN) | (1 << MGMLIS3MDL::OM1)
|
||||
| (1 << MGMLIS3MDL::DO0) | (1 << MGMLIS3MDL::DO1)
|
||||
| (1 << MGMLIS3MDL::DO2);
|
||||
registers[1] = 0;
|
||||
registers[2] = 0;
|
||||
registers[3] = (1 << MGMLIS3MDL::OMZ1);
|
||||
registers[4] = 0;
|
||||
|
||||
return prepareCtrlRegisterWrite();
|
||||
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;
|
||||
|
||||
prepareCtrlRegisterWrite();
|
||||
}
|
||||
|
||||
ReturnValue_t MGMHandlerLIS3MDL::buildNormalDeviceCommand(
|
||||
DeviceCommandId_t *id) {
|
||||
//defines CommandID of MGM in normal operation and build command from command
|
||||
*id = MGMLIS3MDL::READALL_MGM;
|
||||
return buildCommandFromCommand(*id, NULL, 0);
|
||||
// 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ReturnValue_t MGMHandlerLIS3MDL::buildCommandFromCommand(
|
||||
@ -114,13 +123,22 @@ ReturnValue_t MGMHandlerLIS3MDL::buildCommandFromCommand(
|
||||
size_t commandDataLen) {
|
||||
lastSentCommand = deviceCommand;
|
||||
switch(deviceCommand) {
|
||||
case(MGMLIS3MDL::READALL_MGM): {
|
||||
case(MGMLIS3MDL::READ_CONFIG_AND_DATA): {
|
||||
std::memset(commandBuffer, 0, sizeof(commandBuffer));
|
||||
commandBuffer[0] = readCommand(0, true);
|
||||
commandBuffer[0] = readCommand(MGMLIS3MDL::CTRL_REG1, true);
|
||||
|
||||
rawPacket = commandBuffer;
|
||||
rawPacketLen = sizeof(commandBuffer);
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
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);
|
||||
|
||||
rawPacket = commandBuffer;
|
||||
rawPacketLen = 3;
|
||||
return RETURN_OK;
|
||||
}
|
||||
case(MGMLIS3MDL::IDENTIFY_DEVICE): {
|
||||
return identifyDevice();
|
||||
}
|
||||
@ -128,7 +146,8 @@ ReturnValue_t MGMHandlerLIS3MDL::buildCommandFromCommand(
|
||||
return enableTemperatureSensor(commandData, commandDataLen);
|
||||
}
|
||||
case(MGMLIS3MDL::SETUP_MGM): {
|
||||
return setupMGM();
|
||||
setupMgm();
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
case(MGMLIS3MDL::ACCURACY_OP_MODE_SET): {
|
||||
return setOperatingMode(commandData, commandDataLen);
|
||||
@ -137,7 +156,6 @@ ReturnValue_t MGMHandlerLIS3MDL::buildCommandFromCommand(
|
||||
lastSentCommand = DeviceHandlerIF::NO_COMMAND;
|
||||
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
|
||||
}
|
||||
}
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
|
||||
@ -155,22 +173,33 @@ ReturnValue_t MGMHandlerLIS3MDL::identifyDevice() {
|
||||
ReturnValue_t MGMHandlerLIS3MDL::scanForReply(const uint8_t *start,
|
||||
size_t len, DeviceCommandId_t *foundId, size_t *foundLen) {
|
||||
*foundLen = len;
|
||||
if (len == MGMLIS3MDL::TOTAL_NR_OF_ADRESSES + 1) {
|
||||
if (len == MGMLIS3MDL::NR_OF_DATA_AND_CFG_REGISTERS + 1) {
|
||||
*foundLen = len;
|
||||
*foundId = MGMLIS3MDL::READALL_MGM;
|
||||
//WHO AM I test
|
||||
if (*(start + 16) != MGMLIS3MDL::DEVICE_ID) {
|
||||
*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]) {
|
||||
return DeviceHandlerIF::INVALID_DATA;
|
||||
}
|
||||
if(mode == _MODE_START_UP) {
|
||||
commandExecuted = true;
|
||||
}
|
||||
|
||||
} else if (len == MGMLIS3MDL::SETUP_REPLY) {
|
||||
}
|
||||
else if(len == MGMLIS3MDL::TEMPERATURE_REPLY_LEN) {
|
||||
*foundLen = len;
|
||||
*foundId = MGMLIS3MDL::READ_TEMPERATURE;
|
||||
}
|
||||
else if (len == MGMLIS3MDL::SETUP_REPLY_LEN) {
|
||||
*foundLen = len;
|
||||
*foundId = MGMLIS3MDL::SETUP_MGM;
|
||||
} else if (len == SINGLE_COMMAND_ANSWER_LEN) {
|
||||
}
|
||||
else if (len == SINGLE_COMMAND_ANSWER_LEN) {
|
||||
*foundLen = len;
|
||||
*foundId = lastSentCommand;
|
||||
} else {
|
||||
|
||||
}
|
||||
else {
|
||||
return DeviceHandlerIF::INVALID_DATA;
|
||||
}
|
||||
|
||||
@ -193,43 +222,53 @@ ReturnValue_t MGMHandlerLIS3MDL::interpretDeviceReply(DeviceCommandId_t id,
|
||||
case MGMLIS3MDL::SETUP_MGM: {
|
||||
break;
|
||||
}
|
||||
case MGMLIS3MDL::READALL_MGM: {
|
||||
case MGMLIS3MDL::READ_CONFIG_AND_DATA: {
|
||||
// TODO: Store configuration and sensor values in new local datasets.
|
||||
registers[0] = *(packet + 33);
|
||||
registers[1] = *(packet + 34);
|
||||
registers[2] = *(packet + 35);
|
||||
registers[3] = *(packet + 36);
|
||||
registers[4] = *(packet + 37);
|
||||
|
||||
uint8_t reg2_value = *(packet + 34);
|
||||
uint8_t scale = getFullScale(®2_value);
|
||||
uint8_t scale = getFullScale(registers[2]);
|
||||
float sensitivityFactor = getSensitivityFactor(scale);
|
||||
|
||||
int16_t x_value_raw;
|
||||
int16_t y_value_raw;
|
||||
int16_t z_value_raw;
|
||||
int16_t temp_value_raw;
|
||||
//size_t size = 2;
|
||||
uint8_t *accessBuffer;
|
||||
accessBuffer = const_cast<uint8_t*>(packet + 41);
|
||||
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] ;
|
||||
|
||||
x_value_raw = *(accessBuffer + 1) << 8 | *(accessBuffer);
|
||||
accessBuffer += 2;
|
||||
y_value_raw = *(accessBuffer + 1) << 8 | *(accessBuffer);
|
||||
accessBuffer += 2;
|
||||
z_value_raw = *(accessBuffer + 1) << 8 | *(accessBuffer);
|
||||
accessBuffer += 2;
|
||||
|
||||
temp_value_raw = *(accessBuffer + 1) << 8 | *(accessBuffer);
|
||||
|
||||
float x_value = static_cast<float>(x_value_raw) * sensitivityFactor;
|
||||
float y_value = static_cast<float>(y_value_raw) * sensitivityFactor;
|
||||
float z_value = static_cast<float>(z_value_raw) * sensitivityFactor;
|
||||
float temp_value = 25.0 + ((static_cast<float>(temp_value_raw)) / 8.0);
|
||||
// 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
|
||||
break;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
default: {
|
||||
return DeviceHandlerIF::UNKNOW_DEVICE_REPLY;
|
||||
}
|
||||
@ -238,12 +277,12 @@ ReturnValue_t MGMHandlerLIS3MDL::interpretDeviceReply(DeviceCommandId_t id,
|
||||
return RETURN_OK;
|
||||
}
|
||||
|
||||
uint8_t MGMHandlerLIS3MDL::getFullScale(uint8_t *reg2) {
|
||||
uint8_t MGMHandlerLIS3MDL::getFullScale(uint8_t ctrlRegister2) {
|
||||
bool FS0 = false;
|
||||
bool FS1 = false;
|
||||
if ((*reg2 >> 5) == 1)
|
||||
if ((ctrlRegister2 >> 5) == 1)
|
||||
FS0 = true;
|
||||
if ((*reg2 >> 6) == 1)
|
||||
if ((ctrlRegister2 >> 6) == 1)
|
||||
FS1 = true;
|
||||
if ((FS0 == true) && (FS1 == true))
|
||||
return 16;
|
||||
@ -333,7 +372,8 @@ void MGMHandlerLIS3MDL::fillCommandAndReplyMap() {
|
||||
* We dont read single registers, we just expect special
|
||||
* reply from he Readall_MGM
|
||||
*/
|
||||
insertInCommandAndReplyMap(MGMLIS3MDL::READALL_MGM, 1);
|
||||
insertInCommandAndReplyMap(MGMLIS3MDL::READ_CONFIG_AND_DATA, 1);
|
||||
insertInCommandAndReplyMap(MGMLIS3MDL::READ_TEMPERATURE, 1);
|
||||
insertInCommandAndReplyMap(MGMLIS3MDL::SETUP_MGM, 1);
|
||||
insertInCommandAndReplyMap(MGMLIS3MDL::IDENTIFY_DEVICE, 1);
|
||||
insertInCommandAndReplyMap(MGMLIS3MDL::TEMP_SENSOR_ENABLE, 1);
|
||||
@ -348,7 +388,7 @@ ReturnValue_t MGMHandlerLIS3MDL::prepareCtrlRegisterWrite() {
|
||||
commandBuffer[i + 1] = registers[i];
|
||||
}
|
||||
rawPacket = commandBuffer;
|
||||
rawPacketLen = MGMLIS3MDL::NR_OF_CTRL_REGISTERS;
|
||||
rawPacketLen = MGMLIS3MDL::NR_OF_CTRL_REGISTERS + 1;
|
||||
|
||||
// We dont have to check if this is working because we just did it
|
||||
return RETURN_OK;
|
||||
@ -369,3 +409,16 @@ uint32_t MGMHandlerLIS3MDL::getTransitionDelayMs(Mode_t from, Mode_t to) {
|
||||
void MGMHandlerLIS3MDL::modeChanged(void) {
|
||||
internalState = STATE_NONE;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -1,9 +1,14 @@
|
||||
#ifndef MISSION_DEVICES_MGMLIS3MDLHANDLER_H_
|
||||
#define MISSION_DEVICES_MGMLIS3MDLHANDLER_H_
|
||||
|
||||
#include <fsfw/devicehandlers/DeviceHandlerBase.h>
|
||||
#include "devicedefinitions/MGMHandlerLIS3Definitions.h"
|
||||
#include <subsystemIdRanges.h>
|
||||
|
||||
#include <fsfwconfig/OBSWConfig.h>
|
||||
|
||||
#include <fsfw/devicehandlers/DeviceHandlerBase.h>
|
||||
#include <fsfw/globalfunctions/PeriodicOperationDivider.h>
|
||||
|
||||
#include <events/subsystemIdRanges.h>
|
||||
|
||||
/**
|
||||
* @brief Device handler object for the LIS3MDL 3-axis magnetometer
|
||||
@ -14,6 +19,12 @@
|
||||
*/
|
||||
class MGMHandlerLIS3MDL: public DeviceHandlerBase {
|
||||
public:
|
||||
|
||||
enum class CommunicationStep {
|
||||
DATA,
|
||||
TEMPERATURE
|
||||
};
|
||||
|
||||
static const uint8_t INTERFACE_ID = CLASS_ID::MGM_LIS3MDL;
|
||||
static const uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::MGM_LIS3MDL;
|
||||
//Notifies a command to change the setup parameters
|
||||
@ -44,8 +55,12 @@ protected:
|
||||
virtual void fillCommandAndReplyMap() override;
|
||||
virtual void modeChanged(void) override;
|
||||
void setNormalDatapoolEntriesInvalid() override;
|
||||
ReturnValue_t initializeLocalDataPool(LocalDataPool &localDataPoolMap,
|
||||
LocalDataPoolManager &poolManager) override;
|
||||
|
||||
|
||||
private:
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* Device specific commands and variables */
|
||||
/*------------------------------------------------------------------------*/
|
||||
@ -68,7 +83,7 @@ private:
|
||||
* e.g.: +- 4 gauss. See p.25 datasheet.
|
||||
* @return The ReturnValue does not contain the sign of the value
|
||||
*/
|
||||
uint8_t getFullScale(uint8_t *reg2);
|
||||
uint8_t getFullScale(uint8_t ctrlReg2);
|
||||
|
||||
/**
|
||||
* The 16 bit value needs to be divided by the full range of a 16bit value
|
||||
@ -86,7 +101,7 @@ private:
|
||||
*/
|
||||
ReturnValue_t identifyDevice();
|
||||
|
||||
virtual ReturnValue_t setupMGM();
|
||||
virtual void setupMgm();
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* Non normal commands */
|
||||
@ -114,7 +129,7 @@ private:
|
||||
//Single SPIcommand has 2 bytes, first for adress, second for content
|
||||
size_t singleComandSize = 2;
|
||||
//has the size for all adresses of the lis3mdl + the continous write bit
|
||||
uint8_t commandBuffer[MGMLIS3MDL::TOTAL_NR_OF_ADRESSES + 1];
|
||||
uint8_t commandBuffer[MGMLIS3MDL::NR_OF_DATA_AND_CFG_REGISTERS + 1];
|
||||
|
||||
/**
|
||||
* We want to save the registers we set, so we dont have to read the
|
||||
@ -123,6 +138,9 @@ private:
|
||||
*/
|
||||
uint8_t registers[MGMLIS3MDL::NR_OF_CTRL_REGISTERS];
|
||||
|
||||
uint8_t statusRegister = 0;
|
||||
|
||||
|
||||
/**
|
||||
* As this is a SPI Device, we get the Answer of the last sent command in
|
||||
* the next read cycle, so we could check the command for identification.
|
||||
@ -142,6 +160,12 @@ private:
|
||||
};
|
||||
|
||||
InternalState internalState = STATE_NONE;
|
||||
CommunicationStep communicationStep = CommunicationStep::DATA;
|
||||
bool commandExecuted = false;
|
||||
|
||||
#if OBSW_ENHANCED_PRINTOUT == 1
|
||||
PeriodicOperationDivider* debugDivider;
|
||||
#endif
|
||||
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user