This example demonstrates how Moco solves problems with kinematic constraints and includes a visualization of the Lagrange multipliers that Moco solves for.
44model.setName(
'planar_point_mass')
45g = abs(model.get_gravity()[1])
48massless = osim.Body(
'massless', 0, osim.Vec3(0), osim.Inertia(0))
49model.addBody(massless);
51body = osim.Body(
'body', mass, osim.Vec3(0), osim.Inertia(0))
54body.attachGeometry(osim.Sphere(0.05))
56jointX = osim.SliderJoint(
'tx', model.getGround(), massless)
57coordX = jointX.updCoordinate(osim.SliderJoint.Coord_TranslationX)
62jointY = 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))
65coordY = jointY.updCoordinate(osim.SliderJoint.Coord_TranslationX)
70constraint = osim.CoordinateCouplerConstraint()
71independentCoords = osim.ArrayStr()
72independentCoords.append(
'tx')
73constraint.setIndependentCoordinateNames(independentCoords)
74constraint.setDependentCoordinateName(
'ty')
75coefficients = osim.Vector(3, 0)
78constraint.setFunction(osim.PolynomialFunction(coefficients))
79model.addConstraint(constraint)
81study = osim.MocoStudy()
82problem = study.updProblem()
83problem.setModel(model)
85phase0 = problem.getPhase(0)
87phase0.setDefaultSpeedBounds(osim.MocoBounds(-5, 5))
89problem.setTimeBounds(0, 0.8)
91problem.setStateInfo(
'/jointset/tx/tx/value', [-2, 2], -1.0)
92problem.setStateInfo(
'/jointset/ty/ty/value', [-2, 2], 1.0)
94solver = study.initCasADiSolver()
95solution = study.solve()
101if 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))