power_meas_sine_analyzer.h
Go to the documentation of this file.
1 //#############################################################################
2 //
3 // FILE: power_meas_sine_analyzer.h
4 //
5 // TITLE: Sine Analyzer with Power Measurement Module
6 //
7 //#############################################################################
8 // $TI Release: Power Measurement Library v1.02.00.00 $
9 // $Release Date: Wed Oct 5 02:42:52 CDT 2022 $
10 // $Copyright:
11 // Copyright (C) 2022 Texas Instruments Incorporated - http://www.ti.com/
12 //
13 // ALL RIGHTS RESERVED
14 // $
15 //#############################################################################
16 
17 #ifndef POWER_MEAS_SINE_ANALYZER_H
18 #define POWER_MEAS_SINE_ANALYZER_H
19 
20 #ifdef __cplusplus
21 
22 extern "C"
23 {
24 #endif
25 
26 //*****************************************************************************
27 //
30 //
31 //*****************************************************************************
32 
33 //
34 // Included Files
35 //
36 #include <stdint.h>
37 #ifndef __TMS320C28XX_CLA__
38 #include <math.h>
39 #else
40 #include <CLAmath.h>
41 #endif
42 
43 //#############################################################################
44 //
45 // Macro Definitions
46 //
47 //#############################################################################
48 #ifndef C2000_IEEE754_TYPES
49 #define C2000_IEEE754_TYPES
50 #ifdef __TI_EABI__
51 typedef float float32_t;
52 typedef double float64_t;
53 #else // TI COFF
54 typedef float float32_t;
55 typedef long double float64_t;
56 #endif // __TI_EABI__
57 #endif // C2000_IEEE754_TYPES
58 
65 typedef volatile struct {
79  int16_t zcd;
80 
89  int16_t prevSign;
90  int16_t currSign;
91  int32_t nSamples;
92  int32_t nSamplesMin;
93  int32_t nSamplesMax;
96  int16_t slewPowerUpdate;
98  int16_t jitterCount;
101 
107 {
108  v->vRms=0;
109  v->vAvg=0;
110  v->vEma=0;
111  v->acFreq=0;
112  v->iRms=0;
113  v->pRms=0;
114  v->vaRms=0;
115  v->powerFactor=0;
116  v->zcd=0;
117  v->vSum=0;
118  v->vSqrSum=0;
119  v->iSqrSum=0;
120  v->pSum=0;
121  v->vaSumMul=0;
122  v->vNorm=0;
123  v->iNorm=0;
124  v->prevSign=0;
125  v->currSign=0;
126  v->nSamples=0;
127  v->nSamplesMin = 0;
128  v->nSamplesMax = 0;
129  v->inverse_nSamples=0;
131  v->pRmsSumMul=0;
132  v->acFreqSum=0;
133  v->acFreqAvg=0;
134  v->jitterCount=0;
135  v->emaFilterMultiplier=0;
136 }
137 
147  float32_t isrFrequency,
148  float32_t threshold,
149  float32_t gridMaxFreq,
150  float32_t gridMinFreq)
151 {
152  v->sampleFreq = (float)(isrFrequency);
153  v->threshold = (float)(threshold);
154  v->nSamplesMax=isrFrequency/gridMinFreq;
155  v->nSamplesMin=isrFrequency/gridMaxFreq;
156  v->emaFilterMultiplier=2.0f/isrFrequency;
157 }
158 
164 {
165  v->vNorm = fabsf(v->v);
166  v->iNorm = fabsf(v->i);
167  v->currSign = ( v->v > v->threshold) ? 1 : 0;
168  v->nSamples++;
169  v->vSum = v->vSum+v->vNorm;
170  v->vSqrSum = v->vSqrSum+(v->vNorm*v->vNorm);
171  v->vEma = v->vEma+(v->emaFilterMultiplier*(v->vNorm - v->vEma));
172  v->iSqrSum = v->iSqrSum+(v->iNorm*v->iNorm);
173  v->pSum = v->pSum+(v->i*v->v);
174  v->zcd=0;
175 
176  if((v->prevSign != v->currSign) && (v->currSign == 1))
177  {
178  //
179  // check if the nSamples are in the ball park of a real frequency
180  // that can be on the grid, this is done by comparing the nSamples
181  // with the max value and min value it can be for the
182  // AC Grid Connection these Max and Min are initialized by the
183  // user in the code
184  //
185  if(v->nSamplesMin < v->nSamples )
186  {
187  v->zcd=1;
188  v->inverse_nSamples = (1.0f)/(v->nSamples);
190  v->vAvg = (v->vSum*v->inverse_nSamples);
191  v->vRms = sqrtf(v->vSqrSum)*v->sqrt_inverse_nSamples;
192  v->iRms = sqrtf(v->iSqrSum)*v->sqrt_inverse_nSamples;
193  v->pRmsSumMul = v->pRmsSumMul + (v->pSum*v->inverse_nSamples);
194  v->vaSumMul = v->vaSumMul + v->vRms*v->iRms;
195  v->acFreq = (v->sampleFreq*v->inverse_nSamples);
196  v->acFreqSum = v->acFreqSum + v->acFreq;
197 
198  v->slewPowerUpdate++;
199 
200  if(v->slewPowerUpdate >= 100)
201  {
202  v->slewPowerUpdate=0;
203  v->pRms = (v->pRmsSumMul*(0.01f));
204  v->pRmsSumMul = 0;
205  v->vaRms = v->vaSumMul * (0.01f);
206  v->vaSumMul = 0;
207  v->powerFactor=v->pRms/v->vaRms;
208  v->acFreqAvg=v->acFreqSum*0.01f;
209  v->acFreqSum=0;
210  }
211 
212  v->jitterCount=0;
213 
214  v->nSamples=0;
215  v->vSum=0;
216  v->vSqrSum=0;
217  v->iSqrSum=0;
218  v->pSum =0;
219  }
220  else
221  {
222  //
223  // otherwise it may be jitter ignore this reading
224  // but count the number of jitters you are getting
225  // but do not count to infinity as then when the grid comes back
226  // it will take too much time to wind down the jitter count
227  //
228  if(v->jitterCount<30)
229  {
230  v->jitterCount++;
231  }
232  v->nSamples=0;
233  }
234  }
235 
236  if(v->nSamples>v->nSamplesMax || v->jitterCount>20)
237  {
238  //
239  // most certainly the AC voltage is not present
240  //
241  v->vRms = 0;
242  v->vAvg = 0;
243  v->vEma = 0;
244  v->acFreq=0;
245  v->iRms = 0;
246  v->pRms = 0;
247  v->vaRms =0;
248  v->powerFactor=0;
249 
250  v->zcd=0;
251  v->vSum=0;
252  v->vSqrSum=0;
253  v->iSqrSum=0;
254  v->pSum=0;
255  v->vaSumMul=0;
256  v->pRmsSumMul = 0;
257  v->acFreqAvg = 0;
258  v->acFreqSum =0 ;
259  v->nSamples=0;
260  v->jitterCount=0;
261  }
262 
263  v->prevSign = v->currSign;
264 }
265 
266 //*****************************************************************************
267 //
268 // Close the Doxygen group.
270 //
271 //*****************************************************************************
272 
273 #ifdef __cplusplus
274 }
275 #endif // extern "C"
276 
277 #endif // end of _SineAlanyzer_diff_wPower_F_C_H_ definition
278 
279 //
280 // End of File
281 //
282 
POWER_MEAS_SINE_ANALYZER_config
static void POWER_MEAS_SINE_ANALYZER_config(POWER_MEAS_SINE_ANALYZER *v, float32_t isrFrequency, float32_t threshold, float32_t gridMaxFreq, float32_t gridMinFreq)
Configures the power measurment module.
Definition: power_meas_sine_analyzer.h:146
POWER_MEAS_SINE_ANALYZER::nSamples
int32_t nSamples
Internal: No of samples in one cycle of the sine wave.
Definition: power_meas_sine_analyzer.h:91
POWER_MEAS_SINE_ANALYZER_reset
static void POWER_MEAS_SINE_ANALYZER_reset(POWER_MEAS_SINE_ANALYZER *v)
Resets internal data to zero.
Definition: power_meas_sine_analyzer.h:106
POWER_MEAS_SINE_ANALYZER::acFreqSum
float32_t acFreqSum
Internal : running sum of acFreq.
Definition: power_meas_sine_analyzer.h:84
float32_t
float float32_t
Definition: sfra_f32.h:42
POWER_MEAS_SINE_ANALYZER::v
float32_t v
Input: Voltage Sine Signal.
Definition: power_meas_sine_analyzer.h:66
POWER_MEAS_SINE_ANALYZER::vAvg
float32_t vAvg
Output: Average Value.
Definition: power_meas_sine_analyzer.h:71
POWER_MEAS_SINE_ANALYZER::i
float32_t i
Input Current Signal.
Definition: power_meas_sine_analyzer.h:67
POWER_MEAS_SINE_ANALYZER::nSamplesMax
int32_t nSamplesMax
Internal: Upperbound for no of samples in one sine wave cycle.
Definition: power_meas_sine_analyzer.h:93
POWER_MEAS_SINE_ANALYZER
Defines the POWER_MEAS_SINE_ANALYZER structure.
Definition: power_meas_sine_analyzer.h:65
POWER_MEAS_SINE_ANALYZER::acFreq
float32_t acFreq
Output: Signal Freq.
Definition: power_meas_sine_analyzer.h:73
POWER_MEAS_SINE_ANALYZER::threshold
float32_t threshold
Input: Voltage level corresponding to zero i/p.
Definition: power_meas_sine_analyzer.h:69
POWER_MEAS_SINE_ANALYZER::emaFilterMultiplier
float32_t emaFilterMultiplier
Internal: multiplier value used for the exponential moving average filter.
Definition: power_meas_sine_analyzer.h:99
POWER_MEAS_SINE_ANALYZER::vSqrSum
float32_t vSqrSum
Internal : running sum for vacc square calculation over one sine cycle.
Definition: power_meas_sine_analyzer.h:82
POWER_MEAS_SINE_ANALYZER::currSign
int16_t currSign
Internal: Flag to detect ZCD.
Definition: power_meas_sine_analyzer.h:90
POWER_MEAS_SINE_ANALYZER::vEma
float32_t vEma
Output: Exponential Moving Average Value.
Definition: power_meas_sine_analyzer.h:72
POWER_MEAS_SINE_ANALYZER::inverse_nSamples
float32_t inverse_nSamples
Internal: 1/( No of samples in one cycle of the sine wave)
Definition: power_meas_sine_analyzer.h:94
POWER_MEAS_SINE_ANALYZER::jitterCount
int16_t jitterCount
Internal: used to store jitter information due to noise on input.
Definition: power_meas_sine_analyzer.h:98
POWER_MEAS_SINE_ANALYZER::nSamplesMin
int32_t nSamplesMin
Internal: Lowerbound for no of samples in one sine wave cycle.
Definition: power_meas_sine_analyzer.h:92
POWER_MEAS_SINE_ANALYZER::sampleFreq
float32_t sampleFreq
Input: Signal Sampling Freq.
Definition: power_meas_sine_analyzer.h:68
math.h
POWER_MEAS_SINE_ANALYZER::pRmsSumMul
float32_t pRmsSumMul
Internal: used to sum Pac value over multiple sine cycles (100)
Definition: power_meas_sine_analyzer.h:97
POWER_MEAS_SINE_ANALYZER::iSqrSum
float32_t iSqrSum
Internal : running sum for Iacc_rms calculation over one sine cycle.
Definition: power_meas_sine_analyzer.h:83
POWER_MEAS_SINE_ANALYZER::iRms
float32_t iRms
Output: RMS Value of current.
Definition: power_meas_sine_analyzer.h:75
POWER_MEAS_SINE_ANALYZER::powerFactor
float32_t powerFactor
Output: powerFactor.
Definition: power_meas_sine_analyzer.h:78
POWER_MEAS_SINE_ANALYZER::iNorm
float32_t iNorm
Internal: Normalized value of the input current.
Definition: power_meas_sine_analyzer.h:88
POWER_MEAS_SINE_ANALYZER::vSum
float32_t vSum
Internal : running sum for vac calculation over one sine cycles.
Definition: power_meas_sine_analyzer.h:81
POWER_MEAS_SINE_ANALYZER::vaRms
float32_t vaRms
Output: RMS VA.
Definition: power_meas_sine_analyzer.h:77
float64_t
long double float64_t
Definition: power_meas_sine_analyzer.h:55
POWER_MEAS_SINE_ANALYZER::sqrt_inverse_nSamples
float32_t sqrt_inverse_nSamples
Internal: sqrt(1/( No of samples in one cycle of the sine wave))
Definition: power_meas_sine_analyzer.h:95
POWER_MEAS_SINE_ANALYZER::vaSumMul
float32_t vaSumMul
Internal : running sum for Pacc_rms calculation over one sine cycle.
Definition: power_meas_sine_analyzer.h:86
POWER_MEAS_SINE_ANALYZER::acFreqAvg
float32_t acFreqAvg
Output: Signal Freq.
Definition: power_meas_sine_analyzer.h:74
POWER_MEAS_SINE_ANALYZER::pSum
float32_t pSum
Internal : running sum for Pacc_rms calculation over one sine cycle.
Definition: power_meas_sine_analyzer.h:85
float32_t
float float32_t
Definition: power_meas_sine_analyzer.h:54
POWER_MEAS_SINE_ANALYZER::vRms
float32_t vRms
Output: RMS Value.
Definition: power_meas_sine_analyzer.h:70
POWER_MEAS_SINE_ANALYZER_run
static void POWER_MEAS_SINE_ANALYZER_run(POWER_MEAS_SINE_ANALYZER *v)
Perform calculations using the POWER_MEAS_SINE_ANALYZER module.
Definition: power_meas_sine_analyzer.h:163
POWER_MEAS_SINE_ANALYZER::prevSign
int16_t prevSign
Internal: Flag to detect ZCD.
Definition: power_meas_sine_analyzer.h:89
POWER_MEAS_SINE_ANALYZER::slewPowerUpdate
int16_t slewPowerUpdate
Internal: used to slew update of the power value.
Definition: power_meas_sine_analyzer.h:96
POWER_MEAS_SINE_ANALYZER::pRms
float32_t pRms
Output: RMS Value of input power.
Definition: power_meas_sine_analyzer.h:76
POWER_MEAS_SINE_ANALYZER::zcd
int16_t zcd
Output: Zero Cross detected.
Definition: power_meas_sine_analyzer.h:79
POWER_MEAS_SINE_ANALYZER::vNorm
float32_t vNorm
Internal: Normalized value of the input voltage.
Definition: power_meas_sine_analyzer.h:87

Copyright 2023, Texas Instruments Incorporated