/* * main.c * * Created on: 12 May 2025 * Author: Christian Lind Vie Madsen */ #include #include "buck_specs.h" #include "buck_emulator.h" #include "file_print.h" #include #include "cm_pid_regulator/cm_pid_regulator.h" #include "cm_mppt_regulator/cm_mppt_regulator.h" BuckEmulator_t buck_converter; cm_pid_regulator_float_t Voltreg; cm_mppt_regulator_t mppt_reg; float kp = 0.00550; float ki = 1.0; float kd = 0.0; float pid_regulator(float Vout){ static uint32_t run_ctr = 0; static float duty = 0.0; if((run_ctr++ % 10) != 0)return duty; duty = cm_PIDRegulatorf_Compute(&Voltreg, VOUT_TARGET, Vout, SAMPLE_TIME*10); return duty; } float mppt_regulator(float Vout){ static uint32_t run_ctr = 0; static float duty = 0.0; if((run_ctr++ % 10) != 0)return duty; duty = (float)cm_mppt_regulator_Compute(&mppt_reg, Vout*1000.0, (Vout*1000.0)/R_LOAD, 200.0) / 65535.0; printf("duty: %f \r\n",duty); return duty; } int printResult(float duty, float Vout, float time){ showProcentInCmd(time, SIMULATION_TIME); cm_file_print(duty,R_LOAD,Vout,time); return 0; } int main(void){ buck_emulator_init(&buck_converter, VIN,L_INDUCTOR,C_CAPACITOR,R_LOAD,SAMPLE_TIME, SIMULATION_TIME); buck_emulator_RegRegulationEvt(&buck_converter,(regulate_evt_t)mppt_regulator); buck_emulator_RegGetResultEvt(&buck_converter,(getResult_evt_t)printResult); //cm_PIDRegulatorf_Init(&Voltreg, kp, ki, kd, 0.0, 1.0); cm_mppt_regulator_init(&mppt_reg, (VOUT_TARGET*1000.0), 500.0); cm_file_open(BUCK_OUTPUT_FILE,VOUT_TARGET); buck_emulator_Run(&buck_converter); cm_file_close(); // Run Python Plot: char runcmd_string[255] = { 0 }; sprintf(runcmd_string, RUN_CMD, BUCK_OUTPUT_FILE); system(runcmd_string); return 0; } //(regulate_evt_t)pid_regulator /* * * BuckConverter buck = { .Vin = 12.0, .Vout = 0.0, .IL = 0.0, .L = 100e-6, .C = 100e-6, .Rload = 10.0, .dt = 25e-6 }; 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); } * */