How can i use a given C-Code to model polygonal contact between two bodies in Matlab/Simmechanics ?

4 views (last 30 days)
Hello everyone,
i have problems using given C-Code (multiple c-files) that is able to model polygonal contact, see the homepage where i got the code from: (originally it was developed for the use in Simpack)
I have attached the readme.txt file from this source as well as the calltree.txt and the files.txt so that you are able to follow me through my questions.
Some things beforehand: I want to use this code in Matlab/Simmechanics, what i already know is that simulating contact in Simmechanics is possible via measuring distances between bodies and applying penalty forces, like in this easy to understand example: mech_bouncing_ball
Essentially i want similar results, but i want to replace the content of the contact force block/mask with functionalities from this PCM-contact model. Later on, i want to use complexly shaped geometries in STL-format in order to simulate contact between two bodies, that is where this C-Code should kick in (PCM has been developed especially for that purpose).
The interesting part for me is the Multibodysystem (MBS)-Interface, that this library provides in order to be used in different MBS-Codes, like Matlab and Simulink/Simmechanics.
There are three interface routines to be called, which themselves call multiple subroutines in order to be executed (see calltree.txt).
What i already know is that it is possible to use C-Code in Matlab via the MEX File Creation API, there are a lot of very simple examples available that already worked here without any problems.
In order to "build" the mex file for one of these interface routines, i did this (example for pcm_defcont): mex call_pcm_defcont.c pcm_defcont.c pcm_addcont.c pcm_addcosu.c pcm_cosupre.c pcm_calcsgeo.c pcm_creabvtr.c pcm_creadcel.c pcm_root.c pcm_print.c pcm_mathlib.c pcm_centsort.c
call_pcm_defcont.c is just a wrapper function, so that i do not have to change the interface routine whenever anything changes in the source code.
In a similar fashion, i also did this for pcm_calcont and pcm_freemem and it compiled.
But here comes my problem: it seems to me, that the subroutines in these interface are not being called at all, because if i use "false" input arguments, i.e. inputs that should produce errors, the functions are executed without any errors or indications, that something went wrong. Moreover, it seems to me that nothing is done at all, i.e. that the subroutines are never called.
So here is my core question to this: What do i have to do when using mex, if my C-Code is split up in many different source files (see files.txt) and there are some subroutines called within the interface routine ? Are there any other possibilities that i did not mention or considered here ?
I know that it is also possible to call C-Code via S-functions in Simulink, what i have now is a callable mex function that i use via a "Matlab Function" - Block in the Simulink/Simmechanics model. Is there any important difference in one of these approaches ?
Background information: The project i am working on is to be able to use this contact library in Matlab/Simmechanics. What i have in mind is to use pcm_defcont.c as some kind of initialization process/function, pcm_calcont.c to compute contact forces in each time step of the simulation (ideally via a user defined function block) and, last but not least, pcm_freemem.c to clean the memory once after the simulation.
Thank you very much for your help and suggestion in advance!

Answers (1)

Steve Miller
Steve Miller on 20 Nov 2022
Thank you for pointing out this interesting library for contact modeling. If you want us to look into this further, you will need to take a few more steps and provide a more complete example. Zip up what you have with the exact error messages you are receiving and we might be able to provide more guidance.
You should also take a look at the Spatial Contact Force block, which can handle many of the conditions you are exploring. You can look at examples in the online documentation, as well as in this File Exchange submission: Spatial Contact Force Examples in Simscape Multibody. This might help you confirm that your custom code is doing what you expect.


Find more on Physical Units 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!