Index: firmware/App/Controllers/LoadCell.c =================================================================== diff -u -r64b37cc2955d04bbc77ea41940b1b1b30c06651b -rab447ebbc380f4c7abc2ae283042a5d0c9e1b9cb --- firmware/App/Controllers/LoadCell.c (.../LoadCell.c) (revision 64b37cc2955d04bbc77ea41940b1b1b30c06651b) +++ firmware/App/Controllers/LoadCell.c (.../LoadCell.c) (revision ab447ebbc380f4c7abc2ae283042a5d0c9e1b9cb) @@ -15,8 +15,11 @@ * ***************************************************************************/ +#include // For load cells calibration calculations + #include "FPGA.h" #include "LoadCell.h" +#include "NVDataMgmt.h" #include "SystemCommMessages.h" #include "TaskPriority.h" @@ -27,30 +30,32 @@ // ********** private definitions ********** +// TODO check the maximum weight on the load cells in tare. There was 1500 grams limit +// but it has been removed. Check the load cells data sheet. + #define LOAD_CELL_REPORT_PERIOD (100 / TASK_PRIORITY_INTERVAL) ///< Broadcast load cell values message every 100 ms. -#define LOAD_CELL_SAMPLES_TO_AVERAGE (100 / TASK_PRIORITY_INTERVAL) ///< Averaging load cell data over the reporting interval. -#define LOAD_CELL_AVERAGE_MULTIPLIER (1.0 / (F32)LOAD_CELL_SAMPLES_TO_AVERAGE) ///< Optimization - multiplying is faster than dividing. -// TODO - gain and offset for load cells should be read from NV Data calibration record. -#define ADC2GRAM (0.0894 * 1.1338) ///< Conversion factor from ADC counts to grams. -#define LOAD_CELL_ZERO_OFFSET -1215.0 ///< Zero offset (in grams). TODO - right now, this is empty reservoir weight. +/// Conversion factor from ADC counts to grams. +static const F32 ADC2GRAM = (0.0894 * 1.1338); +#define LOAD_CELL_ZERO_OFFSET -1500.0 ///< Zero offset (in grams). TODO - right now, this is empty reservoir weight. +#define LOAD_CELL_FILTER_ALPHA 0.05 ///< Alpha factor for the alpha filter used on load cell readings. -#define SIZE_OF_SMALL_LOAD_CELL_AVG 10 ///< Small load cell moving average has 10 samples. -#define SIZE_OF_LARGE_LOAD_CELL_AVG 40 ///< Large load cell moving average has 40 samples. +#define SIZE_OF_SMALL_LOAD_CELL_AVG 100 ///< Small load cell moving average has 100 raw samples @ 10ms intervals (1-second). +#define SIZE_OF_LARGE_LOAD_CELL_AVG 40 ///< Large load cell moving average has 40 samples from small filter @ 100ms intervals (4-second). /// Load cell data structure. typedef struct { - U32 measuredReadingSum; ///< Raw load cell sums for averaging - OVERRIDE_F32_T filteredWeight; ///< Latest filtered load cell weights + U32 rawReading; ///< Latest raw load cell reading + OVERRIDE_F32_T weight; ///< Latest load cell weight F32 autoCalOffset; ///< Load cell auto-calibration offset F32 smallFilterReadings[ SIZE_OF_SMALL_LOAD_CELL_AVG ]; ///< Load cell samples for small load cell moving average F32 smallFilterTotal; ///< Small filter rolling total - used to calc small load cell moving average - F32 smallFilteredWeight; ///< Load cell small filtered (8 sample) weight + F32 smallFilteredWeight; ///< Load cell small filtered (100 100Hz raw sample) weight F32 largeFilterReadings[ SIZE_OF_LARGE_LOAD_CELL_AVG ]; ///< Load cell samples for large load cell moving average F32 largeFilterTotal; ///< Large filter rolling total - used to calc small load cell moving average - F32 largeFilteredWeight; ///< Load cell large filtered (32 sample) weight + F32 largeFilteredWeight; ///< Load cell large filtered (40 10Hz filtered sample) weight } LOADCELL_T; // ********** private data ********** @@ -63,10 +68,14 @@ static U32 smallReadingsIdx; ///< Index for next sample in load cell small rolling average sample array. static U32 largeReadingsIdx; ///< Index for next sample in load cell large rolling average sample array. +static DG_LOAD_CELLS_CAL_RECORD_T loadCellsCalRecord; ///< Load cells calibration record. +// TODO - gain and offset for load cells should be read from NV Data calibration record. + // ********** private function prototypes ********** static U32 getLoadCellDataPublishInterval( void ); +static BOOL processCalibrationData( void ); /*********************************************************************//** * @brief @@ -77,6 +86,7 @@ *************************************************************************/ void initLoadCell( void ) { + U32 cell; U32 i; U32 j; @@ -85,12 +95,13 @@ for ( i = 0; i < NUM_OF_LOAD_CELLS; i++ ) { - loadcells[ i ].filteredWeight.data = 0.0; - loadcells[ i ].filteredWeight.ovData = 0.0; - loadcells[ i ].filteredWeight.ovInitData = 0.0; - loadcells[ i ].filteredWeight.override = OVERRIDE_RESET; + loadcells[ i ].rawReading = 0; - loadcells[ i ].measuredReadingSum = 0; + loadcells[ i ].weight.data = 0.0; + loadcells[ i ].weight.ovData = 0.0; + loadcells[ i ].weight.ovInitData = 0.0; + loadcells[ i ].weight.override = OVERRIDE_RESET; + loadcells[ i ].autoCalOffset = 0.0; loadcells[ i ].largeFilterTotal = 0.0; @@ -109,6 +120,17 @@ loadcells[ i ].largeFilterReadings[ j ] = 0.0; } } + + // Set all the load cells' calibration values to benign values + for ( cell = CAL_DATA_LOAD_CELL_A1; cell < NUM_OF_CAL_DATA_LOAD_CELLS; cell++ ) + { + // Reset the calibration variables + loadCellsCalRecord.loadCells[ cell ].fourthOrderCoeff = 0.0; + loadCellsCalRecord.loadCells[ cell ].thirdOrderCoeff = 0.0; + loadCellsCalRecord.loadCells[ cell ].secondOrderCoeff = 0.0; + loadCellsCalRecord.loadCells[ cell ].gain = 1.0; + loadCellsCalRecord.loadCells[ cell ].offset = 0.0; + } } /*********************************************************************//** @@ -124,37 +146,38 @@ U32 ii; // update sums for load cell average calculations - loadcells[ LOAD_CELL_RESERVOIR_1_PRIMARY ].measuredReadingSum += getFPGALoadCellA1(); - loadcells[ LOAD_CELL_RESERVOIR_1_BACKUP ].measuredReadingSum += getFPGALoadCellA2(); - loadcells[ LOAD_CELL_RESERVOIR_2_PRIMARY ].measuredReadingSum += getFPGALoadCellB1(); - loadcells[ LOAD_CELL_RESERVOIR_2_BACKUP ].measuredReadingSum += getFPGALoadCellB2(); + loadcells[ LOAD_CELL_RESERVOIR_1_PRIMARY ].rawReading = getFPGALoadCellA1(); + loadcells[ LOAD_CELL_RESERVOIR_1_BACKUP ].rawReading = getFPGALoadCellA2(); + loadcells[ LOAD_CELL_RESERVOIR_2_PRIMARY ].rawReading = getFPGALoadCellB1(); + loadcells[ LOAD_CELL_RESERVOIR_2_BACKUP ].rawReading = getFPGALoadCellB2(); - // filter every 100ms - if ( ++loadCellFilterTimerCount >= LOAD_CELL_SAMPLES_TO_AVERAGE ) + // Rolling average of last 100 raw samples in small filter + for ( ii = 0; ii < NUM_OF_LOAD_CELLS; ++ii ) { - for ( ii = 0; ii < NUM_OF_LOAD_CELLS; ++ii ) - { - // calculate load cell average weights - loadcells[ ii ].filteredWeight.data = (F32)loadcells[ ii ].measuredReadingSum * LOAD_CELL_AVERAGE_MULTIPLIER * - ADC2GRAM + LOAD_CELL_ZERO_OFFSET - loadcells[ ii ].autoCalOffset; + loadcells[ ii ].weight.data = (F32)loadcells[ ii ].rawReading * ADC2GRAM + LOAD_CELL_ZERO_OFFSET - loadcells[ ii ].autoCalOffset; - // reset sums for next averaging - loadcells[ ii ].measuredReadingSum = 0; + // Update small filter with new weight sample + loadcells[ ii ].smallFilterTotal -= loadcells[ ii ].smallFilterReadings[ smallReadingsIdx ]; + loadcells[ ii ].smallFilterReadings[ smallReadingsIdx ] = getLoadCellWeight( (LOAD_CELL_ID_T)ii ); + loadcells[ ii ].smallFilterTotal += getLoadCellWeight( (LOAD_CELL_ID_T)ii ); + loadcells[ ii ].smallFilteredWeight = loadcells[ ii ].smallFilterTotal / (F32)SIZE_OF_SMALL_LOAD_CELL_AVG; + } - // Update filtered with new filtered weight sample and moving averages - loadcells[ ii ].smallFilterTotal -= loadcells[ ii ].smallFilterReadings[ smallReadingsIdx ]; - loadcells[ ii ].smallFilterReadings[ smallReadingsIdx ] = loadcells[ ii ].filteredWeight.data; - loadcells[ ii ].smallFilterTotal += loadcells[ ii ].filteredWeight.data; - loadcells[ ii ].smallFilteredWeight = loadcells[ ii ].smallFilterTotal / (F32)SIZE_OF_SMALL_LOAD_CELL_AVG; + smallReadingsIdx = INC_WRAP( smallReadingsIdx, 0, SIZE_OF_SMALL_LOAD_CELL_AVG - 1 ); + // filter every 100ms + if ( ++loadCellFilterTimerCount >= LOAD_CELL_REPORT_PERIOD ) + { + for ( ii = 0; ii < NUM_OF_LOAD_CELLS; ++ii ) + { + // Update large filter with new small filter weight sample loadcells[ ii ].largeFilterTotal -= loadcells[ ii ].largeFilterReadings[ largeReadingsIdx ]; - loadcells[ ii ].largeFilterReadings[ largeReadingsIdx ] = loadcells[ ii ].filteredWeight.data; - loadcells[ ii ].largeFilterTotal += loadcells[ ii ].filteredWeight.data; + loadcells[ ii ].largeFilterReadings[ largeReadingsIdx ] = loadcells[ ii ].smallFilteredWeight; + loadcells[ ii ].largeFilterTotal += loadcells[ ii ].smallFilteredWeight; loadcells[ ii ].largeFilteredWeight = loadcells[ ii ].largeFilterTotal / (F32)SIZE_OF_LARGE_LOAD_CELL_AVG; } loadCellFilterTimerCount = 0; - smallReadingsIdx = INC_WRAP( smallReadingsIdx, 0, SIZE_OF_SMALL_LOAD_CELL_AVG - 1 ); largeReadingsIdx = INC_WRAP( largeReadingsIdx, 0, SIZE_OF_LARGE_LOAD_CELL_AVG - 1 ); } @@ -163,14 +186,43 @@ { loadCellDataPublicationTimerCounter = 0; - // broadcast load cell data - broadcastLoadCellData( getLoadCellFilteredWeight( LOAD_CELL_RESERVOIR_1_PRIMARY ), getLoadCellFilteredWeight( LOAD_CELL_RESERVOIR_1_BACKUP ), - getLoadCellFilteredWeight( LOAD_CELL_RESERVOIR_2_PRIMARY ), getLoadCellFilteredWeight( LOAD_CELL_RESERVOIR_2_BACKUP ) ); + // broadcast small filtered load cell data + broadcastLoadCellData( loadcells[ LOAD_CELL_RESERVOIR_1_PRIMARY ].smallFilteredWeight, + loadcells[ LOAD_CELL_RESERVOIR_1_BACKUP ].smallFilteredWeight, + loadcells[ LOAD_CELL_RESERVOIR_2_PRIMARY ].smallFilteredWeight, + loadcells[ LOAD_CELL_RESERVOIR_2_BACKUP ].smallFilteredWeight ); } } /*********************************************************************//** * @brief + * The execLoadCellsSelfTest function executes the load cell self test. + * It gets the calibration record from NVDataMgmt and checks whether the + * values have a calibration date. + * @details Inputs: none + * @details Outputs: + * @return result of the load cell self test + *************************************************************************/ +SELF_TEST_STATUS_T execLoadCellsSelfTest ( void ) +{ + SELF_TEST_STATUS_T result = SELF_TEST_STATUS_IN_PROGRESS; + + BOOL calStatus = processCalibrationData(); + + if ( TRUE == calStatus ) + { + result = SELF_TEST_STATUS_PASSED; + } + else + { + result = SELF_TEST_STATUS_FAILED; + } + + return result; +} + +/*********************************************************************//** + * @brief * The tareLoadCell function sets the load cell auto calibration offset * for a given load cell ID. * @details Inputs: none @@ -180,7 +232,7 @@ void tareLoadCell( LOAD_CELL_ID_T loadCellID ) { // Add old auto calibration offset to get back to actual weight value - loadcells[ loadCellID ].autoCalOffset = ( loadcells[ loadCellID ].filteredWeight.data + loadcells[ loadCellID ].autoCalOffset ); + loadcells[ loadCellID ].autoCalOffset = ( loadcells[ loadCellID ].smallFilteredWeight + loadcells[ loadCellID ].autoCalOffset ); } /*********************************************************************//** @@ -198,26 +250,26 @@ /*********************************************************************//** * @brief - * The getLoadCellFilteredWeight function gets the measured filtered load cell - * weight for a given load cell ID. - * @details Inputs: load cell filtered weight + * The getLoadCellWeight function gets the measured load cell weight for + * a given load cell ID. + * @details Inputs: load cell weight * @details Outputs: none - * @param loadCellID ID of load cell to get filtered weight for - * @return the filtered load cell weight for the given load cell ID. + * @param loadCellID ID of load cell to get weight for + * @return the load cell weight for the given load cell ID. *************************************************************************/ -F32 getLoadCellFilteredWeight( LOAD_CELL_ID_T loadCellID ) +F32 getLoadCellWeight( LOAD_CELL_ID_T loadCellID ) { F32 result = 0; if ( loadCellID < NUM_OF_LOAD_CELLS ) { - if ( OVERRIDE_KEY == loadcells[ loadCellID ].filteredWeight.override ) + if ( OVERRIDE_KEY == loadcells[ loadCellID ].weight.override ) { - result = loadcells[ loadCellID ].filteredWeight.ovData; + result = loadcells[ loadCellID ].weight.ovData; } else { - result = loadcells[ loadCellID ].filteredWeight.data; + result = loadcells[ loadCellID ].weight.data; } } else @@ -237,7 +289,7 @@ * @param loadCellID ID of load cell to get small filtered weight for * @return the small filtered load cell weight for the given load cell ID. *************************************************************************/ -F32 getLoadCellSmallFilteredWeight( LOAD_CELL_ID_T loadCellID ) +F32 getLoadCellSmallFilteredWeight( LOAD_CELL_ID_T loadCellID ) { F32 result = 0; @@ -262,7 +314,7 @@ * @param loadCellID ID of load cell to get large filtered weight for * @return the large filtered load cell weight for the given load cell ID. *************************************************************************/ -F32 getLoadCellLargeFilteredWeight( LOAD_CELL_ID_T loadCellID ) +F32 getLoadCellLargeFilteredWeight( LOAD_CELL_ID_T loadCellID ) { F32 result = 0; @@ -297,7 +349,50 @@ return result; } +/*********************************************************************//** + * @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: loadCellsCalRecord + * @details Outputs: loadCellsCalRecord + * @return TRUE if the calibration record is valid, otherwise FALSE + *************************************************************************/ +static BOOL processCalibrationData( void ) +{ + BOOL status = TRUE; + U32 cell; + // Get the calibration record from NVDataMgmt + DG_LOAD_CELLS_CAL_RECORD_T calData = getDGLoadCellsCalibrationRecord(); + + for ( cell = 0; cell < NUM_OF_CAL_DATA_LOAD_CELLS; cell++ ) + { + // 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 load cells data is not stored in the NV memory or it was corrupted. + if ( calData.loadCells[ cell ].calibrationTime == 0 ) + { +#ifndef DISABLE_CAL_CHECK + SET_ALARM_WITH_1_U32_DATA( ALARM_ID_DG_LOAD_CELLS_INVALID_CALIBRATION, (U32)cell ); +#endif + status = FALSE; + } + else + { + // The calibration data was valid, update the local copy + loadCellsCalRecord.loadCells[ cell ].fourthOrderCoeff = calData.loadCells[ cell ].fourthOrderCoeff; + loadCellsCalRecord.loadCells[ cell ].thirdOrderCoeff = calData.loadCells[ cell ].thirdOrderCoeff; + loadCellsCalRecord.loadCells[ cell ].secondOrderCoeff = calData.loadCells[ cell ].secondOrderCoeff; + loadCellsCalRecord.loadCells[ cell ].gain = calData.loadCells[ cell ].gain; + loadCellsCalRecord.loadCells[ cell ].offset = calData.loadCells[ cell ].offset; + } + } + + return status; +} + + /************************************************************************* * TEST SUPPORT FUNCTIONS *************************************************************************/ @@ -321,8 +416,8 @@ if ( TRUE == isTestingActivated() ) { result = TRUE; - loadcells[ loadCellID ].filteredWeight.ovData = value; - loadcells[ loadCellID ].filteredWeight.override = OVERRIDE_KEY; + loadcells[ loadCellID ].weight.ovData = value; + loadcells[ loadCellID ].weight.override = OVERRIDE_KEY; } } @@ -346,8 +441,8 @@ if ( TRUE == isTestingActivated() ) { result = TRUE; - loadcells[ loadCellID ].filteredWeight.override = OVERRIDE_RESET; - loadcells[ loadCellID ].filteredWeight.ovData = loadcells[ loadCellID ].filteredWeight.ovInitData; + loadcells[ loadCellID ].weight.override = OVERRIDE_RESET; + loadcells[ loadCellID ].weight.ovData = loadcells[ loadCellID ].weight.ovInitData; } }