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

Revision:
2:c865023c4b20
Parent:
1:3766b24dab80
Child:
3:6bc9b8543f13
--- a/LEM_HAIS.cpp	Tue Sep 19 16:30:52 2017 +0000
+++ b/LEM_HAIS.cpp	Wed Sep 20 11:40:27 2017 +0000
@@ -19,10 +19,10 @@
 #include "LEM_HAIS.h"
 
 
-LEM_HAIS::LEM_HAIS ( PinName OUTPUT, PinName VREF )
+LEM_HAIS::LEM_HAIS ( PinName OUTPUT )
     : _OUTPUT               ( OUTPUT )
-    , _VREF                 ( VREF )
 {
+    _LEM_HAIS_V_OFFSET   =   0.0;
 }
 
 
@@ -35,7 +35,7 @@
 /**
  * @brief       LEM_HAIS_GetVoltage   ( void )
  *
- * @details     It gets both voltages, OUTPUT and Vref voltage.
+ * @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.
@@ -43,7 +43,7 @@
  * @param[out]   NaN.
  *
  *
- * @return       The actual voltages.
+ * @return       The actual voltage.
  *
  *
  * @author      Manuel Caballero
@@ -56,9 +56,9 @@
 {
     Vector_LEM_HAIS_voltage_t myAuxVoltage;
     
-    myAuxVoltage.VREF_Voltage      =   _VREF.read() * ADC_Vref;
+
     myAuxVoltage.OUTPUT_Voltage    =   VoltageDivider * _OUTPUT.read() * ADC_Vref;
-
+    
 
     return   myAuxVoltage;
 }
@@ -68,10 +68,11 @@
 /**
  * @brief       LEM_HAIS_CalculateCurrent   ( Vector_LEM_HAIS_voltage_t ,uint32_t )
  *
- * @details     It gets both voltages, OUTPUT and Vref voltage.
+ * @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.
  *
@@ -82,17 +83,64 @@
  * @author      Manuel Caballero
  * @date        19/September/2017
  * @version     19/September/2017   The ORIGIN
- * @pre         NaN.
+ * @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 )
+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 - ( myVoltages.VREF_Voltage )  ) * myIPN;
+    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;
+}