creating a Jenkins job for automated model verification and code generation of Simulink models
In one of the recent projects I have worked on, we had to create Simulink models, verify them for correctness, and then generate C++ code, which would be executed on a different machine.
In case you are working alone, or you are the only one working on the models, this is not a big problem; you engage your creativity to create the models, you verify them, and then you generate code. Simple as that. However, if you are working together with five more people on the same models, it is not so efficient to gather everyone around a screen and “work together”. Instead, it would be better if everyone worked on a specific part of the model and each member’s effort is committed to a git repository. (But of course, this is no big news)
Suppose then that you have your repository ready to accept your models and everybody is working hard to create the desired models. But then what? How can you automatically verify that each commit did not break the correctness of the models? Moreover, (just to make it a bit more interesting) how can you generate C++ code in an automated way after the verification of the models? Well, that’s what we are going to see here.
I am going to present the solution we gave to this problem.
Disclaimer: Needless to say that there might be a better solution out there; this was just our solution. If you, however, have a better solution to propose, please let me know in the comments section after this post.
Our configuration in this project consisted of the following (among others):
Remarks:
Simulink Coder
(more info).The first step towards the ultimate automation of this solution, is to create a Matlab script that will open our models and invoke Simulink’s build procedure, which verifies the model, and if everything is fine code is generated.
A script like this, looks like:
addpath(genpath('/home/jenkins/workspace/REPO_NAME/MODELS_FOLDER'));
open_system('Model1');
set_param('Model1','LaunchReport','off');
try
rtwbuild('Model1');
catch
warning('Error while building Model1');
exit(1);
end
save_system('Model1');
close_system('Model1');
open_system('Model2');
set_param('Model2','LaunchReport','off');
try
rtwbuild('Model2');
catch
warning('Error while building Model2');
exit(1);
end
save_system('Model2');
close_system('Model2');
exit(0);
One thing that needs to be mentioned here: if you do not include the command set_param('Model1','LaunchReport','off');
, then an annoying pop-up window from Matlab will open every time your model is built. This was rather annoying for us and, therefore, we disabled it.
You can include this file in your repository, or just place it in the home
folder of the Jenkins node machine and name it autobuild.m
.
The last step here is to create a job on your Jenkins server.
Be careful to select the following:
Restrict where this project can be run
, where you should select your Jenkins node machine.Source Code Management
you should configure your repository/ies.Build Triggers
you should select what will be the trigger(s) for this job.Build
you need to specify the build actions. For this case, first we need to get the autobuild.m
and then invoke Matlab.The Build
section should look like:
cp -f /home/jenkins/autobuild.m /home/jenkins/workspace/REPO_NAME/MODELS_FOLDER/
cd /home/jenkins/workspace/REPO_NAME/MODELS_FOLDER/
matlab -nosplash -nodesktop -noFigureWindows -r autobuild
After you create your job, you should be able to see if your models are correct and get your generated code.
Optionally, you can create a Post-build Action
to transfer the generated code to another folder, or to archive it and give it a specific label.