Revision 0:511cdcc65d9d, committed 2017-09-25
- Comitter:
- mcm
- Date:
- Mon Sep 25 11:57:30 2017 +0000
- Commit message:
- The library was completed and tested, it works as expected.
Changed in this revision
diff -r 000000000000 -r 511cdcc65d9d VAC.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/VAC.cpp Mon Sep 25 11:57:30 2017 +0000
@@ -0,0 +1,145 @@
+/**
+ * @brief VAC.cpp
+ * @details For the electronic measurement of currents: DC, AC, pulsed,
+ * mixed ..., with a galvanic Isolation between the primary circuit
+ * (high power) and the secondary circuit (electronic circuit).
+ * Function file.
+ *
+ *
+ * @return NA
+ *
+ * @author Manuel Caballero
+ * @date 25/September/2017
+ * @version 25/September/2017 The ORIGIN
+ * @pre VAC ( VACUUMSCHMELZE ) current sensor-modules.
+ * @warning NaN
+ * @pre This code belongs to Nimbus Centre ( http://www.nimbus.cit.ie ).
+ */
+
+#include "VAC.h"
+
+
+VAC::VAC ( PinName INPUT )
+ : _INPUT ( INPUT )
+{
+ _VAC_V_OFFSET = 0.0;
+}
+
+
+VAC::~VAC()
+{
+}
+
+
+
+/**
+ * @brief VAC_GetVoltage ( float, float )
+ *
+ * @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 25/September/2017
+ * @version 25/September/2017 The ORIGIN
+ * @pre NaN.
+ * @warning NaN.
+ */
+VAC::Vector_VAC_voltage_t VAC::VAC_GetVoltage ( float VoltageDivider, float ADC_Vref )
+{
+ Vector_VAC_voltage_t myAuxVoltage;
+
+
+ myAuxVoltage.INPUT_Voltage = VoltageDivider * _INPUT.read() * ADC_Vref;
+
+
+ return myAuxVoltage;
+}
+
+
+
+/**
+ * @brief VAC_CalculateCurrent ( Vector_VAC_voltage_t ,uint32_t, float )
+ *
+ * @details It calculates the actual current.
+ *
+ * @param[in] myVoltages: Both voltages, INPUT and Vref voltages.
+ * @param[in] myKN: Turns ratio.
+ * @param[in] myRM: Measuring resistance.
+ *
+ * @param[out] NaN.
+ *
+ *
+ * @return The calculated current.
+ *
+ *
+ * @author Manuel Caballero
+ * @date 25/September/2017
+ * @version 25/September/2017 The ORIGIN
+ * @pre VAC_GetVoltage function MUST be called first.
+ * @warning NaN.
+ */
+VAC::Vector_VAC_current_t VAC::VAC_CalculateCurrent ( Vector_VAC_voltage_t myVoltages, uint32_t myIPN, float myRM )
+{
+ Vector_VAC_current_t myAuxCurrent;
+
+
+ myAuxCurrent.Current = ( ( myVoltages.INPUT_Voltage + _VAC_V_OFFSET ) * myIPN ) / myRM;
+
+
+
+ return myAuxCurrent;
+}
+
+
+
+/**
+ * @brief VAC_SetAutoOffset ( float , float )
+ *
+ * @details It calculates the offset automatically ( at 0A current ).
+ *
+ * @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 25/September/2017
+ * @version 25/September/2017 The ORIGIN
+ * @pre NaN.
+ * @warning This test has to be perfomed at 0A ( no current through
+ * the sensor at all ).
+ */
+float VAC::VAC_SetAutoOffset ( 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 += _INPUT.read();
+ wait ( 0.25 );
+ }
+
+ myVoltage /= ( float )CALIBRATION_AVERAGE;
+
+
+ // Store the offset
+ _VAC_V_OFFSET = 0.0 - ( VoltageDivider * myVoltage * ADC_Vref ); // Offset is calculated at 0A
+
+
+
+ return _VAC_V_OFFSET;
+}
diff -r 000000000000 -r 511cdcc65d9d VAC.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/VAC.h Mon Sep 25 11:57:30 2017 +0000
@@ -0,0 +1,136 @@
+/**
+ * @brief VAC.h
+ * @details For the electronic measurement of currents: DC, AC, pulsed,
+ * mixed ..., with a galvanic Isolation between the primary circuit
+ * (high power) and the secondary circuit (electronic circuit).
+ * Header file.
+ *
+ *
+ * @return NA
+ *
+ * @author Manuel Caballero
+ * @date 25/September/2017
+ * @version 25/September/2017 The ORIGIN
+ * @pre VAC ( VACUUMSCHMELZE ) current sensor-modules.
+ * @warning NaN
+ * @pre This code belongs to Nimbus Centre ( http://www.nimbus.cit.ie ).
+ */
+#ifndef VAC_H
+#define VAC_H
+
+#include "mbed.h"
+
+
+/**
+ Example:
+
+#include "mbed.h"
+#include "VAC.h"
+
+VAC myCurrentTransducer ( p20 );
+Serial pc ( USBTX, USBRX );
+
+Ticker newReading;
+DigitalOut myled1 ( LED1 );
+DigitalOut myled2 ( LED2 );
+
+
+VAC::Vector_VAC_voltage_t myVoltages;
+float myVoltageOffset = 0;
+
+
+void readDATA ( void )
+{
+ VAC::Vector_VAC_current_t myCurrent;
+
+ myled2 = 1;
+
+ myVoltages = myCurrentTransducer.VAC_GetVoltage ( 1.0, 3.3 ); // NO Voltage divider that is why the numer 1, ADC Vref = 3.3 V
+ myCurrent = myCurrentTransducer.VAC_CalculateCurrent ( myVoltages, 2000, 100 ); // KN = 2000, RM = 100 Ohms
+
+ pc.printf( "Vout: %0.5f V IP: %0.5f A\r\n", myVoltages.INPUT_Voltage, myCurrent.Current );
+
+ myled2 = 0;
+}
+
+
+int main()
+{
+ pc.baud ( 115200 );
+
+ // [ OPTIONAL ] CALIBRATION. It calculates the offset to calibrate the future measurements.
+ myled1 = 1;
+
+ // It reads PIN3 from the device. NOTE: This MUST be done at 0A current!!!
+ myVoltageOffset = myCurrentTransducer.VAC_SetAutoOffset ( 1.0, 3.3 ); // NO Voltage divider that is why the numer 1, ADC Vref = 3.3 V
+ myled1 = 0;
+
+ pc.printf( "Voff: %0.5f V\r\n", myVoltageOffset );
+ // CALIBRATION ends here
+
+
+ newReading.attach( &readDATA, 0.5 ); // the address of the function to be attached ( readDATA ) and the interval ( 0.5s )
+
+
+ // Let the callbacks take care of everything
+ while(1)
+ {
+ sleep();
+ }
+}
+
+*/
+
+
+/*!
+ Library for the VAC Current transducer.
+*/
+class VAC
+{
+public:
+#define CALIBRATION_AVERAGE 10 // Change it if you wish to calculate the offset with less measurements
+
+
+
+#ifndef VECTOR_STRUCT_H
+#define VECTOR_STRUCT_H
+ typedef struct {
+ float Current;
+ } Vector_VAC_current_t;
+
+ typedef struct {
+ float INPUT_Voltage;
+ } Vector_VAC_voltage_t;
+#endif
+
+
+ /** Create an VAC object connected to the specified pins.
+ *
+ * @param INPUT Vout from the device
+ */
+ VAC ( PinName INPUT );
+
+ /** Delete VAC object.
+ */
+ ~VAC();
+
+ /** It gets the voltage.
+ */
+ Vector_VAC_voltage_t VAC_GetVoltage ( float VoltageDivider = 1.0, float ADC_Vref = 3.3 );
+
+ /** It calculates the offset automatically ( at 0A current ).
+ */
+ float VAC_SetAutoOffset ( float VoltageDivider = 1.0, float ADC_Vref = 3.3 );
+
+ /** It calculates the current.
+ */
+ Vector_VAC_current_t VAC_CalculateCurrent ( Vector_VAC_voltage_t myVoltages, uint32_t myKN, float myRM );
+
+
+
+private:
+ AnalogIn _INPUT;
+ float _VAC_V_OFFSET;
+};
+
+#endif