Matlab c++ Mex File

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Nicos Haralabidis
Posts: 195
Joined: Tue Aug 16, 2016 1:46 am

Matlab c++ Mex File

Post by Nicos Haralabidis » Tue Aug 07, 2018 8:17 am

Hi all,

I am attempting to implement a direct collocation procedure whereby my design variables also include contact model parameters. I was firstly wondering whether it was at all possible to generate a Matlab C++ Mex File whereby I can output the Model and the State as variables? (I subsequently want to use these variables as input variables to another Matlab C++ Mex File - in order to compute the derivatives and predicted GRFs). My input arguments to the function would be the contact model parameters, and inside the function I would set the ground contact model parameters to the model and initialize the model state.

I hope that makes sense!

Kind regards,

Nicos Haralabidis

Tags:

User avatar
jimmy d
Posts: 1375
Joined: Thu Oct 04, 2007 11:51 pm

Re: Matlab c++ Mex File

Post by jimmy d » Fri Aug 10, 2018 1:56 pm

was at all possible to generate a Matlab C++ Mex File whereby I can output the Model and the State as variables?
Your question seems too general. No one has probably answered because the answer is just 'Yes'. Creating a set of compiled Mex functions is just your choice of what platform you wish to do your coding in.

User avatar
Nicos Haralabidis
Posts: 195
Joined: Tue Aug 16, 2016 1:46 am

Re: Matlab c++ Mex File

Post by Nicos Haralabidis » Tue Aug 14, 2018 2:14 am

Hello James,

Thanks for your response. Apologies for the non-detailed question, I am just starting to get my head around the creation of a mex function and how to use them alongside OpenSim.

I have currently managed to get a mex function to compile, however when I then run the function I get the following error message: What() is:ArrayPtrs.get: Array index out of bounds. Here are the lines of code I am working with (I have omitted the specific mex function details for clarity.):


Model myModel("C:/Users/Nicos/DC/TorqueDrivenModel.osim");
ForceSet &forceSet = ForceSet(myModel);

int a = 95;

HuntCrossleyForce HCforce1 = dynamic_cast<HuntCrossleyForce&>(forceSet.get(a));

double x = 200000;

HCforce1.setStiffness(x);

myModel.print("C:/Users/Nicos/DC/Updated_TorqueDrivenModel.osim");


The ArrayPtrs error message corresponds to the 4th line of code - where I am trying to get a hold of the HuntCrossleyForce, which happens to be the 95th 'force' in my ForceSet. I have also tried '....get("Foot_Ground_R1");' , however I then receive "What() is:ArrayPtrs.get(aName): No object with name Foot_Ground_R1 when I try run the function after compiling it.

Do you have any suggestions on how I may overcome this problem?

Kind regards,

Nicos Haralabidis

User avatar
Thomas Uchida
Posts: 1790
Joined: Wed May 16, 2012 11:40 am

Re: Matlab c++ Mex File

Post by Thomas Uchida » Tue Aug 14, 2018 1:53 pm

The ArrayPtrs error message corresponds to the 4th line of code - where I am trying to get a hold of the HuntCrossleyForce, which happens to be the 95th 'force' in my ForceSet.
The error message indicates that your ForceSet contains fewer than 96 elements (the first element is index 0). You can check the size of the ForceSet by calling ForceSet.getSize().

User avatar
Nicos Haralabidis
Posts: 195
Joined: Tue Aug 16, 2016 1:46 am

Re: Matlab c++ Mex File

Post by Nicos Haralabidis » Tue Aug 14, 2018 4:05 pm

Hello Thomas,

I realised that I wasn't originally getting a correct grasp of the model ForceSet and that it is why I could not access the correct index, I modified that line to:

const ForceSet& forceSet = myModel.getForceSet();

which seems to work and checked the size of forceSet - 101. I am now having issues with downcasting the ForceSet to HuntCrossleyForce:

HuntCrossleyForce* HCF1 = dynamic_cast<HuntCrossleyForce*>( &forceSet.get("Foot_Ground_R2") );

When I attempt to compile the function I receive:
error C2682: cannot use 'dynamic_cast' to
convert from 'const OpenSim::Force *' to 'OpenSim::HuntCrossleyForce *'.
With my limited knowledge of C++ I think that this message is suggesting that there is a mismatch between the variable types. The forceSet has to be const as otherwise the compiler throws a similar error when trying to get a hold of the forceSet at the beginning. Is it possible to modify the const aspect of forceSet?

Kind regards,

Nicos Haralabidis

User avatar
Thomas Uchida
Posts: 1790
Joined: Wed May 16, 2012 11:40 am

Re: Matlab c++ Mex File

Post by Thomas Uchida » Tue Aug 14, 2018 10:34 pm

Is it possible to modify the const aspect of forceSet?
If you need to modify the ForceSet, you should be getting a writable reference using updForceSet() rather than a const reference via getForceSet(). There's a table of naming conventions here: https://github.com/opensim-org/opensim- ... onventions.

User avatar
Nicos Haralabidis
Posts: 195
Joined: Tue Aug 16, 2016 1:46 am

Re: Matlab c++ Mex File

Post by Nicos Haralabidis » Fri Aug 17, 2018 3:54 am

Hello Thomas,

Thanks for your reply. Your suggestion worked a treat! :D I am now able to modify the contact model parameters.

I am now having the difficulty of understanding how to pass my modified Model myModel as an output of the mex function back to Matlab. In the example - double x = 10.0; - I know that variable 'x' is of type double and it is reasonably straight forward to output this value back to Matlab. In the case of - Model myModel - I think I know that myModel is of type Model (?). But I am then not certain how I can correctly get the myModel with its Model type to be passed into one of the return pointers (e.g. outputModel = mxGetPr(plhs[0]);)?

Any help is greatly appreciated!

Kind regards,

Nicos Haralabidis

User avatar
Dimitar Stanev
Posts: 1096
Joined: Fri Jan 31, 2014 5:14 am

Re: Matlab c++ Mex File

Post by Dimitar Stanev » Fri Aug 17, 2018 8:29 am

This project may be of some use to you. Unfortunately the whole process is a bit technical. I would try to avoid mex as much as possible because they are hard to debug.

https://simtk.org/projects/opensimmatlab

POST REPLY