Hello all. Iam trying to covert m-file into a c-code so that it can be used in microprocessor for real-time implementation. Iam following two methods: 1-Using embedded matlab simulink block. 2-Using emlmex and emlc commands.
When i use the first method, an error occured "cannot generate c-file" Here is the matlab code which is on extended kalman filter:
duration=60;
dt=0.1;
% position measurement noise (feet)
MeasNoise = 10;
accelnoise = 0.2; % acceleration noise (feet/sec^2)
a = [1 dt 0; 0 1 0; 0 0 1]; % transition matrix
b = [dt^2/2 0; dt 0; 0 dt]; % input matrix
c = [1 0 0; 0 0 1]; % measurement matrix
x = [0; 0; 0]; % initial state vector
xhat = x; % initial state estimate
Sz = [MeasNoise^2 0; 0 MeasNoise^2]; % measurement error covariance
Sw = [10^-6 0 0; 0 4*10^-4 0; 0 0 0.05]; % process noise cov
P = Sw; % initial estimation covariance
% Initialize arrays for later plotting.
pos = []; % true position array
poshat = []; % estimated position array
posmeas = []; % measured position array
vel = []; % true velocity array
velhat = []; % estimated velocity array
angle = [];
anglehat = [];
anglemeas = [];
for t = 0 : dt: duration,
% Use a constant commanded acceleration of 1 foot/sec^2.
u = [1;4*dt];
% Simulate the linear system.
ProcessNoise = accelnoise * [(dt^2/2)*randn; dt*randn; dt*randn];
x = a * x + b * u + ProcessNoise;
% Simulate the noisy measurement
MeasNoise = [0.5*randn; 0.5*randn]
y = c * x + MeasNoise;
% Extrapolate the most recent state estimate to the present time.
xhat = a * xhat + b * u;
% Form the Innovation vector.
Inn = y - c * xhat;
% Compute the covariance of the Innovation.
s = c * P * c' + Sz;
% Form the Kalman Gain matrix.
K = a * P * c' * inv(s);
% Update the state estimate.
xhat = xhat + K * Inn;
% Compute the covariance of the estimation error.
P = a * P * a' - a * P * c' * inv(s) * c * P * a' + Sw;
% Save some parameters for plotting later.
pos = [pos; x(1)];
posmeas = [posmeas; y(1)];
poshat = [poshat; xhat(1)];
vel = [vel; x(2)];
velhat = [velhat; xhat(2)];
angle = [angle; x(3)];
anglemeas = [anglemeas; y(2)];
anglehat = [anglehat; xhat(3)];
end
% Plot the results
close all;
t = 0 : dt : duration;
figure;
plot(t,pos, t,posmeas, t,poshat);
grid;
xlabel('Time (sec)');
ylabel('Position (feet)');
title('Figure 1 - Vehicle Position (True, Measured, and Estimated)')
figure;
plot(t,angle, t,anglemeas, t,anglehat);
grid;
xlabel('Time (sec)');
ylabel('angle');
title('Figure 1 - Vehicle orientation (True, Measured, and Estimated)')
figure;
plot(t,pos-posmeas, t,pos-poshat);
grid;
xlabel('Time (sec)');
ylabel('Position Error (feet)');
title('Figure 2 - Position Measurement Error and Position Estimation Error');
figure;
plot(t,vel, t,velhat);
grid;
xlabel('Time (sec)');
ylabel('Velocity (feet/sec)');
title('Figure 3 - Velocity (True and Estimated)');
figure;
plot(t,vel-velhat);
grid;
xlabel('Time (sec)');
ylabel('Velocity Error (feet/sec)');
title('Figure 4 - Velocity Estimation Error');
This code is first converted into embedded matlab and then converted into c-file. Can anyone help me out. Thanks alot..!

3 件のコメント

Kaustubha Govind
Kaustubha Govind 2011 年 2 月 22 日
I'm guessing the error occurs because you use functions like PLOT, that are not supported for code generation.
Guillermo Soriano
Guillermo Soriano 2013 年 1 月 23 日
Hi: among function plot, there are others, but the most important is that your code must start with function, otherwise can not be converted to c
Willy
Walter Roberson
Walter Roberson 2013 年 1 月 23 日
That was true in the timeframe that the question was asked, but I have seen hints (and a very brief mention in the documentation) that some of the newer MATLAB versions permit scripts to be compiled.

サインインしてコメントする。

 採用された回答

Gautam Vallabha
Gautam Vallabha 2011 年 2 月 22 日

2 投票

If you just want to convert MATLAB code to C code, you don't need to create a Simulink block. You can just create a MATLAB function file and invoke emlc. Make sure that:
  1. Your MATLAB function has %#eml at the beginning
  2. Your MATLAB function only uses functions and features from the Embedded MATLAB subset. In particular, graphics functions like figure, xlabel, and plot are not supported; you can have them in your file, but then you should use eml.extrinsic to tell EMLC to ignore these functions.
  3. Pay careful attention to the datatypes of the functions inputs
Example:
%#eml
function z = testeml(x, y)
% datatypes and sizes of inputs
assert( isa(x, 'single') && isa(y, 'single') );
assert( all(size(x) == [1 10]) && all(size(y) == [1 10]) );
% functions that EMLC should ignore
eml.extrinsic('figure', 'plot');
z = x .* y;
figure;
plot(x,y);
You can compile this with:
config = emlcoder.RTWConfig;
config.GenCodeOnly = true;
config.GenerateReport = true;
emlc -s config testeml
You can also run it directly from the MATLAB command line:
>> testeml(single(1:10), single(2:2:20))

13 件のコメント

Nalla
Nalla 2011 年 2 月 25 日
very thanks..!
Nalla
Nalla 2011 年 2 月 25 日
can we compile using emlmex command also?
what's the difference between emlmex and emlcoder?
Thanks..!
Nalla
Nalla 2011 年 2 月 25 日
when i am running your code an error occurred:
Build Log:
1 Unable to locate LCC in C:\Program Files\MATLAB\R2008a\sys\lcc
2 Please make sure that you have LCC installed.
Kaustubha Govind
Kaustubha Govind 2011 年 2 月 25 日
emlmex generates MEX code, which emlc generates standalone C code.
To fix the error, try running "mex -setup" first.
Nalla
Nalla 2011 年 2 月 28 日
cannot include the file
Walter Roberson
Walter Roberson 2011 年 2 月 28 日
Cannot include _which_ file ?
Nalla
Nalla 2011 年 2 月 28 日
when i buid the c-code generated in microC software its saying that it cannot include the header files..!
Nalla
Nalla 2011 年 2 月 28 日
I want to convert this generated c-file into hex file so that i can dump it into microprocessor.
Kaustubha Govind
Kaustubha Govind 2011 年 2 月 28 日
Please specify the names of the header files that it cannot include. Are these generated files? If yes, have you added the include paths correctly?
Nalla
Nalla 2011 年 3 月 1 日
#include "rt_nonfinite.h"
#include "ekf.h"
#include "randn.h"
#include "norm.h"
Yes,these .h files are generated along with the C-file.
now, when i compile this c-code in microC software, it's showing an error:"cannot include these files".
My question is whether to include these files in the microC library functions?
Thanks..!
Nalla
Nalla 2011 年 3 月 1 日
error appearing:
0 304 C:/Users/DELL/Documents/gyrokall.c/ss.c:14: error: Can't open include file "norm.h",randn.h,ekf.h,rt_nonfinite.h
Kaustubha Govind
Kaustubha Govind 2011 年 3 月 1 日
It appears that these header files are not on the include path. I'm not familiar with the microC environment, but you should ensure that the location of these headers is added to the list of include directories for your project. If you are compiling from command-line, this typically means adding "-I /path/to/headers" to the compile command.
Nalla
Nalla 2011 年 3 月 3 日
I have compiled the .m file in matlab environment without any errors.
Now i am trying to get an hex file from the .c file(generated from matlab)so that i can use it in micro-controller.Are there any other ways of getting hex file?
Thanks..!

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2011 年 3 月 3 日

2 投票

Here is a function to convert any input file in to a hex format. As I had no idea what a "hex file" for whatever micro controller you are using looks like, I used a common hex dump format.
function write_as_hex(infile, outfile)
fin = fopen(infile, 'r');
if fin < 0
warning('failed to open input file, no output');
return
end
fout = fopen(outfile, 'w');
if fout < 0
warning('failed to open output file, no output');
fclose(fin);
return
end
indata = fread(fin);
fclose(fin);
trailbytes = mod(length(indata), 16);
fprintf(fout, [repmat('%2X ',1,15) '%2X\n']), ...
indata(1:end-trailbytes));
fprint(fout, [repmat(%2X ',1, trailbytes-1) '%2X\n'], ...
indata(end-trailbytes+1:end));
fclose(fout);
end

9 件のコメント

Nalla
Nalla 2011 年 3 月 3 日
Thanks alot..!
Iam using 16F877A PIC controller.Whether the hex file generated by the above function will work or not?
Nalla
Nalla 2011 年 3 月 3 日
I used the above function but i am getting errors. Can you tell me in detail with an example?
Walter Roberson
Walter Roberson 2011 年 3 月 3 日
編集済み: Walter Roberson 2017 年 1 月 19 日
I found a description of the .HEX formats that are supported. The most common one does not correspond to the format from the code segment above. http://ww1.microchip.com/downloads/en/AppNotes/91025a.pdf
It appears to me that you should be using the MPLAB tools such as the MPLAB C compiler, http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=2123&param=en024282 to generate the .HEX file. Free student editions of the compilers are apparently available. You can download the IDE from http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en019469&part=SW007002 but I am not sure at the moment how you would download the student compiler. Running the IDE might tell you, or there is a discussion group link given on that page.
Nalla
Nalla 2011 年 3 月 3 日
Thanks for the links Walter..!
I am using microC PRO for PIC micro-controllers for generating the hex file.When i build the project, it shows "cannot open include files".Where can i get these files:
0 304 c:/documents and settings/admin/desktop/ss/rtwtypes.h:21: error: Can't open include file "limits.h"
0 304 #include <limits.h>
0 304 c:/documents and settings/admin/desktop/ss/ekf.h:13: error: Can't open include file "math.h"
Walter Roberson
Walter Roberson 2011 年 3 月 3 日
limits.h and math.h should have been supplied as part of the C compiler. They will likely be in a directory named "includes" somewhere underneath the compiler directory. Once you have located the appropriate directory, you may have to add it in to the project definition as an include directory; add it _last_ on the list.
Nalla
Nalla 2011 年 3 月 4 日
thanks alot
Nalla
Nalla 2011 年 3 月 7 日
hi..can i use mplab to convert my .m or .c file into hex file..?
Walter Roberson
Walter Roberson 2011 年 3 月 7 日
You can use one of the Mathworks tools to convert your embedded Matlab to C. Then, as best I can tell from the documentation, you can use mplab to convert the C file into executable code in .HEX format suitable for downloading to the PIC processor.
Nalla
Nalla 2011 年 3 月 8 日
thank Q

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeSoftware Development Tools についてさらに検索

質問済み:

2011 年 2 月 22 日

編集済み:

2017 年 1 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by