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