This is an example that predicts a squat-to-stand movement and optimizes the stiffness of an assistive passive device.
4import exampleSquatToStand_helpers
as helpers
5import mocoPlotTrajectory
as plot
8torqueDrivenModel = helpers.getTorqueDrivenModel()
9muscleDrivenModel = helpers.getMuscleDrivenModel()
13study = osim.MocoStudy()
16problem = study.updProblem()
17problem.setModel(torqueDrivenModel)
39problem.setTimeBounds(0, 1)
43problem.setStateInfo(
'/jointset/hip_r/hip_flexion_r/value',
45problem.setStateInfo(
'/jointset/knee_r/knee_angle_r/value',
47problem.setStateInfo(
'/jointset/ankle_r/ankle_angle_r/value',
51problem.setStateInfoPattern(
'/jointset/.*/speed', [], 0, 0)
54problem.addGoal(osim.MocoControlGoal(
'myeffort'))
57solver = study.initCasADiSolver()
58solver.set_num_mesh_intervals(25)
59solver.set_optim_convergence_tolerance(1e-4)
60solver.set_optim_constraint_tolerance(1e-4)
62if not os.path.isfile(
'predictSolution.sto'):
64 predictSolution = study.solve()
65 predictSolution.write(
'predictSolution.sto')
66 study.visualize(predictSolution)
73tableProcessor = osim.TableProcessor(
'predictSolution.sto')
74tableProcessor.append(osim.TabOpLowPassFilter(6))
80tracking = osim.MocoStateTrackingGoal()
81tracking.setName(
'mytracking')
82tracking.setReference(tableProcessor)
83tracking.setAllowUnusedReferences(
True)
84problem.addGoal(tracking)
88problem.updGoal(
'myeffort').setWeight(0.001)
92solver.setGuessFile(
'predictSolution.sto')
93solver.set_optim_convergence_tolerance(1e-6)
95if not os.path.isfile(
'trackingSolution.sto'):
97 trackingSolution = study.solve()
98 trackingSolution.write(
'trackingSolution.sto')
99 study.visualize(trackingSolution)
104plot.mocoPlotTrajectory(
'predictSolution.sto',
'trackingSolution.sto',
109inverse = osim.MocoInverse()
113modelProcessor = osim.ModelProcessor(muscleDrivenModel)
114modelProcessor.append(osim.ModOpAddReserves(2))
115inverse.setModel(modelProcessor)
119inverse.setKinematics(tableProcessor)
122inverse.set_initial_time(0)
123inverse.set_final_time(1)
124inverse.set_mesh_interval(0.05)
125inverse.set_convergence_tolerance(1e-4)
126inverse.set_constraint_tolerance(1e-4)
129inverse.set_kinematics_allow_extra_columns(
True)
130inverse.set_minimize_sum_squared_activations(
True)
134inverse.append_output_paths(
'.*normalized_fiber_length')
135inverse.append_output_paths(
'.*passive_force_multiplier')
138inverseSolution = inverse.solve()
139solution = inverseSolution.getMocoSolution()
140solution.write(
'inverseSolution.sto')
143inverseOutputs = inverseSolution.getOutputs()
144sto = osim.STOFileAdapter()
145sto.write(inverseOutputs,
'muscleOutputs.sto')
150device = osim.SpringGeneralizedForce(
'knee_angle_r')
151device.setStiffness(50)
152device.setRestLength(0)
153device.setViscosity(0)
154muscleDrivenModel.addForce(device)
158modelProcessor = osim.ModelProcessor(muscleDrivenModel)
159modelProcessor.append(osim.ModOpAddReserves(2))
160inverse.setModel(modelProcessor)
163inverseDeviceSolution = inverse.solve()
164deviceSolution = inverseDeviceSolution.getMocoSolution()
165deviceSolution.write(
'inverseDeviceSolution.sto')
168print(
'Cost without device: ', solution.getObjective())
169print(
'Cost with device: ', deviceSolution.getObjective())
173helpers.compareInverseSolutions(inverseSolution, inverseDeviceSolution)