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