This is Moco's simplest example.
20 import opensim
as osim
23 model.setName(
'sliding_mass')
24 model.set_gravity(osim.Vec3(0, 0, 0))
25 body = osim.Body(
'body', 2.0, osim.Vec3(0), osim.Inertia(0))
26 model.addComponent(body)
29 joint = osim.SliderJoint(
'slider', model.getGround(), body)
30 coord = joint.updCoordinate()
31 coord.setName(
'position')
32 model.addComponent(joint)
34 actu = osim.CoordinateActuator()
35 actu.setCoordinate(coord)
36 actu.setName(
'actuator')
37 actu.setOptimalForce(1)
38 model.addComponent(actu)
40 body.attachGeometry(osim.Sphere(0.05))
42 model.finalizeConnections()
46 study = osim.MocoStudy()
47 study.setName(
'sliding_mass')
51 problem = study.updProblem()
55 problem.setModel(model)
60 problem.setTimeBounds(osim.MocoInitialBounds(0.), osim.MocoFinalBounds(0., 5.))
64 problem.setStateInfo(
'/slider/position/value', osim.MocoBounds(-5, 5),
65 osim.MocoInitialBounds(0), osim.MocoFinalBounds(1))
68 problem.setStateInfo(
'/slider/position/speed', [-50, 50], [0], [0])
71 problem.setControlInfo(
'/actuator', osim.MocoBounds(-50, 50))
75 problem.addGoal(osim.MocoFinalTimeGoal())
79 solver = study.initCasADiSolver()
80 solver.set_num_mesh_intervals(100)
83 study.printToXML(
'sliding_mass.omoco')
87 solution = study.solve()
89 solution.write(
'sliding_mass_solution.sto')
91 if os.getenv(
'OPENSIM_USE_VISUALIZER') !=
'0':
92 study.visualize(solution);