rad sensor working

This commit is contained in:
Jakob Meier 2021-05-03 13:35:16 +02:00
parent 8d2993f88d
commit 80b32331c9
2 changed files with 81 additions and 50 deletions

View File

@ -3,12 +3,12 @@
#include <OBSWConfig.h> #include <OBSWConfig.h>
RadiationSensorHandler::RadiationSensorHandler(object_id_t objectId, object_id_t comIF, RadiationSensorHandler::RadiationSensorHandler(object_id_t objectId, object_id_t comIF,
CookieIF * comCookie) : CookieIF * comCookie) :
DeviceHandlerBase(objectId, comIF, comCookie), dataset( DeviceHandlerBase(objectId, comIF, comCookie), dataset(
this) { this) {
if (comCookie == NULL) { if (comCookie == NULL) {
sif::error << "RadiationSensorHandler: Invalid com cookie" << std::endl; sif::error << "RadiationSensorHandler: Invalid com cookie" << std::endl;
} }
} }
RadiationSensorHandler::~RadiationSensorHandler() { RadiationSensorHandler::~RadiationSensorHandler() {
@ -30,54 +30,79 @@ void RadiationSensorHandler::doShutDown(){
} }
ReturnValue_t RadiationSensorHandler::buildNormalDeviceCommand( ReturnValue_t RadiationSensorHandler::buildNormalDeviceCommand(
DeviceCommandId_t * id) { DeviceCommandId_t * id) {
*id = RAD_SENSOR::PERFORM_CONVERSION;
return buildCommandFromCommand(*id, nullptr, 0); switch (communicationStep) {
case CommunicationStep::START_CONVERSION: {
*id = RAD_SENSOR::START_CONVERSION;
communicationStep = CommunicationStep::READ_CONVERSIONS;
break;
}
case CommunicationStep::READ_CONVERSIONS: {
*id = RAD_SENSOR::READ_CONVERSIONS;
communicationStep = CommunicationStep::START_CONVERSION;
break;
}
default: {
sif::debug << "RadiationSensorHandler::buildNormalDeviceCommand: Unknwon communication "
<< "step" << std::endl;
return HasReturnvaluesIF::RETURN_OK;
}
}
return buildCommandFromCommand(*id, nullptr, 0);
} }
ReturnValue_t RadiationSensorHandler::buildTransitionDeviceCommand( ReturnValue_t RadiationSensorHandler::buildTransitionDeviceCommand(
DeviceCommandId_t * id){ DeviceCommandId_t * id){
if (internalState == InternalState::SETUP) { if (internalState == InternalState::SETUP) {
*id = RAD_SENSOR::WRITE_SETUP; *id = RAD_SENSOR::WRITE_SETUP;
} }
else { else {
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
return buildCommandFromCommand(*id, nullptr, 0); return buildCommandFromCommand(*id, nullptr, 0);
} }
ReturnValue_t RadiationSensorHandler::buildCommandFromCommand( ReturnValue_t RadiationSensorHandler::buildCommandFromCommand(
DeviceCommandId_t deviceCommand, const uint8_t * commandData, DeviceCommandId_t deviceCommand, const uint8_t * commandData,
size_t commandDataLen) { size_t commandDataLen) {
switch(deviceCommand) { switch(deviceCommand) {
case(RAD_SENSOR::WRITE_SETUP): { case(RAD_SENSOR::WRITE_SETUP): {
cmdBuffer[0] = RAD_SENSOR::SETUP_DEFINITION; cmdBuffer[0] = RAD_SENSOR::SETUP_DEFINITION;
rawPacket = cmdBuffer; rawPacket = cmdBuffer;
rawPacketLen = 1; rawPacketLen = 1;
internalState = InternalState::CONFIGURED; internalState = InternalState::CONFIGURED;
return RETURN_OK; return RETURN_OK;
} }
case(RAD_SENSOR::PERFORM_CONVERSION): { case(RAD_SENSOR::START_CONVERSION): {
/* Dummy bytes are written to remove old values from fifo */ /* First the fifo will be reset here */
cmdBuffer[0] = RAD_SENSOR::CONVERSION_DEFINITION; cmdBuffer[0] = RAD_SENSOR::RESET_DEFINITION;
cmdBuffer[1] = RAD_SENSOR::DUMMY_BYTE; cmdBuffer[1] = RAD_SENSOR::CONVERSION_DEFINITION;
rawPacket = cmdBuffer;
rawPacketLen = 2;
return RETURN_OK;
}
case(RAD_SENSOR::READ_CONVERSIONS): {
cmdBuffer[0] = RAD_SENSOR::DUMMY_BYTE;
cmdBuffer[1] = RAD_SENSOR::DUMMY_BYTE;
cmdBuffer[2] = RAD_SENSOR::DUMMY_BYTE; cmdBuffer[2] = RAD_SENSOR::DUMMY_BYTE;
cmdBuffer[3] = RAD_SENSOR::DUMMY_BYTE; cmdBuffer[3] = RAD_SENSOR::DUMMY_BYTE;
cmdBuffer[4] = RAD_SENSOR::DUMMY_BYTE; cmdBuffer[4] = RAD_SENSOR::DUMMY_BYTE;
cmdBuffer[5] = RAD_SENSOR::DUMMY_BYTE; cmdBuffer[5] = RAD_SENSOR::DUMMY_BYTE;
rawPacket = cmdBuffer; rawPacket = cmdBuffer;
rawPacketLen = RAD_SENSOR::READ_SIZE; rawPacketLen = RAD_SENSOR::READ_SIZE;
return RETURN_OK; return RETURN_OK;
} }
default: default:
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED; return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
} }
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
void RadiationSensorHandler::fillCommandAndReplyMap() { void RadiationSensorHandler::fillCommandAndReplyMap() {
this->insertInCommandMap(RAD_SENSOR::WRITE_SETUP); this->insertInCommandMap(RAD_SENSOR::WRITE_SETUP);
this->insertInCommandAndReplyMap(RAD_SENSOR::PERFORM_CONVERSION, 1, &dataset, this->insertInCommandMap(RAD_SENSOR::START_CONVERSION);
this->insertInCommandAndReplyMap(RAD_SENSOR::READ_CONVERSIONS, 1, &dataset,
RAD_SENSOR::READ_SIZE); RAD_SENSOR::READ_SIZE);
} }
@ -90,12 +115,12 @@ ReturnValue_t RadiationSensorHandler::scanForReply(const uint8_t *start,
ReturnValue_t RadiationSensorHandler::interpretDeviceReply(DeviceCommandId_t id, ReturnValue_t RadiationSensorHandler::interpretDeviceReply(DeviceCommandId_t id,
const uint8_t *packet) { const uint8_t *packet) {
switch (id) { switch (id) {
case RAD_SENSOR::PERFORM_CONVERSION: { case RAD_SENSOR::READ_CONVERSIONS: {
PoolReadGuard readSet(&dataset); PoolReadGuard readSet(&dataset);
dataset.temperatureCelcius = (*(packet) << 8 | *(packet + 1)) * 0.125; dataset.temperatureCelcius = (*(packet) << 8 | *(packet + 1)) * 0.125;
dataset.channel0 = (*(packet + 2) << 8 | *(packet + 3)); dataset.channel0 = (*(packet + 2) << 8 | *(packet + 3));
dataset.channel1 = (*(packet + 4) << 8 | *(packet + 5)); dataset.channel1 = (*(packet + 4) << 8 | *(packet + 5));
#if OBSW_VERBOSE_LEVEL >= 1 && DEBUG_RAD_SENSOR #if OBSW_VERBOSE_LEVEL >= 1 && DEBUG_RAD_SENSOR
sif::info << "Radiation sensor temperature: " << dataset.temperatureCelcius << " °C" sif::info << "Radiation sensor temperature: " << dataset.temperatureCelcius << " °C"
<< std::endl; << std::endl;
@ -104,15 +129,15 @@ ReturnValue_t RadiationSensorHandler::interpretDeviceReply(DeviceCommandId_t id,
sif::info << "Radiation sensor temperature ADC value channel 1: " << dataset.channel1 sif::info << "Radiation sensor temperature ADC value channel 1: " << dataset.channel1
<< std::endl; << std::endl;
#endif #endif
break; break;
} }
default: { default: {
sif::debug << "RadiationSensorHandler::interpretDeviceReply: Unknown reply id" << std::endl; sif::debug << "RadiationSensorHandler::interpretDeviceReply: Unknown reply id" << std::endl;
return DeviceHandlerIF::UNKNOWN_DEVICE_REPLY; return DeviceHandlerIF::UNKNOWN_DEVICE_REPLY;
} }
} }
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
void RadiationSensorHandler::setNormalDatapoolEntriesInvalid(){ void RadiationSensorHandler::setNormalDatapoolEntriesInvalid(){
@ -120,7 +145,7 @@ void RadiationSensorHandler::setNormalDatapoolEntriesInvalid(){
} }
uint32_t RadiationSensorHandler::getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo){ uint32_t RadiationSensorHandler::getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo){
return 5000; return 5000;
} }
ReturnValue_t RadiationSensorHandler::initializeLocalDataPool(localpool::DataPool& localDataPoolMap, ReturnValue_t RadiationSensorHandler::initializeLocalDataPool(localpool::DataPool& localDataPoolMap,

View File

@ -10,8 +10,9 @@ namespace RAD_SENSOR {
* temperature sensor. * temperature sensor.
*/ */
static const DeviceCommandId_t WRITE_SETUP = 0x1; static const DeviceCommandId_t WRITE_SETUP = 0x1;
static const DeviceCommandId_t PERFORM_CONVERSION = 0x2; static const DeviceCommandId_t START_CONVERSION = 0x2;
static const DeviceCommandId_t READ_CONVERSIONS = 0x3; static const DeviceCommandId_t READ_CONVERSIONS = 0x3;
/** /**
* @brief This is the configuration byte which will be written to the setup register after * @brief This is the configuration byte which will be written to the setup register after
* power on. * power on.
@ -34,6 +35,11 @@ namespace RAD_SENSOR {
*/ */
static const uint8_t CONVERSION_DEFINITION = 0b10001001; static const uint8_t CONVERSION_DEFINITION = 0b10001001;
/**
* @brief Writing this value resets the fifo of the MAX1227.
*/
static const uint8_t RESET_DEFINITION = 0b00011000;
static const uint8_t DUMMY_BYTE = 0xFF; static const uint8_t DUMMY_BYTE = 0xFF;
static const uint8_t RAD_SENSOR_DATA_SET_ID = READ_CONVERSIONS; static const uint8_t RAD_SENSOR_DATA_SET_ID = READ_CONVERSIONS;