Writing to file in compiled c code from simulink model, on Raspberry Pi

2 views (last 30 days)
Hello,
I am trying to write to file in a simulink model that I will later deploy and run as standalone on the Pi.
I have tried a few different versions with no success. When run in external mode in simulink it works with all the solutions, but when i run the built executable from the modelname/MW/ folder on the pie, no file is written.
function savePoints(points, time)
%#codegen
coder.cinclude('<stdio.h>');
format = ['%d ', 0];
formatTime = ['%f ', 0];
fname = coder.nullcopy(uint8(zeros(1, 32)));
coder.ceval('sprintf', coder.wref(fname), ['testPoints.dat', 0]);
fd = coder.opaque('FILE *');
fd = coder.ceval('fopen', fname, ['a', 0]);
coder.ceval('fprintf', fd, formatTime, time);
for i = 1:272
coder.ceval('fprintf', fd, format, int32(points(i)));
end
coder.ceval('fprintf', fd, [char(10) 0]);
coder.ceval('fclose', fd);
Above you can see one attempt, which is a rewamp of the motion sensor example code provided by Mathworks.
I have studied the c code, and it seems "right" as far as I can see, it should write a new line to the file each cycle in the model, and close the file each time. As such, even if it is terminated early/not at all, at some point there should be a file created?
Anyone have any experience with these type of issues? As I said, it works fine when run in external mode.
Have also tried with the "tofile" block, but I have a feeling I have seen somewhere that it does not compile. It behavs similar to my code anyway. Any help would be appriciated!
  1 Comment
Ryan Livingston
Ryan Livingston on 31 Dec 2015
MATLAB Coder supports generating code for the MATLAB fopen, fprintf, and fclose starting in R2013a. You could consider using those and skipping the coder.ceval code if using a sufficiently new version.

Sign in to comment.

Accepted Answer

Jonas Landberg
Jonas Landberg on 2 Apr 2015
Got help from the Mathworks support. If anyone have similar problems in the future:
It does work, but if you want to run the standalone version on the pi, build the code using normal and not external. So use realtime target, select the pi as a target, running mode normal, then ctrl-b in the toplevel of the model. Good luck!
  1 Comment
Adil Faiz
Adil Faiz on 28 Dec 2015
Hey, i need some help ,I m building a robot using vision for line following,is it possible that i can code in matlab for pi and made it as autonomous by image processing ,i mean do i have to give live commands to pi through wired or wireless connection to pi for processing,may be i am not clear as i am naive using raspberry pi for image processing .

Sign in to comment.

More Answers (0)

Categories

Find more on Raspberry Pi Hardware in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!