Index: firmware/App/Controllers/BloodFlow.c =================================================================== diff -u -r5e85dc21e918ecdae841f8398bca20ff8823c52b -rabbad386f4cc94f315300dffef321fe8c03fbd52 --- firmware/App/Controllers/BloodFlow.c (.../BloodFlow.c) (revision 5e85dc21e918ecdae841f8398bca20ff8823c52b) +++ firmware/App/Controllers/BloodFlow.c (.../BloodFlow.c) (revision abbad386f4cc94f315300dffef321fe8c03fbd52) @@ -161,8 +161,6 @@ static MOTOR_DIR_T bloodPumpDirectionSet = MOTOR_DIR_FORWARD; ///< Currently set blood flow direction static PUMP_CONTROL_MODE_T bloodPumpControlMode = PUMP_CONTROL_MODE_CLOSED_LOOP; ///< Requested blood pump control mode. static PUMP_CONTROL_MODE_T bloodPumpControlModeSet = PUMP_CONTROL_MODE_CLOSED_LOOP; ///< Currently set blood pump control mode. -static F32 bloodFlowCalGain = 1.0; ///< Blood flow calibration gain. -static F32 bloodFlowCalOffset = 0.0; ///< Blood flow calibration offset. /// Interval (in task intervals) at which to publish blood flow data to CAN bus. static OVERRIDE_U32_T bloodFlowDataPublishInterval = { BLOOD_FLOW_DATA_PUB_INTERVAL, BLOOD_FLOW_DATA_PUB_INTERVAL, BLOOD_FLOW_DATA_PUB_INTERVAL, 0 }; @@ -195,6 +193,7 @@ static U08 lastBloodFlowSlowPacketReadCtr = 0; ///< Previous read counter for the blood flow slow packets. static U08 lastBloodPumpDirectionCount = 0; ///< Previous pump direction error count reported by FPGA. static U08 lastBloodFlowCommErrorCount = 0; ///< Previous BP flow sensor comm error count. +static HD_FLOW_SENSORS_CAL_RECORD_T bloodFlowCalRecord; ///< Blood flow sensor calibration record. // ********** private function prototypes ********** @@ -215,7 +214,8 @@ static void checkBloodPumpSpeeds( void ); static void checkBloodPumpFlowAgainstSpeed( void ); static void checkBloodPumpMCCurrent( void ); -static void checkBloodFlowSensorSignalStrength( void ); +static void checkBloodFlowSensorSignalStrength( void ); +static BOOL processCalibrationData( void ); static U32 getPublishBloodFlowDataInterval( void ); /*********************************************************************//** @@ -438,20 +438,34 @@ * @brief * The execBloodFlowMonitor function executes the blood flow monitor. * @details Inputs: none - * @details Outputs: measuredBloodFlowRate, adcBloodPumpMCSpeedRPM, adcBloodPumpMCCurrentmA + * @details Outputs: measuredBloodFlowRate, adcBloodPumpMCSpeedRPM, + * adcBloodPumpMCCurrentmA * @return none *************************************************************************/ void execBloodFlowMonitor( void ) { + // Check if a new calibration is available + if ( TRUE == isNewCalibrationRecordAvailable() ) + { + // Get the new calibration data and check its validity + processCalibrationData(); + } + HD_OP_MODE_T opMode = getCurrentOperationMode(); U16 bpRPM = getIntADCReading( INT_ADC_BLOOD_PUMP_SPEED ); U16 bpmA = getIntADCReading( INT_ADC_BLOOD_PUMP_MOTOR_CURRENT ); - F32 bpFlow = ( getFPGABloodFlow() * bloodFlowCalGain ) + bloodFlowCalOffset; U08 fpReadCtr = getFPGABloodFlowFastPacketReadCounter(); U08 spReadCtr = getFPGABloodFlowSlowPacketReadCounter(); U08 flowErrorCtr = getFPGABloodFlowErrorCounter(); U08 flowStatus = getFPGABloodFlowMeterStatus(); + F32 fpgaBloodFlow = getFPGABloodFlow(); + F32 bpFlow = pow(fpgaBloodFlow, 4) * bloodFlowCalRecord.hdFlowSensors[ CAL_DATA_HD_BLOOD_FLOW_SENSOR ].fourthOrderCoeff + + pow(fpgaBloodFlow, 3) * bloodFlowCalRecord.hdFlowSensors[ CAL_DATA_HD_BLOOD_FLOW_SENSOR ].thirdOrderCoeff + + pow(fpgaBloodFlow, 2) * bloodFlowCalRecord.hdFlowSensors[ CAL_DATA_HD_BLOOD_FLOW_SENSOR ].secondOrderCoeff + + fpgaBloodFlow * bloodFlowCalRecord.hdFlowSensors[ CAL_DATA_HD_BLOOD_FLOW_SENSOR ].gain + + bloodFlowCalRecord.hdFlowSensors[ CAL_DATA_HD_BLOOD_FLOW_SENSOR ].offset; + #ifndef DISABLE_PUMP_FLOW_CHECKS if ( TRUE == isPersistentAlarmTriggered( ALARM_ID_HD_BP_FLOW_SENSOR_ERROR, ( flowErrorCtr != lastBloodFlowCommErrorCount ) ) ) { @@ -1232,6 +1246,52 @@ } } #endif +} + +/*********************************************************************//** + * @brief + * The processCalibrationData function gets the calibration data and makes + * sure it is valid by checking the calibration date. The calibration date + * should not be 0. + * @details Inputs: none + * @details Outputs: bloodFlowCalRecord + * @return TRUE if the calibration record is valid, otherwise FALSE + *************************************************************************/ +static BOOL processCalibrationData( void ) +{ + BOOL status = TRUE; + + // Get the calibration record from NVDataMgmt + HD_FLOW_SENSORS_CAL_RECORD_T calData = getHDFlowSensorsCalibrationRecord(); + + // Check if the calibration data that was received from NVDataMgmt is legitimate + // The calibration date item should not be zero. If the calibration date is 0, + // then the blood flow sensors data is not stored in the NV memory or it was corrupted. + if ( 0 == calData.hdFlowSensors[ CAL_DATA_HD_BLOOD_FLOW_SENSOR ].calibrationTime ) + { +#ifndef SKIP_CAL_CHECK + activateAlarmNoData( ALARM_ID_HD_BLOOD_FLOW_INVALID_CAL_RECORD ); + status = FALSE; +#endif + } + + // The calibration data was valid, update the local copy + bloodFlowCalRecord.hdFlowSensors[ CAL_DATA_HD_BLOOD_FLOW_SENSOR ].fourthOrderCoeff = + calData.hdFlowSensors[ CAL_DATA_HD_BLOOD_FLOW_SENSOR ].fourthOrderCoeff; + + bloodFlowCalRecord.hdFlowSensors[ CAL_DATA_HD_BLOOD_FLOW_SENSOR ].thirdOrderCoeff = + calData.hdFlowSensors[ CAL_DATA_HD_BLOOD_FLOW_SENSOR ].thirdOrderCoeff; + + bloodFlowCalRecord.hdFlowSensors[ CAL_DATA_HD_BLOOD_FLOW_SENSOR ].secondOrderCoeff = + calData.hdFlowSensors[ CAL_DATA_HD_BLOOD_FLOW_SENSOR ].secondOrderCoeff; + + bloodFlowCalRecord.hdFlowSensors[ CAL_DATA_HD_BLOOD_FLOW_SENSOR ].gain = + calData.hdFlowSensors[ CAL_DATA_HD_BLOOD_FLOW_SENSOR ].gain; + + bloodFlowCalRecord.hdFlowSensors[ CAL_DATA_HD_BLOOD_FLOW_SENSOR ].offset = + calData.hdFlowSensors[ CAL_DATA_HD_BLOOD_FLOW_SENSOR ].offset; + + return status; } /*********************************************************************//** @@ -1244,27 +1304,21 @@ *************************************************************************/ SELF_TEST_STATUS_T execBloodFlowTest( void ) { - SELF_TEST_STATUS_T result = SELF_TEST_STATUS_FAILED; + SELF_TEST_STATUS_T result = SELF_TEST_STATUS_IN_PROGRESS; U08 const bfmStatus = getFPGABloodFlowMeterStatus(); - // Get the flow sensors calibration record - HD_FLOW_SENSORS_CAL_RECORD_T cal = getHDFlowSensorsCalibrationRecord(); - // Retrieve blood flow sensor calibration data and check for sensor connected status if ( BFM_SENSOR_PARAM_CORRUPT_STATUS != bfmStatus ) { -#ifndef DISABLE_CAL_CHECK - if ( cal.hdFlowSensors[ CAL_DATA_HD_BLOOD_FLOW_SENSOR ].calibrationTime == 0 ) + BOOL calStatus = processCalibrationData(); + + if ( TRUE == calStatus ) { - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_BLOOD_FLOW_INVALID_CALIBRATION, cal.hdFlowSensors[ CAL_DATA_HD_BLOOD_FLOW_SENSOR ].calibrationTime, - cal.hdFlowSensors[ CAL_DATA_HD_BLOOD_FLOW_SENSOR ].crc ); + result = SELF_TEST_STATUS_PASSED; } else -#endif { - bloodFlowCalGain = cal.hdFlowSensors[ CAL_DATA_HD_BLOOD_FLOW_SENSOR ].gain; - bloodFlowCalOffset = cal.hdFlowSensors[ CAL_DATA_HD_BLOOD_FLOW_SENSOR ].offset; - result = SELF_TEST_STATUS_PASSED; + result = SELF_TEST_STATUS_FAILED; } } else