API 4.4.1-2022-10-19-2c4045e59
For MATLAB, Python, Java, and C++ users
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
19import os
20import opensim as osim
21
22model = osim.Model()
23model.setName('sliding_mass')
24model.set_gravity(osim.Vec3(0, 0, 0))
25body = osim.Body('body', 2.0, osim.Vec3(0), osim.Inertia(0))
26model.addComponent(body)
27
28# Allows translation along x.
29joint = osim.SliderJoint('slider', model.getGround(), body)
30coord = joint.updCoordinate()
31coord.setName('position')
32model.addComponent(joint)
33
34actu = osim.CoordinateActuator()
35actu.setCoordinate(coord)
36actu.setName('actuator')
37actu.setOptimalForce(1)
38model.addComponent(actu)
39
40body.attachGeometry(osim.Sphere(0.05))
41
42model.finalizeConnections()
43
44# Create MocoStudy.
45# ================
46study = osim.MocoStudy()
47study.setName('sliding_mass')
48
49# Define the optimal control problem.
50# ===================================
51problem = study.updProblem()
52
53# Model (dynamics).
54# -----------------
55problem.setModel(model)
56
57# Bounds.
58# -------
59# Initial time must be 0, final time can be within [0, 5].
60problem.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.
64problem.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.
68problem.setStateInfo('/slider/position/speed', [-50, 50], [0], [0])
69
70# Applied force must be between -50 and 50.
71problem.setControlInfo('/actuator', osim.MocoBounds(-50, 50))
72
73# Cost.
74# -----
75problem.addGoal(osim.MocoFinalTimeGoal())
76
77# Configure the solver.
78# =====================
79solver = study.initCasADiSolver()
80solver.set_num_mesh_intervals(100)
81
82# Now that we've finished setting up the study, print it to a file.
83study.printToXML('sliding_mass.omoco')
84
85# Solve the problem.
86# ==================
87solution = study.solve()
88
89solution.write('sliding_mass_solution.sto')
90
91if os.getenv('OPENSIM_USE_VISUALIZER') != '0':
92 study.visualize(solution);