This is an example using the MocoInverse tool with a complex model to prescribe walking. This example also shows how to track electromyography data.
30 import opensim
as osim
32 def solveMocoInverse():
35 inverse = osim.MocoInverse()
41 modelProcessor = osim.ModelProcessor(
'subject_walk_scaled.osim')
42 modelProcessor.append(osim.ModOpAddExternalLoads(
'grf_walk.xml'))
43 modelProcessor.append(osim.ModOpIgnoreTendonCompliance())
44 modelProcessor.append(osim.ModOpReplaceMusclesWithDeGrooteFregly2016())
46 modelProcessor.append(osim.ModOpIgnorePassiveFiberForcesDGF())
48 modelProcessor.append(osim.ModOpScaleActiveFiberForceCurveWidthDGF(1.5))
54 modelProcessor.append(osim.ModOpReplacePathsWithFunctionBasedPaths(
55 "subject_walk_scaled_FunctionBasedPathSet.xml"))
56 modelProcessor.append(osim.ModOpAddReserves(1.0))
57 inverse.setModel(modelProcessor)
64 inverse.setKinematics(osim.TableProcessor(
'coordinates.sto'))
67 inverse.set_initial_time(0.48)
68 inverse.set_final_time(1.61)
69 inverse.set_mesh_interval(0.02)
73 inverse.set_kinematics_allow_extra_columns(
True)
76 solution = inverse.solve()
77 solution.getMocoSolution().write(
'example3DWalking_MocoInverse_solution.sto')
80 model = modelProcessor.process()
81 report = osim.report.Report(model,
82 'example3DWalking_MocoInverse_solution.sto',
87 def solveMocoInverseWithEMG():
90 inverse = osim.MocoInverse()
91 modelProcessor = osim.ModelProcessor(
'subject_walk_scaled.osim')
92 modelProcessor.append(osim.ModOpAddExternalLoads(
'grf_walk.xml'))
93 modelProcessor.append(osim.ModOpIgnoreTendonCompliance())
94 modelProcessor.append(osim.ModOpReplaceMusclesWithDeGrooteFregly2016())
95 modelProcessor.append(osim.ModOpIgnorePassiveFiberForcesDGF())
96 modelProcessor.append(osim.ModOpScaleActiveFiberForceCurveWidthDGF(1.5))
97 modelProcessor.append(osim.ModOpReplacePathsWithFunctionBasedPaths(
98 "subject_walk_scaled_FunctionBasedPathSet.xml"))
99 modelProcessor.append(osim.ModOpAddReserves(1.0))
100 inverse.setModel(modelProcessor)
101 inverse.setKinematics(osim.TableProcessor(
'coordinates.sto'))
102 inverse.set_initial_time(0.48)
103 inverse.set_final_time(1.61)
104 inverse.set_mesh_interval(0.02)
105 inverse.set_kinematics_allow_extra_columns(
True)
107 study = inverse.initialize()
108 problem = study.updProblem()
111 emgTracking = osim.MocoControlTrackingGoal(
'emg_tracking')
112 emgTracking.setWeight(50.0)
115 controlsRef = osim.TimeSeriesTable(
'electromyography.sto')
119 soleus = controlsRef.updDependentColumn(
'soleus')
120 gasmed = controlsRef.updDependentColumn(
'gastrocnemius')
121 tibant = controlsRef.updDependentColumn(
'tibialis_anterior')
122 for t
in range(0, controlsRef.getNumRows()):
123 soleus[t] = 0.77 * soleus[t]
124 gasmed[t] = 0.87 * gasmed[t]
125 tibant[t] = 0.37 * tibant[t]
126 emgTracking.setReference(osim.TableProcessor(controlsRef))
128 emgTracking.setReferenceLabel(
'/forceset/soleus_r',
'soleus')
129 emgTracking.setReferenceLabel(
'/forceset/gasmed_r',
'gastrocnemius')
130 emgTracking.setReferenceLabel(
'/forceset/gaslat_r',
'gastrocnemius')
131 emgTracking.setReferenceLabel(
'/forceset/tibant_r',
'tibialis_anterior')
132 problem.addGoal(emgTracking)
135 solution = study.solve()
136 solution.write(
'example3DWalking_MocoInverseWithEMG_solution.sto')
139 controlsRef.removeColumn(
'medial_hamstrings')
140 controlsRef.removeColumn(
'biceps_femoris')
141 controlsRef.removeColumn(
'vastus_lateralis')
142 controlsRef.removeColumn(
'vastus_medius')
143 controlsRef.removeColumn(
'rectus_femoris')
144 controlsRef.removeColumn(
'gluteus_maximus')
145 controlsRef.removeColumn(
'gluteus_medius')
146 controlsRef.setColumnLabels([
'/forceset/soleus_r',
'/forceset/gasmed_r',
147 '/forceset/tibant_r'])
148 controlsRef.appendColumn(
'/forceset/gaslat_r', gasmed)
149 osim.STOFileAdapter.write(controlsRef,
'controls_reference.sto')
153 model = modelProcessor.process()
154 output =
'example3DWalking_MocoInverseWithEMG_report.pdf' 156 'controls_reference.sto',
157 'example3DWalking_MocoInverseWithEMG_solution.sto']
158 report = osim.report.Report(model,
159 'example3DWalking_MocoInverse_solution.sto',
160 output=output, bilateral=
True,
162 colors=[
'black',
'blue',
'red'])
172 solveMocoInverseWithEMG()