exampleSquatToStand- MocoPeriodicityGoal

OpenSim Moco is a software toolkit to solve optimal control problems with musculoskeletal models defined in OpenSim using the direct collocation method.
User avatar
Nicholas Bianco
Posts: 963
Joined: Thu Oct 04, 2012 8:09 pm

Re: exampleSquatToStand- MocoPeriodicityGoal

Post by Nicholas Bianco » Mon Jun 15, 2020 9:47 am

Hi Mouaad,

Could you check that the variables you're passing to createExternalLoadsTableForGait are the same as in example2DWalking? It's hard to know what the issue is without a larger snippet of code.

-Nick

User avatar
Mouaad BOUFADNA
Posts: 22
Joined: Mon Mar 16, 2020 3:24 am

Re: exampleSquatToStand- MocoPeriodicityGoal

Post by Mouaad BOUFADNA » Mon Jun 15, 2020 10:07 am

Hi Nicholas

I wrote the variables like in example2DWalking (while withdrawing what was useless for me)

Code: Select all


LiftPredictionSolution = study.solve();
study.visualize(LiftPredictionSolution);
LiftPredictionSolution.write('LiftPrediction_solution.sto');


contact_l = StdVectorString();
contact_r.add('contactHeel_r');
contact_r.add('contactFront_r');
contact_l.add('contactHeel_l');
contact_l.add('contactFront_l');

externalForcesTableFlat = opensimMoco.createExternalLoadsTableForGait(model, contact_r, contact_l);
opensimMoco.writeTableToFile(externalForcesTableFlat,'LiftPrediction_solutionGRF.sto');


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

Re: exampleSquatToStand- MocoPeriodicityGoal

Post by Nicholas Bianco » Mon Jun 15, 2020 10:16 am

You need to include the solution in your call to createExternalLoadsTableForGait():

Code: Select all

externalForcesTableFlat = opensimMoco.createExternalLoadsTableForGait(model, LiftPredictionSolution, contact_r, contact_l);

User avatar
Mouaad BOUFADNA
Posts: 22
Joined: Mon Mar 16, 2020 3:24 am

Re: exampleSquatToStand- MocoPeriodicityGoal

Post by Mouaad BOUFADNA » Tue Jun 16, 2020 1:22 am

It's working now ! I get the .sto file for GRF.

Only another problem :|

My values are 0 except when the feet touche the ground at the end of the motion. My model is not on the ground at the beginning
I think I should define a Contact constraint between the ground and the feet to avoid such kinematic disorder

I thought about problem.addContactGeomtry or problem.addContactConstraint, but for the arguments I don't know how to define them .
I wondered two possibilities for:
("ground",'feet")
Or '('/bodyset/ground','/bodyset/talus')

Or may be add the ControlTrackingGoal and addContactGroup like in the Tracking Solution of example2DWalking ?

Sorry for asking so much question I'm beginner in Moco and not very familiar with OpsenSim/Matlab interface

Thank you for your help !

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

Re: exampleSquatToStand- MocoPeriodicityGoal

Post by Nicholas Bianco » Tue Jun 16, 2020 9:46 am

Hi Mouaad,

Does your model have similar contact geometry as in example2DWalking? You shouldn't need to add any additional constraints.

I'd need to know more about your problem to know why the model isn't on the ground at the beginning. You can check the model's default configuration in the OpenSim GUI. Also check that they're are no actuators at the pelvis that would artificially lift the model off the ground.

-Nick

User avatar
Mouaad BOUFADNA
Posts: 22
Joined: Mon Mar 16, 2020 3:24 am

Re: exampleSquatToStand- MocoPeriodicityGoal

Post by Mouaad BOUFADNA » Wed Jun 17, 2020 1:14 am

Hi Nick

This is my entire code :

Code: Select all

% Load the Moco libraries
import org.opensim.modeling.*;


% Define the optimal control problem
% ==================================
study = MocoStudy();
study.setName('LiftPrediction');

problem = study.updProblem();
modelProcessor = ModelProcessor('2D_gait.osim');
problem.setModelProcessor(modelProcessor);
model = modelProcessor.process();
model.initSystem();
 

% Bounds
% ======
problem.setTimeBounds(0,[0.6,0.8]);
problem.setStateInfo('/jointset/groundPelvis/pelvis_tilt/value', [-20*pi/180, -10*pi/180]);
problem.setStateInfo('/jointset/groundPelvis/pelvis_tx/value', [0, 1]);
problem.setStateInfo('/jointset/groundPelvis/pelvis_ty/value', [0.75, 1.25]);
problem.setStateInfo('/jointset/hip_l/hip_flexion_l/value', [10*pi/180,2], 2, 10*pi/180);
problem.setStateInfo('/jointset/hip_r/hip_flexion_r/value', [10*pi/180,2], 2, 10*pi/180);
problem.setStateInfo('/jointset/knee_l/knee_angle_l/value',[-2, 0], -2, 0);
problem.setStateInfo('/jointset/knee_r/knee_angle_r/value',[-2, 0], -2, 0);
problem.setStateInfo('/jointset/ankle_l/ankle_angle_l/value', [0,0.5], 0.5, 0);
problem.setStateInfo('/jointset/ankle_r/ankle_angle_r/value', [0,0.5], 0.5, 0);

problem.setStateInfo('/jointset/lumbar/lumbar/value', [-20*pi/180,10*pi/180],-20*pi/180,10*pi/180);


% % Velocity bounds: all model coordinates should start and end at rest.
problem.setKinematicConstraintBounds(MocoBounds(0,0));

% Cost.
% -----
problem.addGoal(MocoFinalTimeGoal("time"));

% Configure the solver
% ====================
solver = study.initCasADiSolver();
solver.set_num_mesh_intervals(50);
solver.set_verbosity(2);
solver.set_optim_solver('ipopt');
solver.set_optim_convergence_tolerance(1e-4);
solver.set_optim_constraint_tolerance(1e-4);
solver.set_optim_max_iterations(1000);



% Solve problem
% =============
LiftPredictionSolution = study.solve();
LiftPredictionSolution.write('LiftPrediction_solution3.sto');
% study.visualize(LiftPredictionSolution);

%Plot trajectories for the different states
mocoPlotTrajectory('LiftPrediction_solution3.sto');
    


% Extract ground reaction forces
% ==============================
contact_r = StdVectorString();
contact_l = StdVectorString();
contact_r.add('contactHeel_r');
contact_r.add('contactFront_r');
contact_l.add('contactHeel_l');
contact_l.add('contactFront_l');

% Create a conventional ground reaction forces file by summing the contact
% forces of contact spheres on each foot.
% For details, navigate to
% User Guide > Utilities > Model and trajectory utilities
% in the Moco Documentation.
% % 
externalForcesTableFlat = opensimMoco.createExternalLoadsTableForGait(model, LiftPredictionSolution,contact_r, contact_l);
opensimMoco.writeTableToFile(externalForcesTableFlat,'LiftPrediction_solutionGRF.sto');

I wrote the same contact as in example2DWalking

But now I'm thinking I withdrew the solver.setGuess(GaitTrackingSolution), because I didn't need anymore the tracking solution.
What I undertsood is that when we don't set an inital guess, Moco use a default one based on the bounds we defined previously.

Should I restaure the setGuess with 'bounds' as argument ?

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

Re: exampleSquatToStand- MocoPeriodicityGoal

Post by Nicholas Bianco » Wed Jun 17, 2020 11:32 am

You should remove this line:

Code: Select all

problem.setKinematicConstraintBounds(MocoBounds(0,0));
It does not set the initial and final speeds to zero, but rather sets the bounds on any kinematic constraints included in the model (e.g., CoordinateCoupler constraints dictating patellar motion). You can specify zero initial and final speeds for all coordinates using the setStateInfoPattern() interface:

Code: Select all

problem.setStateInfoPattern('/jointset/.*/speed', [], 0, 0);
Since you don't want the model to translate along the x-direction, you could consider tightening the bounds on the 'pelvis_tx' coordinate.

I'm still not sure what the issue is. You could consider constructing an initial guess that includes the initial and final lifting positions you would expect to see. You should try visualizing the model in the GUI to make sure that the initial pose you are specifying in the coordinates makes sense.

-Nick

User avatar
Mouaad BOUFADNA
Posts: 22
Joined: Mon Mar 16, 2020 3:24 am

Re: exampleSquatToStand- MocoPeriodicityGoal

Post by Mouaad BOUFADNA » Thu Jun 18, 2020 3:47 am

Ok I didn't undertand the setKinematicConstraints in that way

I removed it and replace by the StateInfoPattern but I didn't get a solution. The moco MocoCasADiSolver. failed.

So now the point is to config the initial guess in the solver
I wrote the inital guess as follows :

Code: Select all

%Create an initial guess from the defined bounds
guess=solver.createGuess('bounds'); % Use bounds as initial guess
guess.setTime(0,0.8);
guess.setState('/joinset/hip_l/hip_flexion_l/value',2, 10*pi/180);
guess.setState('/joinset/hip_r/hip_flexion_r/value',2, 10*pi/180);
guess.setState('/joinset/knee_l/knee_angle_l/value',-2, 0);
guess.setState('/joinset/knee_r/knee_angle_r/value',-2, 0);
guess.setState('/joinset/ankle_l/ankle_angle_l/value',0.5, 0);
guess.setState('/joinset/ankle_r/ankle_angle_r/value',0.5, 0);
guess.setState('jointset/lumbar/lumbar/value',-20*pi/180,10*pi/180);
solver.setGuess(guess);
I got this message

Code: Select all

Check for missing argument or incorrect argument data type in call to function 'setTime'.

Error in example2DWalking (line 122)
guess.setTime(0.6,0.8);
Even if I change the brackets type it' not working.

So I try to wrote guess.setTime([0,0.8]); and it returns another message

Code: Select all

Error using example2DWalking (line 122)
Java exception occurred:
java.lang.RuntimeException: Expected 101 times but got 2.
	Thrown at MocoTrajectory.cpp:156 in setTime().

	at org.opensim.modeling.opensimMocoJNI.MocoTrajectory_setTime(Native Method)

	at org.opensim.modeling.MocoTrajectory.setTime(MocoTrajectory.java:203)

	at org.opensim.modeling.MocoTrajectory.setTime(MocoTrajectory.java:42)

User avatar
Christopher Dembia
Posts: 506
Joined: Fri Oct 12, 2012 4:09 pm

Re: exampleSquatToStand- MocoPeriodicityGoal

Post by Christopher Dembia » Thu Jun 18, 2020 12:02 pm

The syntax `guess.setTime([0,0.8])` is correct but as the error message indicates, the guess has 101 time points and thus you must provide a vector containing 101 values. If you want to provide just 2 values, then first call `guess.resampleWithNumTimes(2)`.

https://github.com/opensim-org/opensim- ... #L152-L156

User avatar
Mouaad BOUFADNA
Posts: 22
Joined: Mon Mar 16, 2020 3:24 am

Re: exampleSquatToStand- MocoPeriodicityGoal

Post by Mouaad BOUFADNA » Mon Jun 22, 2020 8:26 am

Hi Nick

I resolved my problem. The pelvis_ty was to high to have the feet on the ground.
After seeing the model coordinates in the GUI, I reduced pelvis_ty interval of values and fixed initial/final values to the ones where the model has feet on the ground.

But during the motion, feet are moving from the ground and go in backward (see in attached images).
I tried by optimizing my angle values, but it failed.
I withdrew KinematicConstraint and initial/final velocity but still unchanged.


Chris, Thank you ! That seems now clever !
I tried again but now the Command window indicates that :

Code: Select all

Error using example2DWalking (line 125)
Java exception occurred:
java.lang.RuntimeException: Cannot find state named /joinset/hip_l/hip_flexion_l/value.
	Thrown at MocoTrajectory.cpp:169 in setState().

	at org.opensim.modeling.opensimMocoJNI.MocoTrajectory_setState(Native Method)

	at org.opensim.modeling.MocoTrajectory.setState(MocoTrajectory.java:207)

	at org.opensim.modeling.MocoTrajectory.setState(MocoTrajectory.java:48)
But I didn't changed the syntax of my states from the problem.StateInfo :

Code: Select all

%Create an initial guess from the defined bounds
guess=solver.createGuess('bounds'); % Use bounds as initial guess
guess.resampleWithNumTimes(2)
guess.setTime([0,0.8]);
guess.setState('/joinset/hip_l/hip_flexion_l/value',[2, 10*pi/180]);
guess.setState('/joinset/hip_r/hip_flexion_r/value',[2, 10*pi/180]);
guess.setState('/joinset/knee_l/knee_angle_l/value',[-2, 0]);
guess.setState('/joinset/knee_r/knee_angle_r/value',[-2, 0]);
guess.setState('/joinset/ankle_l/ankle_angle_l/value',[0.5, 0]);
guess.setState('/joinset/ankle_r/ankle_angle_r/value',[0.5, 0]);
guess.setState('jointset/lumbar/lumbar/value',[-20*pi/180,10*pi/180]);
solver.setGuess(guess);
Attachments
sim_lift_ascending 3.png
Ending motion
sim_lift_ascending 3.png (202.33 KiB) Viewed 938 times
sim_lift_ascending 2.jpg
Feet moving backward
sim_lift_ascending 2.jpg (98.72 KiB) Viewed 938 times
sim_lift_ascending 1.jpg
Start of the motion
sim_lift_ascending 1.jpg (87.22 KiB) Viewed 938 times

POST REPLY