Current transducer. For the electronic measurement of currents: DC, AC, pulsed..., with galvanic separation between the primary circuit and the secondary circuit.

Committer:
mcm
Date:
Tue Oct 10 13:54:12 2017 +0000
Revision:
3:6bc9b8543f13
Parent:
2:c865023c4b20
Child:
4:5002bb8e907b
I_RMS is calculated correctly using the improvements but there seems some errors with the global variables...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mcm 1:3766b24dab80 1 /**
mcm 1:3766b24dab80 2 * @brief LEM_HAIS.h
mcm 1:3766b24dab80 3 * @details Current transducer. For the electronic measurement of currents:
mcm 1:3766b24dab80 4 * DC, AC, pulsed..., with galvanic separation between the primary
mcm 1:3766b24dab80 5 * circuit and the secondary circuit.
mcm 1:3766b24dab80 6 * Function file.
mcm 1:3766b24dab80 7 *
mcm 1:3766b24dab80 8 *
mcm 1:3766b24dab80 9 * @return NA
mcm 1:3766b24dab80 10 *
mcm 1:3766b24dab80 11 * @author Manuel Caballero
mcm 1:3766b24dab80 12 * @date 19/September/2017
mcm 1:3766b24dab80 13 * @version 19/September/2017 The ORIGIN
mcm 1:3766b24dab80 14 * @pre NaN.
mcm 1:3766b24dab80 15 * @warning NaN
mcm 1:3766b24dab80 16 * @pre This code belongs to Nimbus Centre ( http://www.nimbus.cit.ie ).
mcm 1:3766b24dab80 17 */
mcm 1:3766b24dab80 18
mcm 1:3766b24dab80 19 #include "LEM_HAIS.h"
mcm 1:3766b24dab80 20
mcm 1:3766b24dab80 21
mcm 3:6bc9b8543f13 22 LEM_HAIS::LEM_HAIS ( PinName OUTPUT, float myVoltageDivider, float myADC_Vref, float myIPM )
mcm 1:3766b24dab80 23 : _OUTPUT ( OUTPUT )
mcm 1:3766b24dab80 24 {
mcm 2:c865023c4b20 25 _LEM_HAIS_V_OFFSET = 0.0;
mcm 3:6bc9b8543f13 26 _VOLTAGE_DIVIDER = myVoltageDivider;
mcm 3:6bc9b8543f13 27 _ADC_VREF = myADC_Vref;
mcm 3:6bc9b8543f13 28 _LEM_HAIS_IPM = myIPM;
mcm 1:3766b24dab80 29 }
mcm 1:3766b24dab80 30
mcm 1:3766b24dab80 31
mcm 1:3766b24dab80 32 LEM_HAIS::~LEM_HAIS()
mcm 1:3766b24dab80 33 {
mcm 1:3766b24dab80 34 }
mcm 1:3766b24dab80 35
mcm 1:3766b24dab80 36
mcm 1:3766b24dab80 37
mcm 1:3766b24dab80 38 /**
mcm 1:3766b24dab80 39 * @brief LEM_HAIS_GetVoltage ( void )
mcm 1:3766b24dab80 40 *
mcm 2:c865023c4b20 41 * @details It performs a new voltage measurement.
mcm 1:3766b24dab80 42 *
mcm 3:6bc9b8543f13 43 * @param[in] NaN.
mcm 1:3766b24dab80 44 *
mcm 3:6bc9b8543f13 45 * @param[out] myAuxVoltage: All the samples.
mcm 1:3766b24dab80 46 *
mcm 1:3766b24dab80 47 *
mcm 2:c865023c4b20 48 * @return The actual voltage.
mcm 1:3766b24dab80 49 *
mcm 1:3766b24dab80 50 *
mcm 1:3766b24dab80 51 * @author Manuel Caballero
mcm 1:3766b24dab80 52 * @date 19/September/2017
mcm 3:6bc9b8543f13 53 * @version 27/September/2017 RMS calculations included.
mcm 3:6bc9b8543f13 54 * 19/September/2017 The ORIGIN
mcm 1:3766b24dab80 55 * @pre NaN.
mcm 1:3766b24dab80 56 * @warning NaN.
mcm 1:3766b24dab80 57 */
mcm 3:6bc9b8543f13 58 LEM_HAIS::Vector_LEM_HAIS_voltage_t LEM_HAIS::LEM_HAIS_GetVoltage ( void )
mcm 1:3766b24dab80 59 {
mcm 3:6bc9b8543f13 60 uint32_t i = 0;
mcm 3:6bc9b8543f13 61
mcm 1:3766b24dab80 62 Vector_LEM_HAIS_voltage_t myAuxVoltage;
mcm 1:3766b24dab80 63
mcm 2:c865023c4b20 64
mcm 3:6bc9b8543f13 65 for ( i = 0; i < SAMPLE_DATA; i++ )
mcm 3:6bc9b8543f13 66 {
mcm 3:6bc9b8543f13 67 myAuxVoltage.OUTPUT_Voltage[i] = _OUTPUT.read();
mcm 3:6bc9b8543f13 68
mcm 3:6bc9b8543f13 69 wait_ms ( 1 );
mcm 3:6bc9b8543f13 70 }
mcm 2:c865023c4b20 71
mcm 1:3766b24dab80 72
mcm 1:3766b24dab80 73 return myAuxVoltage;
mcm 1:3766b24dab80 74 }
mcm 1:3766b24dab80 75
mcm 1:3766b24dab80 76
mcm 1:3766b24dab80 77
mcm 1:3766b24dab80 78 /**
mcm 3:6bc9b8543f13 79 * @brief LEM_HAIS_CalculateCurrent ( Vector_LEM_HAIS_voltage_t , float , LEM_HAIS_filter_status_t )
mcm 1:3766b24dab80 80 *
mcm 2:c865023c4b20 81 * @details It calculates the actual current.
mcm 1:3766b24dab80 82 *
mcm 1:3766b24dab80 83 * @param[in] myVoltages: Both voltages, OUTPUT and Vref voltages.
mcm 2:c865023c4b20 84 * @param[in] Device_Vref: Device voltage reference.
mcm 3:6bc9b8543f13 85 * @param[in] myFilter: If a low pass filter is enabled/disabled.
mcm 1:3766b24dab80 86 *
mcm 1:3766b24dab80 87 * @param[out] NaN.
mcm 1:3766b24dab80 88 *
mcm 1:3766b24dab80 89 *
mcm 1:3766b24dab80 90 * @return The calculated current.
mcm 1:3766b24dab80 91 *
mcm 1:3766b24dab80 92 *
mcm 1:3766b24dab80 93 * @author Manuel Caballero
mcm 1:3766b24dab80 94 * @date 19/September/2017
mcm 1:3766b24dab80 95 * @version 19/September/2017 The ORIGIN
mcm 2:c865023c4b20 96 * @pre LEM_HAIS_GetVoltage function MUST be called first.
mcm 1:3766b24dab80 97 * @warning NaN.
mcm 1:3766b24dab80 98 */
mcm 3:6bc9b8543f13 99 LEM_HAIS::Vector_LEM_HAIS_current_t LEM_HAIS::LEM_HAIS_CalculateCurrent ( Vector_LEM_HAIS_voltage_t myVoltages, float Device_Vref, LEM_HAIS_filter_status_t myFilter )
mcm 1:3766b24dab80 100 {
mcm 1:3766b24dab80 101 Vector_LEM_HAIS_current_t myAuxCurrent;
mcm 1:3766b24dab80 102
mcm 3:6bc9b8543f13 103 float myAuxSQI = 0;
mcm 3:6bc9b8543f13 104 float myAuxVol = 0;
mcm 3:6bc9b8543f13 105 float myI_filtered = 0;
mcm 3:6bc9b8543f13 106 float myI_Previousfiltered = 0;
mcm 3:6bc9b8543f13 107 uint32_t i = 0;
mcm 3:6bc9b8543f13 108
mcm 1:3766b24dab80 109
mcm 3:6bc9b8543f13 110
mcm 3:6bc9b8543f13 111 // Check if we want to use a low pass filter
mcm 3:6bc9b8543f13 112 if ( myFilter == FILTER_ENABLED )
mcm 3:6bc9b8543f13 113 {
mcm 3:6bc9b8543f13 114 myI_Previousfiltered = _VOLTAGE_DIVIDER * _ADC_VREF * myVoltages.OUTPUT_Voltage[0];
mcm 3:6bc9b8543f13 115 i = 1;
mcm 3:6bc9b8543f13 116 }
mcm 3:6bc9b8543f13 117 else
mcm 3:6bc9b8543f13 118 i = 0;
mcm 3:6bc9b8543f13 119
mcm 1:3766b24dab80 120
mcm 3:6bc9b8543f13 121 // Calculate the RMS current
mcm 3:6bc9b8543f13 122 for ( ; i < SAMPLE_DATA; i++ )
mcm 3:6bc9b8543f13 123 {
mcm 3:6bc9b8543f13 124 myAuxVol = _VOLTAGE_DIVIDER * _ADC_VREF * myVoltages.OUTPUT_Voltage[i];
mcm 3:6bc9b8543f13 125
mcm 3:6bc9b8543f13 126 if ( myFilter == FILTER_ENABLED )
mcm 3:6bc9b8543f13 127 {
mcm 3:6bc9b8543f13 128 myI_filtered = ( ( 1 - 0.5 ) * myAuxVol ) + ( 0.5 * myI_Previousfiltered );
mcm 3:6bc9b8543f13 129
mcm 3:6bc9b8543f13 130 myI_Previousfiltered = myI_filtered;
mcm 3:6bc9b8543f13 131
mcm 3:6bc9b8543f13 132 myAuxSQI = ( 8.0 / 5.0 ) * ( ( myI_filtered + _LEM_HAIS_V_OFFSET ) - Device_Vref ) * _LEM_HAIS_IPM;
mcm 3:6bc9b8543f13 133 }
mcm 3:6bc9b8543f13 134 else
mcm 3:6bc9b8543f13 135 myAuxSQI = ( 8.0 / 5.0 ) * ( ( myAuxVol + _LEM_HAIS_V_OFFSET ) - Device_Vref ) * _LEM_HAIS_IPM;
mcm 3:6bc9b8543f13 136
mcm 3:6bc9b8543f13 137
mcm 3:6bc9b8543f13 138 myAuxSQI *= myAuxSQI;
mcm 3:6bc9b8543f13 139
mcm 3:6bc9b8543f13 140
mcm 3:6bc9b8543f13 141 myAuxCurrent.Current += myAuxSQI;
mcm 3:6bc9b8543f13 142 }
mcm 3:6bc9b8543f13 143
mcm 3:6bc9b8543f13 144
mcm 3:6bc9b8543f13 145 if ( myFilter == FILTER_ENABLED )
mcm 3:6bc9b8543f13 146 i = 1;
mcm 3:6bc9b8543f13 147 else
mcm 3:6bc9b8543f13 148 i = 0;
mcm 3:6bc9b8543f13 149
mcm 3:6bc9b8543f13 150
mcm 3:6bc9b8543f13 151 myAuxCurrent.Current /= ( float )( SAMPLE_DATA - i );
mcm 3:6bc9b8543f13 152 myAuxCurrent.Current = sqrt( myAuxCurrent.Current );
mcm 3:6bc9b8543f13 153
mcm 3:6bc9b8543f13 154
mcm 1:3766b24dab80 155
mcm 1:3766b24dab80 156 return myAuxCurrent;
mcm 1:3766b24dab80 157 }
mcm 2:c865023c4b20 158
mcm 2:c865023c4b20 159
mcm 2:c865023c4b20 160
mcm 2:c865023c4b20 161 /**
mcm 2:c865023c4b20 162 * @brief LEM_HAIS_SetAutoOffset ( float , float )
mcm 2:c865023c4b20 163 *
mcm 2:c865023c4b20 164 * @details It calculates the offset automatically ( at 0A current ).
mcm 2:c865023c4b20 165 *
mcm 2:c865023c4b20 166 * @param[in] ADC_Vref: Voltage reference for the ADC.
mcm 2:c865023c4b20 167 *
mcm 2:c865023c4b20 168 * @param[out] NaN.
mcm 2:c865023c4b20 169 *
mcm 2:c865023c4b20 170 *
mcm 2:c865023c4b20 171 * @return The actual offset volatge.
mcm 2:c865023c4b20 172 *
mcm 2:c865023c4b20 173 *
mcm 2:c865023c4b20 174 * @author Manuel Caballero
mcm 2:c865023c4b20 175 * @date 19/September/2017
mcm 2:c865023c4b20 176 * @version 19/September/2017 The ORIGIN
mcm 2:c865023c4b20 177 * @pre NaN.
mcm 2:c865023c4b20 178 * @warning This test has to be perfomed at 0A ( no current through
mcm 2:c865023c4b20 179 * the sensor at all ).
mcm 2:c865023c4b20 180 */
mcm 3:6bc9b8543f13 181 float LEM_HAIS::LEM_HAIS_SetAutoOffset ( float Device_Vref )
mcm 2:c865023c4b20 182 {
mcm 2:c865023c4b20 183 uint32_t i = 0;
mcm 2:c865023c4b20 184 float myVoltage = 0;
mcm 2:c865023c4b20 185
mcm 2:c865023c4b20 186
mcm 2:c865023c4b20 187 // Calculate the average, get a new measurement every 0.25s
mcm 2:c865023c4b20 188 for ( i = 0; i < CALIBRATION_AVERAGE; i++ ){
mcm 2:c865023c4b20 189 myVoltage += _OUTPUT.read();
mcm 2:c865023c4b20 190 wait ( 0.25 );
mcm 2:c865023c4b20 191 }
mcm 2:c865023c4b20 192
mcm 2:c865023c4b20 193 myVoltage /= ( float )CALIBRATION_AVERAGE;
mcm 2:c865023c4b20 194
mcm 2:c865023c4b20 195
mcm 2:c865023c4b20 196 // Store the offset
mcm 3:6bc9b8543f13 197 _LEM_HAIS_V_OFFSET = Device_Vref - ( _VOLTAGE_DIVIDER * myVoltage * _ADC_VREF );
mcm 3:6bc9b8543f13 198
mcm 3:6bc9b8543f13 199 //_LEM_HAIS_V_OFFSET = Device_Vref - ( 2.0 * myVoltage * 3.3 );
mcm 2:c865023c4b20 200
mcm 2:c865023c4b20 201
mcm 2:c865023c4b20 202
mcm 2:c865023c4b20 203 return _LEM_HAIS_V_OFFSET;
mcm 2:c865023c4b20 204 }