API  4.5
For C++ developers
exampleSlidingMass.py

This is Moco's simplest example.

1 # -------------------------------------------------------------------------- #
2 # OpenSim Moco: exampleSlidingMass.py #
3 # -------------------------------------------------------------------------- #
4 # Copyright (c) 2017 Stanford University and the Authors #
5 # #
6 # Author(s): Christopher Dembia #
7 # #
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may #
9 # not use this file except in compliance with the License. You may obtain a #
10 # copy of the License at http://www.apache.org/licenses/LICENSE-2.0 #
11 # #
12 # Unless required by applicable law or agreed to in writing, software #
13 # distributed under the License is distributed on an "AS IS" BASIS, #
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 # See the License for the specific language governing permissions and #
16 # limitations under the License. #
17 # -------------------------------------------------------------------------- #
18 
19 import os
20 import opensim as osim
21 
22 model = osim.Model()
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)
27 
28 # Allows translation along x.
29 joint = osim.SliderJoint('slider', model.getGround(), body)
30 coord = joint.updCoordinate()
31 coord.setName('position')
32 model.addComponent(joint)
33 
34 actu = osim.CoordinateActuator()
35 actu.setCoordinate(coord)
36 actu.setName('actuator')
37 actu.setOptimalForce(1)
38 model.addComponent(actu)
39 
40 body.attachGeometry(osim.Sphere(0.05))
41 
42 model.finalizeConnections()
43 
44 # Create MocoStudy.
45 # ================
46 study = osim.MocoStudy()
47 study.setName('sliding_mass')
48 
49 # Define the optimal control problem.
50 # ===================================
51 problem = study.updProblem()
52 
53 # Model (dynamics).
54 # -----------------
55 problem.setModel(model)
56 
57 # Bounds.
58 # -------
59 # Initial time must be 0, final time can be within [0, 5].
60 problem.setTimeBounds(osim.MocoInitialBounds(0.), osim.MocoFinalBounds(0., 5.))
61 
62 # Position must be within [-5, 5] throughout the motion.
63 # Initial position must be 0, final position must be 1.
64 problem.setStateInfo('/slider/position/value', osim.MocoBounds(-5, 5),
65  osim.MocoInitialBounds(0), osim.MocoFinalBounds(1))
66 # Speed must be within [-50, 50] throughout the motion.
67 # Initial and final speed must be 0. Use compact syntax.
68 problem.setStateInfo('/slider/position/speed', [-50, 50], [0], [0])
69 
70 # Applied force must be between -50 and 50.
71 problem.setControlInfo('/actuator', osim.MocoBounds(-50, 50))
72 
73 # Cost.
74 # -----
75 problem.addGoal(osim.MocoFinalTimeGoal())
76 
77 # Configure the solver.
78 # =====================
79 solver = study.initCasADiSolver()
80 solver.set_num_mesh_intervals(100)
81 
82 # Now that we've finished setting up the study, print it to a file.
83 study.printToXML('sliding_mass.omoco')
84 
85 # Solve the problem.
86 # ==================
87 solution = study.solve()
88 
89 solution.write('sliding_mass_solution.sto')
90 
91 if os.getenv('OPENSIM_USE_VISUALIZER') != '0':
92  study.visualize(solution);