some things are working but regulation is fucked!
This commit is contained in:
121
buck_emulator.c
121
buck_emulator.c
@@ -4,59 +4,88 @@
|
||||
* Created on: 12 May 2025
|
||||
* Author: Christian Lind Vie Madsen
|
||||
*/
|
||||
#include "buck_emulator.h"
|
||||
|
||||
static int isStructOk(BuckEmulator_t *inst){
|
||||
|
||||
typedef struct {
|
||||
float Vin; // Input voltage
|
||||
float Vout; // Output voltage
|
||||
float IL; // Inductor current
|
||||
float L; // Inductance
|
||||
float C; // Capacitance
|
||||
float Rload; // Load resistance
|
||||
float dt; // Time step (e.g., 25e-6)
|
||||
} BuckConverter;
|
||||
if(inst == NULL)return 0;
|
||||
|
||||
void simulate_step(BuckConverter *buck, float duty_cycle) {
|
||||
float dIL = (buck->Vin * duty_cycle - buck->Vout) / buck->L;
|
||||
buck->IL += dIL * buck->dt;
|
||||
return 1;
|
||||
|
||||
float dVout = (buck->IL - buck->Vout / buck->Rload) / buck->C;
|
||||
buck->Vout += dVout * buck->dt;
|
||||
}
|
||||
|
||||
/*
|
||||
int main() {
|
||||
BuckConverter buck = {
|
||||
.Vin = 12.0,
|
||||
.Vout = 0.0,
|
||||
.IL = 0.0,
|
||||
.L = 100e-6,
|
||||
.C = 100e-6,
|
||||
.Rload = 10.0,
|
||||
.dt = 25e-6
|
||||
};
|
||||
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;
|
||||
|
||||
float duty_cycle = 0.5;
|
||||
float Vref = 5.0;
|
||||
float error, previous_error = 0, integral = 0;
|
||||
float Kp = 0.2, Ki = 1000;
|
||||
|
||||
for (int i = 0; i < 40000; ++i) {
|
||||
error = Vref - buck.Vout;
|
||||
integral += error * buck.dt;
|
||||
|
||||
duty_cycle = Kp * error + Ki * integral;
|
||||
if (duty_cycle > 1.0) duty_cycle = 1.0;
|
||||
if (duty_cycle < 0.0) duty_cycle = 0.0;
|
||||
|
||||
simulate_step(&buck, duty_cycle);
|
||||
|
||||
// For debug:
|
||||
if (i % 1000 == 0)
|
||||
printf("Time: %.3f ms, Vout: %.2f V, IL: %.2f A, Duty: %.2f\n",
|
||||
i * buck.dt * 1000, buck.Vout, buck.IL, duty_cycle);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user