Beispiel #1
0
    def generate_results(self, root_dir, args):
        model = osim.Model()
        body = osim.Body("b", 1, osim.Vec3(0), osim.Inertia(0))
        model.addBody(body)

        joint = osim.SliderJoint("j", model.getGround(), body)
        joint.updCoordinate().setName("coord")
        model.addJoint(joint)

        damper = osim.SpringGeneralizedForce("coord")
        damper.setViscosity(-1.0)
        model.addForce(damper)

        actu = osim.CoordinateActuator("coord")
        model.addForce(actu)
        model.finalizeConnections()

        moco = osim.MocoStudy()
        problem = moco.updProblem()

        problem.setModel(model)
        problem.setTimeBounds(0, 2)
        problem.setStateInfo("/jointset/j/coord/value", [-10, 10], 0, 5)
        problem.setStateInfo("/jointset/j/coord/speed", [-10, 10], 0, 2)
        problem.setControlInfo("/forceset/coordinateactuator", [-50, 50])

        problem.addGoal(osim.MocoControlGoal("effort", 0.5))

        solver = moco.initCasADiSolver()
        solver.set_num_mesh_intervals(50)
        solution = moco.solve()
        solution.write(self.solution_file % root_dir)
Beispiel #2
0
    def test_Joint(self):
        a = osim.Model()

        body = osim.Body('body', 1.0, osim.Vec3(0, 0, 0),
                         osim.Inertia(0, 0, 0))

        loc_in_parent = osim.Vec3(0, -0, 0)
        orient_in_parent = osim.Vec3(0, 0, 0)
        loc_in_body = osim.Vec3(0, 0, 0)
        orient_in_body = osim.Vec3(0, 0, 0)

        joint = osim.FreeJoint("joint", a.getGround(), loc_in_parent,
                               orient_in_parent, body, loc_in_body,
                               orient_in_parent)
        del joint
        spatialTransform = osim.SpatialTransform()
        joint = osim.CustomJoint("joint", a.getGround(), loc_in_parent,
                                 orient_in_parent, body, loc_in_body,
                                 orient_in_parent, spatialTransform)
        del joint
        joint = osim.EllipsoidJoint("joint", a.getGround(), loc_in_parent,
                                    orient_in_parent, body, loc_in_body,
                                    orient_in_parent, osim.Vec3(1, 1, 1))
        del joint
        joint = osim.BallJoint("joint", a.getGround(), loc_in_parent,
                               orient_in_parent, body, loc_in_body,
                               orient_in_parent)
        del joint
        joint = osim.PinJoint("joint", a.getGround(), loc_in_parent,
                              orient_in_parent, body, loc_in_body,
                              orient_in_parent)
        del joint
        joint = osim.SliderJoint("joint", a.getGround(), loc_in_parent,
                                 orient_in_parent, body, loc_in_body,
                                 orient_in_parent)
        del joint
        joint = osim.WeldJoint("joint", a.getGround(), loc_in_parent,
                               orient_in_parent, body, loc_in_body,
                               orient_in_parent)
        del joint
        joint = osim.GimbalJoint("joint", a.getGround(), loc_in_parent,
                                 orient_in_parent, body, loc_in_body,
                                 orient_in_parent)
        del joint
        joint = osim.UniversalJoint("joint", a.getGround(), loc_in_parent,
                                    orient_in_parent, body, loc_in_body,
                                    orient_in_parent)
        del joint
        joint = osim.PlanarJoint("joint", a.getGround(), loc_in_parent,
                                 orient_in_parent, body, loc_in_body,
                                 orient_in_parent)
        del joint
Beispiel #3
0
    def test_createRep(self):
        model = osim.Model()
        model.setName('sliding_mass')
        model.set_gravity(osim.Vec3(0, 0, 0))
        body = osim.Body('body', 2.0, osim.Vec3(0), osim.Inertia(0))
        model.addComponent(body)

        joint = osim.SliderJoint('slider', model.getGround(), body)
        coord = joint.updCoordinate()
        coord.setName('position')
        model.addComponent(joint)
        model.finalizeConnections()

        study = osim.MocoStudy()
        study.setName('sliding_mass')

        mp = study.updProblem()
        mp.setModel(model)

        pr = mp.createRep()
        assert (len(pr.createStateInfoNames()) == 2)
Beispiel #4
0
def createSlidingMassModel():
    model = osim.Model()
    model.setName("sliding_mass")
    model.set_gravity(osim.Vec3(0, 0, 0))
    body = osim.Body("body", 10.0, osim.Vec3(0), osim.Inertia(0))
    model.addComponent(body)

    # Allows translation along x.
    joint = osim.SliderJoint("slider", model.getGround(), body)
    coord = joint.updCoordinate(osim.SliderJoint.Coord_TranslationX)
    coord.setName("position")
    model.addComponent(joint)

    actu = osim.CoordinateActuator()
    actu.setCoordinate(coord)
    actu.setName("actuator")
    actu.setOptimalForce(1)
    actu.setMinControl(-10)
    actu.setMaxControl(10)
    model.addComponent(actu)

    return model
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
# -------------------------------------------------------------------------- #

import os
import opensim as osim

model = osim.Model()
model.setName('sliding_mass')
model.set_gravity(osim.Vec3(0, 0, 0))
body = osim.Body('body', 2.0, osim.Vec3(0), osim.Inertia(0))
model.addComponent(body)

# Allows translation along x.
joint = osim.SliderJoint('slider', model.getGround(), body)
coord = joint.updCoordinate()
coord.setName('position')
model.addComponent(joint)

actu = osim.CoordinateActuator()
actu.setCoordinate(coord)
actu.setName('actuator')
actu.setOptimalForce(1)
model.addComponent(actu)

body.attachGeometry(osim.Sphere(0.05))

model.finalizeConnections()

# Create MucoTool.
Beispiel #6
0
import numpy as np

model = osim.Model()
model.setName('planar_point_mass')
g = abs(model.get_gravity()[1])

# We use an intermediate massless body for the X translation degree of freedom.
massless = osim.Body('massless', 0, osim.Vec3(0), osim.Inertia(0))
model.addBody(massless)
mass = 1.0
body = osim.Body('body', mass, osim.Vec3(0), osim.Inertia(0))
model.addBody(body)

body.attachGeometry(osim.Sphere(0.05))

jointX = osim.SliderJoint('tx', model.getGround(), massless)
coordX = jointX.updCoordinate(osim.SliderJoint.Coord_TranslationX)
coordX.setName('tx')
model.addJoint(jointX)

# The joint's x axis must point in the global "+y" direction.
jointY = osim.SliderJoint('ty', massless, osim.Vec3(0),
                          osim.Vec3(0, 0, 0.5 * np.pi), body, osim.Vec3(0),
                          osim.Vec3(0, 0, 0.5 * np.pi))
coordY = jointY.updCoordinate(osim.SliderJoint.Coord_TranslationX)
coordY.setName('ty')
model.addJoint(jointY)

# Add the kinematic constraint ty = tx^2.
constraint = osim.CoordinateCouplerConstraint()
independentCoords = osim.ArrayStr()
def createHangingMuscleModel(ignore_activation_dynamics,
                             ignore_tendon_compliance):
    model = osim.Model()
    model.setName("hanging_muscle")
    model.set_gravity(osim.Vec3(9.81, 0, 0))
    body = osim.Body("body", 0.5, osim.Vec3(0), osim.Inertia(1))
    model.addComponent(body)

    # Allows translation along x.
    joint = osim.SliderJoint("joint", model.getGround(), body)
    coord = joint.updCoordinate()
    coord.setName("height")
    model.addComponent(joint)

    # The point mass is supported by a muscle.
    # The DeGrooteFregly2016Muscle is the only muscle model in OpenSim that
    # has been tested with Moco.
    actu = osim.DeGrooteFregly2016Muscle()
    actu.setName("muscle")
    actu.set_max_isometric_force(30.0)
    actu.set_optimal_fiber_length(0.10)
    actu.set_tendon_slack_length(0.05)
    actu.set_tendon_strain_at_one_norm_force(0.10)
    actu.set_ignore_activation_dynamics(ignore_activation_dynamics)
    actu.set_ignore_tendon_compliance(ignore_tendon_compliance)
    actu.set_fiber_damping(0.01)
    # The DeGrooteFregly2016Muscle is the only muscle model in OpenSim that
    # can express its tendon compliance dynamics using an implicit
    # differential equation.
    actu.set_tendon_compliance_dynamics_mode("implicit")
    actu.set_max_contraction_velocity(10)
    actu.set_pennation_angle_at_optimal(0.10)
    actu.addNewPathPoint("origin", model.updGround(), osim.Vec3(0))
    actu.addNewPathPoint("insertion", body, osim.Vec3(0))
    model.addForce(actu)

    # Add metabolics probes: one for the total metabolic rate,
    # and one for each term in the metabolics model.
    probe = osim.Umberger2010MuscleMetabolicsProbe()
    probe.setName("metabolics")
    probe.addMuscle("muscle", 0.5)
    model.addProbe(probe)

    probe = osim.Umberger2010MuscleMetabolicsProbe()
    probe.setName("activation_maintenance_rate")
    probe.set_activation_maintenance_rate_on(True)
    probe.set_shortening_rate_on(False)
    probe.set_basal_rate_on(False)
    probe.set_mechanical_work_rate_on(False)
    probe.addMuscle("muscle", 0.5)
    model.addProbe(probe)

    probe = osim.Umberger2010MuscleMetabolicsProbe()
    probe.setName("shortening_rate")
    probe.set_activation_maintenance_rate_on(False)
    probe.set_shortening_rate_on(True)
    probe.set_basal_rate_on(False)
    probe.set_mechanical_work_rate_on(False)
    probe.addMuscle("muscle", 0.5)
    model.addProbe(probe)

    probe = osim.Umberger2010MuscleMetabolicsProbe()
    probe.setName("basal_rate")
    probe.set_activation_maintenance_rate_on(False)
    probe.set_shortening_rate_on(False)
    probe.set_basal_rate_on(True)
    probe.set_mechanical_work_rate_on(False)
    probe.addMuscle("muscle", 0.5)
    model.addProbe(probe)

    probe = osim.Umberger2010MuscleMetabolicsProbe()
    probe.setName("mechanical_work_rate")
    probe.set_activation_maintenance_rate_on(False)
    probe.set_shortening_rate_on(False)
    probe.set_basal_rate_on(False)
    probe.set_mechanical_work_rate_on(True)
    probe.addMuscle("muscle", 0.5)
    model.addProbe(probe)

    body.attachGeometry(osim.Sphere(0.05))

    model.finalizeConnections()

    return model
Beispiel #8
0
    model = opensim.Model()

    if useInput: model.setUseVisualizer(True)

    model.setName('TelerehabSimulator')
    model.setGravity(opensim.Vec3(0, 0, 0))

    master = opensim.Body('master', OPERATOR_INERTIA, opensim.Vec3(0, 0, 0),
                          opensim.Inertia(0, 0, 0))
    model.addBody(master)
    slave = opensim.Body('slave', OPERATOR_INERTIA, opensim.Vec3(0, 0, 0),
                         opensim.Inertia(0, 0, 0))
    model.addBody(slave)

    ground = model.getGround()
    masterToGround = opensim.SliderJoint('master2ground', ground, master)
    model.addJoint(masterToGround)
    slaveToGround = opensim.SliderJoint('slave2ground', ground, slave)
    model.addJoint(slaveToGround)

    blockMesh = opensim.Brick(opensim.Vec3(0.5, 0.5, 0.5))
    blockMesh.setColor(opensim.Red)
    masterOffsetFrame = opensim.PhysicalOffsetFrame()
    masterOffsetFrame.setParentFrame(master)
    masterOffsetFrame.setOffsetTransform(
        opensim.Transform(opensim.Vec3(0, 0, 0.5)))
    master.addComponent(masterOffsetFrame)
    masterOffsetFrame.attachGeometry(blockMesh.clone())
    blockMesh.setColor(opensim.Blue)
    slaveOffsetFrame = opensim.PhysicalOffsetFrame()
    slaveOffsetFrame.setParentFrame(slave)
Beispiel #9
0
    def test_bounds(self):
        model = osim.Model()
        model.setName('sliding_mass')
        model.set_gravity(osim.Vec3(0, 0, 0))
        body = osim.Body('body', 2.0, osim.Vec3(0), osim.Inertia(0))
        model.addComponent(body)

        joint = osim.SliderJoint('slider', model.getGround(), body)
        coord = joint.updCoordinate()
        coord.setName('position')
        model.addComponent(joint)

        actu = osim.CoordinateActuator()
        actu.setCoordinate(coord)
        actu.setName('actuator')
        actu.setOptimalForce(1)
        model.addComponent(actu)

        study = osim.MocoStudy()
        study.setName('sliding_mass')

        mp = study.updProblem()

        mp.setModel(model)
        ph0 = mp.getPhase()

        mp.setTimeBounds(osim.MocoInitialBounds(0.),
                         osim.MocoFinalBounds(0.1, 5.))
        assert ph0.getTimeInitialBounds().getLower() == 0
        assert ph0.getTimeInitialBounds().getUpper() == 0
        self.assertAlmostEqual(ph0.getTimeFinalBounds().getLower(), 0.1)
        self.assertAlmostEqual(ph0.getTimeFinalBounds().getUpper(), 5.0)

        mp.setTimeBounds([0.2, 0.3], [3.5])
        self.assertAlmostEqual(ph0.getTimeInitialBounds().getLower(), 0.2)
        self.assertAlmostEqual(ph0.getTimeInitialBounds().getUpper(), 0.3)
        self.assertAlmostEqual(ph0.getTimeFinalBounds().getLower(), 3.5)
        self.assertAlmostEqual(ph0.getTimeFinalBounds().getUpper(), 3.5)

        # Use setter on MocoPhase.
        ph0.setTimeBounds([2.2, 2.3], [4.5])
        self.assertAlmostEqual(ph0.getTimeInitialBounds().getLower(), 2.2)
        self.assertAlmostEqual(ph0.getTimeInitialBounds().getUpper(), 2.3)
        self.assertAlmostEqual(ph0.getTimeFinalBounds().getLower(), 4.5)
        self.assertAlmostEqual(ph0.getTimeFinalBounds().getUpper(), 4.5)

        mp.setStateInfo('slider/position/value', osim.MocoBounds(-5, 5),
                        osim.MocoInitialBounds(0))
        assert -5 == ph0.getStateInfo(
            'slider/position/value').getBounds().getLower()
        assert 5 == ph0.getStateInfo(
            'slider/position/value').getBounds().getUpper()
        assert isnan(
            ph0.getStateInfo(
                'slider/position/value').getFinalBounds().getLower())
        assert isnan(
            ph0.getStateInfo(
                'slider/position/value').getFinalBounds().getUpper())
        mp.setStateInfo('slider/position/speed', [-50, 50], [-3], 1.5)
        assert -50 == ph0.getStateInfo(
            'slider/position/speed').getBounds().getLower()
        assert 50 == ph0.getStateInfo(
            'slider/position/speed').getBounds().getUpper()
        assert -3 == ph0.getStateInfo(
            'slider/position/speed').getInitialBounds().getLower()
        assert -3 == ph0.getStateInfo(
            'slider/position/speed').getInitialBounds().getUpper()
        self.assertAlmostEqual(
            1.5,
            ph0.getStateInfo(
                'slider/position/speed').getFinalBounds().getLower())
        self.assertAlmostEqual(
            1.5,
            ph0.getStateInfo(
                'slider/position/speed').getFinalBounds().getUpper())

        # Use setter on MocoPhase.
        ph0.setStateInfo('slider/position/speed', [-6, 10], [-4, 3], [0])
        assert -6 == ph0.getStateInfo(
            'slider/position/speed').getBounds().getLower()
        assert 10 == ph0.getStateInfo(
            'slider/position/speed').getBounds().getUpper()
        assert -4 == ph0.getStateInfo(
            'slider/position/speed').getInitialBounds().getLower()
        assert 3 == ph0.getStateInfo(
            'slider/position/speed').getInitialBounds().getUpper()
        assert 0 == ph0.getStateInfo(
            'slider/position/speed').getFinalBounds().getLower()
        assert 0 == ph0.getStateInfo(
            'slider/position/speed').getFinalBounds().getUpper()

        # Controls.
        mp.setControlInfo('actuator', osim.MocoBounds(-50, 50))
        assert -50 == ph0.getControlInfo('actuator').getBounds().getLower()
        assert 50 == ph0.getControlInfo('actuator').getBounds().getUpper()
        mp.setControlInfo('actuator', [18])
        assert 18 == ph0.getControlInfo('actuator').getBounds().getLower()
        assert 18 == ph0.getControlInfo('actuator').getBounds().getUpper()