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
Matlab c++ Mex File
- Nicos Haralabidis
- Posts: 196
- Joined: Tue Aug 16, 2016 1:46 am
Re: Matlab c++ Mex File
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.was at all possible to generate a Matlab C++ Mex File whereby I can output the Model and the State as variables?
- Nicos Haralabidis
- Posts: 196
- Joined: Tue Aug 16, 2016 1:46 am
Re: Matlab c++ Mex File
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
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
- Thomas Uchida
- Posts: 1793
- Joined: Wed May 16, 2012 11:40 am
Re: Matlab c++ Mex File
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().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.
- Nicos Haralabidis
- Posts: 196
- Joined: Tue Aug 16, 2016 1:46 am
Re: Matlab c++ Mex File
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
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
- Thomas Uchida
- Posts: 1793
- Joined: Wed May 16, 2012 11:40 am
Re: Matlab c++ Mex File
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.Is it possible to modify the const aspect of forceSet?
- Nicos Haralabidis
- Posts: 196
- Joined: Tue Aug 16, 2016 1:46 am
Re: Matlab c++ Mex File
Hello Thomas,
Thanks for your reply. Your suggestion worked a treat! 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
Thanks for your reply. Your suggestion worked a treat! 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
- Dimitar Stanev
- Posts: 1096
- Joined: Fri Jan 31, 2014 5:14 am
Re: Matlab c++ Mex File
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
https://simtk.org/projects/opensimmatlab