Skip to content

Commit

Permalink
Improve CasADi code-generation (#11)
Browse files Browse the repository at this point in the history
Co-authored-by: Silvio Traversaro <silvio@traversaro.it>
  • Loading branch information
FabioBergonti and traversaro authored Jun 18, 2022
1 parent 30b54a7 commit 179a87a
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 18 deletions.
21 changes: 18 additions & 3 deletions +mystica/+state/@StateKinMBody/StateKinMBody.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,31 @@ function clearProperties(obj)
end

function generateMEX(obj)
nameMEX = 'mystica_stateKin';
opts = struct('main', true,'mex', true);
C = casadi.CodeGenerator('mystica_stateKin.c',opts);
initial_path = pwd;
cd(fullfile(mystica.utils.getMysticaFullPath,'deps','csdMEX'));
C = casadi.CodeGenerator([nameMEX,'.c'],opts);
C.add(obj.csdFn.Jc);
C.add(obj.csdFn.intJcV);
C.add(obj.csdFn.rC_from_jointsAngVelPJ_2_jointsAngVel0);
C.add(obj.csdFn.rC_from_mBodyTwist0_2_jointsAngVelPJ);
C.add(obj.csdFn.get_mBodyVelQuat0_from_mBodyTwist0)
C.generate();
mex mystica_stateKin.c -largeArrayDims
delete('mystica_stateKin.c')
fileID = fopen([nameMEX,'.c'] ,'r'); new_code = fscanf(fileID,'%s'); fclose(fileID);
if exist([nameMEX,'_old.c'],'file')
fileID = fopen([nameMEX,'_old.c'],'r'); old_code = fscanf(fileID,'%s'); fclose(fileID);
else
old_code = '';
end
if ~strcmp(new_code,old_code) || isempty(dir([nameMEX,'.mex*']))
fprintf('generating %s.mex\n',nameMEX)
mex([nameMEX,'.c'],'-largeArrayDims')
else
fprintf('%s.mex already exists\n',nameMEX)
end
movefile([nameMEX,'.c'],[nameMEX,'_old.c'])
cd(initial_path)
end

function tform_b = get_link_tform_b(obj,input)
Expand Down
12 changes: 0 additions & 12 deletions +mystica/+utils/deleteGeneratedMEX.m

This file was deleted.

6 changes: 6 additions & 0 deletions +mystica/+utils/getMysticaFullPath.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function p = getMysticaFullPath()
initial_path = pwd;
cd(fullfile(fileparts(mfilename('fullpath')),'..','..'));
p = pwd;
cd(initial_path);
end
1 change: 0 additions & 1 deletion +mystica/runSimDynRel.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
%% Saving Workspace

clear ans k kVec motorsCurrent mBodyPosQuat_0 tout dataLiveStatistics
mystica.utils.deleteGeneratedMEX

if stgs.saving.workspace.run
if stgs.saving.workspace.clearCasadi
Expand Down
1 change: 0 additions & 1 deletion +mystica/runSimKinAbs.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
%% Saving Workspace

clear ans k kVec mBodyTwist_0 mBodyPosQuat_0 tout
mystica.utils.deleteGeneratedMEX

if stgs.saving.workspace.run
if stgs.saving.workspace.clearCasadi
Expand Down
1 change: 0 additions & 1 deletion +mystica/runSimKinRel.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
%% Saving Workspace

clear ans k kVec motorsAngVel motorsAngVelNoise mBodyPosQuat_0 tout dataLiveStatistics
mystica.utils.deleteGeneratedMEX

if stgs.saving.workspace.run
if stgs.saving.workspace.clearCasadi
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ The _maximal_ representation consists of a set of _non-minimum_ variables 𝐪 c
## :hammer: Dependencies

- [`matlab`](https://mathworks.com/)
- a matlab [supported-compilers](https://mathworks.com/support/requirements/supported-compilers.html) for MEX-file compilation

Other requisites are:

Expand Down
13 changes: 13 additions & 0 deletions install.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ function install(input)
system(sprintf('"%s" env config vars set MATLABPATH="%s" -p "%s"',mamba_full_path,matlab_path_env,env_full_path));
fprintf('Installing mystica meshes folder completed\n')

%% Create csdMEX folder

fprintf('Installing mystica csdMEX folder\n')
mystica_csdMEX_fullpath = fullfile(mystica_fullpath,'deps','csdMEX');
mkdir(mystica_csdMEX_fullpath)
matlab_path_env = strcat( mystica_csdMEX_fullpath , env_sep , matlab_path_env );
system(sprintf('"%s" env config vars set MATLABPATH="%s" -p "%s"',mamba_full_path,matlab_path_env,env_full_path));
fprintf('Installing mystica meshes folder completed\n')

%% Creation of setup.m

fprintf('Creating setup script in %s\n', setup_script);
Expand All @@ -80,6 +89,7 @@ function install(input)
fprintf(setupID,'install_prefix = "%s";\n', install_prefix);
fprintf(setupID,'mystica_fullpath = "%s";\n', mystica_fullpath);
fprintf(setupID,'mystica_meshes_fullpath = "%s";\n', mystica_meshes_fullpath);
fprintf(setupID,'mystica_csdMEX_fullpath = "%s";\n', mystica_csdMEX_fullpath);
fprintf(setupID,'\n');
fprintf(setupID,'%% AddPath packages installed with conda\n');
fprintf(setupID,'addpath(fullfile(pckgs_install_prefix,"mex"));\n');
Expand All @@ -93,6 +103,9 @@ function install(input)
fprintf(setupID,'%% AddPath mystica meshes\n');
fprintf(setupID,'addpath(mystica_meshes_fullpath);\n');
fprintf(setupID,'\n');
fprintf(setupID,'%% AddPath mystica csdMEX\n');
fprintf(setupID,'addpath(mystica_csdMEX_fullpath);\n');
fprintf(setupID,'\n');
fprintf(setupID,'%% Add to the env:"PATH" the directory with the packages installed with conda\n');
fprintf(setupID,'setenv("PATH",strcat(fullfile(pckgs_install_prefix,"bin"), env_sep, getenv("PATH")));\n');
fclose( setupID);
Expand Down

0 comments on commit 179a87a

Please sign in to comment.