Index: firmware/App/Controllers/DialysateFlow.c =================================================================== diff -u -r9570255f5b939ca9579564ef54c9d032337a1124 -r59543c15efd37e0e23269768df9a1cb9b6a3d296 --- firmware/App/Controllers/DialysateFlow.c (.../DialysateFlow.c) (revision 9570255f5b939ca9579564ef54c9d032337a1124) +++ firmware/App/Controllers/DialysateFlow.c (.../DialysateFlow.c) (revision 59543c15efd37e0e23269768df9a1cb9b6a3d296) @@ -53,7 +53,7 @@ static S32 measuredFlowReadingsSum = 0; ///< Raw flow reading sums for averaging. static U32 flowFilterCounter = 0; ///< Flow filtering counter. -//static DG_FLOW_SENSORS_CAL_RECORD_T flowSensorsCalRecord; ///< Flow sensors calibration record. +static DG_FLOW_SENSORS_CAL_RECORD_T flowSensorsCalRecord; ///< Flow sensors calibration record. // ********** private function prototypes ********** @@ -70,9 +70,6 @@ *************************************************************************/ void initDialysateFlowMeter( void ) { - // Initialize the persistent alarm for flow out of upper and lower range - initPersistentAlarm( ALARM_ID_DIALYSATE_FLOW_RATE_OUT_OF_RANGE, FLOW_OUT_OF_RANGE_PERSISTENT_INTERVAL, FLOW_OUT_OF_RANGE_PERSISTENT_INTERVAL ); - // Initialize the variables measuredFlowReadingsSum = 0; flowFilterCounter = 0; @@ -96,30 +93,25 @@ // Update sum for flow average calculation measuredFlowReadingsSum += (S32)dialysateFlowReading; -#if 0 // TODO - implement calibration // Check if a new calibration is available if ( TRUE == isNewCalibrationRecordAvailable() ) { // Get the new calibration data and check its validity processCalibrationData(); } -#endif // Read flow at the control set if ( ++flowFilterCounter >= FLOW_SAMPLES_TO_AVERAGE ) { F32 flow = DIALYSATE_FLOW_ADC_TO_LPM_FACTOR / ( (F32)measuredFlowReadingsSum * FLOW_AVERAGE_MULTIPLIER ); // Convert flow sensor period to L/min -#if 0 // TODO - implement calibration + // Apply calibration to flow sensor reading - measuredDialysateFlowRateLPM.data = pow(flow, 4) * flowSensorsCalRecord.flowSensors[ CAL_DATA_RO_PUMP_FLOW_SENSOR ].fourthOrderCoeff + - pow(flow, 3) * flowSensorsCalRecord.flowSensors[ CAL_DATA_RO_PUMP_FLOW_SENSOR ].thirdOrderCoeff + - pow(flow, 2) * flowSensorsCalRecord.flowSensors[ CAL_DATA_RO_PUMP_FLOW_SENSOR ].secondOrderCoeff + - flow * flowSensorsCalRecord.flowSensors[ CAL_DATA_RO_PUMP_FLOW_SENSOR ].gain + - flowSensorsCalRecord.flowSensors[ CAL_DATA_RO_PUMP_FLOW_SENSOR ].offset; -#else - // Calibration of the dialysate flow sensor will be implemented later - measuredDialysateFlowRateLPM.data = flow; -#endif + measuredDialysateFlowRateLPM.data = pow(flow, 4) * flowSensorsCalRecord.flowSensors[ CAL_DATA_DIALYSATE_FLOW_SENSOR ].fourthOrderCoeff + + pow(flow, 3) * flowSensorsCalRecord.flowSensors[ CAL_DATA_DIALYSATE_FLOW_SENSOR ].thirdOrderCoeff + + pow(flow, 2) * flowSensorsCalRecord.flowSensors[ CAL_DATA_DIALYSATE_FLOW_SENSOR ].secondOrderCoeff + + flow * flowSensorsCalRecord.flowSensors[ CAL_DATA_DIALYSATE_FLOW_SENSOR ].gain + + flowSensorsCalRecord.flowSensors[ CAL_DATA_DIALYSATE_FLOW_SENSOR ].offset; + // If the flow is less than a certain value, FPGA will return 0xFFFF meaning that the flow is 0. if ( FLOW_SENSOR_ZERO_READING == dialysateFlowReading ) { @@ -146,9 +138,33 @@ static BOOL processCalibrationData( void ) { BOOL status = TRUE; + U32 sensor; - // TODO - implement calibration for this sensor + // Get the calibration record from NVDataMgmt + DG_FLOW_SENSORS_CAL_RECORD_T calData = getDGFlowSensorsCalibrationRecord(); + for ( sensor = 0; sensor < NUM_OF_CAL_DATA_FLOW_SENSORS; sensor++ ) + { +#ifndef SKIP_CAL_CHECK + // 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 flow sensors data is not stored in the NV memory or it was corrupted. + if ( 0 == calData.flowSensors[ sensor ].calibrationTime ) + { + + SET_ALARM_WITH_1_U32_DATA( ALARM_ID_DG_FLOW_SENSORS_INVALID_CAL_RECORD, (U32)sensor ); + status = FALSE; + } +#endif + + // The calibration data was valid, update the local copy + flowSensorsCalRecord.flowSensors[ sensor ].fourthOrderCoeff = calData.flowSensors[ sensor ].fourthOrderCoeff; + flowSensorsCalRecord.flowSensors[ sensor ].thirdOrderCoeff = calData.flowSensors[ sensor ].thirdOrderCoeff; + flowSensorsCalRecord.flowSensors[ sensor ].secondOrderCoeff = calData.flowSensors[ sensor ].secondOrderCoeff; + flowSensorsCalRecord.flowSensors[ sensor ].gain = calData.flowSensors[ sensor ].gain; + flowSensorsCalRecord.flowSensors[ sensor ].offset = calData.flowSensors[ sensor ].offset; + } + return status; }