diff --git a/buck_emulator.c b/buck_emulator.c index 4ebf29a..ba76a78 100644 --- a/buck_emulator.c +++ b/buck_emulator.c @@ -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; } diff --git a/buck_specs.h b/buck_specs.h index fbc0cf5..7a0393e 100644 --- a/buck_specs.h +++ b/buck_specs.h @@ -14,8 +14,8 @@ // Component Values: -#define L_INDUCTOR 100e-6 // Henry -#define C_CAPACITOR 100e-6 // Farad +#define L_INDUCTOR 68e-6 // Henry +#define C_CAPACITOR 10e-6 // Farad // Sample time @@ -23,10 +23,10 @@ // Load -#define R_LOAD 100 // Ohm +#define R_LOAD 10 // Ohm // Simulation parameters -#define SIMULATION_TIME 1.0 // Seconds +#define SIMULATION_TIME 0.6 // Seconds // Plot specific diff --git a/main.c b/main.c index 008f30a..02f211c 100644 --- a/main.c +++ b/main.c @@ -14,8 +14,8 @@ BuckEmulator_t buck_converter; cm_pid_regulator_float_t Voltreg; -float kp = 0.00075; -float ki = 0.00001; +float kp = 0.00550; +float ki = 1.0; float kd = 0.0; diff --git a/plotscript/python_csv_plotter.py b/plotscript/python_csv_plotter.py index 6f8b52f..5506291 100644 --- a/plotscript/python_csv_plotter.py +++ b/plotscript/python_csv_plotter.py @@ -49,7 +49,7 @@ with open(sys.argv[1], newline='') as csvfile: # Begin plotting 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) # Plot Vout @@ -59,7 +59,7 @@ ax1.set_ylabel('Vout', color=color) ax1.plot(time_array, temperature_array, color=color, label='Vout') ax1.tick_params(axis='y', labelcolor=color) 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) # Plot Power (2nd y-axis)