Manuel Caballero / LEM-HAIS

LEM_HAIS.cpp

Committer:
mcm
Date:
2017-09-20
Revision:
2:c865023c4b20
Parent:
1:3766b24dab80
Child:
3:6bc9b8543f13

File content as of revision 2:c865023c4b20:

/**
 * @brief       LEM_HAIS.h
 * @details     Current transducer. For the electronic measurement of currents: 
 *              DC, AC, pulsed..., with galvanic separation between the primary
 *              circuit and the secondary circuit.
 *              Function file.
 *
 *
 * @return      NA
 *
 * @author      Manuel Caballero
 * @date        19/September/2017
 * @version     19/September/2017    The ORIGIN
 * @pre         NaN.
 * @warning     NaN
 * @pre         This code belongs to Nimbus Centre ( http://www.nimbus.cit.ie ).
 */

#include "LEM_HAIS.h"


LEM_HAIS::LEM_HAIS ( PinName OUTPUT )
    : _OUTPUT               ( OUTPUT )
{
    _LEM_HAIS_V_OFFSET   =   0.0;
}


LEM_HAIS::~LEM_HAIS()
{
}



/**
 * @brief       LEM_HAIS_GetVoltage   ( void )
 *
 * @details     It performs a new voltage measurement.
 *
 * @param[in]    VoltageDivider:    If there is a voltage divider before the ADC pin ( if not VoltageDivider = 1 ).
 * @param[in]    ADC_Vref:          Voltage reference for the ADC.
 *
 * @param[out]   NaN.
 *
 *
 * @return       The actual voltage.
 *
 *
 * @author      Manuel Caballero
 * @date        19/September/2017
 * @version     19/September/2017   The ORIGIN
 * @pre         NaN.
 * @warning     NaN.
 */
LEM_HAIS::Vector_LEM_HAIS_voltage_t  LEM_HAIS::LEM_HAIS_GetVoltage ( float VoltageDivider, float ADC_Vref )
{
    Vector_LEM_HAIS_voltage_t myAuxVoltage;
    

    myAuxVoltage.OUTPUT_Voltage    =   VoltageDivider * _OUTPUT.read() * ADC_Vref;
    

    return   myAuxVoltage;
}



/**
 * @brief       LEM_HAIS_CalculateCurrent   ( Vector_LEM_HAIS_voltage_t ,uint32_t )
 *
 * @details     It calculates the actual current.
 *
 * @param[in]    myVoltages:    Both voltages, OUTPUT and Vref voltages.
 * @param[in]    myIPN:         Primary Nominal rms Current.
 * @param[in]    Device_Vref:   Device voltage reference.
 *
 * @param[out]   NaN.
 *
 *
 * @return       The calculated current.
 *
 *
 * @author      Manuel Caballero
 * @date        19/September/2017
 * @version     19/September/2017   The ORIGIN
 * @pre         LEM_HAIS_GetVoltage function MUST be called first.
 * @warning     NaN.
 */
LEM_HAIS::Vector_LEM_HAIS_current_t  LEM_HAIS::LEM_HAIS_CalculateCurrent ( Vector_LEM_HAIS_voltage_t myVoltages, uint32_t myIPN, float Device_Vref )
{
    Vector_LEM_HAIS_current_t myAuxCurrent;
    
    
    myAuxCurrent.Current    =   ( 8.0 / 5.0 ) * ( ( myVoltages.OUTPUT_Voltage + _LEM_HAIS_V_OFFSET ) - Device_Vref ) * myIPN;
    


    return   myAuxCurrent;
}



/**
 * @brief       LEM_HAIS_SetAutoOffset ( float , float )
 *
 * @details     It calculates the offset automatically ( at 0A current ).
 *
 * @param[in]    Device_Vref:       Device voltage reference.
 * @param[in]    VoltageDivider:    If there is a voltage divider before the ADC pin ( if not VoltageDivider = 1 ).
 * @param[in]    ADC_Vref:          Voltage reference for the ADC.
 *
 * @param[out]   NaN.
 *
 *
 * @return       The actual offset volatge.
 *
 *
 * @author      Manuel Caballero
 * @date        19/September/2017
 * @version     19/September/2017   The ORIGIN
 * @pre         NaN.
 * @warning     This test has to be perfomed at 0A ( no current through
 *              the sensor at all ).
 */
float  LEM_HAIS::LEM_HAIS_SetAutoOffset ( float Device_Vref, float VoltageDivider, float ADC_Vref )
{
    uint32_t i          =   0;
    float    myVoltage  =   0;
    
    
    // Calculate the average, get a new measurement every 0.25s
    for ( i = 0; i < CALIBRATION_AVERAGE; i++ ){
        myVoltage   +=   _OUTPUT.read();
        wait ( 0.25 );
    }
    
    myVoltage   /=   ( float )CALIBRATION_AVERAGE;
    
    
    // Store the offset
    _LEM_HAIS_V_OFFSET   =   Device_Vref - ( VoltageDivider * myVoltage * ADC_Vref );
    


    return   _LEM_HAIS_V_OFFSET;
}