Esempio n. 1
0
    def predict(self, root_dir):
        model = self.muscle_driven_model(root_dir)
        moco = self.create_study(model)
        problem = moco.updProblem()
        problem.addGoal(osim.MocoControlGoal('effort'))
        problem.addGoal(osim.MocoInitialActivationGoal('init_activation'))
        problem.addGoal(osim.MocoFinalTimeGoal('time'))

        solver = osim.MocoCasADiSolver.safeDownCast(moco.updSolver())
        solver.resetProblem(problem)

        guess = solver.createGuess()

        N = guess.getNumTimes()
        for muscle in model.getMuscles():
            dgf = osim.DeGrooteFregly2016Muscle.safeDownCast(muscle)
            if not dgf.get_ignore_tendon_compliance():
                guess.setState(
                    '%s/normalized_tendon_force' % muscle.getAbsolutePathString(),
                    osim.createVectorLinspace(N, 0.1, 0.1))
            guess.setState(
                '%s/activation' % muscle.getAbsolutePathString(),
                osim.createVectorLinspace(N, 0.05, 0.05))
            guess.setControl(muscle.getAbsolutePathString(),
                osim.createVectorLinspace(N, 0.05, 0.05))
        solver.setGuess(guess)

        solution = moco.solve()
        solution.write(self.predict_solution_file % root_dir)
        # moco.visualize(solution)

        return solution
Esempio n. 2
0
    def predict_assisted(self, root_dir):
        model = self.assisted_model(root_dir)

        moco = self.create_study(model)
        problem = moco.updProblem()
        problem.addGoal(osim.MocoControlGoal('effort'))
        problem.addGoal(osim.MocoInitialActivationGoal('init_activation'))
        problem.addGoal(osim.MocoFinalTimeGoal('time'))

        problem.addParameter(
            osim.MocoParameter('stiffness', '/forceset/spring',
                               'stiffness', osim.MocoBounds(0, 100)))

        solver = osim.MocoCasADiSolver.safeDownCast(moco.updSolver())
        solver.resetProblem(problem)

        guess = solver.createGuess()

        N = guess.getNumTimes()
        for muscle in model.getMuscles():
            dgf = osim.DeGrooteFregly2016Muscle.safeDownCast(muscle)
            if not dgf.get_ignore_tendon_compliance():
                guess.setState(
                    '%s/normalized_tendon_force' % muscle.getAbsolutePathString(),
                    osim.createVectorLinspace(N, 0.1, 0.1))
            guess.setState(
                '%s/activation' % muscle.getAbsolutePathString(),
                osim.createVectorLinspace(N, 0.05, 0.05))
            guess.setControl(muscle.getAbsolutePathString(),
                             osim.createVectorLinspace(N, 0.05, 0.05))
        solver.setGuess(guess)

        solver.set_parameters_require_initsystem(False)
        solution = moco.solve()
        # moco.visualize(solution)
        solution.write(self.predict_assisted_solution_file % root_dir)
ignore_tendon_compliance = False
model = createHangingMuscleModel(ignore_activation_dynamics,
                                 ignore_tendon_compliance)
model.printToXML("hanging_muscle.osim")

study = osim.MocoStudy()
problem = study.updProblem()
problem.setModelAsCopy(model)
problem.setTimeBounds(0, [0.05, 1.0])
problem.setStateInfo("/joint/height/value", [0.14, 0.16], 0.15, 0.14)
problem.setStateInfo("/joint/height/speed", [-1, 1], 0, 0)
problem.setControlInfo("/forceset/muscle", [0.01, 1])

# Initial state constraints/costs.
if not ignore_activation_dynamics:
    initial_activation = osim.MocoInitialActivationGoal()
    problem.addGoal(initial_activation)
    initial_activation.setName("initial_activation")
if not ignore_tendon_compliance:
    initial_equilibrium = osim.MocoInitialVelocityEquilibriumDGFGoal()
    problem.addGoal(initial_equilibrium)
    initial_equilibrium.setName("initial_velocity_equilibrium")
    # The problem converges in fewer iterations when this goal is in cost mode.
    initial_equilibrium.setMode("cost")
    initial_equilibrium.setWeight(0.001)

problem.addGoal(osim.MocoFinalTimeGoal())

solver = study.initCasADiSolver()
solver.set_num_mesh_intervals(25)
solver.set_multibody_dynamics_mode("implicit")