I cannot generate C code from MATLAB Coder

I would like to generate C code for a PI controller, but whenever I reach this step I cannot continue any longer. I watched a video from MATLAB, but that one has testbench which I do not need here.
%#codegen
function [PIout,yint] = PI_cont(err,ki, kp,yr)
yint=ki*err+yr; % yint=ki*err/s
PIout=yint+kp*err;
Here, you find how I stored the integral yint into yr

9 comentarios

Ameer Hamza
Ameer Hamza el 12 de Oct. de 2020
If you want to generate C-code, you need to run MATLAB code or Simulink Coder. This window shows a fixed-point converter.
Andy Bartlett
Andy Bartlett el 12 de Oct. de 2020
Editada: Steven Lord el 12 de Oct. de 2020
Code generation needs to know the data types for the arguments.
Option 1
If you already know the desired data types of the inputs you can pass the to codegen via the -args option.
Option 2
If you don't know the data types yet and you want the efficiency benefits of fixed-point math, then the Fixed-Point Converter can help determine desirable type choices. Conversion to fixed-point needs information to make data type scaling decisions. The goal is to prevent overflows and get sufficient precision to achieve the desired behavior. To avoid overflows, there must be information available to determine how big each signal value can get. Fixed-Point Converter always requires some input values to collect ranges against. In additon design ranges can be used to help prevent overflow for the worst-case input combinations.
For your PI controller, to create the test bench needed by Fixed-Point Converter, you could set up a closed loop simulation of this PI controller regulating your plant model.
Tip: For best results with automated Fixed-Point Conversion of MATLAB code, break up your statements so that each line contains only one operation.
%#codegen
function [PIout,yint] = PI_cont(err,ki, kp,yr)
integralProduct = ki * err
integralAccumulation = integralProduct + yr; % can be higher precision
proportionalProduct = kp * err;
combinePI = integralAccumulation + proportionalProduct; % can be higher precision
yint = integralAccumulation; % downcast to integral state to be stored in memory
PIout = combinePI; % downcast to precision to feed to actuator
This approach gives you more control over the tradeoffs of range, precision, and efficiency of the generated code.
[SL: reformatted the text after Option 2 so it is no longer monospaced.]
SimTec
SimTec el 12 de Oct. de 2020
Editada: SimTec el 12 de Oct. de 2020
Yes exactly, I just dicovered that now. But I run Matlab code in matlab and I get fixed point converter!
each time I select MATLAB coder I get Fixed-point converter, could it be a licence error?
Ameer Hamza
Ameer Hamza el 12 de Oct. de 2020
Editada: Ameer Hamza el 12 de Oct. de 2020
How are you running the app? Can you run
coder
in the command window. What happen when you run
ver matlabcoder
Does it show the license or gives a warning?
SimTec
SimTec el 13 de Oct. de 2020
Editada: Ameer Hamza el 13 de Oct. de 2020
this is what it shows:
MATLAB Version: 9.7.0.1190202 (R2019b)
MATLAB License Number: xxxxxx
Operating System: Microsoft Windows 10 Enterprise Version 10.0 (Build 17763)
Java Version: Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
-----------------------------------------------------------------------------------------------------
MATLAB Coder Version 4.3 (R2019b)
SimTec
SimTec el 13 de Oct. de 2020
@Andey Bartlett, thank you for your answer, in my case I want just to generate C code.
in Option1, I already know my data types. is the data types shown in the first figure not the right one?
@TarikTech, I have edited your answer to remove your license number. This shows that you have the correct toolbox installed. What happens when you run
coder
SimTec
SimTec el 16 de Oct. de 2020
Thank you, wait a moment
SimTec
SimTec el 21 de Oct. de 2020
NOw it is working

Iniciar sesión para comentar.

Respuestas (0)

Etiquetas

Preguntada:

el 12 de Oct. de 2020

Comentada:

el 21 de Oct. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by