converting m-file to c-code
古いコメントを表示
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
2011 年 2 月 22 日
I'm guessing the error occurs because you use functions like PLOT, that are not supported for code generation.
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
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.
採用された回答
その他の回答 (1 件)
Walter Roberson
2011 年 3 月 3 日
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
2011 年 3 月 3 日
Nalla
2011 年 3 月 3 日
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¶m=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
2011 年 3 月 3 日
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
2011 年 3 月 4 日
Nalla
2011 年 3 月 7 日
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
2011 年 3 月 8 日
カテゴリ
ヘルプ センター および File Exchange で Software Development Tools についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!