This example demonstrates how Moco solves problems with kinematic constraints and includes a visualization of the Lagrange multipliers that Moco solves for.
40 import opensim
as osim
44 model.setName(
'planar_point_mass')
45 g = abs(model.get_gravity()[1])
48 massless = osim.Body(
'massless', 0, osim.Vec3(0), osim.Inertia(0))
49 model.addBody(massless);
51 body = osim.Body(
'body', mass, osim.Vec3(0), osim.Inertia(0))
54 body.attachGeometry(osim.Sphere(0.05))
56 jointX = osim.SliderJoint(
'tx', model.getGround(), massless)
57 coordX = jointX.updCoordinate(osim.SliderJoint.Coord_TranslationX)
59 model.addJoint(jointX)
62 jointY = osim.SliderJoint(
'ty',
63 massless, osim.Vec3(0), osim.Vec3(0, 0, 0.5 * np.pi),
64 body, osim.Vec3(0), osim.Vec3(0, 0, 0.5 * np.pi))
65 coordY = jointY.updCoordinate(osim.SliderJoint.Coord_TranslationX)
67 model.addJoint(jointY)
70 constraint = osim.CoordinateCouplerConstraint()
71 independentCoords = osim.ArrayStr()
72 independentCoords.append(
'tx')
73 constraint.setIndependentCoordinateNames(independentCoords)
74 constraint.setDependentCoordinateName(
'ty')
75 coefficients = osim.Vector(3, 0)
77 coefficients.set(0, 1)
78 constraint.setFunction(osim.PolynomialFunction(coefficients))
79 model.addConstraint(constraint)
81 study = osim.MocoStudy()
82 problem = study.updProblem()
83 problem.setModel(model)
85 phase0 = problem.getPhase(0)
87 phase0.setDefaultSpeedBounds(osim.MocoBounds(-5, 5))
89 problem.setTimeBounds(0, 0.8)
91 problem.setStateInfo(
'/jointset/tx/tx/value', [-2, 2], -1.0)
92 problem.setStateInfo(
'/jointset/ty/ty/value', [-2, 2], 1.0)
94 solver = study.initCasADiSolver()
95 solution = study.solve()
101 if os.getenv(
'OPENSIM_USE_VISUALIZER') !=
'0':
104 from scipy.interpolate
import InterpolatedUnivariateSpline
107 print(
'Skipping plotting')
113 ax = fig.add_subplot(1, 1, 1)
118 time = solution.getTimeMat()
119 tx = solution.getStateMat(
'/jointset/tx/tx/value')
120 ty = solution.getStateMat(
'/jointset/ty/ty/value')
121 multiplier = -solution.getMultiplierMat(solution.getMultiplierNames()[0])
122 print(
'Number of multipliers: %i' % len(solution.getMultiplierNames()))
125 ax.set_aspect(
'equal')
126 pl.plot(tx, ty, color=
'black')
130 txSpline = InterpolatedUnivariateSpline(time, tx)
132 accelx = txSpline(time, 2)
133 tySpline = InterpolatedUnivariateSpline(time, ty)
134 accely = tySpline(time, 2)
138 statesTraj = solution.exportToStatesTrajectory(model)
139 matter = model.getMatterSubsystem()
140 constraintJacobian = osim.Matrix()
142 while itime < statesTraj.getSize():
143 state = statesTraj.get(itime)
144 model.realizePosition(state)
148 matter.calcPq(state, constraintJacobian)
149 jac = np.array([constraintJacobian.get(0, 0),
150 constraintJacobian.get(1, 0)])
152 constraintForces = jac * multiplier[itime]
155 constraintForcesViz = 0.010 * constraintForces
156 pl.arrow(tx[itime], ty[itime], constraintForcesViz[0],
157 constraintForcesViz[1], width=0.005, color=
'blue')
160 residualx = +constraintForces[0] - mass * accelx[itime]
161 residualy = -mass * g + constraintForces[1] - mass * accely[itime]
163 'time %f: residuals: %f, %f' % (time[itime], residualx, residualy))