i think its working.. but needs verify..

This commit is contained in:
2025-05-25 22:23:34 +02:00
parent 5b329b710a
commit 5ff1d193c7
4 changed files with 44 additions and 13 deletions

View File

@@ -16,11 +16,42 @@ static int isStructOk(BuckEmulator_t *inst){
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 dIL_on = 0.0f, dIL_off = 0.0f;
float dVout = (inst->IL - inst->Vout / inst->Rload) / inst->C;
inst->Vout += dVout * inst->dt;
// ON-phase
if (duty_cycle > 0.0f) {
float V_L_on = inst->Vin - inst->Vout;
dIL_on = (V_L_on / inst->L) * (duty_cycle * inst->dt);
}
// OFF-phase
float V_L_off = 0.0f;
if ((inst->IL + dIL_on) > 0.0f) {
V_L_off = -inst->Vout; // Diode conducts
dIL_off = (V_L_off / inst->L) * ((1.0f - duty_cycle) * inst->dt);
} else {
dIL_off = 0.0f;
}
// Total inductor current update
inst->IL += dIL_on + dIL_off;
if (inst->IL < 0.0f) inst->IL = 0.0f;
// Capacitor current
float capCurrent = inst->IL - inst->Vout / inst->Rload;
// Diode blocking check
if (inst->IL == 0.0f && capCurrent < 0.0f) {
capCurrent = 0.0f;
}
// Vout update
float dVout = capCurrent / inst->C;
inst->Vout += dVout * inst->dt;
// Clamp Vout
if (inst->Vout < 0.0f) inst->Vout = 0.0f;
if (inst->Vout > inst->Vin) inst->Vout = inst->Vin;
return;
}
@@ -86,6 +117,6 @@ int buck_emulator_init(BuckEmulator_t *inst, float Vin, float L, float C, float
inst->simTime = sim_time;
return 0;
}