Converting a Simulink circuit to C-code

12 Ansichten (letzte 30 Tage)
Brian Sigler
Brian Sigler am 15 Jul. 2020
Beantwortet: Kanishk am 20 Nov. 2024 um 10:35
I have a Simulink file (battery charger with a buck converter) that I am trying to convert over to Visual Studio. I am able to convert it over without issue, but my question is more geared towards having this code return parameters that I want (input/output voltage, temps, ripple current, etc.), either through the terminal window or an excel file. My Matlab/Simulink knowledge is fairly basic but my Visual Studio and coding knowledge is even less.
So my question is, within Simulink, do I have to attach any special blocks or issue special commands to those relevant test points so the generated code knows to print or display them? My goal with this is to be able to run parametric models with a wide range of component values. Is there a simple way I could practice this with, say a voltage divider, to understand how Simulink converts the model over?

Antworten (1)

Kanishk
Kanishk am 20 Nov. 2024 um 10:35
Hi Brian,
I understand that you want to extract specific parameters from Simulink model like output voltage, temperatures, and ripple current and display them using generated code.
You can follow this official MathWorks Documentation to access outputs in generated code.
I am also attaching an example Simulink model and the steps to create the model. Below are the steps to Access Parameters in Generated Code:
1. Create Simulink Model:
  • Open Simulink and create a model (e.g., voltCodeGen).
  • Add some components in the model for Simulation.
  • Use Outport Blocks for Parameters:
  • For each parameter you want to access (e.g., input/output voltage), add an Outport block. Connect these Outport blocks to the relevant signals in your model.
2. Configure Outport Blocks:
  • Go to Apps > Simulink Coder > Code Mappings.
  • In the Code Mappings Editor, find the Outport blocks you added.
  • Set the storage class of each Outport block to ExportedGlobal.
  • Assign a unique identifier name to each Outport block. This identifier will be used to access the variable in the generated code.
3. Generate C Code:
  • In Simulink Coder Tab, Click on Build to generate C code for your model.
  • The generated code will be located in the code generation folder. (voltCodeGen_grt_rtw)
  • Access Parameters in Generated Code:
  • Open the main generated C file (voltCodeGen.c) in Visual Studio.
  • Locate the identifiers you set for the Outport blocks. These will be declared as global variables.
  • Use “printf statements in “voltCodeGen_step to display the values of these parameters in the terminal window.
printf("Voltage: %f\n", Voltage);
printf("Current: %f\n", Current);
Hope this helps!

Kategorien

Mehr zu Code Generation finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by