Index: firmware/App/Controllers/LoadCell.c =================================================================== diff -u -r06e5fe353de02fbed03e8e0f9b81c41b79e5cf18 -rab447ebbc380f4c7abc2ae283042a5d0c9e1b9cb --- firmware/App/Controllers/LoadCell.c (.../LoadCell.c) (revision 06e5fe353de02fbed03e8e0f9b81c41b79e5cf18) +++ 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,6 +30,9 @@ // ********** 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. /// Conversion factor from ADC counts to grams. static const F32 ADC2GRAM = (0.0894 * 1.1338); @@ -62,12 +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 @@ -78,6 +86,7 @@ *************************************************************************/ void initLoadCell( void ) { + U32 cell; U32 i; U32 j; @@ -111,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; + } } /*********************************************************************//** @@ -176,6 +196,33 @@ /*********************************************************************//** * @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 @@ -302,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 *************************************************************************/