Another, tidier, mixed mode simulation model. Dual phase IPT buck converter

Here’s a more complicated SMPS to show my latest improvements. It’s a dual phase Inter Phase Transformer (IPT) based buck converter. I contributed to a paper a couple of years ago (https://digital-library.theiet.org/doi/10.1049/icp.2022.1079) but only looked at the transformer so I thought that I would try designing a complete one.

Here’s the model.
2_phase_IPT.zip (1.5 MB)

There’s a switched resistor on the output that provides a load step. If you reduce the value below 1.5 ohms then you can test the current limit (see below).

The microcontroller model has been tidied up a lot compared to the last one. Where it takes more than one line to add something like an ADC or PWM output, I’ve made a sub circuit and shifted it to a separate file.

The c-code is all floating point so it’s easier to follow.

The PWM output is up down triangular mode (instead of up saw tooth only) with dead time generation and two compare points so there are four outputs to play with (anyone want a bidirectional dc motor controller or buck/boost converter instead - should be an easy modification).

There are now three files with models for ngspice.
* “models.lib” has the switches and transformer models. It’s a standard ngspice file.
* “microcontroller_internal_models.bus” has the models for the ADCs, delays and other internal functions that are called by “microcontroller.bus”. There should be no need to modify “microcontroller_internal_models.bus” unless you want to add more subcircuits.
* “microcontroller.bus” is the microcontroller model. It links the pwm and adcs and the inputs from kicad. This one can be modified to add or remove remove pwm channels and adcs or modify delays etc.

The two “.bus” files are non-standard ngspice files. The python script “generate_buses.py” interprets strings of the format “_BUS_NAME_XX” to add the required number of connections as ngspice doesn’t support busses and writing adc1_bus0, adc1_bus1… gets old fast. It outputs “.lib” files.

The controller has a number of files.
* “microcontroller_ngut.c” - no need to touch this one.
* “microcontroller.h” and “microcontroller.c” - these define the interface with ngspice (in_data_message and out_data_params) and set up the environment for the controller function. It reads parameters.p to get the controller parameters.
* “adc.h” has the gains and offsets for the ADCs
* “parameters.p” holds any user modifiable values that you might want to change between runs. Eg the controller gains and the set points. You don’t have to recompile it to make changes here.
* “buck_controller.h” and “buck_controller.c” - these should be exactly the same modules as your controller on the real microcontroller.
* “.compile_all.sh” is a bash script for Linux that will compile the controller and the verilog models (assuming that you have yosys and gcc installed).

As with the previous models (https://forum.kicad.info/t/easier-mixed-mode-simulation-and-real-world-comparison/ and Mixed mode simulation including C code and verilog), you need kicad 8+, ngspice 43+, gcc and yosys if you want to recompile the examples. Run “compile_all.sh” if you’re on a Linux machine. The verilog is already compiled so you don’t strictly need yosys if you don’t want to modify it.

If you modify the microcontroller.bus model then you will also need to run “generate_buses.py” using python to generate the .lib file.

Before you run the example provided, you still need to change the text on the schematic to set the directory that it’s run from. Kicad 8.07 still doesn’t appear to set the directory correctly.

I have still not run it on anything except Ubuntu 24.04. Some modifications were provided by @Briant for Windows but I haven’t tested them.

I also built one of these and it worked just like the simulation model again. I just copied and pasted the buck_converter.c and buck_converter.h and parameter files into the microcontroller code.

Any questions, feel free to ask.

Again, a big thanks to @uros, @holger and @Briant for their work making this possible.

4 Likes

Great use case, thanks for posting it. Works well on my Linux, just needed to recompile the bin due to the libc miss-match.

Regarding the “current directory” issue; for the moment I simply invoke the kicad from the project folder instead of clicking on the desktop icon; and then “cd” can be omitted in the sch to keep project clean from absolute paths.

1 Like

The issue with the working directory should be solved in 9.0 (unless the devs change their mind). Kicad will then always use the project directory as the working directory.

1 Like

I had another look at it and realised that the main directory was rather messy. I’ve moved the models into subdirectories that should be easier to understand what’s going on.
2_phase_IPT_v2.zip (690.6 KB)

It goes:

         2_Phase_IPT/   -- kicad files and parameters.p

         2_Phase_IPT/models/   -- compiled code model, ngspice .lib and .mod files, named pipes

         2_Phase_IPT/models/code_model  -- c code files as per the previous post and compile_code_model.sh which also creates the named pipes

         2_Phase_IPT/models/mixed_sim --files needed for verilog modes, don't touch!
         
         2_Phase_IPT/models/verilog_models -- verilog files and compile_verilog.sh to generate the .mod files

         2_Phase_IPT/models/bus_models -- the .bus files and generate_buses.py to generate the .lib files

To run it, you should only need to run compile_code_model.sh and change the working directory in the schematic file, everything else should be ok.

If you want to mess with the control parameters or set point then modify parameters.p in the base directory.

The only file that you should modify directly in models/ is models.lib which has the switch and transformer models. For the others, modify the file in the subdirectory and run the appropriate scripts.

3 Likes

I just tried this out with MSYS2 on windows and it seems to run out of the box! Maybe I am missing something, but the simulation runs without any errors. You need gcc installed in MSYS2 of course. But this is all I did:

  • Changed the working directory in the control section on the schematic (using double backslashes): cd C:\\Users\\lucas\\kicad_simulation\\2_phase_IPT_v2
  • ran the bash script in the model directory compile_code_model.sh: this will create controller.exe exexcutable in the model directory.

This is really cool stuff!

2 Likes

That’s great! Exactly what I was hoping for! Thank you @lucas and @uros for testing it.

I still need to figure out how to do losses like in PLECs but for now you can drop a MOSFET model and basic gate driver for each switch to get losses.

2 Likes

PS: standard Makefiles are still a very good option to use, as they recompile changes only, i.e.: