This is an example that uses the MocoInverse tool and EMG data to create an EMG-driven simulation of walking.
3import exampleEMGTracking_helpers
as helpers
16model = helpers.getWalkingModel()
19inverse = osim.MocoInverse()
20inverse.setModel(model)
24coordinates = osim.TableProcessor(
'coordinates.mot')
25coordinates.append(osim.TabOpLowPassFilter(6))
26coordinates.append(osim.TabOpUseAbsoluteStateNames())
30inverse.setKinematics(coordinates)
31inverse.set_kinematics_allow_extra_columns(
True)
35inverse.set_initial_time(0.83)
36inverse.set_final_time(2.0)
37inverse.set_mesh_interval(0.04)
38inverse.set_constraint_tolerance(1e-3)
39inverse.set_convergence_tolerance(1e-3)
41if not os.path.isfile(
'effortSolution.sto'):
43 inverseSolution = inverse.solve()
44 solution = inverseSolution.getMocoSolution()
45 solution.write(
'effortSolution.sto')
52emgReference = osim.TimeSeriesTable(
'emg.sto')
53helpers.compareSolutionToEMG(emgReference,
'effortSolution.sto')
62study = inverse.initialize()
63problem = study.updProblem()
68tracking = osim.MocoControlTrackingGoal(
'emg_tracking')
70tracking.setReference(osim.TableProcessor(emgReference))
71tracking.setReferenceLabel(
'/forceset/gasmed_l',
'gastrocnemius')
72tracking.setReferenceLabel(
'/forceset/tibant_l',
'tibialis_anterior')
73tracking.setReferenceLabel(
'/forceset/bfsh_l',
'biceps_femoris')
74tracking.setReferenceLabel(
'/forceset/glmax2_l',
'gluteus')
81tracking.addScaleFactor(
'gastroc_factor',
'/forceset/gasmed_l', [0.01, 1.0])
82tracking.addScaleFactor(
'tibant_factor',
'/forceset/tibant_l', [0.01, 1.0])
83tracking.addScaleFactor(
'bifem_factor',
'/forceset/bfsh_l', [0.01, 1.0])
84tracking.addScaleFactor(
'gluteus_factor',
'/forceset/glmax2_l', [0.01, 1.0])
87problem.addGoal(tracking)
91solver = osim.MocoCasADiSolver.safeDownCast(study.updSolver())
92solver.resetProblem(problem)
97solver.set_parameters_require_initsystem(
False)
99if not os.path.isfile(
'trackingSolution.sto'):
101 solution = study.solve()
102 solution.write(
'trackingSolution.sto')
105trackingSolution = osim.MocoTrajectory(
'trackingSolution.sto')
106gastroc_factor = trackingSolution.getParameter(
'gastroc_factor')
107tibant_factor = trackingSolution.getParameter(
'tibant_factor')
108bifem_factor = trackingSolution.getParameter(
'bifem_factor')
109gluteus_factor = trackingSolution.getParameter(
'gluteus_factor')
113print(
'\nOptimized scale factor values:')
114print(
'------------------------------')
115print(
'gastrocnemius = ' + str(gastroc_factor))
116print(
'tibialis anterior = ' + str(tibant_factor))
117print(
'biceps femoris short head = ' + str(bifem_factor))
118print(
'gluteus = ' + str(gluteus_factor))
121gastroc = emgReference.updDependentColumn(
'gastrocnemius')
122tibant = emgReference.updDependentColumn(
'tibialis_anterior')
123bifem = emgReference.updDependentColumn(
'biceps_femoris')
124gluteus = emgReference.updDependentColumn(
'gluteus')
125for t
in np.arange(emgReference.getNumRows()):
127 gastroc[t] = gastroc_factor * gastroc[t]
128 tibant[t] = tibant_factor * tibant[t]
129 bifem[t] = bifem_factor * bifem[t]
130 gluteus[t] = gluteus_factor * gluteus[t]
134helpers.compareSolutionToEMG(emgReference,
'effortSolution.sto',
135 'trackingSolution.sto')