92 lines
1.6 KiB
C
92 lines
1.6 KiB
C
/*
|
|
* buck_emulator.c
|
|
*
|
|
* Created on: 12 May 2025
|
|
* Author: Christian Lind Vie Madsen
|
|
*/
|
|
#include "buck_emulator.h"
|
|
|
|
static int isStructOk(BuckEmulator_t *inst){
|
|
|
|
if(inst == NULL)return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
static void buck_emulator_step(BuckEmulator_t *inst, float duty_cycle) {
|
|
|
|
float dIL = (inst->Vin * duty_cycle - inst->Vout) / inst->L;
|
|
inst->IL += dIL * inst->dt;
|
|
|
|
float dVout = (inst->IL - inst->Vout / inst->Rload) / inst->C;
|
|
inst->Vout += dVout * inst->dt;
|
|
|
|
return;
|
|
}
|
|
|
|
int buck_emulator_Run(BuckEmulator_t *inst){
|
|
|
|
if(!isStructOk(inst))return 1;
|
|
|
|
float dutyCycle = 0.0;
|
|
|
|
for(float t = 0.0; t < inst->simTime; t+=inst->dt){
|
|
|
|
if(inst->regulate_evt != NULL)dutyCycle = inst->regulate_evt(inst->Vout);
|
|
|
|
|
|
buck_emulator_step(inst, dutyCycle);
|
|
|
|
if(inst->getResult_evt != NULL)inst->getResult_evt(dutyCycle,inst->Vout,t);
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int buck_emulator_RegGetResultEvt(BuckEmulator_t *inst, getResult_evt_t getRes_evt){
|
|
|
|
if(!isStructOk(inst))return 1;
|
|
|
|
if(getRes_evt != NULL){
|
|
|
|
inst->getResult_evt = getRes_evt;
|
|
return 0;
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
int buck_emulator_RegRegulationEvt(BuckEmulator_t *inst, regulate_evt_t reg_evt){
|
|
|
|
if(!isStructOk(inst))return 1;
|
|
|
|
if(reg_evt != NULL){
|
|
|
|
inst->regulate_evt = reg_evt;
|
|
return 0;
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
int buck_emulator_init(BuckEmulator_t *inst, float Vin, float L, float C, float Rload, float dt, float sim_time){
|
|
|
|
if(!isStructOk(inst))return 1;
|
|
|
|
inst->Vin = Vin;
|
|
inst->L = L;
|
|
inst->C = C;
|
|
inst->Rload = Rload;
|
|
inst->dt = dt;
|
|
|
|
inst->Vout = 0.0;
|
|
inst->IL = 0.0;
|
|
|
|
inst->simTime = sim_time;
|
|
|
|
|
|
|
|
}
|