I cannot generate C code from MATLAB Coder
Show older comments
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 Comments
Ameer Hamza
on 12 Oct 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
on 12 Oct 2020
Edited: Steven Lord
on 12 Oct 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
on 12 Oct 2020
Edited: Ameer Hamza
on 12 Oct 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
on 13 Oct 2020
Edited: Ameer Hamza
on 13 Oct 2020
SimTec
on 13 Oct 2020
Ameer Hamza
on 13 Oct 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
on 16 Oct 2020
SimTec
on 21 Oct 2020
Answers (0)
Categories
Find more on Automated Fixed-Point Conversion in MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!