Index: firmware/App/Drivers/ConductivityTeensy.c =================================================================== diff -u -rfea37ad4659d4162d43f3e343cd5b266c2757ea9 -r4954a58fad7d100068baed367ae074ca6c132813 --- firmware/App/Drivers/ConductivityTeensy.c (.../ConductivityTeensy.c) (revision fea37ad4659d4162d43f3e343cd5b266c2757ea9) +++ firmware/App/Drivers/ConductivityTeensy.c (.../ConductivityTeensy.c) (revision 4954a58fad7d100068baed367ae074ca6c132813) @@ -46,13 +46,14 @@ #define RX_SIZE_UPDATE_MEASUREMENT_SETTINGS 1 ///< Expected response bytes length of update measurement settings cmd. #define RX_SIZE_GET_MEASUREMENT_SETTNGS ( sizeof( COND_MEASUREMENT_SETTINGS_T ) ) ///< Expected response bytes length of get measurement settings cmd. #define RX_SIZE_START_MEASUREMENT ( sizeof( COND_SENSOR_DATA_T ) ) ///< Expected response bytes length of start measurement cmd. -#define RX_SIZE_STOP_MEASUREMENT 0 ///< Expected response bytes length of stop measurement cmd. +#define RX_SIZE_STOP_MEASUREMENT 1 ///< Expected response bytes length of stop measurement cmd. #define RX_SIZE_GET_ALL_MEASUREMENTS ( 6 * RX_SIZE_START_MEASUREMENT ) ///< Expected response bytes length of get all sensor measurements cmd -#define RX_SIZE_SELECT_SENSOR 0 ///< Expected response bytes length of select sensor cmd. +#define RX_SIZE_SELECT_SENSOR 1 ///< Expected response bytes length of select sensor cmd. #define RX_SIZE_GET_SINGLE_MEASUREMENT ( sizeof( COND_SENSOR_DATA_T ) ) ///< Expected response bytes length of get single sensor measurement cmd. #define COND_STATUS_TIMEOUT_MS ( 10 * MS_PER_SECOND ) ///< Timeout before which we should receive acknowledgment from teensy #define COND_DATA_TIMEOUT_MS ( 10 * MS_PER_SECOND ) ///< Timeout before which we should receive data from teensy +#define COND_TEMP_OFFSET 25 ///< Temperature offset constant used in RTD calculations. // ********** private data ********** @@ -90,15 +91,17 @@ static COND_UPDATE_EEPROM_STATUS_T condUpdateEEPROMstatus; ///< Received update EEPROM status. static COND_UPDATE_MST_STATUS_T condUpdateSettingStatus[ MAX_COND_MST_PARAM_IDX ]; ///< Received update measurement settings status. static U08 condStopMeasurementSatus; ///< Received acknowledgment of stop measurement command +static U08 condSelectSensorStatus; ///< Received acknowledgment of select sensor status. static COND_SENSOR_DATA_T condRawMeasurement[ MAX_TEENSY_SENSOR ]; ///< Received raw sensor values (includes impedance and rtd). static COND_CALCULATED_MEASUREMENTS_T condCalculatedMeasurement[ MAX_TEENSY_SENSOR ]; ///< Calculated conductivity and temperature values static COND_COEFF_T condCoeff[ MAX_TEENSY_SENSOR ]; ///< Coefficients used to calculate conductivity and temperature values. static TEENSY_SENSOR_INDEX_T currentSelectedSensor; ///< Current selected sensor to get measurement of single sensor. Value 1 to 6. static COND_EEPROM_DATA_T eePromDataTX; ///< Transmitted EEPROM data to Teensy for update EEPROM cmd. -static COND_EEPROM_DATA_T eePromDataRX; ///< Received EEPROM data by get EEPROM data cmd. static COND_MEASUREMENT_SETTINGS_T measurementSettingsTX; ///< Transmitted measurement settings to Teensy for update measurement settings cmd. static COND_MEASUREMENT_SETTINGS_T measurementSettingsRX; ///< Received measurement settings by get measurement settings cmd. +static COND_MODELS_T currentConductivityModel = STANDARD; +static BOOL eepromInit; // Command Map static const COND_CMD_DATA_T teensyCmdMap[] = { @@ -113,6 +116,7 @@ { TEENSY_CMD_STOP_MEASUREMENT , ( U08* )"n" , RX_SIZE_STOP_MEASUREMENT }, { TEENSY_CMD_GET_ALL_MEASUREMENTS , ( U08* )"g" , RX_SIZE_GET_ALL_MEASUREMENTS }, { TEENSY_CMD_GET_SINGLE_MEASUREMENT , ( U08* )"h" , RX_SIZE_GET_SINGLE_MEASUREMENT }, + { TEENSY_CMD_SELECT_SENSOR , ( U08* )"u" , RX_SIZE_SELECT_SENSOR }, }; // ********** private function prototypes ********** @@ -169,15 +173,21 @@ static COND_COMM_STATE_T txGetSingleMeasurement( void); static COND_COMM_STATE_T rxGetSingleMeasurement( void ); -static void enqueueSingleMeasurement(TEENSY_SENSOR_INDEX_T sensorNum); +static COND_COMM_STATE_T txSelectSensor( void ); +static COND_COMM_STATE_T rxSelectSensor( void ); + static COND_PARSE_STATUS parseMeasurementSettings( const U08 *buffer, U32 len ); static COND_PARSE_STATUS parseEEPROMdata( const U08 *buffer, U32 len ); static COND_PARSE_STATUS parseConductivityMeasurements( const U08 *buffer, U32 len ); static U32 getTeensyCondId( CONDUCTIVITY_SENSORS_T sensorId ); -static void calculateConductivity( TEENSY_SENSOR_INDEX_T sensorNum ); +static void calculateConductivity( TEENSY_SENSOR_INDEX_T sensorNum, BOOL isFPSensor ); +static void calculateConductivityAly( TEENSY_SENSOR_INDEX_T sensorNum, BOOL isFPSensor ); +static void calculateConductivityUpdatedStandard( TEENSY_SENSOR_INDEX_T sensorNum, BOOL isFPSensor ); +static void calculateConductivityStandard( TEENSY_SENSOR_INDEX_T sensorNum, BOOL isFPSensor ); static void calculateTemperature( TEENSY_SENSOR_INDEX_T sensorNum ); +static void calculateResistance( TEENSY_SENSOR_INDEX_T sensorNum, BOOL isFPSensor ); /*********************************************************************//** * @brief @@ -213,24 +223,21 @@ queueRearIndex = 0; queueFrontIndex = 0; currentCmd = TEENSY_CMD_INIT_SENSOR; - + eepromInit = TRUE; condInitStatus = COND_INIT_STATUS_UNITIALIZED; condUpdateEEPROMstatus = COND_UPDATE_EEPROM_STATUS_UNITIALIZED; condStopMeasurementSatus = 0; + condSelectSensorStatus = 0; currentSelectedSensor = TEENSY_SENSOR_0; initCondDMAchannels(); - //TODO Clean up after testing. - // add init conductivity commands enqueue( TEENSY_CMD_STOP_MEASUREMENT ); enqueue( TEENSY_CMD_INIT_SENSOR ); enqueue( TEENSY_CMD_GET_INIT_STATUS ); -// initEEPROMdata( ); -// initMeasurementSettings( ); -// enqueueSingleMeasurement(2); + enqueue( TEENSY_CMD_SELECT_SENSOR ); + enqueue( TEENSY_CMD_GET_EEPROM_DATA ); - } /*********************************************************************//** @@ -480,6 +487,9 @@ case TEENSY_CMD_GET_SINGLE_MEASUREMENT: state = txGetSingleMeasurement(); break; + case TEENSY_CMD_SELECT_SENSOR: + state = txSelectSensor(); + break; default: break; } @@ -532,6 +542,9 @@ case TEENSY_CMD_GET_SINGLE_MEASUREMENT: state = rxGetSingleMeasurement( ); break; + case TEENSY_CMD_SELECT_SENSOR: + state = rxSelectSensor(); + break; default: break; @@ -828,7 +841,7 @@ if ( written > 0 ) { // Clear the DMA write comand buffer - memcpy( &condWriteCmdBuffer, 0, COND_WRITE_CMD_BUFFER_LEN ); + memset( &condWriteCmdBuffer, 0, COND_WRITE_CMD_BUFFER_LEN ); // Load the DMA write command buffer with contents of TX buffer. memcpy( &condWriteCmdBuffer, &condTxBuffer, written ); success = TRUE; @@ -1191,12 +1204,24 @@ condResponseTime = 0; // Read the data from the receive buffer - memcpy(&eePromDataRX, &condRxBuffer, teensyCmdMap[ TEENSY_CMD_GET_EEPROM_DATA ].rxSize ); -// parseStatus = parseEEPROMdata( condRxBuffer, COND_RX_BUFFER_LEN ); - + parseStatus = parseEEPROMdata(condRxBuffer, teensyCmdMap[ TEENSY_CMD_GET_EEPROM_DATA ].rxSize); // Check if parsing was done successfully if ( COND_PARSE_SUCCESS == parseStatus ) { + // Queue commands for next sensor to retrieve eeprom data. + // If this is init. + if( ( TRUE == eepromInit ) && ( currentSelectedSensor < MAX_TEENSY_SENSOR ) ) + { + currentSelectedSensor++; + enqueue( TEENSY_CMD_SELECT_SENSOR ); + enqueue( TEENSY_CMD_GET_EEPROM_DATA ); + + } + else + { + eepromInit = FALSE; + currentSelectedSensor = 0; + } // Go to the idle state to execute next cmd in the queue state = COND_COMM_STATE_IDLE; } @@ -1645,7 +1670,7 @@ COND_COMM_STATE_T state = COND_COMM_STATE_TX; U08 paramStr[ 8 ]; - snprintf( ( char* )paramStr, sizeof( paramStr ), "%d", currentSelectedSensor ); + snprintf( ( char* )paramStr, sizeof( paramStr ), "%d", ( currentSelectedSensor + 1 ) ); // Teensy doesn't 0 index // Assumes sensor has already been selected if ( TRUE == txTeensyWriteCmd( TEENSY_CMD_GET_SINGLE_MEASUREMENT, paramStr ) ) @@ -1708,24 +1733,78 @@ /*********************************************************************//** * @brief - * The enqueueSingleMeasurement function updates current select sensor in format - * acceptable to Teensy and enqueues get single measurement command. - * @details \b Inputs : none - * @details \b Outputs: currentSelectedSensor - Current Selected Sensor indexed 1 to 6. - * @param sensorNum - Teensy sensor index - * @return none + * The txSelectSensor function sends command to select a specific sensors + * connected to the Teensy. + * @details \b Inputs: none + * @details \b Outputs: condResponseTime - Time at which command was transmitted. + * @return state - Next state. *************************************************************************/ -static void enqueueSingleMeasurement(TEENSY_SENSOR_INDEX_T sensorNum) + +static COND_COMM_STATE_T txSelectSensor( void ) { - // Sensors are indexed 1 to 6 in Teensy. So send (sensorNum + 1) as parameter. - currentSelectedSensor = sensorNum + 1; + COND_COMM_STATE_T state = COND_COMM_STATE_TX; - // Enqueue get single measurement command. - enqueue(TEENSY_CMD_GET_SINGLE_MEASUREMENT); + U08 paramStr[ 8 ]; + snprintf( ( char* )paramStr, sizeof( paramStr ), "%d", ( currentSelectedSensor + 1 ) ); + + // If select sensor cmd was sent successfully + // Sensors start at 1 + if ( TRUE == txTeensyWriteCmd( TEENSY_CMD_SELECT_SENSOR, paramStr ) ) + { + // Get the current time + condResponseTime = getMSTimerCount(); + + // Go to receive state to receive the measurement data + state = COND_COMM_STATE_RX; + } + else + { + // Go to failed state + state = COND_COMM_STATE_FAILED; + } + + return state; } /*********************************************************************//** * @brief + * The rxSelectSensor function handles received response of + * select sensor command. + * @details \b Inputs: condResponseTime - Time at which command was transmitted, + * @details \b Inputs: condRxBuffer - Received response buffer + * @return state - Next state. + *************************************************************************/ +static COND_COMM_STATE_T rxSelectSensor( void ) +{ + COND_COMM_STATE_T state = COND_COMM_STATE_RX; + + // Check if a response is received in the RX buffer + BOOL recvComplete = rxTeensyReadRsp( TEENSY_CMD_SELECT_SENSOR ); + if ( TRUE == recvComplete ) + { + // Reset the timer for next use. + condResponseTime = 0; + + // Read the data from the receive buffer + memcpy(&condSelectSensorStatus, &condRxBuffer, teensyCmdMap[ TEENSY_CMD_SELECT_SENSOR ].rxSize ); + state = COND_COMM_STATE_IDLE; + + } + else if ( TRUE == didTimeout( condResponseTime, COND_DATA_TIMEOUT_MS ) ) + { + // Go to failed state + state = COND_COMM_STATE_FAILED; + } + else + { + // Do Nothing. Wait until we either receive a response OR timeout happens. + } + + return state; +} + +/*********************************************************************//** + * @brief * The parseMeasurementSettings Reads measurement settings from buffer and * stores in measurementSettingsRX. * @details \b Inputs : none @@ -1786,7 +1865,7 @@ else { // Parse and Store the data - memcpy(&eePromDataRX, buffer, expectedDataLength ); + memcpy(&condCoeff[ currentSelectedSensor ], buffer, expectedDataLength ); parseStatus = COND_PARSE_SUCCESS; } @@ -1927,34 +2006,181 @@ /*********************************************************************//** * @brief * The calculateConductivity function calculates the conductivity value. + * @details \b Inputs: currentConductivityModel - conductivity model being used. + * @details \b Outputs: condCalculatedMeasurement - calculated conductivity value + * @param sensorNum - Teensy sensor index value. + * @param isFPSensor - T/F if sensor is on FP hardware. + * @return + *************************************************************************/ +static void calculateConductivity( TEENSY_SENSOR_INDEX_T sensorNum ) +{ + BOOL isFPSensor = FALSE; + + if (sensorNum <= TEENSY_SENSOR_1 ) + { + isFPSensor = TRUE; + } + + switch ( currentConductivityModel ) + { + case ALY_LINEAR: + calculateConductivityAly( sensorNum, isFPSensor ); + break; + case UPDATED_STANDARD: + calculateConductivityUpdatedStandard( sensorNum, isFPSensor ); + break; + default: + calculateConductivityStandard( sensorNum, isFPSensor ); + break; + } +} +/*********************************************************************//** + * @brief + * The calculateConductivityAly function calculates the conductivity value + * using Aly's Linear model * @details \b Inputs: condCoeff - Conductivity Coefficients * @details \b Inputs: condRawMeasurement - Raw measurement values * @details \b Outputs: condCalculatedMeasurement - calculated conductivity value * @param sensorNum - Teensy sensor index value. + * @param isFPSensor - T/F if sensor is on FP hardware. * @return *************************************************************************/ -static void calculateConductivity( TEENSY_SENSOR_INDEX_T sensorNum ) +static void calculateConductivityAly( TEENSY_SENSOR_INDEX_T sensorNum, BOOL isFPSensor ) { F64 calculatedConductivity = 0.0; - F64 B3 = condCoeff[ sensorNum ].B3; - F64 B2 = condCoeff[ sensorNum ].B2; - F64 B1 = condCoeff[ sensorNum ].B1; - F64 B0 = condCoeff[ sensorNum ].B0; + F32 B3 = 0.0; + F32 B2 = 0.0; + F32 B1 = 0.0; + F32 B0 = 0.0; F64 R = condRawMeasurement[ sensorNum ].impRzMag; F64 Z = condRawMeasurement[ sensorNum ].rtdRzMag; + if ( TRUE == isFPSensor ) + { + B3 = condCoeff[ sensorNum ].beta3FP; + B2 = condCoeff[ sensorNum ].beta2FP; + B1 = condCoeff[ sensorNum ].beta1FP; + B0 = condCoeff[ sensorNum ].beta0FP; + } + else + { + B3 = condCoeff[ sensorNum ].beta3DD; + B2 = condCoeff[ sensorNum ].beta2DD; + B1 = condCoeff[ sensorNum ].beta1DD; + B0 = condCoeff[ sensorNum ].beta0DD; + } + calculatedConductivity = ( ( B3 * ( 1000.0 / R ) ) + ( B2 * ( Z / R ) ) + ( B1 * ( ( 100 * log( Z ) ) / R ) ) + B0 ); condCalculatedMeasurement[ sensorNum ].Conductivity = calculatedConductivity; +} +/*********************************************************************//** + * @brief + * The calculateConductivityUpdatedStandard function calculates the conductivity value. + * @details \b Inputs: condCoeff - Conductivity Coefficients + * @details \b Inputs: condRawMeasurement - Raw measurement values + * @details \b Outputs: condCalculatedMeasurement - calculated conductivity value + * @param sensorNum - Teensy sensor index value. + * @param isFPSensor - T/F if sensor is on FP hardware. + * @return + *************************************************************************/ +static void calculateConductivityUpdatedStandard( TEENSY_SENSOR_INDEX_T sensorNum, BOOL isFPSensor ) +{ + F64 calculatedConductivity = 0.0; + F64 alpha = 0.0; + F64 k = 0.0; + + if ( TRUE == isFPSensor ) + { + alpha = condCoeff[ sensorNum ].alphaLowFPUS; + k = condCoeff[ sensorNum ].kLowFPUS; + } + else + { + alpha = condCoeff[ sensorNum ].alphaHighDDUS; + k = condCoeff[ sensorNum ].kHighDDUS; + } + calculateResistance( sensorNum, isFPSensor ); + calculateTemperature( sensorNum ); + + calculatedConductivity = ( ( k / condCalculatedMeasurement[ sensorNum ].Resistance ) * + ( 1 + alpha * ( COND_TEMP_OFFSET - condCalculatedMeasurement[ sensorNum ].Temperature ) ) ); + condCalculatedMeasurement[ sensorNum ].Conductivity = calculatedConductivity; } /*********************************************************************//** * @brief + * The calculateConductivityUpdatedStandard function calculates the conductivity value. + * @details \b Inputs: condCoeff - Conductivity Coefficients + * @details \b Inputs: condRawMeasurement - Raw measurement values + * @details \b Outputs: condCalculatedMeasurement - calculated conductivity value + * @param sensorNum - Teensy sensor index value. + * @param isFPSensor - T/F if sensor is on FP hardware. + * @return + *************************************************************************/ +static void calculateConductivityStandard( TEENSY_SENSOR_INDEX_T sensorNum, BOOL isFPSensor ) +{ + F64 calculatedConductivity = 0.0; + F64 alpha = 0.0; + F64 k = 0.0; + F64 R = condRawMeasurement[ sensorNum ].impRzMag; + + if ( TRUE == isFPSensor ) + { + alpha = condCoeff[ sensorNum ].alphaLowFPS; + k = condCoeff[ sensorNum ].kLowFPS; + } + else + { + alpha = condCoeff[ sensorNum ].alphaHighDDS; + k = condCoeff[ sensorNum ].kHighDDS; + } + calculateTemperature( sensorNum ); + + calculatedConductivity = ( ( k / R ) * + ( 1 + alpha * ( COND_TEMP_OFFSET - condCalculatedMeasurement[ sensorNum ].Temperature ) ) ); + condCalculatedMeasurement[ sensorNum ].Conductivity = calculatedConductivity; +} + +/*********************************************************************//** + * @brief + * The calculateResistance function calculates the temperature values. + * @details \b Inputs: condCoeff - Conductivity Coefficients + * @details \b Inputs: condRawMeasurement - Raw measurement values + * @details \b Outputs: condCalculatedMeasurement - calculated resistance value + * @param sensorNum - Teensy sensor index value. + * @param isFPSensor - T/F if sensor is on FP hardware. + * @return + *************************************************************************/ +static void calculateResistance( TEENSY_SENSOR_INDEX_T sensorNum, BOOL isFPSensor ) +{ + + F64 calculatedResistance = 0.0; + F64 eta = 0.0; + F64 zeta = 0.0; + F64 R = condRawMeasurement[ sensorNum ].impRzMag; + + if ( TRUE == isFPSensor ) + { + eta = condCoeff[ sensorNum ].etaLowFPUS; + zeta = condCoeff[ sensorNum ].zetaLowFPUS; + } + else + { + eta = condCoeff[ sensorNum ].etaHighDDUS; + zeta = condCoeff[ sensorNum ].zetaHighDDUS; + } + calculatedResistance = ( ( eta * R ) + zeta ); + condCalculatedMeasurement[ sensorNum ].Resistance = calculatedResistance; +} + +/*********************************************************************//** + * @brief * The calculateTemperature function calculates the temperature values. * @details \b Inputs: condCoeff - Conductivity Coefficients * @details \b Inputs: condRawMeasurement - Raw measurement values @@ -1964,13 +2190,13 @@ *************************************************************************/ static void calculateTemperature( TEENSY_SENSOR_INDEX_T sensorNum ) { + F64 calculatedTemperature = 0.0; - F64 A1 = condCoeff[ sensorNum ].A1; - F64 A0 = condCoeff[ sensorNum ].A0; + F64 beta = condCoeff[ sensorNum ].beta; + F64 gamma = condCoeff[ sensorNum ].gamma; F64 Z = condRawMeasurement[ sensorNum ].rtdRzMag; - calculatedTemperature = ( ( A1 * Z ) + A0 ); - + calculatedTemperature = ( ( beta * Z ) + gamma ); condCalculatedMeasurement[ sensorNum ].Temperature = calculatedTemperature; }