This is an example using the MocoInverse tool with a complex model to prescribe walking. This example also shows how to track electromyography data.
26 import opensim
as osim
28 def solveMocoInverse():
31 inverse = osim.MocoInverse()
37 modelProcessor = osim.ModelProcessor(
'subject_walk_armless.osim')
38 modelProcessor.append(osim.ModOpAddExternalLoads(
'grf_walk.xml'))
39 modelProcessor.append(osim.ModOpIgnoreTendonCompliance())
40 modelProcessor.append(osim.ModOpReplaceMusclesWithDeGrooteFregly2016())
42 modelProcessor.append(osim.ModOpIgnorePassiveFiberForcesDGF())
44 modelProcessor.append(osim.ModOpScaleActiveFiberForceCurveWidthDGF(1.5))
45 modelProcessor.append(osim.ModOpAddReserves(1.0))
46 inverse.setModel(modelProcessor)
53 inverse.setKinematics(osim.TableProcessor(
'coordinates.sto'))
56 inverse.set_initial_time(0.81)
57 inverse.set_final_time(1.79)
58 inverse.set_mesh_interval(0.02)
62 inverse.set_kinematics_allow_extra_columns(
True)
65 solution = inverse.solve()
66 solution.getMocoSolution().write(
'example3DWalking_MocoInverse_solution.sto')
69 model = modelProcessor.process()
70 report = osim.report.Report(model,
71 'example3DWalking_MocoInverse_solution.sto',
76 def solveMocoInverseWithEMG():
79 inverse = osim.MocoInverse()
80 modelProcessor = osim.ModelProcessor(
'subject_walk_armless.osim')
81 modelProcessor.append(osim.ModOpAddExternalLoads(
'grf_walk.xml'))
82 modelProcessor.append(osim.ModOpIgnoreTendonCompliance())
83 modelProcessor.append(osim.ModOpReplaceMusclesWithDeGrooteFregly2016())
84 modelProcessor.append(osim.ModOpIgnorePassiveFiberForcesDGF())
85 modelProcessor.append(osim.ModOpScaleActiveFiberForceCurveWidthDGF(1.5))
86 modelProcessor.append(osim.ModOpAddReserves(1.0))
87 inverse.setModel(modelProcessor)
88 inverse.setKinematics(osim.TableProcessor(
'coordinates.sto'))
89 inverse.set_initial_time(0.81)
90 inverse.set_final_time(1.79)
91 inverse.set_mesh_interval(0.02)
92 inverse.set_kinematics_allow_extra_columns(
True)
94 study = inverse.initialize()
95 problem = study.updProblem()
98 emgTracking = osim.MocoControlTrackingGoal(
'emg_tracking')
99 emgTracking.setWeight(50.0)
102 controlsRef = osim.TimeSeriesTable(
'electromyography.sto')
106 soleus = controlsRef.updDependentColumn(
'soleus')
107 gasmed = controlsRef.updDependentColumn(
'gastrocnemius')
108 tibant = controlsRef.updDependentColumn(
'tibialis_anterior')
109 for t
in range(0, controlsRef.getNumRows()):
110 soleus[t] = 0.77 * soleus[t]
111 gasmed[t] = 0.87 * gasmed[t]
112 tibant[t] = 0.37 * tibant[t]
113 emgTracking.setReference(osim.TableProcessor(controlsRef))
115 emgTracking.setReferenceLabel(
'/forceset/soleus_r',
'soleus')
116 emgTracking.setReferenceLabel(
'/forceset/gasmed_r',
'gastrocnemius')
117 emgTracking.setReferenceLabel(
'/forceset/gaslat_r',
'gastrocnemius')
118 emgTracking.setReferenceLabel(
'/forceset/tibant_r',
'tibialis_anterior')
119 problem.addGoal(emgTracking)
122 solution = study.solve()
123 solution.write(
'example3DWalking_MocoInverseWithEMG_solution.sto')
126 controlsRef.removeColumn(
'medial_hamstrings')
127 controlsRef.removeColumn(
'biceps_femoris')
128 controlsRef.removeColumn(
'vastus_lateralis')
129 controlsRef.removeColumn(
'vastus_medius')
130 controlsRef.removeColumn(
'rectus_femoris')
131 controlsRef.removeColumn(
'gluteus_maximus')
132 controlsRef.removeColumn(
'gluteus_medius')
133 controlsRef.setColumnLabels([
'/forceset/soleus_r',
'/forceset/gasmed_r',
134 '/forceset/tibant_r'])
135 controlsRef.appendColumn(
'/forceset/gaslat_r', gasmed)
136 osim.STOFileAdapter.write(controlsRef,
'controls_reference.sto')
140 model = modelProcessor.process()
141 output =
'example3DWalking_MocoInverseWithEMG_report.pdf' 143 'example3DWalking_MocoInverseWithEMG_solution.sto',
144 'controls_reference.sto']
145 report = osim.report.Report(model,
146 'example3DWalking_MocoInverse_solution.sto',
147 output=output, bilateral=
True,
158 solveMocoInverseWithEMG()