Bar Logo 4kW dsPIC33C PSFB DC-DC DA (Part-No. )
 
Content
     
Loading...
Searching...
No Matches
pwrctrl_sm.c
Go to the documentation of this file.
1
8#include <xc.h>
9#include <stdint.h> // include standard integer data types
10#include <stdbool.h> // include standard boolean data types
11#include <stddef.h> // include standard definition data types
12#include <stdlib.h> // for abs function, see how it works
13
14
15// PSFB header files
16#include "pwrctrl.h"
17//#include "config/macros.h"
18#include "device/device.h"
19#include "fault/fault.h"
20//#include "config/config.h"
21#include "pwrctrl_pwm.h"
22#include "system/pins.h"
23
24
25// temp
26//uint16_t current_da = 1303;
27float temp;
28uint16_t ref_diff;
29uint16_t delay_count = 2;
30
31//PRIVATE FUNCTIONS
32static void PCS_INIT_handler(POWER_CONTROL_t* pcInstance);
34static void PCS_STANDBY_handler(POWER_CONTROL_t* pcInstance);
35static void PCS_SOFT_START_handler(POWER_CONTROL_t* pcInstance);
36static void PCS_UP_AND_RUNNING_handler(POWER_CONTROL_t* pcInstance);
37static void PCS_PRECHARGE_handler(POWER_CONTROL_t* pcInstance);
38
39
49{
50 switch (pcInstance->State)
51 {
53 PCS_INIT_handler(pcInstance);
54 break;
55
58 break;
59
61 PCS_STANDBY_handler(pcInstance);
62 break;
63
65 PCS_SOFT_START_handler(pcInstance);
66 break;
67
70 break;
71
73 PCS_PRECHARGE_handler(pcInstance);
74 break;
75
76 default:
77 pcInstance->State = PWRCTRL_STATE_INITIALIZE;
78 break;
79 }
80}
81
82
92static void PCS_INIT_handler(POWER_CONTROL_t* pcInstance)
93{
94 // Current sensor calibration is complete. Update the offset of the current sensor
95 // pcInstance->Data.ISecSensorOffset = Dev_CurrentSensor_Get_Offset();
96
97 // Ensure PWM output is disabled
98 // PwrCtrl_PWM_Disable();
99
100 //Disable short circuit for now -> Temporary should go in fault set/reset
101 // FAULT_EN_SetLow();
102
103 // Reset fault objects status bits
104 Fault_Reset();
105
106 //reset control loop
108
109 //disable Iloop here
110 pcInstance->ILoop.Enable = 0;
111 //disable Vloop here
112 pcInstance->VLoop.Enable = 0;
113
114 // Clear power control fault active bit
115 pcInstance->Status.bits.FaultActive = 0;
116 // Clear power control running bit
117 pcInstance->Status.bits.Running = 0;
118 // Clear power control enable bit
119 pcInstance->Properties.Enable = 0;
120
121
122 Dev_LED_Off(LED_BOARD_GREEN);
123 Dev_LED_Off(LED_BOARD_RED);
124
125 //add delay of some ms to for values to be stable
126 if (delay_count-- == 0) {
127 delay_count = 3;
128 // review the
130 &pcInstance->PrimaryCT_Offset,
132 &&
134 &pcInstance->SecondarySh_Offset,
136 &&
138 &pcInstance->VoutCalibratingAveraging,
139 pcInstance->Data.VOutVoltage) !=0
140 )
141 {
142 pcInstance->Data.IPriSensorOffset = pcInstance->PrimaryCT_Offset.AverageValue;
143 pcInstance->Data.ISecSensorOffset = pcInstance->SecondarySh_Offset.AverageValue;
145
146 //34 amps
147 pcInstance->SecRec.Threshold_high = pcInstance->Data.ISecSensorOffset + 421;
148 //30amps
149 pcInstance->SecRec.Threshold_low = pcInstance->Data.ISecSensorOffset + 372;
150
151 pcInstance->VoutCalibrate.calculated_value = 2293; // for 12 volts
152 pcInstance->VoutCalibrate.gain_factor =
155
156 pcInstance->State = PWRCTRL_STATE_PRECHARGE;
157
159 }
160 }
161
162
163 // if (dev_AreOffsetsCalculated() == 1){
164 // pcInstance->Data.ISecSensorOffset = dev_Get_SecondaryShuntOffset();
165 // pcInstance->Data.IPriSensorOffset = dev_Get_PrimaryCTOffset();
166
167 // //34 amps
168 // pcInstance->SecRec.Threshold_high = pcInstance->Data.ISecSensorOffset + 421;
169 // //30amps
170 // pcInstance->SecRec.Threshold_low = pcInstance->Data.ISecSensorOffset + 372;
171
172 // pcInstance->State = PWRCTRL_STATE_PRECHARGE;
173
174
175 // FAULT_EN_SetHigh();
176 // }
177 // else {
178 // dev_MeasureOffsets();
179 // }
180
181 //pcInstance->State = PWRCTRL_STATE_PRECHARGE;
182
183}
184
185
196{
197 if (pcInstance->Fault.FaultDetected == 0)
198 {
199 pcInstance->Status.bits.FaultActive = 0;
200 FAULT_EN_SetLow(); // exiting fault, making sure secondary current has died down
201 dev_MeasureOffsets_Initialize(); // measure offsets again.
202 pcInstance->State = PWRCTRL_STATE_INITIALIZE; // reinit system
203 }
204
205 // Check for fault event
206 if (pcInstance->Fault.FaultDetected)
207 {
208 // Clear power control enable bit
209 pcInstance->Properties.Enable = 0;
210
211 // override precharge , if fault happens in precharge, to clear precharge bit
212 pcInstance->Precharge.PrechargeEnabled = 0 ;
213
214 // State back to STATE_FAULT_DETECTION
216 }
217
218 Dev_LED_Blink(LED_BOARD_RED);
219 Dev_LED_Off(LED_BOARD_GREEN);
220}
221
222
239{
240 // Check for fault event
241 if (pcInstance->Fault.FaultDetected)
242 {
243 // Clear power control enable bit
244 pcInstance->Properties.Enable = 0;
245
246 // State back to STATE_FAULT_DETECTION
248 return; //TODO: fix state machine and fault jumps
249 }
250 // // NOTE: Power control enable is controlled externally
251 else if ((pcInstance->Properties.Enable)&&(pcInstance->Precharge.precharged == 1))
252 {
253 // Reset fault objects status bits
254 Fault_Reset();
255
256 // Reset the power control properties and control loop histories
258
259 // Update PWM distribution
260// PwrCtrl_PWM_Update(&psfb);
261
262 // Enable PWM physical output
263 //PwrCtrl_PWM_Enable();
264
265 // Enable power control running bit
266 pcInstance->Status.bits.Running = 1;
267
268 // override precharge
269 pcInstance->Precharge.PrechargeEnabled = 0 ;
270
271 // Next State assigned to STATE_SOFT_START
272 pcInstance->State = PWRCTRL_STATE_STANDBY;
273
274 }
275
276 else if (pcInstance->Precharge.PrechargeEnabled == 1) {
277// pcInstance->Precharge.maxDutyCycle = 42;
278 if (pcInstance->Precharge.DutyCycle < pcInstance->Precharge.maxDutyCycle) {
279 pcInstance->Precharge.delayCounter = pcInstance->Precharge.delayCounter + 1;
280 if (pcInstance->Precharge.delayCounter > 25){ //
281 Nop();
282 pcInstance->Precharge.delayCounter = 0;
283 pcInstance->Precharge.DutyCycle = pcInstance->Precharge.DutyCycle + 1;
284 }
285 }
286 else {
287 pcInstance->Precharge.precharged = 1;
288 }
290 pcInstance->State = PWRCTRL_STATE_PRECHARGE;
291 Dev_LED_Blink(LED_BOARD_GREEN);
292 Dev_LED_Off(LED_BOARD_RED);
293 }
294
295 else if(pcInstance->Precharge.PrechargeEnabled == 0) {
296 pcInstance->Precharge.DutyCycle = 0;
299 pcInstance->Precharge.precharged = 0;
300 pcInstance->State = PWRCTRL_STATE_PRECHARGE;
301 }
302}
303
304
305
321static void PCS_STANDBY_handler(POWER_CONTROL_t* pcInstance)
322{
323 // Check for fault event
324 if (pcInstance->Fault.FaultDetected)
325 {
326 // Clear power control enable bit
327 pcInstance->Properties.Enable = 0;
328
329 // State back to STATE_FAULT_DETECTION
331 }
332
333 // Check if Enable bit has been cleared
334 else if (!pcInstance->Properties.Enable)
335 {
336 // State back to STATE_INIT
337 pcInstance->State = PWRCTRL_STATE_INITIALIZE;
338 }
339
340 // NOTE: Power control enable is controlled externally
341 else if (pcInstance->Properties.Enable)
342 {
343 // Reset fault objects status bits
344 // Fault_Reset();
345
346 // Reset the power control properties and control loop histories
347 // PwrCtrl_Reset();
348
349 // Update PWM distribution
350// PwrCtrl_PWM_Update(&psfb);
351
352 // Enable power control running bit
353 pcInstance->Status.bits.Running = 1;
354
355 // Next State assigned to STATE_SOFT_START
356 pcInstance->State = PWRCTRL_STATE_SOFT_START;
357
358 // Enable PWM physical output
359 // PwrCtrl_PWM_Enable();
360
361 // enable SR flag to be controlled by current
362 pcInstance->SecRec.SR_Enabled = 1;
363 //Enable Iloop here
364 pcInstance->ILoop.Enable = 1;
365 //Enable Vloop here
366 pcInstance->VLoop.Enable = 1;
367
368 //reference set to calibrated 12V
369 float vreftem = (float)2293 * pcInstance->VoutCalibrate.gain_factor; // 2293 = 12 V (2293/4095 * 3.3 * Gain) Gain =154mv/v
370 pcInstance->VLoop.Reference = (uint16_t)vreftem;
371 pcInstance->Properties.VSecReference = (uint16_t)vreftem;
372 pcInstance->Droop.Droop_Voltage_Reference_from_PBV = (uint16_t)vreftem;
373 }
374
375 Dev_LED_On(LED_BOARD_GREEN);
376 Dev_LED_Off(LED_BOARD_RED);
377}
378
379
392{
393 // Check for fault event
394 if (pcInstance->Fault.FaultDetected)
395 {
396 // Clear power control enable bit
397 pcInstance->Properties.Enable = 0;
398
399 // State back to STATE_FAULT_DETECTION
401 }
402
403 // Check if Enable bit has been cleared
404 else if (!pcInstance->Properties.Enable)
405 {
406 // Disable PWM physical PRIMARY output
407 //PwrCtrl_PWM_Disable();
409
410 // Clear power control running bit
411 pcInstance->Status.bits.Running = 0;
412
413 // State back to STATE_INIT
414 pcInstance->State = PWRCTRL_STATE_STANDBY;
415 }
416
417 else
418 {
419 // Ramp Up the Voltage, Current and Power reference
420 uint16_t rampComplete = PwrCtrl_RampReference(&pcInstance->VRamp);
421
422 if (rampComplete)
423 // Next State assigned to STATE_ONLINE
424 pcInstance->State = PWRCTRL_STATE_UP_AND_RUNNING;
425 }
426
427 Dev_LED_Blink(LED_BOARD_GREEN);
428 Dev_LED_Off(LED_BOARD_RED);
429}
430
431
442{
443 // Check for fault event
444 if (pcInstance->Fault.FaultDetected)
445 {
446 // Clear power control enable bit
447 pcInstance->Properties.Enable = 0;
448
449 // State back to STATE_FAULT_DETECTION
451 }
452
453 else if (!pcInstance->Properties.Enable)
454 // Check if Enable bit has been cleared
455 {
456 // Disable PWM physical output
457 //PwrCtrl_PWM_Disable();
459
460 // Clear power control running bit
461 pcInstance->Status.bits.Running = 0;
462
463 // State back to STATE_STANDBY AND THEN INITIALITZE
464 pcInstance->State = PWRCTRL_STATE_STANDBY;
465 Dev_LED_Off(LED_BOARD_GREEN);
466 }
467 //removing it for now
468
469 // Check if there is change in power control references
470
471 else if (pcInstance->VLoop.Reference != pcInstance->Properties.VSecReference)
472 {
473 // State back to STATE_SOFT_START
474 pcInstance->State = PWRCTRL_STATE_SOFT_START;
475 }
476
477 else {
478 // average last 1000 ->each sample 10us, called every 100us * 100 times
480 // droop update
481 if (++pcInstance->Droop.Droop_Delay_Counter>=999)
482 {
483 temp = pcInstance->ISecAveraging.AverageValue - pcInstance->Data.ISecSensorOffset; // * 0.073;
484 if (temp > 10) // avoid gar
485 {
486 temp = temp * 0.0855; //droop variable 0.0855 adc counts per amp:
487 pcInstance->Droop.ref_diff = (uint16_t)temp;
488 }
489 else
490 pcInstance->Droop.ref_diff = 0;
491
492 if (pcInstance->Droop.ref_diff > 400) pcInstance->Droop.ref_diff = 0 ; // voltage drop of 1 volt results in 191 counts, something is wrong, set droop to 0
493 if (pcInstance->Droop.ref_diff > 200) pcInstance->Droop.ref_diff = 191; // clamp to 1 V in normal operation
494
495 pcInstance->Properties.VSecReference = pcInstance->Droop.Droop_Voltage_Reference_from_PBV - pcInstance->Droop.ref_diff ;
496 //pcInstance->Droop.Droop_New_Voltage_Reference = pcInstance->Droop.Droop_New_Voltage_Reference >> 8;
497 //pcInstance->Properties.VSecReference = pcInstance->Droop.Droop_Voltage_Reference_from_PBV - pcInstance->Droop.Droop_New_Voltage_Reference ;
498 pcInstance->Droop.Droop_Delay_Counter = 0;
499 }
500 }
501 Dev_LED_Blink(LED_BOARD_GREEN);
502 Dev_LED_Off(LED_BOARD_RED);
503}
Contains h files that are part of device layer.
Contains public fault functions.
POWER_CONTROL_t * psfb_ptr
Definition pwrctrl.c:28
Contains public functions for PWM.
float temp
Definition pwrctrl_sm.c:27
uint16_t delay_count
Definition pwrctrl_sm.c:29
uint16_t ref_diff
Definition pwrctrl_sm.c:28
void dev_MeasureOffsets_Initialize(void)
void PwrCtrl_Reset(void)
Resets the power control properties.
Definition pwrctrl.c:85
void PwrCtrl_DroopAverage(void)
Averages secondary current for droop.
void PwrCtrl_PWM_SetDutyCyclePrimary(uint16_t dutycycle)
set the PWM output to particular duty cycle.
void PwrCtrl_PWM_Stop_Switching(void)
Disable the PWM output.
static void PCS_PRECHARGE_handler(POWER_CONTROL_t *pcInstance)
Executes Standby State machine.
Definition pwrctrl_sm.c:238
void PwrCtrl_StateMachine(POWER_CONTROL_t *pcInstance)
Manages the power control state machine.
Definition pwrctrl_sm.c:48
static void PCS_STANDBY_handler(POWER_CONTROL_t *pcInstance)
Executes Standby State machine.
Definition pwrctrl_sm.c:321
static void PCS_INIT_handler(POWER_CONTROL_t *pcInstance)
Executes function for initialze state machine.
Definition pwrctrl_sm.c:92
static void PCS_UP_AND_RUNNING_handler(POWER_CONTROL_t *pcInstance)
Executes the Online state.
Definition pwrctrl_sm.c:441
static void PCS_WAIT_IF_FAULT_ACTIVE_handler(POWER_CONTROL_t *pcInstance)
Executes the fault handler state machine.
Definition pwrctrl_sm.c:195
static void PCS_SOFT_START_handler(POWER_CONTROL_t *pcInstance)
Executes the power control soft start state machine.
Definition pwrctrl_sm.c:391
uint16_t PwrCtrl_GetAdc_Ipri_ct(void)
API function to get the raw ADC value for primary current transformer current.
uint16_t PwrCtrl_GetAdc_Isec_shunt(void)
API function to get the raw ADC value for secondary current transformer current.
bool PwrCtrl_RampReference(START_UP_RAMP_t *rampUp)
Softly increment / decrement to the set reference target.
uint16_t PwrCtrl_UpdateAverage(AVERAGING_t *data, uint16_t sample)
Average the raw data over number of samples.
@ PWRCTRL_STATE_STANDBY
power converter control state #2: waiting for status bits allowing power supply to run
@ PWRCTRL_STATE_PRECHARGE
power converter control state #1: charging output caps
@ PWRCTRL_STATE_INITIALIZE
power converter control state #0: initializes object properties and clears status bits
@ PWRCTRL_STATE_SOFT_START
power converter control state #4: performs soft-start sequence
@ PWRCTRL_STATE_UP_AND_RUNNING
power converter control state #5: output is in regulation and power is OK (normal continuous operatio...
@ PWRCTRL_STATE_FAULT_DETECTION
power converter control state #6: checks if a fault occurs
void Fault_Reset(void)
Clears the fault object flag bits.
Definition fault.c:247
#define FAULT_EN_SetLow()
Sets the RD9 GPIO Pin which has a custom name of FAULT_EN to Low.
Definition pins.h:164
#define FAULT_EN_SetHigh()
Sets the RD9 GPIO Pin which has a custom name of FAULT_EN to High.
Definition pins.h:155
uint16_t FaultDetected
Fault indication for fault occurrence.
uint16_t IPriSensorOffset
Offset of the primary current sensor.
uint16_t ISecSensorOffset
Offset of the secondary current sensor.
uint16_t VOutVoltage
Data value for output voltage.
unsigned Running
Bit 0: Power converter is running.
unsigned FaultActive
Bit 1: Power converter fault is active.
bool Enable
Enable control loop.
int16_t Reference
actual reference
bool Enable
Control Flag: When set, enables the converter triggering a startup sequence; When cleared,...
uint16_t VSecReference
User secondary-voltage port reference setting used to control the power converter output voltage in b...
uint8_t PrechargeEnabled
uint16_t maxDutyCycle
uint16_t delayCounter
uint16_t DutyCycle
uint16_t Threshold_high
uint16_t Threshold_low
uint8_t SR_Enabled
uint16_t AverageValue
uint16_t Droop_Voltage_Reference_from_PBV
uint16_t Droop_Delay_Counter
uint16_t ref_diff
uint16_t real_value
uint16_t calculated_value
Power control API structure.
CONTROLLER_t ILoop
structure for current controller data
PWR_CTRL_PROPERTIES_t Properties
Power Control properties
PRECHARGE_t Precharge
structure for precharge data
DROOP_t Droop
droop controller data
AVERAGING_t VoutCalibratingAveraging
structure for averaging Vout during Droop
SEC_REC_t SecRec
structure for secondary rectification data
CALIBRATE_t VoutCalibrate
structure for Calibrating Vout during Droop using Averaged value
CONTROLLER_t VLoop
structure for voltage controller data
PWR_CTRL_STATE_t State
Power Control State ID.
AVERAGING_t SecondarySh_Offset
structure for averaging secondary sensor offset
FEEDBACK_SETTINGS_t Data
Feedback channel settings.
AVERAGING_t ISecAveraging
secondary current average
STATUS_FLAGS_t Status
Power Supply status flags.
FAULT_SETTINGS_t Fault
Fault flags and settings.
AVERAGING_t PrimaryCT_Offset
structure for averaging primary CT offset
START_UP_RAMP_t VRamp
Voltage ramp-up settings.