AM263x Digital Power SDK  09.01.00
spll_1ph_notch.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 spll_1ph_notch.h
35 //
36 // TITLE Notch Filter based Software Phase Lock Loop (SPLL)
37 // for Single Phase Grid Module
38 //
39 //#############################################################################
40 
41 #ifndef SPLL_1PH_NOTCH_H
42 #define SPLL_1PH_NOTCH_H
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
59 //
60 // Typedefs
61 //
62 //
63 // Included Files
64 //
65 #include <stdint.h>
66 #include <math.h>
67 
68 //#############################################################################
69 //
70 // Macro Definitions
71 //
72 //#############################################################################
73 #ifndef IEEE754_TYPES
74 #define IEEE754_TYPES
75 typedef float float32_t;
76 typedef double float64_t;
77 #endif // IEEE754_TYPES
78 
79 
80 
83 typedef struct{
90 
93 typedef struct{
98 
105 typedef struct{
106  float32_t upd[3];
107  float32_t y_notch1[3];
108  float32_t y_notch2[3];
109  float32_t ylf[2];
119 
123 static inline void SPLL_1PH_NOTCH_reset(SPLL_1PH_NOTCH *spll_obj)
124 {
125  spll_obj->upd[0]=(float32_t)(0.0);
126  spll_obj->upd[1]=(float32_t)(0.0);
127 
128  spll_obj->y_notch1[0]=(float32_t)(0.0);
129  spll_obj->y_notch1[1]=(float32_t)(0.0);
130  spll_obj->y_notch1[2]=(float32_t)(0.0);
131 
132  spll_obj->y_notch2[0]=(float32_t)(0.0);
133  spll_obj->y_notch2[1]=(float32_t)(0.0);
134  spll_obj->y_notch2[2]=(float32_t)(0.0);
135 
136  spll_obj->ylf[0]=(float32_t)(0.0);
137  spll_obj->ylf[1]=(float32_t)(0.0);
138 
139  spll_obj->fo=(float32_t)(0.0);
140 
141  spll_obj->theta=(float32_t)(0.0);
142 
143  spll_obj->sine=(float32_t)(0.0);
144  spll_obj->cosine=(float32_t)(0.0);
145 }
146 
152 static inline void SPLL_1PH_NOTCH_coeff_calc(SPLL_1PH_NOTCH *spll_obj,
153  float32_t c1, float32_t c2)
154 {
155  float32_t notch_freq;
156  float32_t temp1,temp2;
157  float32_t wn2;
158  float32_t Ts, Fs;
159 
160  notch_freq=2*3.14159265f*spll_obj->fn;
161  Ts = spll_obj->delta_t;
162  Fs=1/Ts;
163 
164  //
165  // pre warp the notch frequency
166  //
167  wn2=2*Fs*tanf(notch_freq* ((float32_t)3.141592653589)*Ts);
168 
169  temp1= 4*Fs*Fs + 4* wn2 * c2 * Fs + wn2*wn2;
170  temp2= 1/ ( 4*Fs*Fs + 4* wn2 * c1 * Fs + wn2*wn2);
171 
172  spll_obj->notch_coeff.b0 = temp1* temp2;
173  spll_obj->notch_coeff.b1 = (-8*Fs*Fs + 2* wn2* wn2)* temp2;
174  spll_obj->notch_coeff.b2 = (4*Fs*Fs-4*wn2*c2*Fs+wn2*wn2)*temp2;
175  spll_obj->notch_coeff.a1 = (-8*Fs*Fs + 2* wn2* wn2)*temp2;
176  spll_obj->notch_coeff.a2 = (4*Fs*Fs-4*wn2*c1*Fs+wn2*wn2)*temp2;
177 }
178 
179 
189 static inline void SPLL_1PH_NOTCH_config(SPLL_1PH_NOTCH *spll_obj,
190  float32_t acFreq,
191  float32_t isrFrequency,
192  float32_t lpf_b0,
193  float32_t lpf_b1,
194  float32_t c1,
195  float32_t c2
196  )
197 {
198  spll_obj->fn=acFreq;
199  spll_obj->delta_t=((1.0f)/isrFrequency);
200 
201  SPLL_1PH_NOTCH_coeff_calc( spll_obj,
202  c1,
203  c2);
204 
205  spll_obj->lpf_coeff.b0=lpf_b0;
206  spll_obj->lpf_coeff.b1=lpf_b1;
207 }
208 
213 static inline void SPLL_1PH_NOTCH_run(SPLL_1PH_NOTCH *spll_obj,
214  float32_t acValue)
215 {
216  //
217  // Phase detect
218  //
219  spll_obj->upd[0] = acValue*spll_obj->cosine;
220 
221  //
222  // Notch Filter
223  //
224  spll_obj->y_notch1[0] = - spll_obj->y_notch1[1]*spll_obj->notch_coeff.a1
225  - spll_obj->y_notch1[2]*spll_obj->notch_coeff.a2
226  + spll_obj->upd[0]*spll_obj->notch_coeff.b0
227  + spll_obj->upd[1]*spll_obj->notch_coeff.b1
228  + spll_obj->upd[2]*spll_obj->notch_coeff.b2;
229 
230  spll_obj->y_notch2[0] = - spll_obj->y_notch2[1]*spll_obj->notch_coeff.a1
231  - spll_obj->y_notch2[2]*spll_obj->notch_coeff.a2
232  + spll_obj->y_notch1[0]*spll_obj->notch_coeff.b0
233  + spll_obj->y_notch1[1]*spll_obj->notch_coeff.b1
234  + spll_obj->y_notch1[2]*spll_obj->notch_coeff.b2;
235 
236  //
237  // Loop Filter
238  //
239  spll_obj->ylf[0] = spll_obj->ylf[1]
240  + (spll_obj->lpf_coeff.b0*spll_obj->y_notch2[0])
241  + (spll_obj->lpf_coeff.b1*spll_obj->y_notch2[1]);
242 
243  //spll_obj->ylf[0] = (spll_obj->ylf[0]>100)?100:spll_obj->ylf[0];
244  //spll_obj->ylf[0] = (spll_obj->ylf[0]<-100)?-100:spll_obj->ylf[0];
245 
246  //
247  // update the Upd array for future sample
248  //
249  spll_obj->upd[2] = spll_obj->upd[1];
250  spll_obj->upd[1] = spll_obj->upd[0];
251 
252  spll_obj->y_notch1[2] = spll_obj->y_notch1[1];
253  spll_obj->y_notch1[1] = spll_obj->y_notch1[0];
254 
255  spll_obj->y_notch2[2] = spll_obj->y_notch2[1];
256  spll_obj->y_notch2[1] = spll_obj->y_notch2[0];
257 
258  spll_obj->ylf[1] = spll_obj->ylf[0];
259 
260  //
261  // VCO
262  //
263  spll_obj->fo = spll_obj->fn+spll_obj->ylf[0];
264 
265  spll_obj->theta= spll_obj->theta
266  + (spll_obj->fo*spll_obj->delta_t)*(float32_t)(2.0*3.1415926);
267 
268  if(spll_obj->theta>(float32_t)(2.0*3.1415926))
269  {
270  spll_obj->theta =spll_obj->theta-(float32_t)(2.0*3.1415926);
271  }
272 
273  spll_obj->sine = (float32_t)sinf(spll_obj->theta);
274  spll_obj->cosine = (float32_t)cosf(spll_obj->theta);
275 }
276 
277 // Close the Doxygen group.
280 #ifdef __cplusplus
281 }
282 #endif // extern "C"
283 
284 #endif // end of _SPLL_1PH_NOTCH_H_ definition
285 
286 
287 //
288 // End of File
289 //
290 
SPLL_1PH_NOTCH::delta_t
float32_t delta_t
Inverse of the ISR rate at which module is called.
Definition: spll_1ph_notch.h:115
SPLL_1PH_NOTCH::theta
float32_t theta
Angle output (0-2*pi)
Definition: spll_1ph_notch.h:112
SPLL_1PH_NOTCH::fn
float32_t fn
Nominal frequency (Hz)
Definition: spll_1ph_notch.h:111
SPLL_1PH_NOTCH_COEFF::a2
float32_t a2
Definition: spll_1ph_notch.h:87
SPLL_1PH_NOTCH_LPF_COEFF
Defines the SPLL_1PH_NOTCH_LPF_COEFF structure.
Definition: spll_1ph_notch.h:93
float32_t
float float32_t
Definition: spll_1ph_notch.h:75
SPLL_1PH_NOTCH_LPF_COEFF::a1
float32_t a1
Definition: spll_1ph_notch.h:96
SPLL_1PH_NOTCH_config
static void SPLL_1PH_NOTCH_config(SPLL_1PH_NOTCH *spll_obj, float32_t acFreq, float32_t isrFrequency, float32_t lpf_b0, float32_t lpf_b1, float32_t c1, float32_t c2)
Configures the SPLL_1PH_NOTCH module.
Definition: spll_1ph_notch.h:189
SPLL_1PH_NOTCH::lpf_coeff
SPLL_1PH_NOTCH_LPF_COEFF lpf_coeff
Loop filter coeffcient structure.
Definition: spll_1ph_notch.h:117
SPLL_1PH_NOTCH::sine
float32_t sine
Sine value of the PLL angle.
Definition: spll_1ph_notch.h:114
SPLL_1PH_NOTCH_COEFF::b1
float32_t b1
Definition: spll_1ph_notch.h:85
SPLL_1PH_NOTCH_COEFF::b2
float32_t b2
Definition: spll_1ph_notch.h:84
SPLL_1PH_NOTCH::y_notch1
float32_t y_notch1[3]
Notch filter1 data storage.
Definition: spll_1ph_notch.h:107
SPLL_1PH_NOTCH_LPF_COEFF::b0
float32_t b0
Definition: spll_1ph_notch.h:95
SPLL_1PH_NOTCH::ylf
float32_t ylf[2]
Loop filter data storage.
Definition: spll_1ph_notch.h:109
SPLL_1PH_NOTCH::fo
float32_t fo
Output frequency of PLL(Hz)
Definition: spll_1ph_notch.h:110
SPLL_1PH_NOTCH::upd
float32_t upd[3]
Phase detect buffer.
Definition: spll_1ph_notch.h:106
SPLL_1PH_NOTCH_COEFF::a1
float32_t a1
Definition: spll_1ph_notch.h:88
SPLL_1PH_NOTCH_LPF_COEFF::b1
float32_t b1
Definition: spll_1ph_notch.h:94
SPLL_1PH_NOTCH_COEFF::b0
float32_t b0
Definition: spll_1ph_notch.h:86
SPLL_1PH_NOTCH_reset
static void SPLL_1PH_NOTCH_reset(SPLL_1PH_NOTCH *spll_obj)
Resets internal data to zero,.
Definition: spll_1ph_notch.h:123
SPLL_1PH_NOTCH::cosine
float32_t cosine
Cosine value of the PLL angle.
Definition: spll_1ph_notch.h:113
SPLL_1PH_NOTCH_COEFF
Defines the SPLL_1PH_NOTCH_COEFF structure.
Definition: spll_1ph_notch.h:83
SPLL_1PH_NOTCH::notch_coeff
SPLL_1PH_NOTCH_COEFF notch_coeff
Notch filter coeffcient structure.
Definition: spll_1ph_notch.h:116
float64_t
double float64_t
Definition: spll_1ph_notch.h:76
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
SPLL_1PH_NOTCH_run
static void SPLL_1PH_NOTCH_run(SPLL_1PH_NOTCH *spll_obj, float32_t acValue)
Runs the SPLL_1PH_NOTCH module.
Definition: spll_1ph_notch.h:213
SPLL_1PH_NOTCH::y_notch2
float32_t y_notch2[3]
Notch filter2 data storage.
Definition: spll_1ph_notch.h:108
SPLL_1PH_NOTCH_coeff_calc
static void SPLL_1PH_NOTCH_coeff_calc(SPLL_1PH_NOTCH *spll_obj, float32_t c1, float32_t c2)
Calculates the coefficients for SPLL_1PH_NOTCH filter.
Definition: spll_1ph_notch.h:152
SPLL_1PH_NOTCH
Defines the SPLL_1PH_NOTCH structure.
Definition: spll_1ph_notch.h:105