McMajan Library Pack  v 2.00
Improve your Arduino !
Ss_Energia.cpp
Go to the documentation of this file.
1 #include "Ss_Energia.h"
2 
3 /** \brief Read voltage and current data from sensors. This function calculates apparent power, real power, rms current and voltage.
4  * \param V is the pointer to voltage sensor. If NULL, Energia uses default voltage (see Ss_Global.h)
5  * \param I is the pointer to current sensor
6  * \param wavenumber number of scanned waves
7  */
8 void Energia::ReadPowerData(struct Mc_AnalogSensor * V,struct Mc_AnalogSensor * I,uint8_t wavenumber)
9 {
10  //se V è posto a null viene usata la tensione di default
11  unsigned long tstart;
12  bool st=false;
13  tstart = micros();
16 
17  ReadMiddlePoint(100);
18 
19  float istV=0,istI=0,istP=0,sqrV=0,sqrI=0,sum_sqrV=0,sum_sqrI=0,sum_inst_power=0;
20  int soglia=(1<<Mc_ADC_extended_bits)/150;
21 
22  double timelen=(double)wavenumber*1/Mc_voltage_freq*1000*1000; //0.02sec...è la lunghezza in microsecondi dell'onda completa.
23 
24  while(st==false) // aspetta di essere intorno allo zero per iniziare a campionare.
25  {
26  istV=Mc_SampleAnalogAvg(V->pin,2)-midpoint; // A2 legge l'onda che arriva dal trasformatore
27  if (istV<soglia && istV>-soglia) st=true; //check its within range
28  if ((micros()-tstart)>10*timelen)
29  {
30  st = true;
31 # ifdef ENERGIA_DEBUG
32  Serial.print("Not 0 wave cross!");
33 # endif
34  }
35  }
36  tstart=micros();
37 
38  counter=0;
39  float sensorIstep=I->sensor_step;
40  float sensorVstep=V->sensor_step;
41  while(micros()-tstart<timelen)
42  {
43  istI=RippleRemover(Mc_SampleAnalogAvg(I->pin,1)-midpoint); // corrente
44  if(V) istV=(Mc_SampleAnalogAvg(V->pin,1)-midpoint); // tensione
45  else istV=Mc_default_voltage;
46 
47 
48  /*istI=(Mc_SampleAnalogAvg(I->pin,1)-midpoint); // corrente
49  if(V) istV=(Mc_SampleAnalogAvg(V->pin,1)-midpoint); // tensione
50  else istV=Mc_default_voltage;
51  */
52  istP=istV*istI;
53  sqrV=istV*istV;
54  sqrI=istI*istI;
55  sum_sqrV+=sqrV;
56  sum_sqrI+=sqrI;
57  sum_inst_power+=istP;
58 
59  if(minV>istV) minV=istV;
60  if(minI>istI) minI=istI;
61 
62  if(maxV<istV) maxV=istV;
63  if(maxI<istI) maxI=istI;
64  counter++;
65  }
66  rmsI=sqrt(sum_sqrI/counter)*sensorIstep;
67  rmsV=sqrt(sum_sqrV/counter)*sensorVstep;
68  rP=sensorIstep*sensorVstep*sum_inst_power/counter;
69  aP=rmsV*rmsI;
70 }
71 
72 
73 //==============================================================================
74 
76 {
77 
78 }
79 //==============================================================================
80 
81 /** \brief Inizialize Energia
82  * \param mp is the analogic pin to read middle point. If not specificated Energia uses perfect half scale as middle point.
83  */
84 void Energia::Start(int mp)
85 {
87  int midpoint=(1<<(Mc_ADC_extended_bits-1)); // da tenere se non fai la lettura analogica;
88  Pin_MiddlePoint=mp;
89 }
90 //==============================================================================
91  /** \brief Set ripple noise
92  * \param mp is the analogic pin to read middle point. If not specified Energia uses perfect half scale as middle point.
93  * \param num specify the number of samples. If not specificated is set to 450.
94  */
95 void Energia::SetRipple(float rip,uint16_t num)
96 {
97  if(rip>=0) ripple=rip;
98  else
99  {
100  struct Mc_AnalogLine MidLine;
101  MidLine=CheckAnalogLine(Pin_MiddlePoint,num);
102  ripple=abs(MidLine.rms-MidLine.klm);
103 
104  }
105 
106 }
107 //==============================================================================
108 
109 /** \brief Remove ripple value from specified value
110  * \param value is the value you want to clean from ripple
111  * \return filtered value
112  */
113 float Energia::RippleRemover(float value)
114 {
115  if(abs(value)<=ripple) value=0;
116  return(value);
117 }
118 
119 //==============================================================================
120 /** \brief Read middle point: this function uses Kalman filter to reduce the noise.
121  * \param campioni is the number of samples
122  */
123 void Energia::ReadMiddlePoint(uint16_t campioni)
124 {
125  //midpoint=Mc_SampleAnalogAvg(Pin_MiddlePoint,campioni);
126  midpoint=Mc_KalmanAnalogAvg(Pin_MiddlePoint,campioni);
127 
128 }
129 //==============================================================================
130 /** \brief Sample waves
131  * \param S is the sensor connected to source
132  * \param wavenumber is the number of waves you want to sample. If not specified it's setted to 1
133  */
134 
135 void Energia::GetWaveValue(struct Mc_AnalogSensor * S,uint8_t wavenumber)
136 {
137  unsigned long tstart;
138  float istS,sqrS,sum_sqrS;
139  bool st=false;
140  tstart = millis();
141  int soglia=(1<<Mc_ADC_extended_bits)/10;
142 
143  double timelen=(double)wavenumber*1/Mc_voltage_freq*1000*1000; //0.02sec...è la lunghezza in microsecondi dell'onda completa.
144  counter=0;
145  while(st==false) // aspetta di essere intorno allo zero per iniziare a campionare.
146  {
147 
148  istS=Mc_SampleAnalogAvg(S->pin,2)-midpoint; // A2 legge l'onda che arriva dal trasformatore
149  if (istS<soglia && istS>-soglia) st=true; //check its within range
150  if ((micros()-tstart)>5*timelen)
151  {
152  st = true;
153 # ifdef ENERGIA_DEBUG
154  if(counter)
155  {
156  Serial.print("Not 0 wave cross: ");
157  Serial.println(counter);
158  }
159 # endif
160  }
161  counter++;
162  }
163  counter=0;
164  tstart=micros();
165 
166  float sensorSstep=S->sensor_step;
167  while(micros()-tstart<timelen)
168  {
169  istS=(Mc_SampleAnalogAvg(S->pin,2)-midpoint); // pinza amperometrica.
170 
171  if(minS>istS) minS=istS;
172  if(maxS<istS) maxS=istS;
173  sqrS=istS*istS;
174  sum_sqrS+=sqrS;
175  counter++;
176  }
177  rmsS=sqrt(sum_sqrS/counter)*sensorSstep;
178 
179 
180 }
181 //==============================================================================
float midpoint
Definition: Ss_Energia.h:19
void Mc_Analog_Inist()
Inizialize McMajanLibrary analogic section.
Definition: Ss_Analogico.cpp:7
float Mc_SampleAnalogAvg(int, uint16_t)
This function sample an analogic input and return the averange value.
#define Mc_High_ADC_Value
Definition: Ss_Global.h:37
float maxI
Definition: Ss_Energia.h:29
void SetRipple(float, uint16_t num=450)
Set ripple noise.
Definition: Ss_Energia.cpp:95
float ripple
Definition: Ss_Energia.h:20
float maxV
Definition: Ss_Energia.h:31
float RippleRemover(float)
Remove ripple value from specified value.
Definition: Ss_Energia.cpp:113
void ReadMiddlePoint(uint16_t)
Read middle point: this function uses Kalman filter to reduce the noise.
Definition: Ss_Energia.cpp:123
int Pin_MiddlePoint
Definition: Ss_Energia.h:12
void Start(int mp=-1)
Inizialize Energia.
Definition: Ss_Energia.cpp:84
float aP
Definition: Ss_Energia.h:27
#define Mc_ADC_extended_bits
Definition: Ss_Global.h:36
void GetWaveValue(struct Mc_AnalogSensor *, uint8_t wavenumber=1)
Sample waves.
Definition: Ss_Energia.cpp:135
void ReadPowerData(struct Mc_AnalogSensor *, struct Mc_AnalogSensor *, uint8_t wavenumber=1)
Read voltage and current data from sensors. This function calculates apparent power, real power, rms current and voltage.
Definition: Ss_Energia.cpp:8
float minI
Definition: Ss_Energia.h:28
float rmsV
Definition: Ss_Energia.h:24
float rmsI
Definition: Ss_Energia.h:23
float minV
Definition: Ss_Energia.h:30