From 179a87a1e77deec58b465e6ed91528205d9e2463 Mon Sep 17 00:00:00 2001 From: Fabio Bergonti <38210073+FabioBergonti@users.noreply.github.com> Date: Sat, 18 Jun 2022 11:35:04 +0200 Subject: [PATCH] Improve CasADi code-generation (#11) Co-authored-by: Silvio Traversaro --- .../+state/@StateKinMBody/StateKinMBody.m | 21 ++++++++++++++++--- +mystica/+utils/deleteGeneratedMEX.m | 12 ----------- +mystica/+utils/getMysticaFullPath.m | 6 ++++++ +mystica/runSimDynRel.m | 1 - +mystica/runSimKinAbs.m | 1 - +mystica/runSimKinRel.m | 1 - README.md | 1 + install.m | 13 ++++++++++++ 8 files changed, 38 insertions(+), 18 deletions(-) delete mode 100644 +mystica/+utils/deleteGeneratedMEX.m create mode 100644 +mystica/+utils/getMysticaFullPath.m diff --git a/+mystica/+state/@StateKinMBody/StateKinMBody.m b/+mystica/+state/@StateKinMBody/StateKinMBody.m index 5013ffd..83704d6 100644 --- a/+mystica/+state/@StateKinMBody/StateKinMBody.m +++ b/+mystica/+state/@StateKinMBody/StateKinMBody.m @@ -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) diff --git a/+mystica/+utils/deleteGeneratedMEX.m b/+mystica/+utils/deleteGeneratedMEX.m deleted file mode 100644 index b40f1cb..0000000 --- a/+mystica/+utils/deleteGeneratedMEX.m +++ /dev/null @@ -1,12 +0,0 @@ -function deleteGeneratedMEX() - delFnc('mystica_stateKin') -end -function delFnc(name) - d = dir([name,'.mex*']); - name_full = d.name; - if exist(name_full,'file')==3 - delete(name_full) - fprintf('%s deleted\n',name_full) - end -end - diff --git a/+mystica/+utils/getMysticaFullPath.m b/+mystica/+utils/getMysticaFullPath.m new file mode 100644 index 0000000..42e530d --- /dev/null +++ b/+mystica/+utils/getMysticaFullPath.m @@ -0,0 +1,6 @@ +function p = getMysticaFullPath() + initial_path = pwd; + cd(fullfile(fileparts(mfilename('fullpath')),'..','..')); + p = pwd; + cd(initial_path); +end diff --git a/+mystica/runSimDynRel.m b/+mystica/runSimDynRel.m index ea7a6c1..85b9255 100644 --- a/+mystica/runSimDynRel.m +++ b/+mystica/runSimDynRel.m @@ -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 diff --git a/+mystica/runSimKinAbs.m b/+mystica/runSimKinAbs.m index 8171e3a..b2f0bad 100644 --- a/+mystica/runSimKinAbs.m +++ b/+mystica/runSimKinAbs.m @@ -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 diff --git a/+mystica/runSimKinRel.m b/+mystica/runSimKinRel.m index f3b767a..c3d09c9 100644 --- a/+mystica/runSimKinRel.m +++ b/+mystica/runSimKinRel.m @@ -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 diff --git a/README.md b/README.md index 6ac67e1..8d3411f 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/install.m b/install.m index af5500e..58a8041 100644 --- a/install.m +++ b/install.m @@ -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); @@ -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'); @@ -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);