Page 1 of 2

Adding a Custom Muscle to a model

Posted: Tue Jul 22, 2014 9:20 am
by jakubk2
Hello,

I am trying to implement a new muscle into OpenSim thru MATLAB. I have the complete muscle function written in MATLAB already. I am trying to implement it as a path actuator however I can not get OpenSim to recognize my function. I have also gone through the example of the Dynamic Walker Challenge and have been able to successfully run those scripts and alter them in some ways. However I have not been able to modify them in order to recognize my function. What is the calling convention in to call my function to calculate the tension of the path actuator?

Thanks
Kristen

Re: Adding a Custom Muscle to a model

Posted: Tue Jul 22, 2014 10:52 am
by aseth
It is not clear by your description what you are expecting to happen. What calls are you making that are not working?

As a general principle OpenSim cannot call functions in MATLAB. MATLAB can provide OpenSim with inputs and it computes system dynamics that can be solved in MATLAB. For example, your model of muscle dynamics can be used to set the tension of the path actuator by specifying its control value at each new state. The driver script Main_WalkerForwardSimWithControls.m shows how a user's control function (e.g. OpenSimPlantControlsFunction.m that could call your Muscle model function) can be called during forward integration in MATLAB.

The OpenSimPlantControlsFunction takes a reference to the model and the state so you should be able to access the path lengths and velocities that you need to compute the muscle tension (scalar value). The OpenSimPlantFunction computes the model state derivatives (coordinate speeds and accelerations as well as any OpenSim muscle model state derivatives like fiber velocity and activation rate). You will need to tack on the state derivatives of your muscle model to the x_dot vector so that the dynamics of your muscle model are solved simultaneously with the OpenSim model.

Perhaps you can post more details of what you have attempted so that forum feedback can be more targeted and helpful for you.

Re: Adding a Custom Muscle to a model

Posted: Mon Sep 22, 2014 6:50 pm
by jakubk2
Hi Ajay,

I have a few clarifying questions. I have successfully edited the Main_WalkerFunctionSimWithControls.m to run with a path actuator instead of a coordinate actuator. Then added my muscle function to the OpenSimPlantControlsFunction.m file, like you suggested. I am currently using LKnee_rz as the position, and LKnee_rz_u as velocity. Is that correct? When I try and run Main_walker again, I get a number of errors and I am unsure on how to successful debug.

Additionally, I am unsure of how to attach the state derivative to the x_dot vector.

Any suggestions would be greatly appreciated.

Thanks
Kristen

Re: Adding a Custom Muscle to a model

Posted: Sat Sep 27, 2014 8:28 pm
by aseth
I am currently using LKnee_rz as the position, and LKnee_rz_u as velocity. Is that correct?
That does not sound right, unless you are somehow using the change in knee angle to compute muscle length in your code? If your MATLAB muscle code requires muscle length and velocity then you could ask the path actuator directly for its length and speed.
Have you added a PathActuator to the model? Are you able to get its length and speed given a (SimTK) State?
Also, have you solved an ODE in MATLAB before? if you are solving xdot = F(t,x) you simply increase the length of x for any new state variables and have F return a longer xdot vector to correspond to the derivatives of the new state variables. I assume your simulation of the muscle (without OpenSim) does this already.

Re: Adding a Custom Muscle to a model

Posted: Sun Oct 05, 2014 11:33 am
by jakubk2
I have been able to add a path actuator, pathAct_LK, to the model. But I have not been able to get the length and speed.

I keep getting the error

Code: Select all

Error using OpenSimPlantControlsFunction (line 89)
The name 'pathAct_LK' is not an accessible property for an instance of class 'org.opensim.modeling.Model'.
my code is currently

Code: Select all

VMT= osimModel.get('pathAct_LK').getLengtheningSpeed();
LINIT= osimMode.get('pathAct_LK').getLength(); 

Re: Adding a Custom Muscle to a model

Posted: Mon Oct 06, 2014 5:02 pm
by aseth
You need to get the set of actuators to access yours. You also need to cast to the right actuator type, since you get list of actuators and only you know it is a PathActuator. E.g.:

Code: Select all

shouldBePathAct = model.getActuators().get('pathAct_LK'');
pathAct = PathActuator.safeDownCast(shouldBePathAct);
VMT= pathAct.getLengtheningSpeed(state);
... 
You can get the state from the model when it creates the underlying system.

Code: Select all

 state = model.initSystem(); 
IntegratOpenSimPlant.m will call initSystem() for you should you forget.

The OpenSimPlantFunction() translates the matlab vector of states, x, to the SimTK::State that OpenSim components use to compute values (including the derivatives you want to integrate). In your case, x, contains addition state variables related to your muscle model in matlab.

Re: Adding a Custom Muscle to a model

Posted: Mon Nov 03, 2014 7:32 am
by jakubk2
I have been able to implement my model into the dynamic walker scripts and it will run without errors. However, I am unsure if it is using my model to calculate force, or if it is still using the path acutator (I get the same results with my muscle implemented and without). Is there a way to ensure that it is running my muscle?

Also I have tried to implement the same scripts from the dynamic walker model, modified into a tug of war model. And I keep getting an error in the IntegrateOpenSimPlant.m that the "array index is out of bounds" Do you have any suggestions on how to debug this? or a way to implement my muscle into the tug of war model?

Thanks
Kristen

Re: Adding a Custom Muscle to a model

Posted: Tue Nov 04, 2014 11:33 am
by aseth
1. Have you tried putting a breakpoint in your muscle model function? What are the muscle forces coming out?
2. if the muscle code is being executed are you setting the control value on the path actuator to be the muscle model force?

Re: Adding a Custom Muscle to a model

Posted: Tue Nov 04, 2014 1:18 pm
by jakubk2
1. Yes, i have put a break point n the muscle model function, and I will get an output of force from my muscle model function.

2. I have tried to, but i keep getting errors. that is how im doing it?

Code: Select all

    % Update modelControls with the new values
    actControls.set(FD, val);  %FD= muscle force
    osimModel.updActuators().get('pathAct_LK').addInControls(actControls, modelControls)

Re: Adding a Custom Muscle to a model

Posted: Tue Nov 04, 2014 3:30 pm
by aseth
actControls.set(FD, val); %FD= muscle force
looks incorrect.

actControls should be a Vector of length 1, so you should be setting the value not the index to the force value:

Code: Select all

 actControls.set(0, FD); %0 is the index in the Vector (0th element) and FD is your force value