Index: firmware/App/Controllers/Thermistors.c =================================================================== diff -u -rf6016459473bdb85aebe393c07cd580b973d7247 -r0b17c6271cdc3c55697a74ecaadb477d9c8f5687 --- firmware/App/Controllers/Thermistors.c (.../Thermistors.c) (revision f6016459473bdb85aebe393c07cd580b973d7247) +++ firmware/App/Controllers/Thermistors.c (.../Thermistors.c) (revision 0b17c6271cdc3c55697a74ecaadb477d9c8f5687) @@ -18,7 +18,7 @@ #define THERMISTORS_DATA_PUBLISH_INTERVAL ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) ///< Thermistors publish data time interval. #define THERMISTORS_ADC_READ_INTERVAL ( MS_PER_SECOND / ( 2 * TASK_GENERAL_INTERVAL ) ) ///< Thermistors ADC read time interval. #define ADC_FPGA_READ_DELAY_COUNT 1.0 ///< FGPA read delay upon startup. -#define TWELVE_BIT_RESOLUTION 4096.0 ///< 12 bit resolution conversion. +#define TWELVE_BIT_RESOLUTION 4096U ///< 12 bit resolution conversion. #define THERMISTOR_REFERENCE_VOLTAGE 3.0 ///< Thermistors source voltage. #define THERMISTOR_REFERENCE_RESISTOR_AT_25 10000.0 ///< Thermistors reference resistor in ohms. #define THERMISTOR_REFERENCE_TEMPERATURE 298.0 ///< Thermistors reference temperature in kelvin. @@ -27,22 +27,22 @@ #define CELSIUS_TO_KELVIN_CONVERSION 273.15 ///< Celsius to Kelvin temperature conversion. #define MIN_ALLOWED_TEMPERATURE 5.0 ///< Thermistors/sensors minimum allowed temperature reading. #define MAX_ALLOWED_TEMPERATURE 70.0 ///< Thermistors/sensors maximum allowed temperature reading. -#define MAX_ALLOWED_TEMP_OUT_OF_RANGE_COUNT ( MS_PER_SECOND / ( 4 * TASK_GENERAL_INTERVAL ) ) ///< Thermistors/sensors maximum allowed temperature out of range count. +#define MAX_ALLOWED_TEMP_OUT_OF_RANGE_PERIOD ( MS_PER_SECOND / ( 4 * TASK_GENERAL_INTERVAL ) ) ///< Thermistors/sensors maximum allowed temperature out of range period. /// Thermistors self test states typedef enum thermistors_Self_Test_States { - THERMISTROS_SELF_TEST_CHECK_RANGE = 0, ///< Thermistors self test range check - THERMISTORS_SELF_TEST_COMPLETE, ///< Thermistors self test complete - NUM_OF_THERMISTORS_SELF_TEST_STATES, ///< Number of thermistors self test states + THERMISTROS_SELF_TEST_CHECK_RANGE_STATE = 0, ///< Thermistors self test range check state + THERMISTORS_SELF_TEST_COMPLETE_STATE, ///< Thermistors self test complete state + NUM_OF_THERMISTORS_SELF_TEST_STATES, ///< Number of thermistors self test states } THERMISTORS_SELF_TEST_STATES_T; /// Thermistors exec states typedef enum thermistors_Exec_States { - THERMISTORS_EXEC_STATE_START = 0, ///< Thermistors exec state start - THERMISTORS_EXEC_STATE_GET_ADC_VALUES, ///< Thermistors exec state get ADC values - NUM_OF_THERMISTORS_EXEC_STATES, ///< Number of thermistors exec state + THERMISTORS_EXEC_STATE_START_STATE = 0, ///< Thermistors exec state start state + THERMISTORS_EXEC_STATE_GET_ADC_VALUES_STATE, ///< Thermistors exec state get ADC values state + NUM_OF_THERMISTORS_EXEC_STATES, ///< Number of thermistors exec state } THERMISTORS_EXEC_STATES_T; /// Thermistor struct @@ -54,17 +54,17 @@ } THERMISTOR_T; static SELF_TEST_STATUS_T thermistorsSelfTestReslt = SELF_TEST_STATUS_IN_PROGRESS; ///< Thermistors self test result. -static THERMISTORS_SELF_TEST_STATES_T thermistorsSelfTestState = THERMISTROS_SELF_TEST_CHECK_RANGE; ///< Thermistors self test state. -static THERMISTORS_EXEC_STATES_T thermistorsExecState = THERMISTORS_EXEC_STATE_START; ///< Thermistors exec state. +static THERMISTORS_SELF_TEST_STATES_T thermistorsSelfTestState = THERMISTROS_SELF_TEST_CHECK_RANGE_STATE; ///< Thermistors self test state. +static THERMISTORS_EXEC_STATES_T thermistorsExecState = THERMISTORS_EXEC_STATE_START_STATE; ///< Thermistors exec state. static THERMISTOR_T thermistorsStatus[ NUM_OF_THERMISTORS ]; ///< Thermistors array. static OVERRIDE_U32_T thermistorsPublishInterval = { THERMISTORS_DATA_PUBLISH_INTERVAL, THERMISTORS_DATA_PUBLISH_INTERVAL, 0, 0 }; ///< Thermistors publish time interval override. static U32 dataPublishCounter; ///< Thermistors data publish timer counter. static U32 adcReadCounter; ///< Thermistors ADC read counter. -static const F32 thermistorVoltageConvCoeff = THERMISTOR_REFERENCE_VOLTAGE - / TWELVE_BIT_RESOLUTION; ///< On board thermistor ADC to voltage conversion coefficient. -static const F32 onBoardThermistorRefTempInv = 1 / THERMISTOR_REFERENCE_TEMPERATURE; ///< On board thermistor reference inverse. +static const F32 THERMISTOR_VOLTAGE_CONV_COEFF = THERMISTOR_REFERENCE_VOLTAGE / + (F32)TWELVE_BIT_RESOLUTION; ///< On board thermistor ADC to voltage conversion coefficient. +static const F32 ON_BOARD_THERMISTOR_REF_TEMP_INV = 1 / THERMISTOR_REFERENCE_TEMPERATURE; ///< On board thermistor reference inverse. // ********** private function prototypes ********** @@ -92,8 +92,8 @@ { // Reset the thermistors values for a run thermistorsSelfTestReslt = SELF_TEST_STATUS_IN_PROGRESS; - thermistorsExecState = THERMISTORS_EXEC_STATE_START; - thermistorsSelfTestState = THERMISTROS_SELF_TEST_CHECK_RANGE; + thermistorsExecState = THERMISTORS_EXEC_STATE_START_STATE; + thermistorsSelfTestState = THERMISTROS_SELF_TEST_CHECK_RANGE_STATE; dataPublishCounter = 0; // Initialize the beta values of each thermistor @@ -103,7 +103,7 @@ // Initialize a persistent alarm for thermistors temeprature out of range initPersistentAlarm( PERSISTENT_ALARM_THERMISTOR_TEMPERATURE_OUT_OF_RANGE, ALARM_ID_DG_THERMISOTRS_TEMPERATURE_OUT_OF_RANGE, - TRUE, MAX_ALLOWED_TEMP_OUT_OF_RANGE_COUNT, MAX_ALLOWED_TEMP_OUT_OF_RANGE_COUNT ); + TRUE, MAX_ALLOWED_TEMP_OUT_OF_RANGE_PERIOD, MAX_ALLOWED_TEMP_OUT_OF_RANGE_PERIOD ); } /*********************************************************************//** @@ -117,18 +117,18 @@ { switch ( thermistorsSelfTestState ) { - case THERMISTROS_SELF_TEST_CHECK_RANGE: + case THERMISTROS_SELF_TEST_CHECK_RANGE_STATE: thermistorsSelfTestState = handleSelfTestCheckRange(); break; - case THERMISTORS_SELF_TEST_COMPLETE: + case THERMISTORS_SELF_TEST_COMPLETE_STATE: // Done with POST. Do nothing. break; default: // Wrong state was called, raise an alarm SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_THERMISTORS_INVALID_SELF_TEST_STATE, thermistorsSelfTestState ); - thermistorsSelfTestState = THERMISTORS_SELF_TEST_COMPLETE; + thermistorsSelfTestState = THERMISTORS_SELF_TEST_COMPLETE_STATE; break; } @@ -146,18 +146,18 @@ { switch ( thermistorsExecState ) { - case THERMISTORS_EXEC_STATE_START: + case THERMISTORS_EXEC_STATE_START_STATE: thermistorsExecState = handleExecStart(); break; - case THERMISTORS_EXEC_STATE_GET_ADC_VALUES: + case THERMISTORS_EXEC_STATE_GET_ADC_VALUES_STATE: thermistorsExecState = handleExecGetADCValues(); break; default: // Wrong state was called, raise an alarm SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_THERMISTORS_INVALID_EXEC_STATE, thermistorsExecState ); - thermistorsExecState = THERMISTORS_EXEC_STATE_GET_ADC_VALUES; + thermistorsExecState = THERMISTORS_EXEC_STATE_GET_ADC_VALUES_STATE; break; } @@ -171,7 +171,7 @@ * a requested thermistor or temperature sensor. * @details Inputs: thermistorsStatus * @details Outputs: none - * @param thermistor to get its temperature value + * @param thermistor index to get its temperature value * @return temperature of a thermistor or temperature sensor *************************************************************************/ F32 getThermistorTemperatureValue( THERMISTORS_TEMP_SENSORS_T thermistor ) @@ -208,7 +208,7 @@ *************************************************************************/ static THERMISTORS_SELF_TEST_STATES_T handleSelfTestCheckRange( void ) { - THERMISTORS_SELF_TEST_STATES_T state = THERMISTROS_SELF_TEST_CHECK_RANGE; + THERMISTORS_SELF_TEST_STATES_T state = THERMISTROS_SELF_TEST_CHECK_RANGE_STATE; // Give a short time for FPGA to boot up and start sending the ADC reads if ( ++adcReadCounter > ADC_FPGA_READ_DELAY_COUNT ) @@ -240,7 +240,7 @@ } } // Done with POST - state = THERMISTORS_SELF_TEST_COMPLETE; + state = THERMISTORS_SELF_TEST_COMPLETE_STATE; adcReadCounter = 0; } @@ -257,12 +257,12 @@ *************************************************************************/ static THERMISTORS_EXEC_STATES_T handleExecStart( void ) { - THERMISTORS_EXEC_STATES_T state = THERMISTORS_EXEC_STATE_START; + THERMISTORS_EXEC_STATES_T state = THERMISTORS_EXEC_STATE_START_STATE; // Give a short time for FPGA to boot up and start sending the ADC reads if ( ++adcReadCounter > ADC_FPGA_READ_DELAY_COUNT ) { - state = THERMISTORS_EXEC_STATE_GET_ADC_VALUES; + state = THERMISTORS_EXEC_STATE_GET_ADC_VALUES_STATE; adcReadCounter = 0; } @@ -279,27 +279,21 @@ *************************************************************************/ static THERMISTORS_EXEC_STATES_T handleExecGetADCValues( void ) { - THERMISTORS_EXEC_STATES_T state = THERMISTORS_EXEC_STATE_GET_ADC_VALUES; + THERMISTORS_EXEC_STATES_T state = THERMISTORS_EXEC_STATE_GET_ADC_VALUES_STATE; // If time has elapsed to read the ADCs, read them all if ( ++adcReadCounter >= THERMISTORS_ADC_READ_INTERVAL ) { + // Monitor the values for a gross range check + monitorThermistors(); + // Get all the raw readings in ADC thermistorsStatus[ THERMISTOR_ONBOARD_NTC ].rawADCRead = getIntADCReading( INT_ADC_BOARD_THERMISTOR ); thermistorsStatus[ THERMISTOR_POWER_SUPPLY_1 ].rawADCRead = getIntADCReading( INT_ADC_POWER_SUPPLY_1_THERMISTOR ); thermistorsStatus[ THERMISTOR_POWER_SUPPLY_2 ].rawADCRead = getIntADCReading( INT_ADC_POWER_SUPPLY_2_THERMISTOR ); // Zero the counter for the next round of reading adcReadCounter = 0; - - // Convert and monitor functions are called here to convert and monitor - // the data at the control interval. Otherwise, they should have been called - // in the exec function, but the plan was to use the control interval - // Convert all the latest ADCs to temperature. - convertADC2Temperature(); - - // Monitor the values for a gross range check - monitorThermistors(); } return state; @@ -318,6 +312,9 @@ THERMISTORS_TEMP_SENSORS_T thermistor; F32 temperature; + // First convert the values + convertADC2Temperature(); + for ( thermistor = THERMISTOR_ONBOARD_NTC; thermistor < NUM_OF_THERMISTORS; thermistor++ ) { temperature = getThermistorTemperatureValue( thermistor ); @@ -359,7 +356,12 @@ /*********************************************************************//** * @brief * The calcualteOnBoardThemristorTemperature function converts the ADC value - * of the onboard thermistor into temperature in C + * of the onboard thermistor into temperature in C. Below are the calculation + * steps: + * voltage = ADC x 3 / 2^12 + * voltage = 3 x 10 / ( 10 + R(T) ) + * R(T) = 10000 x e^(beta x (1/T - 1/298)) + * Solve for T which is temperature in Kelvin * @details Inputs: onBoardThermistorVoltageConvCoeff, * onBoardThermistorBetaValueInv * @details Outputs: none @@ -369,24 +371,19 @@ *************************************************************************/ static F32 calculateTemperature( U32 adcValue, F32 betaValue ) { - /* - * voltage = ADC x 3 / 2^12 - * voltage = 3 x 10 / ( 10 + R(T) ) - * R(T) = 10000 x e^(beta x (1/T - 1/298)) - * Solve for T which is temperature in Kelvin - */ + // Voltage = ADC x 3 / 2^12 for 12 bits resolution and a 3V ADC // The value of 3 / 2^12 has been calculated in a const to prevent the division again - F32 const thermistorVoltage = adcValue * thermistorVoltageConvCoeff; + F32 const thermistorVoltage = adcValue * THERMISTOR_VOLTAGE_CONV_COEFF; // Calculate the thermistor resistor by solving: thermistorVoltage = (3 x 10) / (10 + R(T)) F32 const thermistorResistor = ( ( THERMISTOR_REFERENCE_RESISTOR_AT_25 * THERMISTOR_REFERENCE_VOLTAGE ) - ( THERMISTOR_REFERENCE_RESISTOR_AT_25 * thermistorVoltage ) ) / thermistorVoltage; // 1/T = Ln(thermistorResistor/10000)/3380 + 1/298 F32 const invTemperature = ( logf( thermistorResistor / THERMISTOR_REFERENCE_RESISTOR_AT_25 ) / betaValue ) + - onBoardThermistorRefTempInv; + ON_BOARD_THERMISTOR_REF_TEMP_INV; // Inverse the value to get the temperature in Kelvin and then convert it to Celsius F32 const temperature = ( 1 / invTemperature ) - CELSIUS_TO_KELVIN_CONVERSION; @@ -444,6 +441,7 @@ * TEST SUPPORT FUNCTIONS *************************************************************************/ + /*********************************************************************//** * @brief * The testSetThermistorPublishIntervalOverride function overrides the