Some questions about how to create and use some customed plugins in Moco

OpenSim Moco is a software toolkit to solve optimal control problems with musculoskeletal models defined in OpenSim using the direct collocation method.
POST REPLY
User avatar
Matthew Lee
Posts: 52
Joined: Sat Jun 20, 2020 7:46 pm

Some questions about how to create and use some customed plugins in Moco

Post by Matthew Lee » Sun Feb 25, 2024 6:40 am

Dear Moco development team and Moco users,

Thank you all for your contributions to the biomechanics community! I want to use some customed plugins in Moco to achieve personalized needs, but I got stuck in how to implement it through programming, I would appreciate it if anyone could give me some advice.

1. I want to use the 'MuscleLikeCoordinateActuator' (located in the sandbox https://github.com/opensim-org/opensim- ... gWholeBody) in the Opensim model, and then use Matlab-Moco API to minimize the squared control of the actuator. This actuator cannot be used directly in OpenSim and I have no idea what operations need to be performed. Could anyone give me some guidance?

2. Assuming the torque generated by the CoordinateActuator is tau and the time is t. I want to customize an objective function (dtau/dt)^2 through C++. My question is, how should I obtain (dtau/dt) in calcIntegrationImpl?

I would be very grateful if someone could give me some advice.

Best Wishes,
Matthew

User avatar
Nicholas Bianco
Posts: 963
Joined: Thu Oct 04, 2012 8:09 pm

Re: Some questions about how to create and use some customed plugins in Moco

Post by Nicholas Bianco » Wed Feb 28, 2024 5:39 pm

Hi Matthew,

For an example on how to create a plug-in in Moco, you can review the following example: https://github.com/opensim-org/opensim- ... EffortGoal.

If you want to use the MuscleLikeCoordinateActuator with a custom MocoGoal, you could put both in one pair of header and source files and then compile the DLL for the plugin. The cpp file in the custom effort goal example contains instructions on how to load the plug-in for use in matlab or python scripting.

Best,
Nick

User avatar
Matthew Lee
Posts: 52
Joined: Sat Jun 20, 2020 7:46 pm

Re: Some questions about how to create and use some customed plugins in Moco

Post by Matthew Lee » Wed Feb 28, 2024 6:27 pm

nbianco wrote:
Wed Feb 28, 2024 5:39 pm
For an example on how to create a plug-in in Moco, you can review the following example: https://github.com/opensim-org/opensim- ... EffortGoal.
Hi Nick,

Thanks very much for your professional answer! I can successfully run the example you mentioned, but I still feel confused about some issues and need your help:

1.How can I use the MuscleLikeCoordinateActuator with the existed MocoGoal, such as MocoControlGoal?
2.I still don't know how to obtain (dtau/dt) in calcIntegrationImpl when customizing the objective function in C++.

I really appreciate your professional answers and help, thanks again for your patience and time.

Best Wishes,
Matthew

User avatar
Nicholas Bianco
Posts: 963
Joined: Thu Oct 04, 2012 8:09 pm

Re: Some questions about how to create and use some customed plugins in Moco

Post by Nicholas Bianco » Wed Feb 28, 2024 8:14 pm

Hi Matthew,

Happy to clarify:

1. If you copy the code from MuscleLikeCoordinateActuator and put it into the same .h and .cpp files along with the code for your custom goal, then it should be available when you compile and load your DLL.

2. If you want to minimize dtau/dt, one option could be to add a layer of dynamics to the actuator where tau is the state. You can then add an Output to the actuator that retrieves the state variable derivative value, which could be minimized using a MocoOutputGoal. But to do this you would need to provide an expression for the state variable derivative, which kind of defeats the purpose. Perhaps the better way would be too supportive implicit dynamics formulation for your actuator, since this would give you a control value equal to the state derivative in the problem. You can refer to DeGrooteFregly2016Muscle for an actuator that support implicit dynamics. After you do that, you can use the setting 'minimize_implicit_auxiliary_derivatives' in MocoCasADiSolver to minimize dtau/dt.

Best,
Nick

User avatar
Matthew Lee
Posts: 52
Joined: Sat Jun 20, 2020 7:46 pm

Re: Some questions about how to create and use some customed plugins in Moco

Post by Matthew Lee » Thu Feb 29, 2024 12:12 am

Hi Nick,
Thanks very much for your quick reply! Related to the above two questions, I still need your help:

1. If I don't customize a new objective function. In other words, I am only using the objective function provided by Moco, such as the most common MocoControlGoal, to minimize the control squared of the MuscleLikeCoordinateActuator. What should I do?

2. This question is independent of the previous one. If I use 'CoordinateActuator' which currently available in OpenSim to drive the model, can I retrieve the derivative of this actuator's control over time (i.e. du/dt) in calcIntegrationImpl? If possible, how should I retrieve it?

Best Wishes,
Matthew

User avatar
Nicholas Bianco
Posts: 963
Joined: Thu Oct 04, 2012 8:09 pm

Re: Some questions about how to create and use some customed plugins in Moco

Post by Nicholas Bianco » Thu Feb 29, 2024 10:12 am

Hi Matthew,

1. If you want to minimize the squared control for a MuscleLikeCoordinateActuator, all you need to do is expose this actuator via a plug-in. Then you will be able to include it in a Moco problem.

2. You won't be able to minimize dtau/dt using calcIntegrationImpl unless you create a custom component that provides this value as a state or control variable. My suggestions above will help you get closer to what you need for this.

Best,
Nick

POST REPLY