i think its working.. but needs verify..
This commit is contained in:
@@ -16,11 +16,42 @@ static int isStructOk(BuckEmulator_t *inst){
|
|||||||
|
|
||||||
static void buck_emulator_step(BuckEmulator_t *inst, float duty_cycle) {
|
static void buck_emulator_step(BuckEmulator_t *inst, float duty_cycle) {
|
||||||
|
|
||||||
float dIL = (inst->Vin * duty_cycle - inst->Vout) / inst->L;
|
float dIL_on = 0.0f, dIL_off = 0.0f;
|
||||||
inst->IL += dIL * inst->dt;
|
|
||||||
|
|
||||||
float dVout = (inst->IL - inst->Vout / inst->Rload) / inst->C;
|
// ON-phase
|
||||||
inst->Vout += dVout * inst->dt;
|
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;
|
return;
|
||||||
}
|
}
|
||||||
@@ -86,6 +117,6 @@ int buck_emulator_init(BuckEmulator_t *inst, float Vin, float L, float C, float
|
|||||||
|
|
||||||
inst->simTime = sim_time;
|
inst->simTime = sim_time;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,8 +14,8 @@
|
|||||||
|
|
||||||
|
|
||||||
// Component Values:
|
// Component Values:
|
||||||
#define L_INDUCTOR 100e-6 // Henry
|
#define L_INDUCTOR 68e-6 // Henry
|
||||||
#define C_CAPACITOR 100e-6 // Farad
|
#define C_CAPACITOR 10e-6 // Farad
|
||||||
|
|
||||||
|
|
||||||
// Sample time
|
// Sample time
|
||||||
@@ -23,10 +23,10 @@
|
|||||||
|
|
||||||
|
|
||||||
// Load
|
// Load
|
||||||
#define R_LOAD 100 // Ohm
|
#define R_LOAD 10 // Ohm
|
||||||
|
|
||||||
// Simulation parameters
|
// Simulation parameters
|
||||||
#define SIMULATION_TIME 1.0 // Seconds
|
#define SIMULATION_TIME 0.6 // Seconds
|
||||||
|
|
||||||
|
|
||||||
// Plot specific
|
// Plot specific
|
||||||
|
|||||||
4
main.c
4
main.c
@@ -14,8 +14,8 @@
|
|||||||
BuckEmulator_t buck_converter;
|
BuckEmulator_t buck_converter;
|
||||||
cm_pid_regulator_float_t Voltreg;
|
cm_pid_regulator_float_t Voltreg;
|
||||||
|
|
||||||
float kp = 0.00075;
|
float kp = 0.00550;
|
||||||
float ki = 0.00001;
|
float ki = 1.0;
|
||||||
float kd = 0.0;
|
float kd = 0.0;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ with open(sys.argv[1], newline='') as csvfile:
|
|||||||
|
|
||||||
# Begin plotting
|
# Begin plotting
|
||||||
fig1, ax1 = plt.subplots()
|
fig1, ax1 = plt.subplots()
|
||||||
fig1.canvas.manager.set_window_title('DVT-Light Python Plotter V1.1')
|
fig1.canvas.manager.set_window_title('OZ1CM Python Plotter V1.1')
|
||||||
fig1.suptitle(plot_title)
|
fig1.suptitle(plot_title)
|
||||||
|
|
||||||
# Plot Vout
|
# Plot Vout
|
||||||
@@ -59,7 +59,7 @@ ax1.set_ylabel('Vout', color=color)
|
|||||||
ax1.plot(time_array, temperature_array, color=color, label='Vout')
|
ax1.plot(time_array, temperature_array, color=color, label='Vout')
|
||||||
ax1.tick_params(axis='y', labelcolor=color)
|
ax1.tick_params(axis='y', labelcolor=color)
|
||||||
ax1.set_ylim(min_temp, max_temp + temperature_end_offset)
|
ax1.set_ylim(min_temp, max_temp + temperature_end_offset)
|
||||||
ax1.set_yticks(np.arange(min_temp, max_temp + temperature_end_offset, step=10))
|
ax1.set_yticks(np.arange(min_temp, max_temp + temperature_end_offset, step=1.0))
|
||||||
ax1.grid(True)
|
ax1.grid(True)
|
||||||
|
|
||||||
# Plot Power (2nd y-axis)
|
# Plot Power (2nd y-axis)
|
||||||
|
|||||||
Reference in New Issue
Block a user