some things are working but regulation is fucked!
This commit is contained in:
35
.cproject
35
.cproject
@@ -14,21 +14,22 @@
|
||||
</extensions>
|
||||
</storageModule>
|
||||
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
|
||||
<configuration artifactName="${ProjName}" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.exe" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.exe,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.debug" cleanCommand="rm -rf" description="" id="cdt.managedbuild.config.gnu.cross.exe.debug.1636823514" name="Debug" parent="cdt.managedbuild.config.gnu.cross.exe.debug">
|
||||
<configuration artifactName="${ProjName}" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.exe" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.exe,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.debug" cleanCommand="rm -rf" description="" id="cdt.managedbuild.config.gnu.cross.exe.debug.1636823514" name="Debug" optionalBuildProperties="org.eclipse.cdt.docker.launcher.containerbuild.property.selectedvolumes=,org.eclipse.cdt.docker.launcher.containerbuild.property.volumes=" parent="cdt.managedbuild.config.gnu.cross.exe.debug">
|
||||
<folderInfo id="cdt.managedbuild.config.gnu.cross.exe.debug.1636823514." name="/" resourcePath="">
|
||||
<toolChain id="cdt.managedbuild.toolchain.gnu.cross.exe.debug.1961198660" name="Cross GCC" superClass="cdt.managedbuild.toolchain.gnu.cross.exe.debug">
|
||||
<option id="cdt.managedbuild.option.gnu.cross.prefix.118603444" superClass="cdt.managedbuild.option.gnu.cross.prefix" value="gcc" valueType="string"/>
|
||||
<option id="cdt.managedbuild.option.gnu.cross.path.1045961818" superClass="cdt.managedbuild.option.gnu.cross.path" value="C:\dvt_programs\mingw\mingw32\bin" valueType="string"/>
|
||||
<option id="cdt.managedbuild.option.gnu.cross.prefix.118603444" name="Prefix" superClass="cdt.managedbuild.option.gnu.cross.prefix" value="" valueType="string"/>
|
||||
<option id="cdt.managedbuild.option.gnu.cross.path.1045961818" name="Path" superClass="cdt.managedbuild.option.gnu.cross.path" value="C:\mingw64\bin" valueType="string"/>
|
||||
<targetPlatform archList="all" binaryParser="org.eclipse.cdt.core.ELF" id="cdt.managedbuild.targetPlatform.gnu.cross.952308367" isAbstract="false" osList="all" superClass="cdt.managedbuild.targetPlatform.gnu.cross"/>
|
||||
<builder buildPath="${workspace_loc:/buck_simulator}/Debug" id="cdt.managedbuild.builder.gnu.cross.1134084760" managedBuildOn="true" name="Gnu Make Builder.Debug" superClass="cdt.managedbuild.builder.gnu.cross"/>
|
||||
<builder buildPath="${workspace_loc:/buck_simulator}/Debug" id="cdt.managedbuild.builder.gnu.cross.1134084760" keepEnvironmentInBuildfile="false" managedBuildOn="true" name="Gnu Make Builder" superClass="cdt.managedbuild.builder.gnu.cross"/>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cross.c.compiler.1966827029" name="Cross GCC Compiler" superClass="cdt.managedbuild.tool.gnu.cross.c.compiler">
|
||||
<option defaultValue="gnu.c.optimization.level.none" id="gnu.c.compiler.option.optimization.level.978230008" superClass="gnu.c.compiler.option.optimization.level" useByScannerDiscovery="false" valueType="enumerated"/>
|
||||
<option defaultValue="gnu.c.debugging.level.max" id="gnu.c.compiler.option.debugging.level.5662297" superClass="gnu.c.compiler.option.debugging.level" useByScannerDiscovery="false" valueType="enumerated"/>
|
||||
<option defaultValue="gnu.c.optimization.level.none" id="gnu.c.compiler.option.optimization.level.978230008" name="Optimization Level" superClass="gnu.c.compiler.option.optimization.level" useByScannerDiscovery="false" valueType="enumerated"/>
|
||||
<option defaultValue="gnu.c.debugging.level.max" id="gnu.c.compiler.option.debugging.level.5662297" name="Debug Level" superClass="gnu.c.compiler.option.debugging.level" useByScannerDiscovery="false" valueType="enumerated"/>
|
||||
<option id="gnu.c.compiler.option.dialect.std.1295232558" name="Language standard" superClass="gnu.c.compiler.option.dialect.std" useByScannerDiscovery="true" value="gnu.c.compiler.dialect.c11" valueType="enumerated"/>
|
||||
<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.59240368" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
|
||||
</tool>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cross.cpp.compiler.526121749" name="Cross G++ Compiler" superClass="cdt.managedbuild.tool.gnu.cross.cpp.compiler">
|
||||
<option id="gnu.cpp.compiler.option.optimization.level.934384086" superClass="gnu.cpp.compiler.option.optimization.level" useByScannerDiscovery="false" value="gnu.cpp.compiler.optimization.level.none" valueType="enumerated"/>
|
||||
<option defaultValue="gnu.cpp.compiler.debugging.level.max" id="gnu.cpp.compiler.option.debugging.level.2140329590" superClass="gnu.cpp.compiler.option.debugging.level" useByScannerDiscovery="false" valueType="enumerated"/>
|
||||
<option id="gnu.cpp.compiler.option.optimization.level.934384086" name="Optimization Level" superClass="gnu.cpp.compiler.option.optimization.level" useByScannerDiscovery="false" value="gnu.cpp.compiler.optimization.level.none" valueType="enumerated"/>
|
||||
<option defaultValue="gnu.cpp.compiler.debugging.level.max" id="gnu.cpp.compiler.option.debugging.level.2140329590" name="Debug Level" superClass="gnu.cpp.compiler.option.debugging.level" useByScannerDiscovery="false" valueType="enumerated"/>
|
||||
</tool>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cross.c.linker.2131910008" name="Cross GCC Linker" superClass="cdt.managedbuild.tool.gnu.cross.c.linker">
|
||||
<inputType id="cdt.managedbuild.tool.gnu.c.linker.input.657749791" superClass="cdt.managedbuild.tool.gnu.c.linker.input">
|
||||
@@ -60,21 +61,21 @@
|
||||
</extensions>
|
||||
</storageModule>
|
||||
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
|
||||
<configuration artifactName="${ProjName}" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.exe" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.exe,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.release" cleanCommand="rm -rf" description="" id="cdt.managedbuild.config.gnu.cross.exe.release.1058631589" name="Release" parent="cdt.managedbuild.config.gnu.cross.exe.release">
|
||||
<configuration artifactName="${ProjName}" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.exe" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.exe,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.release" cleanCommand="rm -rf" description="" id="cdt.managedbuild.config.gnu.cross.exe.release.1058631589" name="Release" optionalBuildProperties="" parent="cdt.managedbuild.config.gnu.cross.exe.release">
|
||||
<folderInfo id="cdt.managedbuild.config.gnu.cross.exe.release.1058631589." name="/" resourcePath="">
|
||||
<toolChain id="cdt.managedbuild.toolchain.gnu.cross.exe.release.1067917023" name="Cross GCC" superClass="cdt.managedbuild.toolchain.gnu.cross.exe.release">
|
||||
<option id="cdt.managedbuild.option.gnu.cross.prefix.373869523" superClass="cdt.managedbuild.option.gnu.cross.prefix" value="gcc" valueType="string"/>
|
||||
<option id="cdt.managedbuild.option.gnu.cross.path.75719477" superClass="cdt.managedbuild.option.gnu.cross.path" value="C:\dvt_programs\mingw\mingw32\bin" valueType="string"/>
|
||||
<option id="cdt.managedbuild.option.gnu.cross.prefix.373869523" name="Prefix" superClass="cdt.managedbuild.option.gnu.cross.prefix" value="gcc" valueType="string"/>
|
||||
<option id="cdt.managedbuild.option.gnu.cross.path.75719477" name="Path" superClass="cdt.managedbuild.option.gnu.cross.path" value="C:\dvt_programs\mingw\mingw32\bin" valueType="string"/>
|
||||
<targetPlatform archList="all" binaryParser="org.eclipse.cdt.core.ELF" id="cdt.managedbuild.targetPlatform.gnu.cross.2117362498" isAbstract="false" osList="all" superClass="cdt.managedbuild.targetPlatform.gnu.cross"/>
|
||||
<builder buildPath="${workspace_loc:/buck_simulator}/Release" id="cdt.managedbuild.builder.gnu.cross.2017838130" managedBuildOn="true" name="Gnu Make Builder.Release" superClass="cdt.managedbuild.builder.gnu.cross"/>
|
||||
<builder buildPath="${workspace_loc:/buck_simulator}/Release" id="cdt.managedbuild.builder.gnu.cross.2017838130" keepEnvironmentInBuildfile="false" managedBuildOn="true" name="Gnu Make Builder" superClass="cdt.managedbuild.builder.gnu.cross"/>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cross.c.compiler.1691130753" name="Cross GCC Compiler" superClass="cdt.managedbuild.tool.gnu.cross.c.compiler">
|
||||
<option defaultValue="gnu.c.optimization.level.most" id="gnu.c.compiler.option.optimization.level.2098155161" superClass="gnu.c.compiler.option.optimization.level" useByScannerDiscovery="false" valueType="enumerated"/>
|
||||
<option defaultValue="gnu.c.debugging.level.none" id="gnu.c.compiler.option.debugging.level.1485984450" superClass="gnu.c.compiler.option.debugging.level" useByScannerDiscovery="false" valueType="enumerated"/>
|
||||
<option defaultValue="gnu.c.optimization.level.most" id="gnu.c.compiler.option.optimization.level.2098155161" name="Optimization Level" superClass="gnu.c.compiler.option.optimization.level" useByScannerDiscovery="false" valueType="enumerated"/>
|
||||
<option defaultValue="gnu.c.debugging.level.none" id="gnu.c.compiler.option.debugging.level.1485984450" name="Debug Level" superClass="gnu.c.compiler.option.debugging.level" useByScannerDiscovery="false" valueType="enumerated"/>
|
||||
<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.1944050434" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
|
||||
</tool>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cross.cpp.compiler.1602081645" name="Cross G++ Compiler" superClass="cdt.managedbuild.tool.gnu.cross.cpp.compiler">
|
||||
<option id="gnu.cpp.compiler.option.optimization.level.1419627403" superClass="gnu.cpp.compiler.option.optimization.level" useByScannerDiscovery="false" value="gnu.cpp.compiler.optimization.level.most" valueType="enumerated"/>
|
||||
<option defaultValue="gnu.cpp.compiler.debugging.level.none" id="gnu.cpp.compiler.option.debugging.level.1064281557" superClass="gnu.cpp.compiler.option.debugging.level" useByScannerDiscovery="false" valueType="enumerated"/>
|
||||
<option id="gnu.cpp.compiler.option.optimization.level.1419627403" name="Optimization Level" superClass="gnu.cpp.compiler.option.optimization.level" useByScannerDiscovery="false" value="gnu.cpp.compiler.optimization.level.most" valueType="enumerated"/>
|
||||
<option defaultValue="gnu.cpp.compiler.debugging.level.none" id="gnu.cpp.compiler.option.debugging.level.1064281557" name="Debug Level" superClass="gnu.cpp.compiler.option.debugging.level" useByScannerDiscovery="false" valueType="enumerated"/>
|
||||
</tool>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cross.c.linker.2093010337" name="Cross GCC Linker" superClass="cdt.managedbuild.tool.gnu.cross.c.linker">
|
||||
<inputType id="cdt.managedbuild.tool.gnu.c.linker.input.1785054278" superClass="cdt.managedbuild.tool.gnu.c.linker.input">
|
||||
@@ -107,4 +108,6 @@
|
||||
</scannerConfigBuildInfo>
|
||||
</storageModule>
|
||||
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
|
||||
<storageModule moduleId="refreshScope"/>
|
||||
<storageModule moduleId="org.eclipse.cdt.make.core.buildtargets"/>
|
||||
</cproject>
|
||||
4
.gitmodules
vendored
Normal file
4
.gitmodules
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
[submodule "cm_pid_regulator"]
|
||||
path = cm_pid_regulator
|
||||
url = https://OZ1CM@bitbucket.org/oz1cm/cm_pid_regulator.git
|
||||
branch = master
|
||||
@@ -5,7 +5,7 @@
|
||||
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
|
||||
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
|
||||
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
|
||||
<provider class="org.eclipse.cdt.internal.build.crossgcc.CrossGCCBuiltinSpecsDetector" console="false" env-hash="311939512288" id="org.eclipse.cdt.build.crossgcc.CrossGCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT Cross GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true">
|
||||
<provider class="org.eclipse.cdt.internal.build.crossgcc.CrossGCCBuiltinSpecsDetector" console="false" env-hash="1324177269032659242" id="org.eclipse.cdt.build.crossgcc.CrossGCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT Cross GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}" -std=c++1z" prefer-non-shared="true">
|
||||
<language-scope id="org.eclipse.cdt.core.gcc"/>
|
||||
<language-scope id="org.eclipse.cdt.core.g++"/>
|
||||
</provider>
|
||||
@@ -16,7 +16,7 @@
|
||||
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
|
||||
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
|
||||
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
|
||||
<provider class="org.eclipse.cdt.internal.build.crossgcc.CrossGCCBuiltinSpecsDetector" console="false" env-hash="311939512288" id="org.eclipse.cdt.build.crossgcc.CrossGCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT Cross GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true">
|
||||
<provider class="org.eclipse.cdt.internal.build.crossgcc.CrossGCCBuiltinSpecsDetector" console="false" env-hash="-968694645784" id="org.eclipse.cdt.build.crossgcc.CrossGCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT Cross GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true">
|
||||
<language-scope id="org.eclipse.cdt.core.gcc"/>
|
||||
<language-scope id="org.eclipse.cdt.core.g++"/>
|
||||
</provider>
|
||||
|
||||
6
.settings/org.eclipse.cdt.core.prefs
Normal file
6
.settings/org.eclipse.cdt.core.prefs
Normal file
@@ -0,0 +1,6 @@
|
||||
eclipse.preferences.version=1
|
||||
environment/project/cdt.managedbuild.config.gnu.cross.exe.debug.1636823514/PATH/delimiter=;
|
||||
environment/project/cdt.managedbuild.config.gnu.cross.exe.debug.1636823514/PATH/operation=replace
|
||||
environment/project/cdt.managedbuild.config.gnu.cross.exe.debug.1636823514/PATH/value=C\:\\mingw64\\bin;C\:\\Users\\chris\\.espressif\\tools\\xtensa-esp-elf-gdb\\14.2_20240403\\xtensa-esp-elf-gdb\\bin;C\:\\Users\\chris\\.espressif\\tools\\riscv32-esp-elf-gdb\\14.2_20240403\\riscv32-esp-elf-gdb\\bin;C\:\\Users\\chris\\.espressif\\tools\\xtensa-esp-elf\\esp-14.2.0_20241119\\xtensa-esp-elf\\bin;C\:\\Users\\chris\\.espressif\\tools\\esp-clang\\esp-18.1.2_20240912\\esp-clang\\bin;C\:\\Users\\chris\\.espressif\\tools\\riscv32-esp-elf\\esp-14.2.0_20241119\\riscv32-esp-elf\\bin;C\:\\Users\\chris\\.espressif\\tools\\esp32ulp-elf\\2.38_20240113\\esp32ulp-elf\\bin;C\:\\Users\\chris\\.espressif\\tools\\cmake\\3.30.2\\bin;C\:\\Users\\chris\\.espressif\\tools\\openocd-esp32\\v0.12.0-esp32-20241016\\openocd-esp32\\bin;C\:\\Users\\chris\\.espressif\\tools\\ninja\\1.12.1\\;C\:\\Users\\chris\\.espressif\\tools\\idf-exe\\1.0.3\\;C\:\\Users\\chris\\.espressif\\tools\\ccache\\4.10.2\\ccache-4.10.2-windows-x86_64;C\:\\Users\\chris\\.espressif\\tools\\dfu-util\\0.11\\dfu-util-0.11-win64;C\:\\Users\\chris\\.espressif\\tools\\qemu-xtensa\\esp_develop_9.0.0_20240606\\qemu\\bin;C\:\\Users\\chris\\.espressif\\tools\\qemu-riscv32\\esp_develop_9.0.0_20240606\\qemu\\bin;C\:\\Users\\chris\\.espressif\\python_env\\idf5.4_py3.8_env\\Scripts;E\:\\cm_programs\\espressif_idf\\esp-idf-v5.4\\tools;E\:/cm_programs/eclipse//plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_21.0.4.v20240802-1551/jre/bin/server;E\:/cm_programs/eclipse//plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_21.0.4.v20240802-1551/jre/bin;C\:\\Windows\\system32;C\:\\Windows;C\:\\Windows\\System32\\Wbem;C\:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C\:\\Windows\\System32\\OpenSSH\\;C\:\\Program Files\\dotnet\\;C\:\\Program Files (x86)\\IVI Foundation\\VISA\\WinNT\\Bin\\;C\:\\Program Files\\IVI Foundation\\VISA\\Win64\\Bin\\;C\:\\Program Files (x86)\\IVI Foundation\\VISA\\WinNT\\Bin;E\:\\cm_programs\\git_extensions\\;C\:\\Program Files\\Microchip\\xc8\\v2.36\\bin;E\:\\cm_programs\\Git\\cmd;C\:\\Program Files\\TortoiseGit\\bin;C\:\\Program Files (x86)\\Windows Kits\\10\\Windows Performance Toolkit\\;C\:\\Users\\chris\\AppData\\Local\\Microsoft\\WindowsApps;C\:\\Users\\chris\\.dotnet\\tools;E\:\\cm_programs\\eclipse
|
||||
environment/project/cdt.managedbuild.config.gnu.cross.exe.debug.1636823514/append=true
|
||||
environment/project/cdt.managedbuild.config.gnu.cross.exe.debug.1636823514/appendContributed=true
|
||||
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;
|
||||
}
|
||||
*/
|
||||
|
||||
35
buck_emulator.h
Normal file
35
buck_emulator.h
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* buck_emulator.h
|
||||
*
|
||||
* Created on: 13. maj 2025
|
||||
* Author: Christian L. V. Madsen (OZ1CM)
|
||||
*/
|
||||
|
||||
#ifndef BUCK_EMULATOR_H_
|
||||
#define BUCK_EMULATOR_H_
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef float (*regulate_evt_t)(float Vout);
|
||||
typedef int (*getResult_evt_t)(float duty, float Vout, float time);
|
||||
|
||||
typedef struct {
|
||||
|
||||
regulate_evt_t regulate_evt;
|
||||
getResult_evt_t getResult_evt;
|
||||
float simTime;
|
||||
|
||||
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)
|
||||
} BuckEmulator_t;
|
||||
|
||||
int buck_emulator_Run(BuckEmulator_t *inst);
|
||||
int buck_emulator_RegGetResultEvt(BuckEmulator_t *inst, getResult_evt_t getRes_evt);
|
||||
int buck_emulator_RegRegulationEvt(BuckEmulator_t *inst, regulate_evt_t reg_evt);
|
||||
int buck_emulator_init(BuckEmulator_t *inst, float Vin, float L, float C, float Rload, float dt, float sim_time);
|
||||
#endif /* BUCK_EMULATOR_H_ */
|
||||
38
buck_specs.h
Normal file
38
buck_specs.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* buck_specs.h
|
||||
*
|
||||
* Created on: 13. maj 2025
|
||||
* Author: Christian L. V. Madsen (OZ1CM)
|
||||
*/
|
||||
|
||||
#ifndef BUCK_SPECS_H_
|
||||
#define BUCK_SPECS_H_
|
||||
|
||||
// Voltages
|
||||
#define VIN 20.0 // Volt
|
||||
#define VOUT_TARGET 4.2 // Volt
|
||||
|
||||
|
||||
// Component Values:
|
||||
#define L_INDUCTOR 100e-6 // Henry
|
||||
#define C_CAPACITOR 100e-6 // Farad
|
||||
|
||||
|
||||
// Sample time
|
||||
#define SAMPLE_TIME 25e-6
|
||||
|
||||
|
||||
// Load
|
||||
#define R_LOAD 100 // Ohm
|
||||
|
||||
// Simulation parameters
|
||||
#define SIMULATION_TIME 1.0 // Seconds
|
||||
|
||||
|
||||
// Plot specific
|
||||
// OutputFile:
|
||||
#define BUCK_OUTPUT_FILE "buck_regulation_emul.csv"
|
||||
#define RUN_CMD "cmd.exe /c python ../plotscript/python_csv_plotter.py %s"
|
||||
|
||||
|
||||
#endif /* BUCK_SPECS_H_ */
|
||||
1
cm_pid_regulator
Submodule
1
cm_pid_regulator
Submodule
Submodule cm_pid_regulator added at c3b9479903
44
file_print.c
Normal file
44
file_print.c
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* file_print.c
|
||||
*
|
||||
* Created on: 13. maj 2025
|
||||
* Author: Christian L. V. Madsen (OZ1CM)
|
||||
*/
|
||||
#include "file_print.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
FILE *fpt;
|
||||
|
||||
void cm_file_open(char *fileName, float vtarget){
|
||||
|
||||
fpt = fopen(fileName,"w+");
|
||||
fprintf(fpt,"time_s, Vout, Power, Vtarget = %.2f \n",vtarget);
|
||||
|
||||
}
|
||||
|
||||
void cm_file_close(){
|
||||
|
||||
fclose(fpt);
|
||||
|
||||
}
|
||||
|
||||
void cm_file_print(float duty,float Rload, float Vout, float time){
|
||||
|
||||
fprintf(fpt,"%.3f, %.2f, %.2f, %.2f\n", time, Vout, Vout/Rload, duty);
|
||||
|
||||
}
|
||||
|
||||
void showProcentInCmd(float time, float simulate_target_time){
|
||||
|
||||
float time_left = time / simulate_target_time;
|
||||
|
||||
time_left *= 100;
|
||||
|
||||
printf("\r Progress: %.1f%%", time_left);
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
18
file_print.h
Normal file
18
file_print.h
Normal file
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* file_print.h
|
||||
*
|
||||
* Created on: 13. maj 2025
|
||||
* Author: Christian L. V. Madsen (OZ1CM)
|
||||
*/
|
||||
|
||||
#ifndef FILE_PRINT_H_
|
||||
#define FILE_PRINT_H_
|
||||
|
||||
|
||||
|
||||
void cm_file_open(char *fileName, float vtarget);
|
||||
void cm_file_close();
|
||||
void cm_file_print(float duty,float Rload, float Vout, float time);
|
||||
void showProcentInCmd(float time, float simulate_target_time);
|
||||
|
||||
#endif /* FILE_PRINT_H_ */
|
||||
93
main.c
93
main.c
@@ -5,11 +5,104 @@
|
||||
* Author: Christian Lind Vie Madsen
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include "buck_specs.h"
|
||||
#include "buck_emulator.h"
|
||||
#include "cm_pid_regulator/cm_pid_regulator.h"
|
||||
#include "file_print.h"
|
||||
#include <Windows.h>
|
||||
|
||||
BuckEmulator_t buck_converter;
|
||||
cm_pid_regulator_float_t Voltreg;
|
||||
|
||||
float kp = 0.00075;
|
||||
float ki = 0.00001;
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
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)pid_regulator);
|
||||
buck_emulator_RegGetResultEvt(&buck_converter,(getResult_evt_t)printResult);
|
||||
|
||||
cm_PIDRegulatorf_Init(&Voltreg, kp, ki, kd, 0.0, 1.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);
|
||||
}
|
||||
* */
|
||||
|
||||
94
plotscript/python_csv_plotter.py
Normal file
94
plotscript/python_csv_plotter.py
Normal file
@@ -0,0 +1,94 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Spyder Editor
|
||||
|
||||
This is a temporary script file.
|
||||
"""
|
||||
import sys
|
||||
import matplotlib.pyplot as plt
|
||||
import csv
|
||||
import numpy as np
|
||||
|
||||
|
||||
|
||||
# Initialize arrays
|
||||
time_array = []
|
||||
temperature_array = []
|
||||
power_array = []
|
||||
fan_speed_array = []
|
||||
|
||||
temperature_end_offset = 10 # Optional, adjust as needed
|
||||
|
||||
with open(sys.argv[1], newline='') as csvfile:
|
||||
datareader = csv.reader(csvfile, delimiter=',', quotechar='|')
|
||||
|
||||
for count, row in enumerate(datareader):
|
||||
if count == 0:
|
||||
# Labels row
|
||||
time_name = row[0]
|
||||
temp_name = row[1]
|
||||
power_name = row[2]
|
||||
plot_title = row[3]
|
||||
#fan_speed_name = row[4] # Assuming fan speed is at index 5
|
||||
else:
|
||||
time_array.append(float(row[0]))
|
||||
temperature_array.append(float(row[1]))
|
||||
power_array.append(float(row[2]))
|
||||
fan_speed_array.append(float(row[3]))
|
||||
|
||||
# Track min/max for scaling
|
||||
if count == 1:
|
||||
min_temp = float(row[1])
|
||||
max_temp = float(row[1])
|
||||
else:
|
||||
temp_val = float(row[1])
|
||||
if temp_val > max_temp:
|
||||
max_temp = temp_val
|
||||
if temp_val < min_temp:
|
||||
min_temp = temp_val
|
||||
|
||||
# Begin plotting
|
||||
fig1, ax1 = plt.subplots()
|
||||
fig1.canvas.manager.set_window_title('DVT-Light Python Plotter V1.1')
|
||||
fig1.suptitle(plot_title)
|
||||
|
||||
# Plot Vout
|
||||
color = 'tab:blue'
|
||||
ax1.set_xlabel('time (s)')
|
||||
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.grid(True)
|
||||
|
||||
# Plot Power (2nd y-axis)
|
||||
ax2 = ax1.twinx()
|
||||
color = 'tab:red'
|
||||
ax2.set_ylabel('Current', color=color)
|
||||
ax2.plot(time_array, power_array, color=color, label='Current')
|
||||
ax2.tick_params(axis='y', labelcolor=color)
|
||||
|
||||
# Plot Duty (3rd y-axis)
|
||||
ax3 = ax1.twinx()
|
||||
color = 'tab:purple'
|
||||
ax3.spines["right"].set_position(("axes", 1.15)) # Offset 3rd y-axis
|
||||
ax3.set_frame_on(True)
|
||||
ax3.patch.set_visible(False)
|
||||
for sp in ax3.spines.values():
|
||||
sp.set_visible(False)
|
||||
ax3.spines["right"].set_visible(True)
|
||||
|
||||
ax3.set_ylabel('Duty', color=color)
|
||||
ax3.plot(time_array, fan_speed_array, color=color, linestyle=':', label='Duty')
|
||||
ax3.tick_params(axis='y', labelcolor=color)
|
||||
|
||||
# Combine legends
|
||||
lines, labels = ax1.get_legend_handles_labels()
|
||||
lines2, labels2 = ax2.get_legend_handles_labels()
|
||||
lines3, labels3 = ax3.get_legend_handles_labels()
|
||||
ax1.legend(lines + lines2 + lines3, labels + labels2 + labels3, loc='upper left')
|
||||
|
||||
fig1.tight_layout()
|
||||
plt.show()
|
||||
|
||||
Reference in New Issue
Block a user