Index: firmware/App/Controllers/LoadCell.c =================================================================== diff -u -r4af713cb53b909ba87893dc814d39af89fb8e8d2 -r87a22cbaa87daab0d7cedabc67452cead83d8630 --- firmware/App/Controllers/LoadCell.c (.../LoadCell.c) (revision 4af713cb53b909ba87893dc814d39af89fb8e8d2) +++ firmware/App/Controllers/LoadCell.c (.../LoadCell.c) (revision 87a22cbaa87daab0d7cedabc67452cead83d8630) @@ -34,13 +34,19 @@ #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. +/// Load cell data structure +typedef struct +{ + U32 measuredReadingSum; ///< Raw load cell sums for averaging + OVERRIDE_F32_T filteredWeight; ///< Latest filtered load cell weights + F32 autoCalOffset; ///< Load cell auto-calibration offset +} LOADCELL_T; + // ********** private data ********** static OVERRIDE_U32_T loadCellDataPublishInterval = { LOAD_CELL_REPORT_PERIOD, LOAD_CELL_REPORT_PERIOD, 0, 0 }; ///< Broadcast load cell data publish interval. -static U32 measuredLoadCellReadingsSum[ NUM_OF_LOAD_CELLS ]; ///< Raw load cell sums for averaging. -static OVERRIDE_F32_T filteredLoadCellWeights[ NUM_OF_LOAD_CELLS ]; ///< Latest filtered load cell weights. -static F32 autoCalOffset[ NUM_OF_LOAD_CELLS ]; ///< Auto calibration offset for load cell. +static LOADCELL_T loadcells[ NUM_OF_LOAD_CELLS ]; ///< Load cell data structures. static U32 loadCellFilterTimerCount = 0; ///< Load cell filtering timer count. static U32 loadCellDataPublicationTimerCounter = 0; ///< Load cell data publication timer counter to CAN bus. @@ -61,14 +67,13 @@ for ( i = 0; i < NUM_OF_LOAD_CELLS; i++ ) { - filteredLoadCellWeights[ i ].data = 0.0; - filteredLoadCellWeights[ i ].ovData = 0.0; - filteredLoadCellWeights[ i ].ovInitData = 0.0; - filteredLoadCellWeights[ i ].override = OVERRIDE_RESET; + loadcells[ i ].filteredWeight.data = 0.0; + loadcells[ i ].filteredWeight.ovData = 0.0; + loadcells[ i ].filteredWeight.ovInitData = 0.0; + loadcells[ i ].filteredWeight.override = OVERRIDE_RESET; - measuredLoadCellReadingsSum[ i ] = 0; - - autoCalOffset[ i ] = 0.0; + loadcells[ i ].measuredReadingSum = 0; + loadcells[ i ].autoCalOffset = 0.0; } } @@ -82,32 +87,28 @@ *************************************************************************/ void execLoadCell( void ) { + U32 ii; + // update sums for load cell average calculations - measuredLoadCellReadingsSum[ LOAD_CELL_A1 ] += getFPGALoadCellA1(); - measuredLoadCellReadingsSum[ LOAD_CELL_A2 ] += getFPGALoadCellA2(); - measuredLoadCellReadingsSum[ LOAD_CELL_B1 ] += getFPGALoadCellB1(); - measuredLoadCellReadingsSum[ LOAD_CELL_B2 ] += getFPGALoadCellB2(); + loadcells[ LOAD_CELL_A1 ].measuredReadingSum += getFPGALoadCellA1(); + loadcells[ LOAD_CELL_A2 ].measuredReadingSum += getFPGALoadCellA2(); + loadcells[ LOAD_CELL_B1 ].measuredReadingSum += getFPGALoadCellB1(); + loadcells[ LOAD_CELL_B2 ].measuredReadingSum += getFPGALoadCellB2(); // filter every 100ms if ( ++loadCellFilterTimerCount >= LOAD_CELL_SAMPLES_TO_AVERAGE ) { - loadCellFilterTimerCount = 0; + 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; - // calculate load cell average weights - filteredLoadCellWeights[ LOAD_CELL_A1 ].data = (F32)measuredLoadCellReadingsSum[ LOAD_CELL_A1 ] * - LOAD_CELL_AVERAGE_MULTIPLIER * ADC2GRAM + LOAD_CELL_ZERO_OFFSET - autoCalOffset[ LOAD_CELL_A1 ]; - filteredLoadCellWeights[ LOAD_CELL_A2 ].data = (F32)measuredLoadCellReadingsSum[ LOAD_CELL_A2 ] * - LOAD_CELL_AVERAGE_MULTIPLIER * ADC2GRAM + LOAD_CELL_ZERO_OFFSET - autoCalOffset[ LOAD_CELL_A2 ]; - filteredLoadCellWeights[ LOAD_CELL_B1 ].data = (F32)measuredLoadCellReadingsSum[ LOAD_CELL_B1 ] * - LOAD_CELL_AVERAGE_MULTIPLIER * ADC2GRAM + LOAD_CELL_ZERO_OFFSET - autoCalOffset[ LOAD_CELL_B1 ]; - filteredLoadCellWeights[ LOAD_CELL_B2 ].data = (F32)measuredLoadCellReadingsSum[ LOAD_CELL_B2 ] * - LOAD_CELL_AVERAGE_MULTIPLIER * ADC2GRAM + LOAD_CELL_ZERO_OFFSET - autoCalOffset[ LOAD_CELL_B2 ]; + // reset sums for next averaging + loadcells[ ii ].measuredReadingSum = 0; + } - // reset sums for next averaging - measuredLoadCellReadingsSum[ LOAD_CELL_A1 ] = 0; - measuredLoadCellReadingsSum[ LOAD_CELL_A2 ] = 0; - measuredLoadCellReadingsSum[ LOAD_CELL_B1 ] = 0; - measuredLoadCellReadingsSum[ LOAD_CELL_B2 ] = 0; + loadCellFilterTimerCount = 0; } // broadcast load cell data if we are at scheduled interval. @@ -123,23 +124,35 @@ /*********************************************************************//** * @brief - * The setLoadCellAutoCal function sets the load cell auto calibration offset + * The tareLoadCell function sets the load cell auto calibration offset * for a given load cell ID. * @details Inputs: none - * @details Outputs: autoCalibrationOffset[] + * @details Outputs: load cell autoCalOffset * @param loadCellID ID of load cell to set calibration offset for - * @param calOffset auto calibration offset value *************************************************************************/ -void setLoadCellAutoCal( LOAD_CELL_ID_T loadCellID, F32 calOffset ) +void tareLoadCell( LOAD_CELL_ID_T loadCellID ) { - autoCalOffset[ loadCellID ] = calOffset; + loadcells[ loadCellID ].autoCalOffset = loadcells[ loadCellID ].filteredWeight.data; } /*********************************************************************//** * @brief + * The resetLoadCellOffset function resets the load cell auto calibration offset + * to zero for a given load cell ID. + * @details Inputs: none + * @details Outputs: load cell autoCalOffset + * @param loadCellID ID of load cell to set calibration offset for + *************************************************************************/ +void resetLoadCellOffset( LOAD_CELL_ID_T loadCellID ) +{ + loadcells[ loadCellID ].autoCalOffset = 0.0; +} + +/*********************************************************************//** + * @brief * The getLoadCellFilteredWeight function gets the measured filtered load cell * weight for a given load cell ID. - * @details Inputs: filteredLoadCellWeights[] + * @details Inputs: load cell filtered 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. @@ -150,13 +163,13 @@ if ( loadCellID < NUM_OF_LOAD_CELLS ) { - if ( OVERRIDE_KEY == filteredLoadCellWeights[ loadCellID ].override ) + if ( OVERRIDE_KEY == loadcells[ loadCellID ].filteredWeight.override ) { - result = filteredLoadCellWeights[ loadCellID ].ovData; + result = loadcells[ loadCellID ].filteredWeight.ovData; } else { - result = filteredLoadCellWeights[ loadCellID ].data; + result = loadcells[ loadCellID ].filteredWeight.data; } } else @@ -196,7 +209,7 @@ * @brief * The testSetLoadCellOverride function overrides the measured load cell data. * @details Inputs: none - * @details Outputs: filteredLoadCellWeights[] + * @details Outputs: load cell filtered weight * @param loadCellID ID of the load cell to override * @param value override filtered load cell weight * @return TRUE if override successful, FALSE if not @@ -210,8 +223,8 @@ if ( TRUE == isTestingActivated() ) { result = TRUE; - filteredLoadCellWeights[ loadCellID ].ovData = value; - filteredLoadCellWeights[ loadCellID ].override = OVERRIDE_KEY; + loadcells[ loadCellID ].filteredWeight.ovData = value; + loadcells[ loadCellID ].filteredWeight.override = OVERRIDE_KEY; } } @@ -222,7 +235,7 @@ * @brief * The testResetLoadCellOverride function resets the override of the load cell. * @details Inputs: none - * @details Outputs: filteredLoadCellWeights[] + * @details Outputs: load cell filtered weight * @param loadCellID ID of the load cell to override * @return TRUE if reset successful, FALSE if not *************************************************************************/ @@ -235,8 +248,8 @@ if ( TRUE == isTestingActivated() ) { result = TRUE; - filteredLoadCellWeights[ loadCellID ].override = OVERRIDE_RESET; - filteredLoadCellWeights[ loadCellID ].ovData = filteredLoadCellWeights[ loadCellID ].ovInitData; + loadcells[ loadCellID ].filteredWeight.override = OVERRIDE_RESET; + loadcells[ loadCellID ].filteredWeight.ovData = loadcells[ loadCellID ].filteredWeight.ovInitData; } }