Digital Power Starter Kit 3 Firmware
dsPIC33C Boost Converter Voltage Mode Control Example
v_loop.c
1 /* *********************************************************************************
2  * PowerSmartâ„¢ Digital Control Library Designer, Version 0.9.14.676
3  * *********************************************************************************
4  * 3p3z controller function declarations and compensation filter coefficients
5  * derived for following operating conditions:
6  * *********************************************************************************
7  *
8  * Controller Type: 3P3Z - Basic Voltage Mode Compensator
9  * Sampling Frequency: 500000 Hz
10  * Fixed Point Format: Q15
11  * Scaling Mode: 4 - Fast Floating Point Coefficient Scaling
12  * Input Gain: 0.12531328320802
13  *
14  * *********************************************************************************
15  * CGS Version: 3.0.8
16  * CGS Date: 03/12/2021
17  * *********************************************************************************
18  * User: M91406
19  * Date/Time: 04/01/2021 23:47:39
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: 360 Hz
54  * fP1: 40000 Hz
55  * fP2: 160000 Hz
56  * fZ1: 1810 Hz
57  * fZ2: 3833 Hz
58  *
59  * *********************************************************************************
60  * Filter Coefficients and Parameters:
61  * ********************************************************************************/
62 
63 volatile int32_t v_loop_ACoefficients [3] =
64 {
65  0x6620FFFF, // Coefficient A1 will be multiplied with controller output u(n-1)
66  0xB3F60000, // Coefficient A2 will be multiplied with controller output u(n-2)
67  0x982E0009 // Coefficient A3 will be multiplied with controller output u(n-3)
68 };
69 
70 volatile int32_t v_loop_BCoefficients [4] =
71 {
72  0x6DFEFFFD, // Coefficient B0 will be multiplied with error input e(n-0)
73  0x99A9FFFD, // Coefficient B1 will be multiplied with error input e(n-1)
74  0x9221FFFD, // Coefficient B2 will be multiplied with error input e(n-2)
75  0x6676FFFD // Coefficient B3 will be multiplied with error input e(n-3)
76 };
77 
78 // Coefficient normalization factors
79 volatile int16_t v_loop_pre_scaler = 3; // Bit-shift value used to perform input value normalization
80 volatile int16_t v_loop_post_shift_A = 0; // Bit-shift value A used to perform control output value backward normalization
81 volatile int16_t v_loop_post_shift_B = 0; // Bit-shift value B used to perform control output value backward normalization
82 volatile fractional v_loop_post_scaler = 0x0000; // Q15 fractional factor used to perform control output value backward normalization
83 
84 // P-Term Coefficient for Plant Measurements
85 volatile int16_t v_loop_pterm_factor = 0x6188; // Q15 fractional of the P-Term factor
86 volatile int16_t v_loop_pterm_scaler = 0xFFFF; // Bit-shift scaler of the P-Term factor
87 
88 
89 // User-defined NPNZ16b_s controller data object
90 volatile struct NPNZ16b_s v_loop; // user-controller data object
91 
92 /* ********************************************************************************/
93 
94 /* *********************************************************************************
95  * Controller Initialization:
96  * ==========================
97  *
98  * Public controller initialization function loading known default settings
99  * into the NPNZ16b data structure.
100  *
101  * ********************************************************************************/
102 
103 volatile uint16_t v_loop_Initialize(volatile struct NPNZ16b_s* controller)
104 {
105  volatile uint16_t i=0;
106 
107  // Initialize controller data structure at runtime with pre-defined default values
108  controller->status.value = NPNZ_STATUS_CLEAR; // clear all status flag bits (will turn off execution))
109 
110  controller->Filter.ptrACoefficients = &v_loop_coefficients.ACoefficients[0]; // initialize pointer to A-coefficients array
111  controller->Filter.ptrBCoefficients = &v_loop_coefficients.BCoefficients[0]; // initialize pointer to B-coefficients array
112  controller->Filter.ptrControlHistory = &v_loop_histories.ControlHistory[0]; // initialize pointer to control history array
113  controller->Filter.ptrErrorHistory = &v_loop_histories.ErrorHistory[0]; // initialize pointer to error history array
114  controller->Filter.normPostShiftA = v_loop_post_shift_A; // initialize A-coefficients/single bit-shift scaler
115  controller->Filter.normPostShiftB = v_loop_post_shift_B; // initialize B-coefficients/dual/post scale factor bit-shift scaler
116  controller->Filter.normPostScaler = v_loop_post_scaler; // initialize control output value normalization scaling factor
117  controller->Filter.normPreShift = v_loop_pre_scaler; // initialize A-coefficients/single bit-shift scaler
118 
119  controller->Filter.ACoefficientsArraySize = v_loop_ACoefficients_size; // initialize A-coefficients array size
120  controller->Filter.BCoefficientsArraySize = v_loop_BCoefficients_size; // initialize A-coefficients array size
121  controller->Filter.ControlHistoryArraySize = v_loop_ControlHistory_size; // initialize control history array size
122  controller->Filter.ErrorHistoryArraySize = v_loop_ErrorHistory_size; // initialize error history array size
123 
124  // Load default set of A-coefficients from user RAM into controller A-array located in X-Space
125  for(i=0; i<controller->Filter.ACoefficientsArraySize; i++)
126  {
127  v_loop_coefficients.ACoefficients[i] = v_loop_ACoefficients[i]; // Load coefficient A1 value into v_loop coefficient data space
128  v_loop_coefficients.ACoefficients[i] = v_loop_ACoefficients[i]; // Load coefficient A2 value into v_loop coefficient data space
129  v_loop_coefficients.ACoefficients[i] = v_loop_ACoefficients[i]; // Load coefficient A3 value into v_loop coefficient data space
130  }
131 
132  // Load default set of B-coefficients from user RAM into controller B-array located in X-Space
133  for(i=0; i<controller->Filter.BCoefficientsArraySize; i++)
134  {
135  v_loop_coefficients.BCoefficients[i] = v_loop_BCoefficients[i]; // Load coefficient B0 value into v_loop coefficient data space
136  v_loop_coefficients.BCoefficients[i] = v_loop_BCoefficients[i]; // Load coefficient B1 value into v_loop coefficient data space
137  v_loop_coefficients.BCoefficients[i] = v_loop_BCoefficients[i]; // Load coefficient B2 value into v_loop coefficient data space
138  v_loop_coefficients.BCoefficients[i] = v_loop_BCoefficients[i]; // Load coefficient B3 value into v_loop coefficient data space
139  }
140 
141  // Clear error and control histories of the 3P3Z controller
142  v_loop_Reset(&v_loop);
143 
144  // Load P-Term factor and scaler into data structure
145  controller->Filter.PTermFactor = v_loop_pterm_factor;
146  controller->Filter.PTermScaler = v_loop_pterm_scaler;
147 
148  return(1);
149 }
150 
151 
152 //**********************************************************************************
153 // Download latest version of this tool here: https://areiter128.github.io/DCLD
154 //**********************************************************************************
155 
volatile uint16_t value
Controller status full register access.
Definition: npnz16b.h:204
volatile int16_t normPostShiftA
Normalization of A-term control output to Q15 (R/W)
Definition: npnz16b.h:299
volatile int16_t PTermScaler
Q15 P-Term Coefficient Bit-Shift Scaler (R/W)
Definition: npnz16b.h:304
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 uint16_t ControlHistoryArraySize
Size of the control history array in Y-space.
Definition: npnz16b.h:294
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 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 BCoefficientsArraySize
Size of the B coefficients array in X-space.
Definition: npnz16b.h:293
volatile int32_t * ptrBCoefficients
Pointer to B coefficients located in X-space.
Definition: npnz16b.h:287
volatile struct NPNZ_STATUS_s status
Control Loop Status and Control flags.
Definition: npnz16b.h:504
volatile uint16_t ErrorHistoryArraySize
Size of the error history array in Y-space.
Definition: npnz16b.h:295
volatile int16_t normPreShift
Normalization of ADC-resolution to Q15 (R/W)
Definition: npnz16b.h:298
volatile int16_t normPostScaler
Control output normalization factor (Q15) (R/W)
Definition: npnz16b.h:301
volatile int16_t normPostShiftB
Normalization of B-term control output to Q15 (R/W)
Definition: npnz16b.h:300
volatile struct NPNZ_FILTER_PARAMS_s Filter
Filter parameters such as pointer to history and coefficient arrays and number scaling.
Definition: npnz16b.h:506
volatile int16_t PTermFactor
Q15 P-Term Coefficient Factor (R/W)
Definition: npnz16b.h:305