This is an example using the MocoTrack tool with a complex model to track walking.
31 import opensim
as osim
33 def torqueDrivenMarkerTracking():
36 track = osim.MocoTrack()
37 track.setName(
"torque_driven_marker_tracking")
44 modelProcessor = osim.ModelProcessor(
"subject_walk_scaled.osim")
46 modelProcessor.append(osim.ModOpAddExternalLoads(
"grf_walk.xml"))
48 modelProcessor.append(osim.ModOpRemoveMuscles())
51 modelProcessor.append(osim.ModOpAddReserves(250.0, 1.0))
52 track.setModel(modelProcessor)
57 track.setMarkersReferenceFromTRC(
"marker_trajectories.trc")
62 track.set_allow_unused_references(
True)
66 track.set_markers_global_tracking_weight(10)
70 markerWeights = osim.MocoWeightSet()
71 markerWeights.cloneAndAppend(osim.MocoWeight(
"R.ASIS", 20))
72 markerWeights.cloneAndAppend(osim.MocoWeight(
"L.ASIS", 20))
73 markerWeights.cloneAndAppend(osim.MocoWeight(
"R.PSIS", 20))
74 markerWeights.cloneAndAppend(osim.MocoWeight(
"L.PSIS", 20))
75 markerWeights.cloneAndAppend(osim.MocoWeight(
"R.Knee", 10))
76 markerWeights.cloneAndAppend(osim.MocoWeight(
"R.Ankle", 10))
77 markerWeights.cloneAndAppend(osim.MocoWeight(
"R.Heel", 10))
78 markerWeights.cloneAndAppend(osim.MocoWeight(
"R.MT5", 5))
79 markerWeights.cloneAndAppend(osim.MocoWeight(
"R.Toe", 2))
80 markerWeights.cloneAndAppend(osim.MocoWeight(
"L.Knee", 10))
81 markerWeights.cloneAndAppend(osim.MocoWeight(
"L.Ankle", 10))
82 markerWeights.cloneAndAppend(osim.MocoWeight(
"L.Heel", 10))
83 markerWeights.cloneAndAppend(osim.MocoWeight(
"L.MT5", 5))
84 markerWeights.cloneAndAppend(osim.MocoWeight(
"L.Toe", 2))
85 track.set_markers_weight_set(markerWeights)
89 track.set_initial_time(0.48)
90 track.set_final_time(1.61)
91 track.set_mesh_interval(0.02)
94 solution = track.solveAndVisualize()
96 def muscleDrivenStateTracking():
99 track = osim.MocoTrack()
100 track.setName(
"muscle_driven_state_tracking")
106 modelProcessor = osim.ModelProcessor(
"subject_walk_scaled.osim")
107 modelProcessor.append(osim.ModOpAddExternalLoads(
"grf_walk.xml"))
108 modelProcessor.append(osim.ModOpIgnoreTendonCompliance())
109 modelProcessor.append(osim.ModOpReplaceMusclesWithDeGrooteFregly2016())
111 modelProcessor.append(osim.ModOpIgnorePassiveFiberForcesDGF())
113 modelProcessor.append(osim.ModOpScaleActiveFiberForceCurveWidthDGF(1.5))
119 modelProcessor.append(osim.ModOpReplacePathsWithFunctionBasedPaths(
120 "subject_walk_scaled_FunctionBasedPathSet.xml"))
121 track.setModel(modelProcessor)
128 track.setStatesReference(osim.TableProcessor(
"coordinates.sto"))
132 track.set_allow_unused_references(
True)
137 track.set_track_reference_position_derivatives(
True)
140 track.set_initial_time(0.48)
141 track.set_final_time(1.61)
142 track.set_mesh_interval(0.02)
147 study = track.initialize()
151 problem = study.updProblem()
152 effort = osim.MocoControlGoal.safeDownCast(problem.updGoal(
"control_effort"))
153 effort.setWeight(0.1)
158 model = modelProcessor.process()
160 forceSet = model.getForceSet()
161 for i
in range(forceSet.getSize()):
162 forcePath = forceSet.get(i).getAbsolutePathString()
163 if 'pelvis' in str(forcePath):
164 effort.setWeightForControl(forcePath, 10)
167 periodicityGoal = osim.MocoPeriodicityGoal(
"periodicity")
168 for i
in range(model.getNumStateVariables()):
169 currentStateName = str(model.getStateVariableNames().getitem(i))
170 if 'pelvis_tx/value' not in currentStateName:
171 periodicityGoal.addStatePair(osim.MocoPeriodicityGoalPair(currentStateName))
173 forceSet = model.getForceSet()
174 for i
in range(forceSet.getSize()):
175 forcePath = forceSet.get(i).getAbsolutePathString()
176 periodicityGoal.addControlPair(osim.MocoPeriodicityGoalPair(forcePath))
178 problem.addGoal(periodicityGoal)
181 solver = osim.MocoCasADiSolver.safeDownCast(study.updSolver())
182 solver.set_optim_convergence_tolerance(1e-3)
183 solver.set_optim_constraint_tolerance(1e-4)
184 solver.resetProblem(problem)
187 solution = study.solve()
188 solution.write(
'exampleMocoTrack_state_tracking_solution.sto')
191 study.visualize(solution)
193 def muscleDrivenJointMomentTracking():
196 track = osim.MocoTrack()
197 track.setName(
'muscle_driven_joint_moment_tracking')
200 modelProcessor = osim.ModelProcessor(
'subject_walk_scaled.osim')
201 modelProcessor.append(osim.ModOpAddExternalLoads(
'grf_walk.xml'))
202 modelProcessor.append(osim.ModOpIgnoreTendonCompliance())
203 modelProcessor.append(osim.ModOpReplaceMusclesWithDeGrooteFregly2016())
204 modelProcessor.append(osim.ModOpIgnorePassiveFiberForcesDGF())
205 modelProcessor.append(osim.ModOpScaleActiveFiberForceCurveWidthDGF(1.5))
206 modelProcessor.append(osim.ModOpReplacePathsWithFunctionBasedPaths(
207 'subject_walk_scaled_FunctionBasedPathSet.xml'))
208 track.setModel(modelProcessor)
211 track.setStatesReference(osim.TableProcessor(
'coordinates.sto'))
212 track.set_states_global_tracking_weight(0.1)
213 track.set_allow_unused_references(
True)
214 track.set_track_reference_position_derivatives(
True)
217 track.set_initial_time(0.48)
218 track.set_final_time(1.61)
219 track.set_mesh_interval(0.02)
222 controlsWeightSet = osim.MocoWeightSet()
223 model = modelProcessor.process()
225 forceSet = model.getForceSet()
226 for i
in range(forceSet.getSize()):
227 forcePath = forceSet.get(i).getAbsolutePathString()
228 if 'pelvis' in str(forcePath):
229 controlsWeightSet.cloneAndAppend(osim.MocoWeight(forcePath, 10))
231 track.set_control_effort_weight(0.1)
232 track.set_controls_weight_set(controlsWeightSet)
235 study = track.initialize()
236 problem = study.updProblem()
239 periodicityGoal = osim.MocoPeriodicityGoal(
'periodicity')
240 for i
in range(model.getNumStateVariables()):
241 currentStateName = str(model.getStateVariableNames().getitem(i))
242 if 'pelvis_tx/value' not in currentStateName:
243 periodicityGoal.addStatePair(osim.MocoPeriodicityGoalPair(currentStateName))
245 forceSet = model.getForceSet()
246 for i
in range(forceSet.getSize()):
247 forcePath = forceSet.get(i).getAbsolutePathString()
248 periodicityGoal.addControlPair(osim.MocoPeriodicityGoalPair(forcePath))
250 problem.addGoal(periodicityGoal)
253 jointMomentTracking = osim.MocoGeneralizedForceTrackingGoal(
254 'joint_moment_tracking', 1e-2)
259 jointMomentRef = osim.TableProcessor(
'inverse_dynamics.sto')
260 jointMomentRef.append(osim.TabOpLowPassFilter(10))
261 jointMomentTracking.setReference(jointMomentRef)
268 forcePaths = osim.StdVectorString()
269 forcePaths.append(
'.*externalloads.*')
270 jointMomentTracking.setForcePaths(forcePaths)
273 jointMomentTracking.setAllowUnusedReferences(
True)
277 jointMomentTracking.setNormalizeTrackingError(
True)
281 jointMomentTracking.setIgnoreConstrainedCoordinates(
True)
282 coordinateSet = model.getCoordinateSet()
283 for i
in range(coordinateSet.getSize()):
284 coordinate = coordinateSet.get(i)
285 coordName = coordinate.getName()
287 if 'pelvis' in coordName:
288 jointMomentTracking.setWeightForCoordinate(coordName, 0)
291 if 'ankle' in coordName:
292 jointMomentTracking.setWeightForCoordinate(coordName, 100)
294 problem.addGoal(jointMomentTracking)
297 solver = osim.MocoCasADiSolver.safeDownCast(study.updSolver())
298 solver.set_optim_convergence_tolerance(1e-3)
299 solver.set_optim_constraint_tolerance(1e-4)
300 solver.resetProblem(problem)
303 solution = study.solve()
304 solution.write(
'exampleMocoTrack_joint_moment_tracking_solution.sto')
307 model.printToXML(
'exampleMocoTrack_model.osim')
310 forcePaths = osim.StdVectorString()
311 forcePaths.append(
'.*externalloads.*')
312 jointMoments = study.calcGeneralizedForces(solution, forcePaths)
313 osim.STOFileAdapter.write(jointMoments,
'exampleMocoTrack_joint_moments.sto')
316 study.visualize(solution)
320 torqueDrivenMarkerTracking()
323 muscleDrivenStateTracking()
326 muscleDrivenJointMomentTracking()