OpenSim Moco  0.2.0
exampleMocoInverse.py

This is an example using the MocoInverse tool with a complex model to prescribe walking.

1 # -------------------------------------------------------------------------- #
2 # OpenSim Moco: exampleMocoInverse.py #
3 # -------------------------------------------------------------------------- #
4 # Copyright (c) 2019 Stanford University and the Authors #
5 # #
6 # Author(s): Christopher Dembia #
7 # #
8 # Licensed under the Apache License, Version 2.0 (the "License") you may #
9 # not use this file except in compliance with the License. You may obtain a #
10 # copy of the License at http://www.apache.org/licenses/LICENSE-2.0 #
11 # #
12 # Unless required by applicable law or agreed to in writing, software #
13 # distributed under the License is distributed on an "AS IS" BASIS, #
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 # See the License for the specific language governing permissions and #
16 # limitations under the License. #
17 # -------------------------------------------------------------------------- #
18 
19 # This example shows how to use the MocoInverse tool to exactly prescribe a
20 # motion and estimate muscle behavior for walking.
21 # This problem solves in about 5 minutes.
22 #
23 # See the README.txt next to this file for more information.
24 
25 import opensim as osim
26 
27 # Construct the MocoInverse tool.
28 inverse = osim.MocoInverse()
29 
30 # Construct a ModelProcessor and set it on the tool. The default
31 # muscles in the model are replaced with optimization-friendly
32 # DeGrooteFregly2016Muscles, and adjustments are made to the default muscle
33 # parameters.
34 modelProcessor = osim.ModelProcessor('subject_walk_armless.osim')
35 modelProcessor.append(osim.ModOpAddExternalLoads('grf_walk.xml'))
36 modelProcessor.append(osim.ModOpIgnoreTendonCompliance())
37 modelProcessor.append(osim.ModOpReplaceMusclesWithDeGrooteFregly2016())
38 # Only valid for DeGrooteFregly2016Muscles.
39 modelProcessor.append(osim.ModOpIgnorePassiveFiberForcesDGF())
40 # Only valid for DeGrooteFregly2016Muscles.
41 modelProcessor.append(osim.ModOpScaleActiveFiberForceCurveWidthDGF(1.5))
42 modelProcessor.append(osim.ModOpAddReserves(1.0))
43 inverse.setModel(modelProcessor)
44 
45 # Construct a TableProcessor of the coordinate data and pass it to the
46 # inverse tool. TableProcessors can be used in the same way as
47 # ModelProcessors by appending TableOperators to modify the base table.
48 # A TableProcessor with no operators, as we have here, simply returns the
49 # base table.
50 inverse.setKinematics(osim.TableProcessor('coordinates.sto'))
51 
52 # Initial time, final time, and mesh interval.
53 inverse.set_initial_time(0.81)
54 inverse.set_final_time(1.79)
55 inverse.set_mesh_interval(0.02)
56 
57 # By default, Moco gives an error if the kinematics contains extra columns.
58 # Here, we tell Moco to allow (and ignore) those extra columns.
59 inverse.set_kinematics_allow_extra_columns(True)
60 
61 # Solve the problem and write the solution to a Storage file.
62 solution = inverse.solve()
63 solution.getMocoSolution().write('example3DWalking_MocoInverse_solution.sto')
64 
65 # Generate a PDF with plots for the solution trajectory.
66 model = modelProcessor.process()
67 report = osim.Report(model, 'example3DWalking_MocoInverse_solution.sto',
68  bilateral=True)
69 # The PDF is saved to the working directory.
70 report.generate()