I cannot generate C code from MATLAB Coder
Mostrar comentarios más antiguos
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
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
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.]
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
el 13 de Oct. de 2020
Editada: Ameer Hamza
el 13 de Oct. de 2020
SimTec
el 13 de Oct. de 2020
Ameer Hamza
el 13 de Oct. de 2020
@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
el 16 de Oct. de 2020
SimTec
el 21 de Oct. de 2020
Respuestas (0)
Categorías
Más información sobre Automated Fixed-Point Conversion in MATLAB en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!