Functions for prescribed force/motion managed by MATLAB

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Hide Kimpara
Posts: 135
Joined: Mon Sep 19, 2016 5:12 am

Functions for prescribed force/motion managed by MATLAB

Post by Hide Kimpara » Thu Apr 27, 2017 9:59 am

Dear Staff of OpenSim and researchers in OpenSim community,

I am Hide Kimpara from WPI. I have started to learn OpenSim with MatLab.
My project is an interactive analyses between human and machines items.
The machine dynamics would be determined from external system. I am interested in the methods to set item's motions with the functions of prescribed force (or prescribed motion).
When I found an example of prescribed force on osim file, the function was set with piecewiseLinearFunction. In this example, user can define curves with x1, x2, x3, ... and y1, y2, y3, ...
I think this would be one of method and representing interface to reproduce item motions which were determined from external system.
But it is not so flexible format for me to modify as demands.

Now I have three questions.
1) Can I refer the function plot of piecewiseLinearFunction with external files, such as STO file?
2) Can I change functional parameters during the analyses? For example, we may be possible to change state variables using OpenSim API functions. Does OpenSim have similar modification capabilities for function curve data?
3) I assume currently available method to obtain prescribed motion would be only prescribed force. But does OpenSim API have an interface or functions of prescribed motion by desired velocity or acceleration?

Also I have further two questions. I am also modeling a flexible mechanical structures supported with 4 spring suspensions. In order to analyse the mechanical system, the definition of gravity was too strong to get stable responses. Initial state of system does not have balanced against gravity forces, therefore, system represents a transient response. Sometimes, 1G of sudden acceleration is severe for a system. On the contrary, FE engineers sometimes use a technical know-how of gravity acceleration with soft gradients. Some software allows user to apply a functional curve for gravity definition. Can OpenSim represent similar solutions?

The last question is constraint definition. I used a constraint definition in order to fix the ends of 4 suspensions on the ground. Currently available options for constraints are point constraint or point on line constraint. However, I'd like to use a kind of point on plane constraint which may fix only one translational DOF. In case of this demand, how can I represent using existing options?

Thank you and Best,
Hide

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

Re: Functions for prescribed force/motion managed by MATLAB

Post by Dimitar Stanev » Fri Apr 28, 2017 12:26 am

Hi,

Please see the API for more details:

https://simtk.org/api_docs/opensim/api_ ... Force.html
https://simtk.org/api_docs/simbody/3.5/ ... otion.html

1) You can Load an external force from a file (https://simtk.org/api_docs/opensim/api_ ... Loads.html) and then add this force to the model (please see how it is used in the source code https://github.com/opensim-org/opensim- ... l.cpp#L221).

2) Yes, PrescribedForce can accept a function. You can implement your own function by deriving form Function (https://simtk.org/api_docs/opensim/api_ ... ction.html) that will calculate its value. But I am not sure if it is possible from Matlab.

3) Yes see second link.
Initial state of system does not have balanced against gravity forces, therefore, system represents a transient response. Sometimes, 1G of sudden acceleration is severe for a system.
Why should it be balanced? You can assign a state variables prior to integrating the system. Maybe you have to find an equilibrium point of the system and use this state as a starting point.
On the contrary, FE engineers sometimes use a technical know-how of gravity acceleration with soft gradients. Some software allows user to apply a functional curve for gravity definition. Can OpenSim represent similar solutions?
I am not sure if I understood this technique. You can change the gravity from model.setGravity() but this is a property of the model (https://github.com/opensim-org/opensim- ... del.h#L151) so the model will be invalidated. OpenSim is a little strict on this type of changes. I am not sure if it is possible to achieve this by changing the gravity.
However, I'd like to use a kind of point on plane constraint which may fix only one translational DOF. In case of this demand, how can I represent using existing options?
Maybe you can define a body that is permitted to translate in some directions. Then you would connect your suspensions to the body.

Hope this helps!

User avatar
Hide Kimpara
Posts: 135
Joined: Mon Sep 19, 2016 5:12 am

Re: Functions for prescribed force/motion managed by MATLAB

Post by Hide Kimpara » Tue May 02, 2017 1:14 pm

Dear Dimitar,

Thank you for your suggestion.

I found an example of prescribed motion "prescribeMotionInModel.m" from MATLAB_Scripting. This example may change model input osim file with additional SimmSpline function and prescribedMotion switch-on.
Since I could not find any methods to call PrescribedMotion method of SimBody from MatLab, I modified scripts of this example and placed them before the initSystem().
However, when I placed them after initSystem, the scripts won't work.

Do you know any scripts to update modifications of coordinate set into the model?


Also I realized that OpenSim can set gravity as constant variables. As you say, it may be better to input motions on body.
However, in case of the body running with greater speeds, how can we visualize those motions? Some of other software or post-processer has a function that visual frame anchors a selected body. So visual frame can move associated with a target item body.
Do you know any solutions to visualize with moving frame?

Best regards,
Hide

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

Re: Functions for prescribed force/motion managed by MATLAB

Post by Dimitar Stanev » Wed May 03, 2017 2:44 am

Hi,
However, when I placed them after initSystem, the scripts won't work.
If you post the script I may point out where the problem is, because there are some specifics regarding the initialization of the system.
Do you know any scripts to update modifications of coordinate set into the model?
Please see the user guide examples (C++) and adapt them accordingly for Matlab.

http://simtk-confluence.stanford.edu:80 ... Simulation

(there are other examples in the user guide)
However, in case of the body running with greater speeds, how can we visualize those motions? Some of other software or post-processer has a function that visual frame anchors a selected body. So visual frame can move associated with a target item body.
Do you know any solutions to visualize with moving frame?
I am not sure if you can achieve this with Simbody visualizer, whose purpose is limited to inspect the model and the simulation results. I would suggest to check whether you can have a workaround to your problem and if not to implement your own visualizer or extend Simbody's.

Best

User avatar
Hide Kimpara
Posts: 135
Joined: Mon Sep 19, 2016 5:12 am

Re: Functions for prescribed force/motion managed by MATLAB

Post by Hide Kimpara » Fri May 05, 2017 10:55 am

Dear Dimitar,

Thank you for your kind proposal of review scripts.
I am happy to share my MATLAB code here. The sample script what I post here includes reading prescribed motion and initial states from sto file.

Can I ask you to indicate what sentences from OpenSim API are necessary to execute those in MATLAB environment?

For the viewer issue; if I wish to add a new function into OpenSim GUI such as a fixing view frame onto a selected body coordinate, do you know where or whom I can ask a development request?

Thank you and Best,
Hide

main_type1_w_prescmot_share.m.txt
(6.62 KiB) Downloaded 43 times

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

Re: Functions for prescribed force/motion managed by MATLAB

Post by Dimitar Stanev » Sat May 06, 2017 12:19 am

Hi,

Is it possible to send all the files so I can run your script and see the error, because its hard to tell otherwise?
For the viewer issue; if I wish to add a new function into OpenSim GUI such as a fixing view frame onto a selected body coordinate, do you know where or whom I can ask a development request?
You can find the code of the visualizer here:

https://github.com/simbody/simbody/tree ... Visualizer

You can either ask a question in:

indexPhpbb.php?group_id=47&pluginname=phpBB

or request an issue:

https://github.com/simbody/simbody

** BodyFollower

Please have a look at this:

https://github.com/simbody/simbody/blob ... zer.h#L150

Maybe this can help you in your problem, but I am not sure how much assess to the underlying visualizer you can get from Matlab.

Best

User avatar
Hide Kimpara
Posts: 135
Joined: Mon Sep 19, 2016 5:12 am

Re: Functions for prescribed force/motion managed by MATLAB

Post by Hide Kimpara » Mon May 08, 2017 7:31 am

Hello,

I share a simple sample model with script. It's a modified bouncing block model.

The zip file contains following 8 files;
main_w_prescmot_1_share.m (MatLab script with reading prescribed motion and initial states)
main_w_prescmot_2_share.m (place initSystem before setting prescribed motion)
bouncing_block_slide_wG.osim (bouncing block model with gravity)
bouncing_block_slide_noG.osim (without gravity)
block_mot.mot (prescribed motion of vertical kinematics)
block_mot2.mot (prescribed motion for keep vertical position)
toy_with_forces_init_states.sto (initial state file)
toy_with_forces_init_states_2.sto (initial state file with different slider joint state)

The MatLab scripts are only different in position of osimModel.initSystem(); type 1 has line# 96, while type 2 has line# 46.

When you execute "main_w_prescmot_1_share.m" with "bouncing_block_slide_noG.osim" and "block_mot.mot", you may see a motion from output state file. However, when you execute "main_w_prescmot_2_share.m" with same input files, we do not see any motions.

I'd like to activate prescribed motion definition even in type 2 MatLab script.



Thank you,
Hide
Attachments
Share.zip
(15.67 KiB) Downloaded 21 times

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

Re: Functions for prescribed force/motion managed by MATLAB

Post by Dimitar Stanev » Tue May 09, 2017 1:04 am

I am not sure what experiment you would like to perform. I tried to play a little bit with the model and it seems that it does not behave well. Some observations:

It seems that the model is restricted to move in the y-direction. Unfortunately for some reason if you perform forward simulation in the default pose the model behaves similarly as to the case of the script #2 (a very small movement is observed, but the model don't bounce on the ground). If you lower the model near the ground (yTranslation~=1) and perform a forward simulation the model bounces and goes up. My guess is that near the ground large forces are applied by the contact model and the model overcomes its restrictive forces (constraints) and bounces. When the model is in its default position and you perform a forward simulation, no ground reaction force is applied and for some reasons the joint implicit constraints and actual constraints do not permit the model to move.

Then I tried to adjust the integrator error tolerance by reducing it 1e-10 from 1e-5 which is the default for the forward tool. Then it seems that the system begun to move normally. My guess is that small constraint violation during numerical integration produced forces that do not permit the model to move correctly. I was able to change the "Integrator error tolerance" in the "Integrator Settings" from the gui but I am not sure if it is possible through Matlab, since SimTK::Integrator is not exposed.

## About your second script, probably the prescribed motion is not used since the model was initialized prior to adding the prescribe functions.

Best

User avatar
Hide Kimpara
Posts: 135
Joined: Mon Sep 19, 2016 5:12 am

Re: Functions for prescribed force/motion managed by MATLAB

Post by Hide Kimpara » Wed May 10, 2017 1:19 pm

Dear Dimitar,

Thank you for taking time of executing test models and scripts.
I learned many things through the conversation with you.
I appreciate your voluntary helps!

Best regards,
Hide

POST REPLY