exampleMocoTrack (MATLAB)

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
Vishal R
Posts: 47
Joined: Wed Oct 29, 2014 4:20 pm

exampleMocoTrack (MATLAB)

Post by Vishal R » Mon Dec 02, 2019 5:56 am

Dear Moco Team,

I was successful in running slidingMass example in MATLAB.
I have been trying to run the exampleMocoTrack example using MATLAB2018a in Windows 10. I am getting the following error while executing `muscleDrivenStateTracking()`

the err.log shows these:

Code: Select all

Warning: intermediate_callback is disfunctional in your installation. You will only be able to use stats(). See https://github.com/casadi/casadi/wiki/enableIpoptCallback to enable it.
CasADi - 2019-12-02 12:36:39 WARNING("nlp:nlp_jac_g failed: NaN detected for output jac_g_x, at nonzero index 94 (row 94, col 0).") [C:\projects\opensim-moco\dependencies\casadi\casadi\core\oracle_function.cpp:265]
CasADi - 2019-12-02 12:36:47 WARNING("nlp:nlp_jac_g failed: NaN detected for output jac_g_x, at nonzero index 94 (row 94, col 0).") [C:\projects\opensim-moco\dependencies\casadi\casadi\core\oracle_function.cpp:265]
MocoCasADiSolver did NOT succeed:
  Invalid_Number_Detected
First, I am not able to understand where does this path for casadi is taken from in the error log. I have installed casadi and linked it up with MATLAB as shown in casadi website

Second, I could see the visualization for `torqueDrivenMarkerTracking()` without any problem. While executing `muscleDrivenStateTracking()` I get a java exception in the command window and the err.log as shown above.

Code: Select all

...
Path constraints: none
-------------------------------------------------------------------------------
Elapsed real time: 25 second(s).
12/02/19 13:53:12
MocoCasADiSolver did NOT succeed:
  Invalid_Number_Detected
===============================================================================
Error using exampleMocoTrack>muscleDrivenStateTracking (line 194)
Java exception occurred:
java.lang.RuntimeException: This iterate is sealed, to force you to acknowledge the solver failed; call unseal() to gain access.
	Thrown at MocoTrajectory.cpp:1302 in ensureUnsealed().

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

	at org.opensim.modeling.MocoStudy.visualize(MocoStudy.java:129)


Error in exampleMocoTrack (line 54)
muscleDrivenStateTracking();
Any idea on how to rectify this?
Thank you :)

Vishal

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

Re: exampleMocoTrack (MATLAB)

Post by Nicholas Bianco » Mon Dec 02, 2019 10:28 am

Hi Vishal,

Thanks for trying out Moco! This issue is caused by the problem not handling tendon compliance properly -- a feature we added recently but forgot to update in the example. To fix it, you can update the ModelProcessor in the muscle-driven example to ignore tendon compliance. To do that, add the following line somewhere between lines 138 and 146:

Code: Select all

 modelProcessor.append(ModOpIgnoreTendonCompliance()); 
This issue will be fixed for both Matlab and Python examples in the next release.

-Nick

User avatar
Vishal R
Posts: 47
Joined: Wed Oct 29, 2014 4:20 pm

Re: exampleMocoTrack (MATLAB)

Post by Vishal R » Tue Dec 03, 2019 11:55 am

Thanks a lot, Nick! It worked :)

User avatar
Ricky Pimentel
Posts: 7
Joined: Wed Aug 22, 2012 10:08 am

Re: exampleMocoTrack (MATLAB)

Post by Ricky Pimentel » Thu Dec 12, 2019 12:23 pm

Thanks OpenSim&Moco teams for putting this tool out for us!

I have successfully ran the sliding mass and 2DWalk (from Brian Umberger) examples in Matlab. I'm having an issue running exampleMocoTrack in Matlab. This happened before and after adding in what you fixed for Vishal. The error message is:

Error using exampleMocoTrack>muscleDrivenStateTracking (line 173)
Java exception occurred:
java.lang.RuntimeException: Property 'num_mesh_intervals' (in MocoCasADiSolver) has invalid value -1;
expected value to be in range 0,2147483647, or one of the following:.
Thrown at MocoUtilities.h:591 in checkPropertyInRangeOrSet().

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

at org.opensim.modeling.MocoTrack.initialize(MocoTrack.java:549)


Error in exampleMocoTrack (line 54)
muscleDrivenStateTracking();


I believe the mesh interval is set on line 168(track.set_mesh_interval), but changing the number doesn't work and there appears to be no class "num_mesh_intervals" for track. Just like Vishal's issue, the torquDrivenMarkerTracking portion ran fine, and the error came in the muscleDrivenStateTracking. Any ideas?

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

Re: exampleMocoTrack (MATLAB)

Post by Christopher Dembia » Thu Dec 12, 2019 12:36 pm

Thank you, Richard. It's excellent that you were able to run Brian's example.

Correct, MocoTrack has `track.set_mesh_interval()`, and the MocoSolver has `solver.set_num_mesh_intervals()`. These are different. The first should be a time in seconds, while the latter should be a positive integer.

If you share some code, we may be able to help you more.

User avatar
Ricky Pimentel
Posts: 7
Joined: Wed Aug 22, 2012 10:08 am

Re: exampleMocoTrack (MATLAB)

Post by Ricky Pimentel » Fri Dec 13, 2019 7:26 am

Wow, props on the quick response!

I don't see any solver.set_num_mesh_intervals() in there. The track setting mesh interval is on line 168, and the error occurs right after on line 173. Here's the Matlab code:

Code: Select all

% -------------------------------------------------------------------------- %
% OpenSim Moco: exampleMocoTrack.m                                           %
% -------------------------------------------------------------------------- %
% Copyright (c) 2019 Stanford University and the Authors                     %
%                                                                            %
% Author(s): Nicholas Bianco                                                 %
%                                                                            %
% Licensed under the Apache License, Version 2.0 (the "License"); you may    %
% not use this file except in compliance with the License. You may obtain a  %
% copy of the License at http://www.apache.org/licenses/LICENSE-2.0          %
%                                                                            %
% Unless required by applicable law or agreed to in writing, software        %
% distributed under the License is distributed on an "AS IS" BASIS,          %
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
% See the License for the specific language governing permissions and        %
% limitations under the License.                                             %
% -------------------------------------------------------------------------- %

% This example features two different tracking problems solved using the
% MocoTrack tool. 
%  - The first problem demonstrates the basic usage of the tool interface
%    to solve a torque-driven marker tracking problem. 
%  - The second problem shows how to customize a muscle-driven state tracking 
%    problem using more advanced features of the tool interface.
% 
% Data and model source: https://simtk.org/projects/full_body
% 
% Model
% -----
% The model described in the file 'subject_walk_armless.osim' included in this
% file is a modified version of the Rajagopoal et al. 2016 musculoskeletal 
% model. The lumbar, subtalar, and mtp coordinates have been replaced with
% WeldJoints and residual actuators have been added to the pelvis (1 N-m for
% rotational coordinates and 10 N for translational coordinates). Finally, the
% arms and all associated components have been removed for simplicity.
% 
% Data
% ----
% The coordinate and marker data included in the 'coordinates.sto' and 
% 'marker_trajectories.trc' files also come from the Rajagopal et al. 2016
% model distribution. The coordinates were computed using inverse kinematics
% and modified via the Residual Reduction Algorithm (RRA). 

function exampleMocoTrack()

% Solve the torque-driven marker tracking problem.
% This problem takes a few minutes to solve.
torqueDrivenMarkerTracking();

% Solve the muscle-driven state tracking problem.
% This problem could take an hour or more to solve, depending on the number of
% processor cores available for parallelization. With 12 cores, it takes around
% 25 minutes.
muscleDrivenStateTracking();

end

function torqueDrivenMarkerTracking()

import org.opensim.modeling.*;

% Create and name an instance of the MocoTrack tool.
track = MocoTrack();
track.setName("torque_driven_marker_tracking");

% Construct a ModelProcessor and add it to the tool. ModelProcessors
% accept a base model and allow you to easily modify the model by appending
% ModelOperators. Operations are performed in the order that they are
% appended to the model.
% Create the base Model by passing in the model file.
modelProcessor = ModelProcessor("subject_walk_armless.osim");
% Add ground reaction external loads in lieu of a ground-contact model.
modelProcessor.append(ModOpAddExternalLoads("grf_walk.xml"));
% Remove all the muscles in the model's ForceSet.
modelProcessor.append(ModOpRemoveMuscles());
% Add CoordinateActuators to the model degrees-of-freedom. This
% ignores the pelvis coordinates which already have residual 
% CoordinateActuators.
modelProcessor.append(ModOpAddReserves(250));
track.setModel(modelProcessor);

% Use this convenience function to set the MocoTrack markers reference
% directly from a TRC file. By default, the markers data is filtered at
% 6 Hz and if in millimeters, converted to meters.
track.setMarkersReferenceFromTRC("marker_trajectories.trc");

% There is marker data in the 'marker_trajectories.trc' associated with
% model markers that no longer exists (i.e. markers on the arms). Set this
% flag to avoid an exception from being thrown.
track.set_allow_unused_references(true);

% Increase the global marker tracking weight, which is the weight
% associated with the internal MocoMarkerTrackingGoal term.
track.set_markers_global_tracking_weight(10);

% Increase the tracking weights for individual markers in the data set 
% placed on bony landmarks compared to markers located on soft tissue.
markerWeights = MocoWeightSet();
markerWeights.cloneAndAppend(MocoWeight("R.ASIS", 20));
markerWeights.cloneAndAppend(MocoWeight("L.ASIS", 20));
markerWeights.cloneAndAppend(MocoWeight("R.PSIS", 20));
markerWeights.cloneAndAppend(MocoWeight("L.PSIS", 20));
markerWeights.cloneAndAppend(MocoWeight("R.Knee", 10));
markerWeights.cloneAndAppend(MocoWeight("R.Ankle", 10));
markerWeights.cloneAndAppend(MocoWeight("R.Heel", 10));
markerWeights.cloneAndAppend(MocoWeight("R.MT5", 5));
markerWeights.cloneAndAppend(MocoWeight("R.Toe", 2));
markerWeights.cloneAndAppend(MocoWeight("L.Knee", 10));
markerWeights.cloneAndAppend(MocoWeight("L.Ankle", 10));
markerWeights.cloneAndAppend(MocoWeight("L.Heel", 10));
markerWeights.cloneAndAppend(MocoWeight("L.MT5", 5));
markerWeights.cloneAndAppend(MocoWeight("L.Toe", 2));
track.set_markers_weight_set(markerWeights);

% Initial time, final time, and mesh interval. The number of mesh points
% used to discretize the problem is computed internally using these values.
track.set_initial_time(0.81);
track.set_final_time(1.65);
track.set_mesh_interval(0.05);

% Solve! The boolean argument indicates to visualize the solution.
solution = track.solve(true);

end

function muscleDrivenStateTracking()

import org.opensim.modeling.*;

% Create and name an instance of the MocoTrack tool. 
track = MocoTrack();
track.setName("muscle_driven_state_tracking");

% Construct a ModelProcessor and set it on the tool. The default
% muscles in the model are replaced with optimization-friendly
% DeGrooteFregly2016Muscles, and adjustments are made to the default muscle
% parameters.
modelProcessor = ModelProcessor("subject_walk_armless.osim");
modelProcessor.append(ModOpAddExternalLoads("grf_walk.xml"));
modelProcessor.append(ModOpReplaceMusclesWithDeGrooteFregly2016());
% Only valid for DeGrooteFregly2016Muscles.
modelProcessor.append(ModOpIgnorePassiveFiberForcesDGF());
% Only valid for DeGrooteFregly2016Muscles.
modelProcessor.append(ModOpScaleActiveFiberForceCurveWidthDGF(1.5));
track.setModel(modelProcessor);
modelProcessor.append(ModOpIgnoreTendonCompliance());

% Construct a TableProcessor of the coordinate data and pass it to the 
% tracking tool. TableProcessors can be used in the same way as
% ModelProcessors by appending TableOperators to modify the base table.
% A TableProcessor with no operators, as we have here, simply returns the
% base table.track.setStatesReference(TableProcessor("coordinates.sto"));
track.set_states_global_tracking_weight(10);


% This setting allows extra data columns contained in the states
% reference that don't correspond to model coordinates.
track.set_allow_unused_references(true);

% Since there is only coordinate position data the states references, this
% setting is enabled to fill in the missing coordinate speed data using
% the derivative of splined position data.
track.set_track_reference_position_derivatives(true);

% Initial time, final time, and mesh interval.
track.set_initial_time(0.81);
track.set_final_time(1.65);
track.set_mesh_interval(0.08);

% Instead of calling solve(), call initialize() to receive a pre-configured
% MocoStudy object based on the settings above. Use this to customize the
% problem beyond the MocoTrack interface.
study = track.initialize();

% Get a reference to the MocoControlGoal that is added to every MocoTrack
% problem by default.
problem = study.updProblem();
effort = MocoControlGoal.safeDownCast(problem.updGoal("control_effort"));

% Put a large weight on the pelvis CoordinateActuators, which act as the
% residual, or 'hand-of-god', forces which we would like to keep as small
% as possible.
model = modelProcessor.process();
model.initSystem();
forceSet = model.getForceSet();
for i = 0:forceSet.getSize()-1
   forcePath = forceSet.get(i).getAbsolutePathString();
   if contains(string(forcePath), 'pelvis')
       effort.setWeightForControl(forcePath, 10);
   end
end

% Solve and visualize.
solution = study.solve();
study.visualize(solution);

end

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

Re: exampleMocoTrack (MATLAB)

Post by Christopher Dembia » Fri Dec 13, 2019 12:05 pm

So, MocoTrack uses the provided `mesh_interval` to compute a number of mesh intervals to pass (internally) to the MocoSolver (inside of MocoTrack). And perhaps there is a bug in this calculation.

Can you try different values for MocoTrack's `mesh_interval`?

POST REPLY