AM263x Digital Power SDK  09.01.00
power_meas_sine_analyzer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2024 Texas Instruments Incorporated
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * distribution.
15  *
16  * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 //#############################################################################
33 //
34 // FILE: power_meas_sine_analyzer.h
35 //
36 // TITLE: Sine Analyzer with Power Measurement Module
37 //
38 //#############################################################################
39 
40 #ifndef POWER_MEAS_SINE_ANALYZER_H
41 #define POWER_MEAS_SINE_ANALYZER_H
42 
43 #ifdef __cplusplus
44 
45 extern "C"
46 {
47 #endif
48 
49 
50 //
51 // Included Files
52 //
53 #include <stdint.h>
54 #include <math.h>
55 
70 //#############################################################################
71 //
72 // Macro Definitions
73 //
74 #ifndef IEEE754_TYPES
75 #define IEEE754_TYPES
76 
77 typedef float float32_t;
78 typedef double float64_t;
79 
80 #endif // IEEE754_TYPES
81 
82 
89 typedef volatile struct {
103  int16_t zcd;
104 
113  int16_t prevSign;
114  int16_t currSign;
115  int32_t nSamples;
116  int32_t nSamplesMin;
117  int32_t nSamplesMax;
120  int16_t slewPowerUpdate;
122  int16_t jitterCount;
125 
130 {
131  v->vRms=0;
132  v->vAvg=0;
133  v->vEma=0;
134  v->acFreq=0;
135  v->iRms=0;
136  v->pRms=0;
137  v->vaRms=0;
138  v->powerFactor=0;
139  v->zcd=0;
140  v->vSum=0;
141  v->vSqrSum=0;
142  v->iSqrSum=0;
143  v->pSum=0;
144  v->vaSumMul=0;
145  v->vNorm=0;
146  v->iNorm=0;
147  v->prevSign=0;
148  v->currSign=0;
149  v->nSamples=0;
150  v->nSamplesMin = 0;
151  v->nSamplesMax = 0;
152  v->inverse_nSamples=0;
154  v->pRmsSumMul=0;
155  v->acFreqSum=0;
156  v->acFreqAvg=0;
157  v->jitterCount=0;
158  v->emaFilterMultiplier=0;
159 }
160 
169  float32_t isrFrequency,
170  float32_t threshold,
171  float32_t gridMaxFreq,
172  float32_t gridMinFreq)
173 {
174  v->sampleFreq = (float32_t)(isrFrequency);
175  v->threshold = (float32_t)(threshold);
176  v->nSamplesMax=isrFrequency/gridMinFreq;
177  v->nSamplesMin=isrFrequency/gridMaxFreq;
178  v->emaFilterMultiplier=2.0f/isrFrequency;
179 }
180 
185 {
186  v->vNorm = fabsf(v->v);
187  v->iNorm = fabsf(v->i);
188  v->currSign = ( v->v > v->threshold) ? 1 : 0;
189  v->nSamples++;
190  v->vSum = v->vSum+v->vNorm;
191  v->vSqrSum = v->vSqrSum+(v->vNorm*v->vNorm);
192  v->vEma = v->vEma+(v->emaFilterMultiplier*(v->vNorm - v->vEma));
193  v->iSqrSum = v->iSqrSum+(v->iNorm*v->iNorm);
194  v->pSum = v->pSum+(v->i*v->v);
195  v->zcd=0;
196 
197  if((v->prevSign != v->currSign) && (v->currSign == 1))
198  {
199  //
200  // check if the nSamples are in the ball park of a real frequency
201  // that can be on the grid, this is done by comparing the nSamples
202  // with the max value and min value it can be for the
203  // AC Grid Connection these Max and Min are initialized by the
204  // user in the code
205  //
206  if(v->nSamplesMin < v->nSamples )
207  {
208  v->zcd=1;
209  v->inverse_nSamples = (1.0f)/(v->nSamples);
211  v->vAvg = (v->vSum*v->inverse_nSamples);
212  v->vRms = sqrtf(v->vSqrSum)*v->sqrt_inverse_nSamples;
213  v->iRms = sqrtf(v->iSqrSum)*v->sqrt_inverse_nSamples;
214  v->pRmsSumMul = v->pRmsSumMul + (v->pSum*v->inverse_nSamples);
215  v->vaSumMul = v->vaSumMul + v->vRms*v->iRms;
216  v->acFreq = (v->sampleFreq*v->inverse_nSamples);
217  v->acFreqSum = v->acFreqSum + v->acFreq;
218 
219  v->slewPowerUpdate++;
220 
221  if(v->slewPowerUpdate >= 100)
222  {
223  v->slewPowerUpdate=0;
224  v->pRms = (v->pRmsSumMul*(0.01f));
225  v->pRmsSumMul = 0;
226  v->vaRms = v->vaSumMul * (0.01f);
227  v->vaSumMul = 0;
228  v->powerFactor=v->pRms/v->vaRms;
229  v->acFreqAvg=v->acFreqSum*0.01f;
230  v->acFreqSum=0;
231  }
232 
233  v->jitterCount=0;
234 
235  v->nSamples=0;
236  v->vSum=0;
237  v->vSqrSum=0;
238  v->iSqrSum=0;
239  v->pSum =0;
240  }
241  else
242  {
243  //
244  // otherwise it may be jitter ignore this reading
245  // but count the number of jitters you are getting
246  // but do not count to infinity as then when the grid comes back
247  // it will take too much time to wind down the jitter count
248  //
249  if(v->jitterCount<30)
250  {
251  v->jitterCount++;
252  }
253  v->nSamples=0;
254  }
255  }
256 
257  if(v->nSamples>v->nSamplesMax || v->jitterCount>20)
258  {
259  //
260  // most certainly the AC voltage is not present
261  //
262  v->vRms = 0;
263  v->vAvg = 0;
264  v->vEma = 0;
265  v->acFreq=0;
266  v->iRms = 0;
267  v->pRms = 0;
268  v->vaRms =0;
269  v->powerFactor=0;
270 
271  v->zcd=0;
272  v->vSum=0;
273  v->vSqrSum=0;
274  v->iSqrSum=0;
275  v->pSum=0;
276  v->vaSumMul=0;
277  v->pRmsSumMul = 0;
278  v->acFreqAvg = 0;
279  v->acFreqSum =0 ;
280  v->nSamples=0;
281  v->jitterCount=0;
282  }
283 
284  v->prevSign = v->currSign;
285 }
286 
287 
289 #ifdef __cplusplus
290 }
291 #endif // extern "C"
292 
293 #endif // end of _POWER_MEAS_SINE_ANALYZER_H definition
294 
295 //
296 // End of File
297 //
298 
POWER_MEAS_SINE_ANALYZER::threshold
float32_t threshold
Input: Voltage level corresponding to zero i/p.
Definition: power_meas_sine_analyzer.h:93
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:110
POWER_MEAS_SINE_ANALYZER::vEma
float32_t vEma
Output: Exponential Moving Average Value.
Definition: power_meas_sine_analyzer.h:96
POWER_MEAS_SINE_ANALYZER::acFreqSum
float32_t acFreqSum
Internal : running sum of acFreq.
Definition: power_meas_sine_analyzer.h:108
POWER_MEAS_SINE_ANALYZER::vNorm
float32_t vNorm
Internal: Normalized value of the input voltage.
Definition: power_meas_sine_analyzer.h:111
POWER_MEAS_SINE_ANALYZER::iNorm
float32_t iNorm
Internal: Normalized value of the input current.
Definition: power_meas_sine_analyzer.h:112
POWER_MEAS_SINE_ANALYZER::acFreqAvg
float32_t acFreqAvg
Output: Signal Freq.
Definition: power_meas_sine_analyzer.h:98
POWER_MEAS_SINE_ANALYZER::pRms
float32_t pRms
Output: RMS Value of input power.
Definition: power_meas_sine_analyzer.h:100
POWER_MEAS_SINE_ANALYZER::slewPowerUpdate
int16_t slewPowerUpdate
Internal: used to slew update of the power value.
Definition: power_meas_sine_analyzer.h:120
POWER_MEAS_SINE_ANALYZER::emaFilterMultiplier
float32_t emaFilterMultiplier
Internal: multiplier value used for the exponential moving average filter.
Definition: power_meas_sine_analyzer.h:123
POWER_MEAS_SINE_ANALYZER::currSign
int16_t currSign
Internal: Flag to detect ZCD.
Definition: power_meas_sine_analyzer.h:114
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:168
float64_t
double float64_t
Definition: power_meas_sine_analyzer.h:78
POWER_MEAS_SINE_ANALYZER::prevSign
int16_t prevSign
Internal: Flag to detect ZCD.
Definition: power_meas_sine_analyzer.h:113
POWER_MEAS_SINE_ANALYZER::v
float32_t v
Input: Voltage Sine Signal.
Definition: power_meas_sine_analyzer.h:90
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:107
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:184
POWER_MEAS_SINE_ANALYZER::zcd
int16_t zcd
Output: Zero Cross detected.
Definition: power_meas_sine_analyzer.h:103
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:115
POWER_MEAS_SINE_ANALYZER::acFreq
float32_t acFreq
Output: Signal Freq.
Definition: power_meas_sine_analyzer.h:97
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:109
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:106
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:121
POWER_MEAS_SINE_ANALYZER::vRms
float32_t vRms
Output: RMS Value.
Definition: power_meas_sine_analyzer.h:94
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:119
float32_t
float float32_t
Definition: power_meas_sine_analyzer.h:77
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:129
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:122
POWER_MEAS_SINE_ANALYZER::vaRms
float32_t vaRms
Output: RMS VA.
Definition: power_meas_sine_analyzer.h:101
POWER_MEAS_SINE_ANALYZER::vAvg
float32_t vAvg
Output: Average Value.
Definition: power_meas_sine_analyzer.h:95
POWER_MEAS_SINE_ANALYZER::sampleFreq
float32_t sampleFreq
Input: Signal Sampling Freq.
Definition: power_meas_sine_analyzer.h:92
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:118
POWER_MEAS_SINE_ANALYZER::vSum
float32_t vSum
Internal : running sum for vac calculation over one sine cycles.
Definition: power_meas_sine_analyzer.h:105
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:116
POWER_MEAS_SINE_ANALYZER
Defines the POWER_MEAS_SINE_ANALYZER structure.
Definition: power_meas_sine_analyzer.h:89
POWER_MEAS_SINE_ANALYZER::i
float32_t i
Input Current Signal.
Definition: power_meas_sine_analyzer.h:91
POWER_MEAS_SINE_ANALYZER::powerFactor
float32_t powerFactor
Output: powerFactor.
Definition: power_meas_sine_analyzer.h:102
float32_t
float float32_t
Defines single,double precision data type. Note: Assumes ABI to be TI_EABI, does not support legacy T...
Definition: dcl_common.h:54
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:117
POWER_MEAS_SINE_ANALYZER::iRms
float32_t iRms
Output: RMS Value of current.
Definition: power_meas_sine_analyzer.h:99