Current transducer. For the electronic measurement of currents: DC, AC, pulsed..., with galvanic separation between the primary circuit and the secondary circuit.
LEM_HAIS.cpp@4:5002bb8e907b, 2017-10-10 (annotated)
- Committer:
- mcm
- Date:
- Tue Oct 10 14:56:04 2017 +0000
- Revision:
- 4:5002bb8e907b
- Parent:
- 3:6bc9b8543f13
A structure with several parameters were declared, these parameters will define the system. ; ; NOTE: All the test are performed using RMS theory. Sampling at 1ms for 1 second ( = 1000 samples ).; ; The library was tested and it works as expected.
Who changed what in which revision?
User | Revision | Line number | New 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 | 4:5002bb8e907b | 21 | LEM_HAIS::LEM_HAIS ( PinName OUTPUT ) |
mcm | 1:3766b24dab80 | 22 | : _OUTPUT ( OUTPUT ) |
mcm | 1:3766b24dab80 | 23 | { |
mcm | 1:3766b24dab80 | 24 | } |
mcm | 1:3766b24dab80 | 25 | |
mcm | 1:3766b24dab80 | 26 | LEM_HAIS::~LEM_HAIS() |
mcm | 1:3766b24dab80 | 27 | { |
mcm | 1:3766b24dab80 | 28 | } |
mcm | 1:3766b24dab80 | 29 | |
mcm | 1:3766b24dab80 | 30 | |
mcm | 1:3766b24dab80 | 31 | |
mcm | 1:3766b24dab80 | 32 | /** |
mcm | 1:3766b24dab80 | 33 | * @brief LEM_HAIS_GetVoltage ( void ) |
mcm | 1:3766b24dab80 | 34 | * |
mcm | 2:c865023c4b20 | 35 | * @details It performs a new voltage measurement. |
mcm | 1:3766b24dab80 | 36 | * |
mcm | 3:6bc9b8543f13 | 37 | * @param[in] NaN. |
mcm | 1:3766b24dab80 | 38 | * |
mcm | 3:6bc9b8543f13 | 39 | * @param[out] myAuxVoltage: All the samples. |
mcm | 1:3766b24dab80 | 40 | * |
mcm | 1:3766b24dab80 | 41 | * |
mcm | 2:c865023c4b20 | 42 | * @return The actual voltage. |
mcm | 1:3766b24dab80 | 43 | * |
mcm | 1:3766b24dab80 | 44 | * |
mcm | 1:3766b24dab80 | 45 | * @author Manuel Caballero |
mcm | 1:3766b24dab80 | 46 | * @date 19/September/2017 |
mcm | 3:6bc9b8543f13 | 47 | * @version 27/September/2017 RMS calculations included. |
mcm | 3:6bc9b8543f13 | 48 | * 19/September/2017 The ORIGIN |
mcm | 4:5002bb8e907b | 49 | * @pre If SAMPLE_DATA = 1000, this funcion will last for 1000 * 1ms |
mcm | 4:5002bb8e907b | 50 | * = 1 second. |
mcm | 1:3766b24dab80 | 51 | * @warning NaN. |
mcm | 1:3766b24dab80 | 52 | */ |
mcm | 4:5002bb8e907b | 53 | LEM_HAIS::LEM_HAIS_voltage_t LEM_HAIS::LEM_HAIS_GetVoltage ( void ) |
mcm | 1:3766b24dab80 | 54 | { |
mcm | 3:6bc9b8543f13 | 55 | uint32_t i = 0; |
mcm | 3:6bc9b8543f13 | 56 | |
mcm | 4:5002bb8e907b | 57 | LEM_HAIS_voltage_t myAuxVoltage; |
mcm | 1:3766b24dab80 | 58 | |
mcm | 2:c865023c4b20 | 59 | |
mcm | 3:6bc9b8543f13 | 60 | for ( i = 0; i < SAMPLE_DATA; i++ ) |
mcm | 3:6bc9b8543f13 | 61 | { |
mcm | 3:6bc9b8543f13 | 62 | myAuxVoltage.OUTPUT_Voltage[i] = _OUTPUT.read(); |
mcm | 3:6bc9b8543f13 | 63 | |
mcm | 3:6bc9b8543f13 | 64 | wait_ms ( 1 ); |
mcm | 3:6bc9b8543f13 | 65 | } |
mcm | 2:c865023c4b20 | 66 | |
mcm | 1:3766b24dab80 | 67 | |
mcm | 1:3766b24dab80 | 68 | return myAuxVoltage; |
mcm | 1:3766b24dab80 | 69 | } |
mcm | 1:3766b24dab80 | 70 | |
mcm | 1:3766b24dab80 | 71 | |
mcm | 1:3766b24dab80 | 72 | |
mcm | 1:3766b24dab80 | 73 | /** |
mcm | 4:5002bb8e907b | 74 | * @brief LEM_HAIS_CalculateCurrent ( Vector_LEM_HAIS_voltage_t , LEM_HAIS_parameters_t , LEM_HAIS_filter_status_t ) |
mcm | 1:3766b24dab80 | 75 | * |
mcm | 2:c865023c4b20 | 76 | * @details It calculates the actual current. |
mcm | 1:3766b24dab80 | 77 | * |
mcm | 1:3766b24dab80 | 78 | * @param[in] myVoltages: Both voltages, OUTPUT and Vref voltages. |
mcm | 4:5002bb8e907b | 79 | * @param[in] myParameters: Parameters are necessary to calculate the |
mcm | 4:5002bb8e907b | 80 | * result. |
mcm | 3:6bc9b8543f13 | 81 | * @param[in] myFilter: If a low pass filter is enabled/disabled. |
mcm | 1:3766b24dab80 | 82 | * |
mcm | 1:3766b24dab80 | 83 | * @param[out] NaN. |
mcm | 1:3766b24dab80 | 84 | * |
mcm | 1:3766b24dab80 | 85 | * |
mcm | 1:3766b24dab80 | 86 | * @return The calculated current. |
mcm | 1:3766b24dab80 | 87 | * |
mcm | 1:3766b24dab80 | 88 | * |
mcm | 1:3766b24dab80 | 89 | * @author Manuel Caballero |
mcm | 1:3766b24dab80 | 90 | * @date 19/September/2017 |
mcm | 4:5002bb8e907b | 91 | * @version 10/October/2017 New parameters are sent to this function to |
mcm | 4:5002bb8e907b | 92 | * process the data in the right way. |
mcm | 4:5002bb8e907b | 93 | * 19/September/2017 The ORIGIN |
mcm | 2:c865023c4b20 | 94 | * @pre LEM_HAIS_GetVoltage function MUST be called first. |
mcm | 1:3766b24dab80 | 95 | * @warning NaN. |
mcm | 1:3766b24dab80 | 96 | */ |
mcm | 4:5002bb8e907b | 97 | LEM_HAIS::LEM_HAIS_current_t LEM_HAIS::LEM_HAIS_CalculateCurrent ( LEM_HAIS_voltage_t myVoltages, LEM_HAIS_parameters_t myParameters, LEM_HAIS_filter_status_t myFilter ) |
mcm | 1:3766b24dab80 | 98 | { |
mcm | 4:5002bb8e907b | 99 | LEM_HAIS_current_t myAuxCurrent; |
mcm | 1:3766b24dab80 | 100 | |
mcm | 3:6bc9b8543f13 | 101 | float myAuxSQI = 0; |
mcm | 3:6bc9b8543f13 | 102 | float myAuxVol = 0; |
mcm | 3:6bc9b8543f13 | 103 | float myI_filtered = 0; |
mcm | 3:6bc9b8543f13 | 104 | float myI_Previousfiltered = 0; |
mcm | 3:6bc9b8543f13 | 105 | uint32_t i = 0; |
mcm | 3:6bc9b8543f13 | 106 | |
mcm | 1:3766b24dab80 | 107 | |
mcm | 3:6bc9b8543f13 | 108 | |
mcm | 3:6bc9b8543f13 | 109 | // Check if we want to use a low pass filter |
mcm | 3:6bc9b8543f13 | 110 | if ( myFilter == FILTER_ENABLED ) |
mcm | 4:5002bb8e907b | 111 | { |
mcm | 4:5002bb8e907b | 112 | myI_Previousfiltered = myParameters.voltage_divider * myParameters.adc_reference_voltage * myVoltages.OUTPUT_Voltage[0]; |
mcm | 3:6bc9b8543f13 | 113 | i = 1; |
mcm | 3:6bc9b8543f13 | 114 | } |
mcm | 3:6bc9b8543f13 | 115 | else |
mcm | 3:6bc9b8543f13 | 116 | i = 0; |
mcm | 3:6bc9b8543f13 | 117 | |
mcm | 1:3766b24dab80 | 118 | |
mcm | 3:6bc9b8543f13 | 119 | // Calculate the RMS current |
mcm | 3:6bc9b8543f13 | 120 | for ( ; i < SAMPLE_DATA; i++ ) |
mcm | 4:5002bb8e907b | 121 | { |
mcm | 4:5002bb8e907b | 122 | myAuxVol = myParameters.voltage_divider * myParameters.adc_reference_voltage * myVoltages.OUTPUT_Voltage[i]; |
mcm | 3:6bc9b8543f13 | 123 | |
mcm | 3:6bc9b8543f13 | 124 | if ( myFilter == FILTER_ENABLED ) |
mcm | 3:6bc9b8543f13 | 125 | { |
mcm | 3:6bc9b8543f13 | 126 | myI_filtered = ( ( 1 - 0.5 ) * myAuxVol ) + ( 0.5 * myI_Previousfiltered ); |
mcm | 3:6bc9b8543f13 | 127 | |
mcm | 3:6bc9b8543f13 | 128 | myI_Previousfiltered = myI_filtered; |
mcm | 3:6bc9b8543f13 | 129 | |
mcm | 4:5002bb8e907b | 130 | myAuxSQI = ( 8.0 / 5.0 ) * ( ( myI_filtered + myParameters.lem_hais_offset_voltage ) - myParameters.lem_hais_reference_voltage ) * myParameters.lem_hais_ipm; |
mcm | 3:6bc9b8543f13 | 131 | } |
mcm | 4:5002bb8e907b | 132 | else |
mcm | 4:5002bb8e907b | 133 | myAuxSQI = ( 8.0 / 5.0 ) * ( ( myAuxVol + myParameters.lem_hais_offset_voltage ) - myParameters.lem_hais_reference_voltage ) * myParameters.lem_hais_ipm; |
mcm | 3:6bc9b8543f13 | 134 | |
mcm | 3:6bc9b8543f13 | 135 | |
mcm | 3:6bc9b8543f13 | 136 | myAuxSQI *= myAuxSQI; |
mcm | 3:6bc9b8543f13 | 137 | |
mcm | 3:6bc9b8543f13 | 138 | |
mcm | 3:6bc9b8543f13 | 139 | myAuxCurrent.Current += myAuxSQI; |
mcm | 3:6bc9b8543f13 | 140 | } |
mcm | 3:6bc9b8543f13 | 141 | |
mcm | 3:6bc9b8543f13 | 142 | |
mcm | 3:6bc9b8543f13 | 143 | if ( myFilter == FILTER_ENABLED ) |
mcm | 3:6bc9b8543f13 | 144 | i = 1; |
mcm | 3:6bc9b8543f13 | 145 | else |
mcm | 3:6bc9b8543f13 | 146 | i = 0; |
mcm | 3:6bc9b8543f13 | 147 | |
mcm | 3:6bc9b8543f13 | 148 | |
mcm | 3:6bc9b8543f13 | 149 | myAuxCurrent.Current /= ( float )( SAMPLE_DATA - i ); |
mcm | 3:6bc9b8543f13 | 150 | myAuxCurrent.Current = sqrt( myAuxCurrent.Current ); |
mcm | 3:6bc9b8543f13 | 151 | |
mcm | 3:6bc9b8543f13 | 152 | |
mcm | 1:3766b24dab80 | 153 | |
mcm | 1:3766b24dab80 | 154 | return myAuxCurrent; |
mcm | 1:3766b24dab80 | 155 | } |
mcm | 2:c865023c4b20 | 156 | |
mcm | 2:c865023c4b20 | 157 | |
mcm | 2:c865023c4b20 | 158 | |
mcm | 2:c865023c4b20 | 159 | /** |
mcm | 4:5002bb8e907b | 160 | * @brief LEM_HAIS_SetAutoOffset ( LEM_HAIS_parameters_t ) |
mcm | 2:c865023c4b20 | 161 | * |
mcm | 2:c865023c4b20 | 162 | * @details It calculates the offset automatically ( at 0A current ). |
mcm | 2:c865023c4b20 | 163 | * |
mcm | 4:5002bb8e907b | 164 | * @param[in] myParameters: voltage_divider, adc_reference_voltage. These |
mcm | 4:5002bb8e907b | 165 | * parameters are necessary to calculate the |
mcm | 4:5002bb8e907b | 166 | * result. |
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 | 4:5002bb8e907b | 176 | * @version 10/October/2017 New parameters are sent to this function to |
mcm | 4:5002bb8e907b | 177 | * process the data in the right way. |
mcm | 4:5002bb8e907b | 178 | * 19/September/2017 The ORIGIN |
mcm | 4:5002bb8e907b | 179 | * @pre If CALIBRATION_AVERAGE = 10, this function will last for |
mcm | 4:5002bb8e907b | 180 | * 10 * 0.25 = 2.5 seconds. |
mcm | 2:c865023c4b20 | 181 | * @warning This test has to be perfomed at 0A ( no current through |
mcm | 2:c865023c4b20 | 182 | * the sensor at all ). |
mcm | 2:c865023c4b20 | 183 | */ |
mcm | 4:5002bb8e907b | 184 | float LEM_HAIS::LEM_HAIS_SetAutoOffset ( LEM_HAIS_parameters_t myParameters ) |
mcm | 2:c865023c4b20 | 185 | { |
mcm | 2:c865023c4b20 | 186 | uint32_t i = 0; |
mcm | 2:c865023c4b20 | 187 | float myVoltage = 0; |
mcm | 2:c865023c4b20 | 188 | |
mcm | 2:c865023c4b20 | 189 | |
mcm | 2:c865023c4b20 | 190 | // Calculate the average, get a new measurement every 0.25s |
mcm | 2:c865023c4b20 | 191 | for ( i = 0; i < CALIBRATION_AVERAGE; i++ ){ |
mcm | 2:c865023c4b20 | 192 | myVoltage += _OUTPUT.read(); |
mcm | 2:c865023c4b20 | 193 | wait ( 0.25 ); |
mcm | 2:c865023c4b20 | 194 | } |
mcm | 2:c865023c4b20 | 195 | |
mcm | 2:c865023c4b20 | 196 | myVoltage /= ( float )CALIBRATION_AVERAGE; |
mcm | 2:c865023c4b20 | 197 | |
mcm | 2:c865023c4b20 | 198 | |
mcm | 2:c865023c4b20 | 199 | // Store the offset |
mcm | 4:5002bb8e907b | 200 | myParameters.lem_hais_offset_voltage = myParameters.lem_hais_reference_voltage - ( myParameters.voltage_divider * myVoltage * myParameters.adc_reference_voltage ); |
mcm | 2:c865023c4b20 | 201 | |
mcm | 2:c865023c4b20 | 202 | |
mcm | 4:5002bb8e907b | 203 | |
mcm | 4:5002bb8e907b | 204 | return myParameters.lem_hais_offset_voltage; |
mcm | 2:c865023c4b20 | 205 | } |