Digital Power Starter Kit 3 Firmware
dsPIC33C Buck Converter Adaptive Gain Control Example
v_loop.c
1 /* *********************************************************************************
2  * PowerSmartâ„¢ Digital Control Library Designer, Version 0.9.14.676
3  * *********************************************************************************
4  * 4p4z controller function declarations and compensation filter coefficients
5  * derived for following operating conditions:
6  * *********************************************************************************
7  *
8  * Controller Type: 4P4Z - Advanced High-Q Compensator
9  * Sampling Frequency: 500000 Hz
10  * Fixed Point Format: Q15
11  * Scaling Mode: 3 - Dual Bit-Shift Scaling
12  * Input Gain: 0.5
13  *
14  * *********************************************************************************
15  * CGS Version: 3.0.8
16  * CGS Date: 03/12/2021
17  * *********************************************************************************
18  * User: M91406
19  * Date/Time: 04/02/2021 00:19:31
20  * ********************************************************************************/
21 
22 #include "v_loop.h"
23 
24 /* *********************************************************************************
25  * Data Arrays:
26  * ============
27  *
28  * This source file declares the default parameters of the z-domain compensation
29  * filter. The NPNZ16b_s data structure contains two pointers to A- and B-
30  * coefficient arrays and two pointers to control and error history arrays.
31  *
32  * For optimized data processing during DSP computations, these arrays must be
33  * located in specific memory locations (X-space for coefficient arrays and
34  * Y-space for control and error history arrays).
35  *
36  * The following declarations are used to define the array data contents, their
37  * length and memory location. These declarations are made publicly accessible
38  * through extern declarations in header file v_loop.h
39  * ********************************************************************************/
40 
41 volatile struct V_LOOP_CONTROL_LOOP_COEFFICIENTS_s __attribute__((space(xmemory), near)) v_loop_coefficients; // A/B-Coefficients
42 volatile uint16_t v_loop_ACoefficients_size = (sizeof(v_loop_coefficients.ACoefficients)/sizeof(v_loop_coefficients.ACoefficients[0])); // A-coefficient array size
43 volatile uint16_t v_loop_BCoefficients_size = (sizeof(v_loop_coefficients.BCoefficients)/sizeof(v_loop_coefficients.BCoefficients[0])); // B-coefficient array size
44 
45 volatile struct V_LOOP_CONTROL_LOOP_HISTORIES_s __attribute__((space(ymemory), far)) v_loop_histories; // Control/Error Histories
46 volatile uint16_t v_loop_ControlHistory_size = (sizeof(v_loop_histories.ControlHistory)/sizeof(v_loop_histories.ControlHistory[0])); // Control history array size
47 volatile uint16_t v_loop_ErrorHistory_size = (sizeof(v_loop_histories.ErrorHistory)/sizeof(v_loop_histories.ErrorHistory[0])); // Error history array size
48 
49 /* *********************************************************************************
50  * Pole&Zero Placement:
51  * *********************************************************************************
52  *
53  * fP0: 604 Hz
54  * fP1: 82540 Hz
55  * fP2: 118860 Hz
56  * fP3: 220000 Hz
57  * fZ1: 2021 Hz
58  * fZ2: 3833 Hz
59  * fZ3: 49866 Hz
60  *
61  * *********************************************************************************
62  * Filter Coefficients and Parameters:
63  * ********************************************************************************/
64 
65 volatile int32_t v_loop_ACoefficients [4] =
66 {
67  0x0000534C, // Coefficient A1 will be multiplied with controller output u(n-1)
68  0x0000EE83, // Coefficient A2 will be multiplied with controller output u(n-2)
69  0x0000FDBA, // Coefficient A3 will be multiplied with controller output u(n-3)
70  0x00000079 // Coefficient A4 will be multiplied with controller output u(n-4)
71 };
72 
73 volatile int32_t v_loop_BCoefficients [5] =
74 {
75  0x00004924, // Coefficient B0 will be multiplied with error input e(n-0)
76  0x000095E6, // Coefficient B1 will be multiplied with error input e(n-1)
77  0x0000DA6F, // Coefficient B2 will be multiplied with error input e(n-2)
78  0x00006A25, // Coefficient B3 will be multiplied with error input e(n-3)
79  0x0000DC79 // Coefficient B4 will be multiplied with error input e(n-4)
80 };
81 
82 // Coefficient normalization factors
83 volatile int16_t v_loop_pre_scaler = 3; // Bit-shift value used to perform input value normalization
84 volatile int16_t v_loop_post_shift_A = -1; // Bit-shift value A used to perform control output value backward normalization
85 volatile int16_t v_loop_post_shift_B = -4; // Bit-shift value B used to perform control output value backward normalization
86 volatile fractional v_loop_post_scaler = 0x0000; // Q15 fractional factor used to perform control output value backward normalization
87 
88 // P-Term Coefficient for Plant Measurements
89 volatile int16_t v_loop_pterm_factor = 0x639F; // Q15 fractional of the P-Term factor
90 volatile int16_t v_loop_pterm_scaler = 0xFFFF; // Bit-shift scaler of the P-Term factor
91 
92 //Adaptive Gain Control Coefficient
93 volatile int16_t v_loop_agc_factor_default = 0x7FFF; // Q15 fractional of the AGC factor
94 volatile int16_t v_loop_agc_scaler_default = 0x0000; // Bit-shift scaler of the AGC factor
95 
96 
97 // User-defined NPNZ16b_s controller data object
98 volatile struct NPNZ16b_s v_loop; // user-controller data object
99 
100 /* ********************************************************************************/
101 
102 /* *********************************************************************************
103  * Controller Initialization:
104  * ==========================
105  *
106  * Public controller initialization function loading known default settings
107  * into the NPNZ16b data structure.
108  *
109  * ********************************************************************************/
110 
111 volatile uint16_t v_loop_Initialize(volatile struct NPNZ16b_s* controller)
112 {
113  volatile uint16_t i=0;
114 
115  // Initialize controller data structure at runtime with pre-defined default values
116  controller->status.value = NPNZ_STATUS_CLEAR; // clear all status flag bits (will turn off execution))
117 
118  controller->Filter.ptrACoefficients = &v_loop_coefficients.ACoefficients[0]; // initialize pointer to A-coefficients array
119  controller->Filter.ptrBCoefficients = &v_loop_coefficients.BCoefficients[0]; // initialize pointer to B-coefficients array
120  controller->Filter.ptrControlHistory = &v_loop_histories.ControlHistory[0]; // initialize pointer to control history array
121  controller->Filter.ptrErrorHistory = &v_loop_histories.ErrorHistory[0]; // initialize pointer to error history array
122  controller->Filter.normPostShiftA = v_loop_post_shift_A; // initialize A-coefficients/single bit-shift scaler
123  controller->Filter.normPostShiftB = v_loop_post_shift_B; // initialize B-coefficients/dual/post scale factor bit-shift scaler
124  controller->Filter.normPostScaler = v_loop_post_scaler; // initialize control output value normalization scaling factor
125  controller->Filter.normPreShift = v_loop_pre_scaler; // initialize A-coefficients/single bit-shift scaler
126 
127  controller->Filter.ACoefficientsArraySize = v_loop_ACoefficients_size; // initialize A-coefficients array size
128  controller->Filter.BCoefficientsArraySize = v_loop_BCoefficients_size; // initialize A-coefficients array size
129  controller->Filter.ControlHistoryArraySize = v_loop_ControlHistory_size; // initialize control history array size
130  controller->Filter.ErrorHistoryArraySize = v_loop_ErrorHistory_size; // initialize error history array size
131 
132  // Load default set of A-coefficients from user RAM into controller A-array located in X-Space
133  for(i=0; i<controller->Filter.ACoefficientsArraySize; i++)
134  {
135  v_loop_coefficients.ACoefficients[i] = v_loop_ACoefficients[i]; // Load coefficient A1 value into v_loop coefficient data space
136  v_loop_coefficients.ACoefficients[i] = v_loop_ACoefficients[i]; // Load coefficient A2 value into v_loop coefficient data space
137  v_loop_coefficients.ACoefficients[i] = v_loop_ACoefficients[i]; // Load coefficient A3 value into v_loop coefficient data space
138  v_loop_coefficients.ACoefficients[i] = v_loop_ACoefficients[i]; // Load coefficient A4 value into v_loop coefficient data space
139  }
140 
141  // Load default set of B-coefficients from user RAM into controller B-array located in X-Space
142  for(i=0; i<controller->Filter.BCoefficientsArraySize; i++)
143  {
144  v_loop_coefficients.BCoefficients[i] = v_loop_BCoefficients[i]; // Load coefficient B0 value into v_loop coefficient data space
145  v_loop_coefficients.BCoefficients[i] = v_loop_BCoefficients[i]; // Load coefficient B1 value into v_loop coefficient data space
146  v_loop_coefficients.BCoefficients[i] = v_loop_BCoefficients[i]; // Load coefficient B2 value into v_loop coefficient data space
147  v_loop_coefficients.BCoefficients[i] = v_loop_BCoefficients[i]; // Load coefficient B3 value into v_loop coefficient data space
148  v_loop_coefficients.BCoefficients[i] = v_loop_BCoefficients[i]; // Load coefficient B4 value into v_loop coefficient data space
149  }
150 
151  // Clear error and control histories of the 3P3Z controller
153 
154  // Load P-Term factor and scaler into data structure
155  controller->Filter.PTermFactor = v_loop_pterm_factor;
156  controller->Filter.PTermScaler = v_loop_pterm_scaler;
157 
158  // Load initial AGC factor and scaler into data structure
161 
162  return(1);
163 }
164 
165 
166 //**********************************************************************************
167 // Download latest version of this tool here: https://areiter128.github.io/DCLD
168 //**********************************************************************************
169 
volatile uint16_t v_loop_Initialize(volatile struct NPNZ16b_s *controller)
Initializes controller coefficient arrays and normalization factors.
Definition: v_loop.c:111
void v_loop_Reset(volatile struct NPNZ16b_s *controller)
Prototype of the Assembly routine '_v_loop_Reset' clearing the NPNZ16b controller output and error hi...
volatile struct NPNZ16b_s v_loop
External reference to user-defined NPNZ16b controller data object 'v_loop'.
Definition: v_loop.c:98
volatile int16_t v_loop_pterm_scaler
Bit-shift scaler of the P-Term Coefficient for Plant Measurements.
Definition: v_loop.c:90
volatile int16_t v_loop_agc_factor_default
Q15 fractional of the Adaptive Gain Control Coefficient.
Definition: v_loop.c:93
volatile int16_t v_loop_agc_scaler_default
Bit-shift scaler of the Adaptive Gain Control Coefficient.
Definition: v_loop.c:94
volatile int16_t v_loop_pterm_factor
Q15 fractional of the P-Term Coefficient for Plant Measurements.
Definition: v_loop.c:89
volatile uint16_t value
Controller status full register access.
Definition: npnz16b.h:204
volatile uint16_t BCoefficientsArraySize
Size of the B coefficients array in X-space.
Definition: npnz16b.h:293
volatile fractional * ptrControlHistory
Pointer to n delay-line samples located in Y-space with first sample being the most recent.
Definition: npnz16b.h:288
volatile int16_t normPostShiftB
Normalization of B-term control output to Q15 (R/W)
Definition: npnz16b.h:300
volatile uint16_t ErrorHistoryArraySize
Size of the error history array in Y-space.
Definition: npnz16b.h:295
volatile int16_t PTermFactor
Q15 P-Term Coefficient Factor (R/W)
Definition: npnz16b.h:305
volatile int16_t normPostScaler
Control output normalization factor (Q15) (R/W)
Definition: npnz16b.h:301
volatile int32_t * ptrBCoefficients
Pointer to B coefficients located in X-space.
Definition: npnz16b.h:287
volatile int16_t PTermScaler
Q15 P-Term Coefficient Bit-Shift Scaler (R/W)
Definition: npnz16b.h:304
volatile int32_t * ptrACoefficients
Pointer to A coefficients located in X-space.
Definition: npnz16b.h:286
volatile uint16_t ACoefficientsArraySize
Size of the A coefficients array in X-space.
Definition: npnz16b.h:292
volatile uint16_t ControlHistoryArraySize
Size of the control history array in Y-space.
Definition: npnz16b.h:294
volatile int16_t normPostShiftA
Normalization of A-term control output to Q15 (R/W)
Definition: npnz16b.h:299
volatile int16_t normPreShift
Normalization of ADC-resolution to Q15 (R/W)
Definition: npnz16b.h:298
volatile fractional * ptrErrorHistory
Pointer to n+1 delay-line samples located in Y-space with first sample being the most recent.
Definition: npnz16b.h:289
volatile fractional AgcFactor
Q15 value of Adaptive Gain Modulation factor.
Definition: npnz16b.h:454
volatile uint16_t AgcScaler
Bit-shift scaler of Adaptive Gain Modulation factor.
Definition: npnz16b.h:453
Global NPNZ controller data object.
Definition: npnz16b.h:502
volatile struct NPNZ_STATUS_s status
Control Loop Status and Control flags.
Definition: npnz16b.h:504
volatile struct NPNZ_GAIN_CONTROL_s GainControl
Parameter section for advanced control options.
Definition: npnz16b.h:507
volatile struct NPNZ_FILTER_PARAMS_s Filter
Filter parameters such as pointer to history and coefficient arrays and number scaling.
Definition: npnz16b.h:506
Data structure packing A- and B- coefficient arrays in a linear memory space for optimized DSP code e...
Definition: v_loop.h:54
Data structure packing A- and B- coefficient arrays in a linear memory space for optimized DSP code e...
Definition: v_loop.h:66