optimizing analog port in formula

This commit is contained in:
Noah Piqué 2021-11-29 10:33:58 +01:00
parent 7613d510fb
commit c8bcffdc57

View File

@ -54,10 +54,7 @@
#define ADC_RES (4096) // ADC resolution: 12 bits
#define NR_OF_ADCS 5 // number of internal adc channels
// definitions of internal adc
#define INT_ADC_REF_HI (3.2968f) // HI int. reference voltage for conversion
#define INT_ADC_REF_LO (0.0f) // LO int. reference voltage for conversion
#define INT_ADC_REF (INT_ADC_REF_HI-INT_ADC_REF_LO)// int. reference voltage for conversion
#define INT_ADC_REF (3.2968f)// int. reference voltage for conversion
#define BUFFER_SIZE NR_OF_ADCS * 2
#define BUFFER_HALF_SIZE NR_OF_ADCS
@ -82,12 +79,6 @@
// Description: Definition of local enumerations (visible by this module only).
//=================================================================================================
// the number of ADCs
typedef enum
{
eADC1 = 0,
eNumberOfADCs,
} EnADC;
//=================================================================================================
// Section: STRUCTURES
@ -107,8 +98,7 @@ typedef struct
LOCAL FLOAT m_aflValues[ANPI_eInNumberOfInputs]; // values
LOCAL U16 m_au16ADCDataBuffer[eNumberOfADCs][BUFFER_SIZE];
LOCAL U32 m_au32ADCRawData[eNumberOfADCs*NR_OF_ADCS] = {0}; // raw adc values
LOCAL U16 m_au16ADCDataBuffer[BUFFER_SIZE];
LOCAL osThreadId_t m_pstThreadID = NULL;
LOCAL osEventFlagsId_t m_pstEventID = NULL;
@ -124,32 +114,20 @@ LOCAL osMutexId_t m_pstMutexID = NULL;
LOCAL CONST FLOAT m_aflConversionFactor[ANPI_eInNumberOfInputs] =
{
34.103f * 1.0f / ADC_RES * INT_ADC_REF, // 00 ANPI_eControlVoltage
10.0f / ADC_RES * INT_ADC_REF, // 01 ANPI_eSupplyVoltage24V
-(1.0f / (2.0f / 10.0f)) / ADC_RES * INT_ADC_REF, // 02 ANPI_eSupplyCurrent24V
10.0f * 1.0f / ADC_RES * INT_ADC_REF, // 03 ANPI_eOutputVoltage
(1.0f / (2.0f / 10.0f)) / ADC_RES * INT_ADC_REF, // 04 ANPI_eOutputCurrent
10, // 01 ANPI_eSupplyVoltage24V
-5, // 02 ANPI_eSupplyCurrent24V
10, // 03 ANPI_eOutputVoltage
5, // 04 ANPI_eOutputCurrent
};
// offsets for the values before it gets multiplied
// Order must fit enumeration "ANPI_EnAnalogInput"
LOCAL CONST FLOAT m_aflOffset1[ANPI_eInNumberOfInputs] =
{
0.0f, // 00 ANPI_eControlVoltage
0.0f, // 01 ANPI_eSupplyVoltage24V
1.65f * ADC_RES / INT_ADC_REF, // 02 ANPI_eSupplyCurrent24V
0.0f, // 03 ANPI_eOutputVoltage
1.65f * ADC_RES / INT_ADC_REF, // 04 ANPI_eOutputCurrent
};
// offsets for the values after it gets multiplied
// Order must fit enumeration "ANPI_EnAnalogInput"
LOCAL CONST FLOAT m_aflOffset2[ANPI_eInNumberOfInputs] =
LOCAL CONST FLOAT m_aflOffset[ANPI_eInNumberOfInputs] =
{
20.088f, // 00 ANPI_eControlVoltage
0.0f, // 01 ANPI_eSupplyVoltage24V
0.0f, // 02 ANPI_eSupplyCurrent24V
4.5f * INT_ADC_REF, // 03 ANPI_eOutputVoltage
0.0f, // 04 ANPI_eOutputCurrent
2.5f, // 02 ANPI_eSupplyCurrent24V
4.5f, // 03 ANPI_eOutputVoltage
2.5f, // 04 ANPI_eOutputCurrent
};
// initial values. Order must fit enumeration "ANPI_EnAnalogInput"
@ -262,49 +240,33 @@ VOID ANPI_vTask( PVOID arg )
{
U32 u32Flags;
U16 u16Offset;
FLOAT u_adc;
static U32 m_au32ADCRawData[ANPI_eInNumberOfInputs];
osDelay( 1 ); // Wait 1ms to have a Valid Value
HAL_ADC_Start_DMA( &hadc1, (PU32)&m_au16ADCDataBuffer[eADC1][0], BUFFER_SIZE);
HAL_ADC_Start_DMA( &hadc1, (PU32)&m_au16ADCDataBuffer[0], BUFFER_SIZE);
while ( TRUE )
{
u32Flags = osEventFlagsWait( m_pstEventID, ANPI_FLAGS_ALL, osFlagsWaitAny, osWaitForever );
if( u32Flags & ANPI_ADC_FULL_COMPLETE )
{
u16Offset = BUFFER_HALF_SIZE;
}
if( u32Flags & ANPI_ADC_FULL_COMPLETE ) u16Offset = BUFFER_HALF_SIZE;
else if( u32Flags & ANPI_ADC_HALF_COMPLETE ) u16Offset = 0;
else continue;
if( u32Flags & ANPI_ADC_HALF_COMPLETE )
{
u16Offset = 0;
}
// reset the sum for calculating the mean
memset( m_au32ADCRawData, 0, sizeof(m_au32ADCRawData) );
// ... multiply by the conversion factor and add the offset
// aquire mutex: access to m_afcValues blocked for ANPI_flGetInputValue
osMutexAcquire( m_pstMutexID, osWaitForever ); // aquire mutex
// save the values in the buffer...
// copy the values in the buffer...
for(U16 u16Cnt = 0; u16Cnt < BUFFER_HALF_SIZE; u16Cnt++ )
{
m_au32ADCRawData[ u16Cnt ] = m_au16ADCDataBuffer[eADC1][u16Cnt + u16Offset];
}
// ... multiply by the conversion factor and add the offset
// multiply conversion factor and add the offset
for(U16 u16Cnt = 0; u16Cnt < ANPI_eInNumberOfInputs; u16Cnt++ )
{
if(u16Cnt == ANPI_eOutputVoltage && !PECO_isEnabled()){
m_aflValues[u16Cnt] = 0.0f;
continue;
}
m_aflValues[u16Cnt] = ((((FLOAT)m_au32ADCRawData[u16Cnt] / OVERSAMPLING_DIVISOR) - (FLOAT)m_aflOffset1[u16Cnt] ) *
(FLOAT)m_aflConversionFactor[u16Cnt]) - (FLOAT)m_aflOffset2[u16Cnt];
u_adc = (FLOAT)m_au32ADCRawData[u16Cnt] / OVERSAMPLING_DIVISOR / ADC_RES * INT_ADC_REF;
m_aflValues[u16Cnt] = u_adc * m_aflConversionFactor[u16Cnt] - m_aflOffset[u16Cnt];
}
osMutexRelease( m_pstMutexID ); // release mutex