Ejemplo n.º 1
0
def cartpole():
    # Loading the double pendulum model
    robot = example_robot_data.loadDoublePendulum()
    robot_model = robot.model

    state = crocoddyl.StateMultibody(robot_model)
    actModel = ActuationModelDoublePendulum(state, actLink=1)

    weights = np.array([1, 1, 1, 1] + [0.1] * 2)
    runningCostModel = crocoddyl.CostModelSum(state, actModel.nu)
    terminalCostModel = crocoddyl.CostModelSum(state, actModel.nu)
    xRegCost = crocoddyl.CostModelState(
        state, crocoddyl.ActivationModelQuad(state.ndx), state.zero(),
        actModel.nu)
    uRegCost = crocoddyl.CostModelControl(state,
                                          crocoddyl.ActivationModelQuad(1),
                                          actModel.nu)
    xPendCost = CostModelDoublePendulum(
        state, crocoddyl.ActivationModelWeightedQuad(np.matrix(weights).T),
        actModel.nu)

    dt = 1e-2

    runningCostModel.addCost("uReg", uRegCost, 1e-4 / dt)
    runningCostModel.addCost("xGoal", xPendCost, 1e-5 / dt)
    terminalCostModel.addCost("xGoal", xPendCost, 1e4)

    runningModel = crocoddyl.IntegratedActionModelEuler(
        crocoddyl.DifferentialActionModelFreeFwdDynamics(
            state, actModel, runningCostModel), dt)
    terminalModel = crocoddyl.IntegratedActionModelEuler(
        crocoddyl.DifferentialActionModelFreeFwdDynamics(
            state, actModel, terminalCostModel), dt)

    return runningModel, terminalModel
Ejemplo n.º 2
0
def runBenchmark(model):
    robot_model = ROBOT.model
    q0 = np.matrix([0.173046, 1., -0.52366, 0., 0., 0.1, -0.005]).T
    x0 = np.vstack([q0, np.zeros((robot_model.nv, 1))])

    # Note that we need to include a cost model (i.e. set of cost functions) in
    # order to fully define the action model for our optimal control problem.
    # For this particular example, we formulate three running-cost functions:
    # goal-tracking cost, state and control regularization; and one terminal-cost:
    # goal cost. First, let's create the common cost functions.
    state = crocoddyl.StateMultibody(robot_model)
    Mref = crocoddyl.FramePlacement(
        robot_model.getFrameId("gripper_left_joint"),
        pinocchio.SE3(np.eye(3), np.matrix([[.0], [.0], [.4]])))
    goalTrackingCost = crocoddyl.CostModelFramePlacement(state, Mref)
    xRegCost = crocoddyl.CostModelState(state)
    uRegCost = crocoddyl.CostModelControl(state)

    # Create a cost model per the running and terminal action model.
    runningCostModel = crocoddyl.CostModelSum(state)
    terminalCostModel = crocoddyl.CostModelSum(state)

    # Then let's added the running and terminal cost functions
    runningCostModel.addCost("gripperPose", goalTrackingCost, 1e-3)
    runningCostModel.addCost("xReg", xRegCost, 1e-7)
    runningCostModel.addCost("uReg", uRegCost, 1e-7)
    terminalCostModel.addCost("gripperPose", goalTrackingCost, 1)

    # Next, we need to create an action model for running and terminal knots. The
    # forward dynamics (computed using ABA) are implemented
    # inside DifferentialActionModelFullyActuated.
    runningModel = crocoddyl.IntegratedActionModelEuler(
        model(state, runningCostModel), 1e-3)
    runningModel.differential.armature = np.matrix(
        [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.]).T
    terminalModel = crocoddyl.IntegratedActionModelEuler(
        model(state, terminalCostModel), 1e-3)
    terminalModel.differential.armature = np.matrix(
        [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.]).T

    # For this optimal control problem, we define 100 knots (or running action
    # models) plus a terminal knot
    problem = crocoddyl.ShootingProblem(x0, [runningModel] * N, terminalModel)

    # Creating the DDP solver for this OC problem, defining a logger
    ddp = crocoddyl.SolverDDP(problem)

    duration = []
    for i in range(T):
        c_start = time.time()
        ddp.solve([], [], MAXITER)
        c_end = time.time()
        duration.append(1e3 * (c_end - c_start))

    avrg_duration = sum(duration) / len(duration)
    min_duration = min(duration)
    max_duration = max(duration)
    return avrg_duration, min_duration, max_duration
Ejemplo n.º 3
0
def createProblem(model):
    robot_model = ROBOT.model
    q0 = np.matrix([0.173046, 1., -0.52366, 0., 0., 0.1, -0.005]).T
    x0 = np.vstack([q0, np.zeros((robot_model.nv, 1))])

    # Note that we need to include a cost model (i.e. set of cost functions) in
    # order to fully define the action model for our optimal control problem.
    # For this particular example, we formulate three running-cost functions:
    # goal-tracking cost, state and control regularization; and one terminal-cost:
    # goal cost. First, let's create the common cost functions.
    state = crocoddyl.StateMultibody(robot_model)
    Mref = crocoddyl.FramePlacement(
        robot_model.getFrameId("gripper_left_joint"),
        pinocchio.SE3(np.eye(3), np.matrix([[.0], [.0], [.4]])))
    goalTrackingCost = crocoddyl.CostModelFramePlacement(state, Mref)
    xRegCost = crocoddyl.CostModelState(state)
    uRegCost = crocoddyl.CostModelControl(state)

    # Create a cost model per the running and terminal action model.
    runningCostModel = crocoddyl.CostModelSum(state)
    terminalCostModel = crocoddyl.CostModelSum(state)

    # Then let's added the running and terminal cost functions
    runningCostModel.addCost("gripperPose", goalTrackingCost, 1)
    runningCostModel.addCost("xReg", xRegCost, 1e-4)
    runningCostModel.addCost("uReg", uRegCost, 1e-4)
    terminalCostModel.addCost("gripperPose", goalTrackingCost, 1)

    # Next, we need to create an action model for running and terminal knots. The
    # forward dynamics (computed using ABA) are implemented
    # inside DifferentialActionModelFullyActuated.
    actuation = crocoddyl.ActuationModelFull(state)
    runningModel = crocoddyl.IntegratedActionModelEuler(
        model(state, actuation, runningCostModel), 1e-3)
    runningModel.differential.armature = np.matrix(
        [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.]).T
    terminalModel = crocoddyl.IntegratedActionModelEuler(
        model(state, actuation, terminalCostModel), 1e-3)
    terminalModel.differential.armature = np.matrix(
        [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.]).T

    # For this optimal control problem, we define 100 knots (or running action
    # models) plus a terminal knot
    problem = crocoddyl.ShootingProblem(x0, [runningModel] * N, terminalModel)
    xs = [x0] * (len(problem.runningModels) + 1)
    us = [
        m.quasiStatic(d, x0)
        for m, d in list(zip(problem.runningModels, problem.runningDatas))
    ]
    return xs, us, problem
Ejemplo n.º 4
0
class TalosArmIntegratedRK4Test(ActionModelAbstractTestCase):
    ROBOT_MODEL = example_robot_data.load('talos_arm').model
    STATE = crocoddyl.StateMultibody(ROBOT_MODEL)
    ACTUATION = crocoddyl.ActuationModelFull(STATE)
    COST_SUM = crocoddyl.CostModelSum(STATE)
    COST_SUM.addCost(
        'gripperPose',
        crocoddyl.CostModelResidual(
            STATE,
            crocoddyl.ResidualModelFramePlacement(
                STATE, ROBOT_MODEL.getFrameId("gripper_left_joint"),
                pinocchio.SE3.Random())), 1e-3)
    COST_SUM.addCost(
        "xReg",
        crocoddyl.CostModelResidual(STATE,
                                    crocoddyl.ResidualModelState(STATE)), 1e-7)
    COST_SUM.addCost(
        "uReg",
        crocoddyl.CostModelResidual(STATE,
                                    crocoddyl.ResidualModelControl(STATE)),
        1e-7)
    DIFFERENTIAL = crocoddyl.DifferentialActionModelFreeFwdDynamics(
        STATE, ACTUATION, COST_SUM)
    MODEL = crocoddyl.IntegratedActionModelRK(DIFFERENTIAL,
                                              crocoddyl.RKType.four, 1e-3)
    MODEL_DER = IntegratedActionModelRK4Derived(DIFFERENTIAL, 1e-3)
Ejemplo n.º 5
0
    def createPseudoImpulseModel(self, supportFootIds, swingFootTask):
        """ Action model for pseudo-impulse models.

        A pseudo-impulse model consists of adding high-penalty cost for the contact velocities.
        :param swingFootTask: swinging foot task
        :return pseudo-impulse differential action model
        """

        # Creating a 6D multi-contact model, and then including the supporting
        # foot
        contactModel = crocoddyl.ContactModelMultiple(self.state,
                                                      self.actuation.nu)
        for i in supportFootIds:
            Mref = crocoddyl.FramePlacement(i, pinocchio.SE3.Identity())
            supportContactModel = crocoddyl.ContactModel6D(
                self.state, Mref, self.actuation.nu, np.array([0., 0.]))
            contactModel.addContact(self.rmodel.frames[i].name + "_contact",
                                    supportContactModel)

        # Creating the cost model for a contact phase
        costModel = crocoddyl.CostModelSum(self.state, self.actuation.nu)
        for i in supportFootIds:
            cone = crocoddyl.WrenchCone(np.identity(3), self.mu,
                                        np.array([0.1, 0.05]))
            wrenchCone = crocoddyl.CostModelContactWrenchCone(
                self.state,
                crocoddyl.ActivationModelQuadraticBarrier(
                    crocoddyl.ActivationBounds(cone.lb, cone.ub)),
                crocoddyl.FrameWrenchCone(i, cone), self.actuation.nu)
            costModel.addCost(self.rmodel.frames[i].name + "_wrenchCone",
                              wrenchCone, 1e1)

        if swingFootTask is not None:
            for i in swingFootTask:
                footTrack = crocoddyl.CostModelFramePlacement(
                    self.state, i, self.actuation.nu)
                costModel.addCost(self.rmodel.frames[i.id].name + "_footTrack",
                                  footTrack, 1e8)
                footVel = crocoddyl.FrameMotion(i.id, pinocchio.Motion.Zero())
                impulseFootVelCost = crocoddyl.CostModelFrameVelocity(
                    self.state, footVel, self.actuation.nu)
                costModel.addCost(
                    self.rmodel.frames[i.id].name + "_impulseVel",
                    impulseFootVelCost, 1e6)

        stateWeights = np.array([0] * 3 + [500.] * 3 + [0.01] *
                                (self.state.nv - 6) + [10] * self.state.nv)
        stateReg = crocoddyl.CostModelState(
            self.state, crocoddyl.ActivationModelWeightedQuad(stateWeights**2),
            self.rmodel.defaultState, self.actuation.nu)
        ctrlReg = crocoddyl.CostModelControl(self.state, self.actuation.nu)
        costModel.addCost("stateReg", stateReg, 1e1)
        costModel.addCost("ctrlReg", ctrlReg, 1e-3)

        # Creating the action model for the KKT dynamics with simpletic Euler
        # integration scheme
        dmodel = crocoddyl.DifferentialActionModelContactFwdDynamics(
            self.state, self.actuation, contactModel, costModel, 0., True)
        model = crocoddyl.IntegratedActionModelEuler(dmodel, 0.)
        return model
Ejemplo n.º 6
0
    def createPseudoImpulseModel(self, supportFootIds, swingFootTask):
        """ Action model for pseudo-impulse models.

        A pseudo-impulse model consists of adding high-penalty cost for the contact velocities.
        :param supportFootIds: Ids of the constrained feet
        :param swingFootTask: swinging foot task
        :return pseudo-impulse differential action model
        """
        # Creating a 3D multi-contact model, and then including the supporting
        # foot
        nu = self.actuation.nu
        contactModel = crocoddyl.ContactModelMultiple(self.state, nu)
        for i in supportFootIds:
            supportContactModel = crocoddyl.ContactModel3D(self.state, i, np.array([0., 0., 0.]), nu,
                                                           np.array([0., 50.]))
            contactModel.addContact(self.rmodel.frames[i].name + "_contact", supportContactModel)

        # Creating the cost model for a contact phase
        costModel = crocoddyl.CostModelSum(self.state, nu)
        for i in supportFootIds:
            cone = crocoddyl.FrictionCone(self.Rsurf, self.mu, 4, False)
            coneResidual = crocoddyl.ResidualModelContactFrictionCone(self.state, i, cone, nu)
            coneActivation = crocoddyl.ActivationModelQuadraticBarrier(crocoddyl.ActivationBounds(cone.lb, cone.ub))
            frictionCone = crocoddyl.CostModelResidual(self.state, coneActivation, coneResidual)
            costModel.addCost(self.rmodel.frames[i].name + "_frictionCone", frictionCone, 1e1)
        if swingFootTask is not None:
            for i in swingFootTask:
                frameTranslationResidual = crocoddyl.ResidualModelFrameTranslation(self.state, i[0], i[1].translation,
                                                                                   nu)
                frameVelocityResidual = crocoddyl.ResidualModelFrameVelocity(self.state, i[0], pinocchio.Motion.Zero(),
                                                                             pinocchio.LOCAL, nu)
                footTrack = crocoddyl.CostModelResidual(self.state, frameTranslationResidual)
                impulseFootVelCost = crocoddyl.CostModelResidual(self.state, frameVelocityResidual)
                costModel.addCost(self.rmodel.frames[i[0]].name + "_footTrack", footTrack, 1e7)
                costModel.addCost(self.rmodel.frames[i[0]].name + "_impulseVel", impulseFootVelCost, 1e6)

        stateWeights = np.array([0.] * 3 + [500.] * 3 + [0.01] * (self.rmodel.nv - 6) + [10.] * self.rmodel.nv)
        stateResidual = crocoddyl.ResidualModelState(self.state, self.rmodel.defaultState, nu)
        stateActivation = crocoddyl.ActivationModelWeightedQuad(stateWeights**2)
        ctrlResidual = crocoddyl.ResidualModelControl(self.state, nu)
        stateReg = crocoddyl.CostModelResidual(self.state, stateActivation, stateResidual)
        ctrlReg = crocoddyl.CostModelResidual(self.state, ctrlResidual)
        costModel.addCost("stateReg", stateReg, 1e1)
        costModel.addCost("ctrlReg", ctrlReg, 1e-3)

        # Creating the action model for the KKT dynamics with simpletic Euler
        # integration scheme
        dmodel = crocoddyl.DifferentialActionModelContactFwdDynamics(self.state, self.actuation, contactModel,
                                                                     costModel, 0., True)
        if self.integrator == 'euler':
            model = crocoddyl.IntegratedActionModelEuler(dmodel, 0.)
        elif self.integrator == 'rk4':
            model = crocoddyl.IntegratedActionModelRK(dmodel, crocoddyl.RKType.four, 0.)
        elif self.integrator == 'rk3':
            model = crocoddyl.IntegratedActionModelRK(dmodel, crocoddyl.RKType.three, 0.)
        elif self.integrator == 'rk2':
            model = crocoddyl.IntegratedActionModelRK(dmodel, crocoddyl.RKType.two, 0.)
        else:
            model = crocoddyl.IntegratedActionModelEuler(dmodel, 0.)
        return model
Ejemplo n.º 7
0
class TalosArmShootingTest(ShootingProblemTestCase):
    ROBOT_MODEL = example_robot_data.load('talos_arm').model
    STATE = crocoddyl.StateMultibody(ROBOT_MODEL)
    ACTUATION = crocoddyl.ActuationModelFull(STATE)
    COST_SUM = crocoddyl.CostModelSum(STATE)
    COST_SUM.addCost(
        'gripperPose',
        crocoddyl.CostModelResidual(
            STATE,
            crocoddyl.ResidualModelFramePlacement(
                STATE, ROBOT_MODEL.getFrameId("gripper_left_joint"),
                pinocchio.SE3.Random())), 1e-3)
    COST_SUM.addCost(
        "xReg",
        crocoddyl.CostModelResidual(STATE,
                                    crocoddyl.ResidualModelState(STATE)), 1e-7)
    COST_SUM.addCost(
        "uReg",
        crocoddyl.CostModelResidual(STATE,
                                    crocoddyl.ResidualModelControl(STATE)),
        1e-7)
    DIFF_MODEL = crocoddyl.DifferentialActionModelFreeFwdDynamics(
        STATE, ACTUATION, COST_SUM)
    DIFF_MODEL_DER = DifferentialFreeFwdDynamicsModelDerived(
        STATE, ACTUATION, COST_SUM)
    MODEL = crocoddyl.IntegratedActionModelEuler(DIFF_MODEL, 1e-3)
    MODEL_DER = crocoddyl.IntegratedActionModelEuler(DIFF_MODEL_DER, 1e-3)
Ejemplo n.º 8
0
    def createImpulseModel(self, supportFootIds, swingFootTask, JMinvJt_damping=1e-12, r_coeff=0.0):
        """ Action model for impulse models.

        An impulse model consists of describing the impulse dynamics against a set of contacts.
        :param supportFootIds: Ids of the constrained feet
        :param swingFootTask: swinging foot task
        :return impulse action model
        """
        # Creating a 3D multi-contact model, and then including the supporting foot
        impulseModel = crocoddyl.ImpulseModelMultiple(self.state)
        for i in supportFootIds:
            supportContactModel = crocoddyl.ImpulseModel3D(self.state, i)
            impulseModel.addImpulse(self.rmodel.frames[i].name + "_impulse", supportContactModel)

        # Creating the cost model for a contact phase
        costModel = crocoddyl.CostModelSum(self.state, 0)
        if swingFootTask is not None:
            for i in swingFootTask:
                xref = crocoddyl.FrameTranslation(i.id, i.placement.translation)
                footTrack = crocoddyl.CostModelFrameTranslation(self.state, xref, 0)
                costModel.addCost(self.rmodel.frames[i.id].name + "_footTrack", footTrack, 1e7)
        stateWeights = np.array([1.] * 6 + [10.] * (self.rmodel.nv - 6) + [10.] * self.rmodel.nv)
        stateReg = crocoddyl.CostModelState(self.state, crocoddyl.ActivationModelWeightedQuad(stateWeights**2),
                                            self.rmodel.defaultState, 0)
        costModel.addCost("stateReg", stateReg, 1e1)

        # Creating the action model for the KKT dynamics with simpletic Euler
        # integration scheme
        model = crocoddyl.ActionModelImpulseFwdDynamics(self.state, impulseModel, costModel)
        model.JMinvJt_damping = JMinvJt_damping
        model.r_coeff = r_coeff
        return model
Ejemplo n.º 9
0
class TalosArmFreeFwdDynamicsWithArmatureTest(ActionModelAbstractTestCase):
    ROBOT_MODEL = example_robot_data.load('talos_arm').model
    STATE = crocoddyl.StateMultibody(ROBOT_MODEL)
    ACTUATION = crocoddyl.ActuationModelFull(STATE)
    COST_SUM = crocoddyl.CostModelSum(STATE)
    COST_SUM.addCost(
        'gripperPose',
        crocoddyl.CostModelResidual(
            STATE,
            crocoddyl.ResidualModelFramePlacement(
                STATE, ROBOT_MODEL.getFrameId("gripper_left_joint"),
                pinocchio.SE3.Random())), 1e-3)
    COST_SUM.addCost(
        "xReg",
        crocoddyl.CostModelResidual(STATE,
                                    crocoddyl.ResidualModelState(STATE)), 1e-7)
    COST_SUM.addCost(
        "uReg",
        crocoddyl.CostModelResidual(STATE,
                                    crocoddyl.ResidualModelControl(STATE)),
        1e-7)
    MODEL = crocoddyl.DifferentialActionModelFreeFwdDynamics(
        STATE, ACTUATION, COST_SUM)
    MODEL_DER = DifferentialFreeFwdDynamicsModelDerived(
        STATE, ACTUATION, COST_SUM)
    MODEL.armature = 0.1 * np.ones(ROBOT_MODEL.nv)
    MODEL_DER.set_armature(0.1 * np.ones(ROBOT_MODEL.nv))
Ejemplo n.º 10
0
    def createPseudoImpulseModel(self, supportFootIds, swingFootTask):
        """ Action model for pseudo-impulse models.

        A pseudo-impulse model consists of adding high-penalty cost for the contact velocities.
        :param supportFootIds: Ids of the constrained feet
        :param swingFootTask: swinging foot task
        :return pseudo-impulse differential action model
        """
        # Creating a 3D multi-contact model, and then including the supporting
        # foot
        contactModel = crocoddyl.ContactModelMultiple(self.state,
                                                      self.actuation.nu)
        for i in supportFootIds:
            xref = crocoddyl.FrameTranslation(i, np.array([0., 0., 0.]))
            supportContactModel = crocoddyl.ContactModel3D(
                self.state, xref, self.actuation.nu, np.array([0., 50.]))
            contactModel.addContact(self.rmodel.frames[i].name + "_contact",
                                    supportContactModel)

        # Creating the cost model for a contact phase
        costModel = crocoddyl.CostModelSum(self.state, self.actuation.nu)
        for i in supportFootIds:
            cone = crocoddyl.FrictionCone(self.nsurf, self.mu, 4, False)
            frictionCone = crocoddyl.CostModelContactFrictionCone(
                self.state,
                crocoddyl.ActivationModelQuadraticBarrier(
                    crocoddyl.ActivationBounds(cone.lb, cone.ub)),
                crocoddyl.FrameFrictionCone(i, cone), self.actuation.nu)
            costModel.addCost(self.rmodel.frames[i].name + "_frictionCone",
                              frictionCone, 1e1)
        if swingFootTask is not None:
            for i in swingFootTask:
                xref = crocoddyl.FrameTranslation(i.frame, i.oMf.translation)
                vref = crocoddyl.FrameMotion(i.frame, pinocchio.Motion.Zero())
                footTrack = crocoddyl.CostModelFrameTranslation(
                    self.state, xref, self.actuation.nu)
                impulseFootVelCost = crocoddyl.CostModelFrameVelocity(
                    self.state, vref, self.actuation.nu)
                costModel.addCost(
                    self.rmodel.frames[i.frame].name + "_footTrack", footTrack,
                    1e7)
                costModel.addCost(
                    self.rmodel.frames[i.frame].name + "_impulseVel",
                    impulseFootVelCost, 1e6)
        stateWeights = np.array([0.] * 3 + [500.] * 3 + [0.01] *
                                (self.rmodel.nv - 6) + [10.] * self.rmodel.nv)
        stateReg = crocoddyl.CostModelState(
            self.state, crocoddyl.ActivationModelWeightedQuad(stateWeights**2),
            self.rmodel.defaultState, self.actuation.nu)
        ctrlReg = crocoddyl.CostModelControl(self.state, self.actuation.nu)
        costModel.addCost("stateReg", stateReg, 1e1)
        costModel.addCost("ctrlReg", ctrlReg, 1e-3)

        # Creating the action model for the KKT dynamics with simpletic Euler
        # integration scheme
        dmodel = crocoddyl.DifferentialActionModelContactFwdDynamics(
            self.state, self.actuation, contactModel, costModel, 0., True)
        model = crocoddyl.IntegratedActionModelEuler(dmodel, 0.)
        return model
Ejemplo n.º 11
0
class AnymalFreeFwdDynamicsTest(ActionModelAbstractTestCase):
    ROBOT_MODEL = example_robot_data.loadANYmal().model
    STATE = crocoddyl.StateMultibody(ROBOT_MODEL)
    ACTUATION = crocoddyl.ActuationModelFloatingBase(STATE)
    COST_SUM = crocoddyl.CostModelSum(STATE, ACTUATION.nu)
    COST_SUM.addCost("xReg", crocoddyl.CostModelState(STATE, ACTUATION.nu), 1e-7)
    COST_SUM.addCost("uReg", crocoddyl.CostModelControl(STATE, ACTUATION.nu), 1e-7)
    MODEL = crocoddyl.DifferentialActionModelFreeFwdDynamics(STATE, ACTUATION, COST_SUM)
    MODEL_DER = DifferentialFreeFwdDynamicsDerived(STATE, ACTUATION, COST_SUM)
Ejemplo n.º 12
0
    def createSwingFootModel(self, timeStep, supportFootIds, comTask=None, swingFootTask=None):
        """ Action model for a swing foot phase.

        :param timeStep: step duration of the action model
        :param supportFootIds: Ids of the constrained feet
        :param comTask: CoM task
        :param swingFootTask: swinging foot task
        :return action model for a swing foot phase
        """
        # Creating a 3D multi-contact model, and then including the supporting
        # foot
        contactModel = crocoddyl.ContactModelMultiple(self.state, self.actuation.nu)
        for i in supportFootIds:
            xref = crocoddyl.FrameTranslation(i, np.matrix([0., 0., 0.]).T)
            supportContactModel = crocoddyl.ContactModel3D(self.state, xref, self.actuation.nu, np.matrix([0., 50.]).T)
            contactModel.addContact(self.rmodel.frames[i].name + "_contact", supportContactModel)

        # Creating the cost model for a contact phase
        costModel = crocoddyl.CostModelSum(self.state, self.actuation.nu)
        if isinstance(comTask, np.ndarray):
            comTrack = crocoddyl.CostModelCoMPosition(self.state, comTask, self.actuation.nu)
            costModel.addCost("comTrack", comTrack, 1e6)
        for i in supportFootIds:
            cone = crocoddyl.FrictionCone(self.nsurf, self.mu, 4, False)
            frictionCone = crocoddyl.CostModelContactFrictionCone(
                self.state, crocoddyl.ActivationModelQuadraticBarrier(crocoddyl.ActivationBounds(cone.lb, cone.ub)),
                cone, i, self.actuation.nu)
            costModel.addCost(self.rmodel.frames[i].name + "_frictionCone", frictionCone, 1e1)
        if swingFootTask is not None:
            for i in swingFootTask:
                xref = crocoddyl.FrameTranslation(i.frame, i.oMf.translation)
                footTrack = crocoddyl.CostModelFrameTranslation(self.state, xref, self.actuation.nu)
                costModel.addCost(self.rmodel.frames[i.frame].name + "_footTrack", footTrack, 1e6)

        stateWeights = np.array([0.] * 3 + [500.] * 3 + [0.01] * (self.rmodel.nv - 6) + [10.] * 6 + [1.] *
                                (self.rmodel.nv - 6))
        stateReg = crocoddyl.CostModelState(self.state,
                                            crocoddyl.ActivationModelWeightedQuad(np.matrix(stateWeights**2).T),
                                            self.rmodel.defaultState, self.actuation.nu)
        ctrlReg = crocoddyl.CostModelControl(self.state, self.actuation.nu)
        costModel.addCost("stateReg", stateReg, 1e1)
        costModel.addCost("ctrlReg", ctrlReg, 1e-1)

        lb = self.state.diff(0 * self.state.lb, self.state.lb)
        ub = self.state.diff(0 * self.state.ub, self.state.ub)
        stateBounds = crocoddyl.CostModelState(
            self.state, crocoddyl.ActivationModelQuadraticBarrier(crocoddyl.ActivationBounds(lb, ub)),
            0 * self.rmodel.defaultState, self.actuation.nu)
        costModel.addCost("stateBounds", stateBounds, 1e3)

        # Creating the action model for the KKT dynamics with simpletic Euler
        # integration scheme
        dmodel = crocoddyl.DifferentialActionModelContactFwdDynamics(self.state, self.actuation, contactModel,
                                                                     costModel, 0., True)
        model = crocoddyl.IntegratedActionModelEuler(dmodel, timeStep)
        return model
Ejemplo n.º 13
0
    def createSwingFootModel(self,
                             timeStep,
                             supportFootIds,
                             comTask=None,
                             swingFootTask=None):
        """ Action model for a swing foot phase.

        :param timeStep: step duration of the action model
        :param supportFootIds: Ids of the constrained feet
        :param comTask: CoM task
        :param swingFootTask: swinging foot task
        :return action model for a swing foot phase
        """
        # Creating a 6D multi-contact model, and then including the supporting
        # foot
        contactModel = crocoddyl.ContactModelMultiple(self.state,
                                                      self.actuation.nu)
        for i in supportFootIds:
            Mref = crocoddyl.FramePlacement(i, pinocchio.SE3.Identity())
            supportContactModel = \
                crocoddyl.ContactModel6D(self.state, Mref, self.actuation.nu, np.matrix([0., 0.]).T)
            contactModel.addContact(self.rmodel.frames[i].name + "_contact",
                                    supportContactModel)

        # Creating the cost model for a contact phase
        costModel = crocoddyl.CostModelSum(self.state, self.actuation.nu)
        if isinstance(comTask, np.ndarray):
            comTrack = crocoddyl.CostModelCoMPosition(self.state, comTask,
                                                      self.actuation.nu)
            costModel.addCost("comTrack", comTrack, 1e6)
        if swingFootTask is not None:
            for i in swingFootTask:
                footTrack = crocoddyl.CostModelFramePlacement(
                    self.state, i, self.actuation.nu)
                costModel.addCost(
                    self.rmodel.frames[i.frame].name + "_footTrack", footTrack,
                    1e6)

        stateWeights = np.array([0] * 3 + [500.] * 3 + [0.01] *
                                (self.state.nv - 6) + [10] * self.state.nv)
        stateReg = crocoddyl.CostModelState(
            self.state,
            crocoddyl.ActivationModelWeightedQuad(
                np.matrix(stateWeights**2).T), self.rmodel.defaultState,
            self.actuation.nu)
        ctrlReg = crocoddyl.CostModelControl(self.state, self.actuation.nu)
        costModel.addCost("stateReg", stateReg, 1e1)
        costModel.addCost("ctrlReg", ctrlReg, 1e-1)

        # Creating the action model for the KKT dynamics with simpletic Euler
        # integration scheme
        dmodel = crocoddyl.DifferentialActionModelContactFwdDynamics(
            self.state, self.actuation, contactModel, costModel)
        model = crocoddyl.IntegratedActionModelEuler(dmodel, timeStep)
        return model
Ejemplo n.º 14
0
def solver(starting_condition, T=30, precision=1e-9, maxiters=1000):
    """Solve one pendulum problem"""
    robot = example_robot_data.loadDoublePendulum()
    robot_model = robot.model

    state = crocoddyl.StateMultibody(robot_model)
    actModel = ActuationModelDoublePendulum(state, actLink=1)

    weights = np.array([1.5, 1.5, 1, 1] + [0.1] * 2)
    runningCostModel = crocoddyl.CostModelSum(state, actModel.nu)
    dt = 1e-2

    xRegCost = crocoddyl.CostModelState(
        state, crocoddyl.ActivationModelQuad(state.ndx), state.zero(),
        actModel.nu)

    uRegCost = crocoddyl.CostModelControl(state,
                                          crocoddyl.ActivationModelQuad(1),
                                          actModel.nu)
    xPendCost = CostModelDoublePendulum(
        state, crocoddyl.ActivationModelWeightedQuad(weights), actModel.nu)

    runningCostModel.addCost("uReg", uRegCost, 1e-4 / dt)
    runningCostModel.addCost("xGoal", xPendCost, 1e-5 / dt)

    runningModel = crocoddyl.IntegratedActionModelEuler(
        crocoddyl.DifferentialActionModelFreeFwdDynamics(
            state, actModel, runningCostModel), dt)

    terminalCostModel = crocoddyl.CostModelSum(state, actModel.nu)
    terminalCostModel.addCost("xGoal", xPendCost, 1e4)
    terminal_model = crocoddyl.IntegratedActionModelEuler(
        crocoddyl.DifferentialActionModelFreeFwdDynamics(
            state, actModel, terminalCostModel), dt)

    problem = crocoddyl.ShootingProblem(starting_condition, [runningModel] * T,
                                        terminal_model)

    fddp = crocoddyl.SolverFDDP(problem)
    fddp.th_stop = precision

    fddp.solve([], [], maxiters)
Ejemplo n.º 15
0
class AnymalIntegratedRK4Test(ActionModelAbstractTestCase):
    ROBOT_MODEL = example_robot_data.load('anymal').model
    STATE = crocoddyl.StateMultibody(ROBOT_MODEL)
    ACTUATION = crocoddyl.ActuationModelFloatingBase(STATE)
    COST_SUM = crocoddyl.CostModelSum(STATE, ACTUATION.nu)
    COST_SUM.addCost("xReg", crocoddyl.CostModelResidual(STATE, crocoddyl.ResidualModelState(STATE, ACTUATION.nu)),
                     1e-7)
    COST_SUM.addCost("uReg", crocoddyl.CostModelResidual(STATE, crocoddyl.ResidualModelControl(STATE, ACTUATION.nu)),
                     1e-7)
    DIFFERENTIAL = crocoddyl.DifferentialActionModelFreeFwdDynamics(STATE, ACTUATION, COST_SUM)
    MODEL = crocoddyl.IntegratedActionModelRK4(DIFFERENTIAL, 1e-3)
    MODEL_DER = IntegratedActionModelRK4Derived(DIFFERENTIAL, 1e-3)
Ejemplo n.º 16
0
class FreeFwdDynamicsTest(ActionModelAbstractTestCase):
    ROBOT_MODEL = pinocchio.buildSampleModelManipulator()
    STATE = crocoddyl.StateMultibody(ROBOT_MODEL)
    COST_SUM = crocoddyl.CostModelSum(STATE, ROBOT_MODEL.nv)
    COST_SUM.addCost('xReg', crocoddyl.CostModelState(STATE), 1.)
    COST_SUM.addCost(
        'frTrack',
        crocoddyl.CostModelFramePlacement(
            STATE,
            crocoddyl.FramePlacement(ROBOT_MODEL.getFrameId("effector_body"),
                                     pinocchio.SE3.Random())), 1.)
    MODEL = crocoddyl.DifferentialActionModelFreeFwdDynamics(STATE, COST_SUM)
    MODEL_DER = DifferentialFreeFwdDynamicsDerived(STATE, COST_SUM)
Ejemplo n.º 17
0
    def createFootSwitchModel(self, supportFootIds, swingFootTask):
        """ Action model for a foot switch phase.

        :param supportFootIds: Ids of the constrained feet
        :param swingFootTask: swinging foot task
        :return action model for a foot switch phase
        """
        # Creating a 3D multi-contact model, and then including the supporting
        # foot
        contactModel = crocoddyl.ContactModelMultiple(self.state,
                                                      self.actuation.nu)
        for i in supportFootIds:
            xref = crocoddyl.FrameTranslation(i, np.matrix([0., 0., 0.]).T)
            supportContactModel = crocoddyl.ContactModel3D(
                self.state, xref, self.actuation.nu,
                np.matrix([0., 0.]).T)
            contactModel.addContact('contact_' + str(i), supportContactModel)

        # Creating the cost model for a contact phase
        costModel = crocoddyl.CostModelSum(self.state, self.actuation.nu)
        if swingFootTask is not None:
            for i in swingFootTask:
                xref = crocoddyl.FrameTranslation(i.frame, i.oMf.translation)
                footTrack = crocoddyl.CostModelFrameTranslation(
                    self.state, xref, self.actuation.nu)
                costModel.addCost("footTrack_" + str(i), footTrack, 1e7)
        stateWeights = np.array([0] * 3 + [500.] * 3 + [0.01] *
                                (self.rmodel.nv - 6) + [10] * self.rmodel.nv)
        stateReg = crocoddyl.CostModelState(
            self.state,
            crocoddyl.ActivationModelWeightedQuad(
                np.matrix(stateWeights**2).T), self.rmodel.defaultState,
            self.actuation.nu)
        ctrlReg = crocoddyl.CostModelControl(self.state, self.actuation.nu)
        costModel.addCost("stateReg", stateReg, 1e1)
        costModel.addCost("ctrlReg", ctrlReg, 1e-3)

        for i in swingFootTask:
            vref = crocoddyl.FrameMotion(i.frame, pinocchio.Motion.Zero())
            impactFootVelCost = crocoddyl.CostModelFrameVelocity(
                self.state, vref, self.actuation.nu)
            costModel.addCost('impactVel_' + str(i.frame), impactFootVelCost,
                              1e6)

        # Creating the action model for the KKT dynamics with simpletic Euler
        # integration scheme
        dmodel = crocoddyl.DifferentialActionModelContactFwdDynamics(
            self.state, self.actuation, contactModel, costModel)
        model = crocoddyl.IntegratedActionModelEuler(dmodel, 0.)
        return model
Ejemplo n.º 18
0
class TalosArmFreeFwdDynamicsTest(ActionModelAbstractTestCase):
    ROBOT_MODEL = example_robot_data.loadTalosArm().model
    STATE = crocoddyl.StateMultibody(ROBOT_MODEL)
    ACTUATION = crocoddyl.ActuationModelFull(STATE)
    COST_SUM = crocoddyl.CostModelSum(STATE)
    COST_SUM.addCost(
        'gripperPose',
        crocoddyl.CostModelFramePlacement(
            STATE, crocoddyl.FramePlacement(ROBOT_MODEL.getFrameId("gripper_left_joint"), pinocchio.SE3.Random())),
        1e-3)
    COST_SUM.addCost("xReg", crocoddyl.CostModelState(STATE), 1e-7)
    COST_SUM.addCost("uReg", crocoddyl.CostModelControl(STATE), 1e-7)
    MODEL = crocoddyl.DifferentialActionModelFreeFwdDynamics(STATE, ACTUATION, COST_SUM)
    MODEL_DER = DifferentialFreeFwdDynamicsDerived(STATE, ACTUATION, COST_SUM)
Ejemplo n.º 19
0
class ManipulatorDDPTest(SolverAbstractTestCase):
    ROBOT_MODEL = pinocchio.buildSampleModelManipulator()
    STATE = crocoddyl.StateMultibody(ROBOT_MODEL)
    COST_SUM = crocoddyl.CostModelSum(STATE, ROBOT_MODEL.nv)
    COST_SUM.addCost('xReg', crocoddyl.CostModelState(STATE), 1e-7)
    COST_SUM.addCost('uReg', crocoddyl.CostModelControl(STATE), 1e-7)
    COST_SUM.addCost(
        'frTrack',
        crocoddyl.CostModelFramePlacement(
            STATE, crocoddyl.FramePlacement(ROBOT_MODEL.getFrameId("effector_body"), pinocchio.SE3.Random())), 1.)
    DIFF_MODEL = crocoddyl.DifferentialActionModelFreeFwdDynamics(STATE, COST_SUM)
    MODEL = crocoddyl.IntegratedActionModelEuler(crocoddyl.DifferentialActionModelFreeFwdDynamics(STATE, COST_SUM),
                                                 1e-3)
    SOLVER = crocoddyl.SolverDDP
    SOLVER_DER = DDPDerived
Ejemplo n.º 20
0
    def setUp(self):
        self.robot_data = self.ROBOT_MODEL.createData()
        self.x = self.ROBOT_STATE.rand()
        self.u = pinocchio.utils.rand(self.ROBOT_MODEL.nv)

        self.cost_sum = crocoddyl.CostModelSum(self.ROBOT_STATE)
        self.cost_sum.addCost('myCost', self.COST, 1.)

        self.multibody_data = crocoddyl.DataCollectorMultibody(self.robot_data)
        self.data = self.COST.createData(self.multibody_data)
        self.data_sum = self.cost_sum.createData(self.multibody_data)

        nq, nv = self.ROBOT_MODEL.nq, self.ROBOT_MODEL.nv
        pinocchio.forwardKinematics(self.ROBOT_MODEL, self.robot_data, self.x[:nq], self.x[nq:])
        pinocchio.computeForwardKinematicsDerivatives(self.ROBOT_MODEL, self.robot_data, self.x[:nq], self.x[nq:],
                                                      pinocchio.utils.zero(nv))
        pinocchio.computeJointJacobians(self.ROBOT_MODEL, self.robot_data, self.x[:nq])
        pinocchio.updateFramePlacements(self.ROBOT_MODEL, self.robot_data)
        pinocchio.jacobianCenterOfMass(self.ROBOT_MODEL, self.robot_data, self.x[:nq], False)
Ejemplo n.º 21
0
class TalosArmFDDPTest(SolverAbstractTestCase):
    ROBOT_MODEL = example_robot_data.load('talos_arm').model
    STATE = crocoddyl.StateMultibody(ROBOT_MODEL)
    ACTUATION = crocoddyl.ActuationModelFull(STATE)
    COST_SUM = crocoddyl.CostModelSum(STATE)
    COST_SUM.addCost(
        'gripperPose',
        crocoddyl.CostModelFramePlacement(
            STATE,
            crocoddyl.FramePlacement(
                ROBOT_MODEL.getFrameId("gripper_left_joint"),
                pinocchio.SE3.Random())), 1e-3)
    COST_SUM.addCost("xReg", crocoddyl.CostModelState(STATE), 1e-7)
    COST_SUM.addCost("uReg", crocoddyl.CostModelControl(STATE), 1e-7)
    DIFF_MODEL = crocoddyl.DifferentialActionModelFreeFwdDynamics(
        STATE, ACTUATION, COST_SUM)
    MODEL = crocoddyl.IntegratedActionModelEuler(DIFF_MODEL, 1e-3)
    SOLVER = crocoddyl.SolverFDDP
    SOLVER_DER = FDDPDerived
Ejemplo n.º 22
0
    def createImpulseModel(self, supportFootIds, swingFootTask):
        """ Action model for impulse models.

        An impulse model consists of describing the impulse dynamics against a set of contacts.
        :param supportFootIds: Ids of the constrained feet
        :param swingFootTask: swinging foot task
        :return impulse action model
        """
        # Creating a 6D multi-contact model, and then including the supporting foot
        impulseModel = crocoddyl.ImpulseModelMultiple(self.state)
        for i in supportFootIds:
            supportContactModel = crocoddyl.ImpulseModel6D(self.state, i)
            impulseModel.addImpulse(self.rmodel.frames[i].name + "_impulse",
                                    supportContactModel)

        # Creating the cost model for a contact phase
        costModel = crocoddyl.CostModelSum(self.state, 0)
        if swingFootTask is not None:
            for i in swingFootTask:
                frameTranslationResidual = crocoddyl.ResidualModelFrameTranslation(
                    self.state, i[0], i[1].translation, 0)
                footTrack = crocoddyl.CostModelResidual(
                    self.state, frameTranslationResidual)
                costModel.addCost(self.rmodel.frames[i[0]].name + "_footTrack",
                                  footTrack, 1e8)

        stateWeights = np.array([1.] * 6 + [0.1] * (self.rmodel.nv - 6) +
                                [10] * self.rmodel.nv)
        stateResidual = crocoddyl.ResidualModelState(self.state,
                                                     self.rmodel.defaultState,
                                                     0)
        stateActivation = crocoddyl.ActivationModelWeightedQuad(
            stateWeights**2)
        stateReg = crocoddyl.CostModelResidual(self.state, stateActivation,
                                               stateResidual)
        costModel.addCost("stateReg", stateReg, 1e1)

        # Creating the action model for the KKT dynamics with simpletic Euler
        # integration scheme
        model = crocoddyl.ActionModelImpulseFwdDynamics(
            self.state, impulseModel, costModel)
        return model
Ejemplo n.º 23
0
WITHPLOT = 'plot' in sys.argv or 'CROCODDYL_PLOT' in os.environ

crocoddyl.switchToNumpyMatrix()

# In this example test, we will solve the reaching-goal task with the Talos arm.
# For that, we use the forward dynamics (with its analytical derivatives)
# developed inside crocoddyl; it describes inside DifferentialActionModelFullyActuated class.
# Finally, we use an Euler sympletic integration scheme.

# First, let's load the Pinocchio model for the Talos arm.
talos_arm = example_robot_data.loadTalosArm()
robot_model = talos_arm.model

# Create a cost model per the running and terminal action model.
state = crocoddyl.StateMultibody(robot_model)
runningCostModel = crocoddyl.CostModelSum(state)
terminalCostModel = crocoddyl.CostModelSum(state)

# Note that we need to include a cost model (i.e. set of cost functions) in
# order to fully define the action model for our optimal control problem.
# For this particular example, we formulate three running-cost functions:
# goal-tracking cost, state and control regularization; and one terminal-cost:
# goal cost. First, let's create the common cost functions.
Mref = crocoddyl.FramePlacement(
    robot_model.getFrameId("gripper_left_joint"),
    pinocchio.SE3(np.eye(3), np.matrix([[.0], [.0], [.4]])))
goalTrackingCost = crocoddyl.CostModelFramePlacement(state, Mref)
xRegCost = crocoddyl.CostModelState(state)
uRegCost = crocoddyl.CostModelControl(state)

# Then let's added the running and terminal cost functions
Ejemplo n.º 24
0
def solve_problem(terminal_model=None,
                  initial_configuration=None,
                  horizon: int = 100,
                  precision: float = 1e-9,
                  maxiters: int = 1000):
    """
    Solve the problem for a given initial_position.
    
    @params:
        1: terminal_model    = Terminal model with neural network inside it, for the crocoddyl problem.
                               If none, then Crocoddyl Integrated Action Model will be used as terminal model.
        
        2: initial_configuration = initial position for the unicycle, 
                                    either a list or a numpy array or a tensor.
        
        3: horizon           = Time horizon for the unicycle. Defaults to 100
        
        4: stop              = ddp.th_stop. Defaults to 1e-9
        
        5: maxiters          = maximum iterations allowed for crocoddyl.Defaults to 1000

        
    @returns:
        ddp
    """

    if isinstance(initial_configuration, list):
        initial_configuration = np.array(initial_configuration)

    elif isinstance(initial_configuration, torch.Tensor):
        initial_configuration = initial_configuration.numpy()

    # Loading the double pendulum model
    robot = example_robot_data.loadDoublePendulum()
    robot_model = robot.model

    state = crocoddyl.StateMultibody(robot_model)
    actModel = ActuationModelDoublePendulum(state, actLink=1)

    weights = np.array([1, 1, 1, 1] + [0.1] * 2)
    runningCostModel = crocoddyl.CostModelSum(state, actModel.nu)
    xRegCost = crocoddyl.CostModelState(
        state, crocoddyl.ActivationModelQuad(state.ndx), state.zero(),
        actModel.nu)

    uRegCost = crocoddyl.CostModelControl(state,
                                          crocoddyl.ActivationModelQuad(1),
                                          actModel.nu)

    xPendCost = CostModelDoublePendulum(
        state, crocoddyl.ActivationModelWeightedQuad(np.matrix(weights).T),
        actModel.nu)

    dt = 1e-2

    runningCostModel.addCost("uReg", uRegCost, 1e-4 / dt)
    runningCostModel.addCost("xGoal", xPendCost, 1e-5 / dt)

    runningModel = crocoddyl.IntegratedActionModelEuler(
        crocoddyl.DifferentialActionModelFreeFwdDynamics(
            state, actModel, runningCostModel), dt)

    if terminal_model is None:
        terminalCostModel = crocoddyl.CostModelSum(state, actModel.nu)
        terminalCostModel.addCost("xGoal", xPendCost, 1e4)
        terminal_model = crocoddyl.IntegratedActionModelEuler(
            crocoddyl.DifferentialActionModelFreeFwdDynamics(
                state, actModel, terminalCostModel), dt)

    # Creating the shooting problem and the FDDP solver
    problem = crocoddyl.ShootingProblem(initial_configuration.T,
                                        [runningModel] * horizon,
                                        terminal_model)

    fddp = crocoddyl.SolverFDDP(problem)

    fddp.th_stop = precision

    fddp.solve([], [], maxiters)

    return fddp
Ejemplo n.º 25
0
target_pos = np.array([1, 0, 1])
target_quat = pinocchio.Quaternion(1, 0, 0, 0)

state = crocoddyl.StateMultibody(robot_model)
d_cog = 0.1525
cf = 6.6e-5
cm = 1e-6
u_lim = 5
l_lim = 0.1
tau_f = np.array([[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0], [0.0, d_cog, 0.0, -d_cog],
                  [-d_cog, 0.0, d_cog, 0.0], [-cm / cf, cm / cf, -cm / cf, cm / cf]])

actModel = crocoddyl.ActuationModelMultiCopterBase(state, 4, tau_f)

runningCostModel = crocoddyl.CostModelSum(state, actModel.nu)
terminalCostModel = crocoddyl.CostModelSum(state, actModel.nu)

# Needed objects to create the costs
Mref = crocoddyl.FramePlacement(robot_model.getFrameId("base_link"), pinocchio.SE3(target_quat.matrix(), target_pos))
wBasePos, wBaseOri, wBaseVel = 0.1, 1000, 1000
stateWeights = np.array([wBasePos] * 3 + [wBaseOri] * 3 + [wBaseVel] * robot_model.nv)

# Costs
goalTrackingCost = crocoddyl.CostModelFramePlacement(state, Mref, actModel.nu)
xRegCost = crocoddyl.CostModelState(state, crocoddyl.ActivationModelWeightedQuad(stateWeights), state.zero(),
                                    actModel.nu)
uRegCost = crocoddyl.CostModelControl(state, actModel.nu)
runningCostModel.addCost("xReg", xRegCost, 1e-6)
runningCostModel.addCost("uReg", uRegCost, 1e-6)
runningCostModel.addCost("trackPose", goalTrackingCost, 1e-2)
Ejemplo n.º 26
0
    def createSwingFootModel(self,
                             timeStep,
                             supportFootIds,
                             comTask=None,
                             swingFootTask=None):
        """ Action model for a swing foot phase.

        :param timeStep: step duration of the action model
        :param supportFootIds: Ids of the constrained feet
        :param comTask: CoM task
        :param swingFootTask: swinging foot task
        :return action model for a swing foot phase
        """
        # Creating a 6D multi-contact model, and then including the supporting
        # foot
        nu = self.actuation.nu
        contactModel = crocoddyl.ContactModelMultiple(self.state, nu)
        for i in supportFootIds:
            supportContactModel = \
                crocoddyl.ContactModel6D(self.state, i,
                                         pinocchio.SE3.Identity(),
                                         nu, np.array([0., 0.]))
            contactModel.addContact(self.rmodel.frames[i].name + "_contact",
                                    supportContactModel)

        # Creating the cost model for a contact phase
        costModel = crocoddyl.CostModelSum(self.state, nu)
        if isinstance(comTask, np.ndarray):
            comResidual = crocoddyl.ResidualModelCoMPosition(
                self.state, comTask, nu)
            comTrack = crocoddyl.CostModelResidual(self.state, comResidual)
            costModel.addCost("comTrack", comTrack, 1e6)
        for i in supportFootIds:
            cone = crocoddyl.WrenchCone(self.Rsurf, self.mu,
                                        np.array([0.1, 0.05]))
            wrenchResidual = crocoddyl.ResidualModelContactWrenchCone(
                self.state, i, cone, nu)
            wrenchActivation = crocoddyl.ActivationModelQuadraticBarrier(
                crocoddyl.ActivationBounds(cone.lb, cone.ub))
            wrenchCone = crocoddyl.CostModelResidual(self.state,
                                                     wrenchActivation,
                                                     wrenchResidual)
            costModel.addCost(self.rmodel.frames[i].name + "_wrenchCone",
                              wrenchCone, 1e1)

        if swingFootTask is not None:
            for i in swingFootTask:
                framePlacementResidual = crocoddyl.ResidualModelFramePlacement(
                    self.state, i[0], i[1], nu)
                footTrack = crocoddyl.CostModelResidual(
                    self.state, framePlacementResidual)
                costModel.addCost(self.rmodel.frames[i[0]].name + "_footTrack",
                                  footTrack, 1e6)

        stateWeights = np.array([0] * 3 + [500.] * 3 + [0.01] *
                                (self.state.nv - 6) + [10] * self.state.nv)
        stateResidual = crocoddyl.ResidualModelState(self.state,
                                                     self.rmodel.defaultState,
                                                     nu)
        stateActivation = crocoddyl.ActivationModelWeightedQuad(
            stateWeights**2)
        ctrlResidual = crocoddyl.ResidualModelControl(self.state, nu)
        stateReg = crocoddyl.CostModelResidual(self.state, stateActivation,
                                               stateResidual)
        ctrlReg = crocoddyl.CostModelResidual(self.state, ctrlResidual)
        costModel.addCost("stateReg", stateReg, 1e1)
        costModel.addCost("ctrlReg", ctrlReg, 1e-1)

        # Creating the action model for the KKT dynamics with simpletic Euler
        # integration scheme
        dmodel = crocoddyl.DifferentialActionModelContactFwdDynamics(
            self.state, self.actuation, contactModel, costModel, 0., True)
        model = crocoddyl.IntegratedActionModelEuler(dmodel, timeStep)
        return model
Ejemplo n.º 27
0
    def optimalSolution(init_states: Union[list, np.ndarray, torch.Tensor],
                        terminal_model: crocoddyl.ActionModelAbstract = None,
                        horizon: int = 150,
                        precision: float = 1e-9,
                        maxiters: int = 1000,
                        use_fddp: bool = True):
        """Solve double pendulum problem with the given terminal model for the given position
        
        Parameters
        ----------
        init_states   : list or array or tensor
                            These are the initial, starting configurations for the double pendulum
        
        terminal_model: crocoddyl.ActionModelAbstract
                            The terminal model to be used to solve the problem
                            
        horizon       : int
                            Time horizon for the running model
                            
        precision     : float
                            precision for ddp.th_stop
                            
        maxiters      : int
                            Maximum iterations allowed for the problem
                            
        use_fddp      : boolean
                            Solve using ddp or fddp
        
        Returns
        --------
        
        ddp           : crocoddyl.Solverddp
                            the optimal ddp or fddp of the prblem
        """

        if isinstance(init_states, torch.Tensor):
            init_states = init_states.numpy()
        init_states = np.atleast_2d(init_states)

        solutions = []

        for init_state in init_states:
            robot = example_robot_data.loadDoublePendulum()
            robot_model = robot.model

            state = crocoddyl.StateMultibody(robot_model)
            actModel = ActuationModelDoublePendulum(state, actLink=1)

            weights = np.array([1.5, 1.5, 1, 1] + [0.1] * 2)
            runningCostModel = crocoddyl.CostModelSum(state, actModel.nu)
            dt = 1e-2

            xRegCost = crocoddyl.CostModelState(
                state, crocoddyl.ActivationModelQuad(state.ndx), state.zero(),
                actModel.nu)

            uRegCost = crocoddyl.CostModelControl(
                state, crocoddyl.ActivationModelQuad(1), actModel.nu)
            xPendCost = CostModelDoublePendulum(
                state, crocoddyl.ActivationModelWeightedQuad(weights),
                actModel.nu)

            runningCostModel.addCost("uReg", uRegCost, 1e-4 / dt)
            runningCostModel.addCost("xGoal", xPendCost, 1e-5 / dt)

            runningModel = crocoddyl.IntegratedActionModelEuler(
                crocoddyl.DifferentialActionModelFreeFwdDynamics(
                    state, actModel, runningCostModel), dt)

            if terminal_model is None:
                terminalCostModel = crocoddyl.CostModelSum(state, actModel.nu)
                terminalCostModel.addCost("xGoal", xPendCost, 1e4)
                terminal_model = crocoddyl.IntegratedActionModelEuler(
                    crocoddyl.DifferentialActionModelFreeFwdDynamics(
                        state, actModel, terminalCostModel), dt)

            problem = crocoddyl.ShootingProblem(init_state,
                                                [runningModel] * horizon,
                                                terminal_model)
            if use_fddp:
                fddp = crocoddyl.SolverFDDP(problem)
            else:
                fddp = crocoddyl.SolverDDP(problem)

            fddp.th_stop = precision

            fddp.solve([], [], maxiters)

            solutions.append(fddp)
        return solutions
Ejemplo n.º 28
0
ROBOT_STATE = crocoddyl.StateMultibody(ROBOT_MODEL)
ACTUATION = crocoddyl.ActuationModelFloatingBase(ROBOT_STATE)
CONTACTS = crocoddyl.ContactModelMultiple(ROBOT_STATE, ACTUATION.nu)
CONTACT_6D_1 = crocoddyl.ContactModel6D(
    ROBOT_STATE,
    crocoddyl.FramePlacement(ROBOT_MODEL.getFrameId('r_sole'),
                             pinocchio.SE3.Random()), ACTUATION.nu,
    pinocchio.utils.rand(2))
CONTACT_6D_2 = crocoddyl.ContactModel6D(
    ROBOT_STATE,
    crocoddyl.FramePlacement(ROBOT_MODEL.getFrameId('l_sole'),
                             pinocchio.SE3.Random()), ACTUATION.nu,
    pinocchio.utils.rand(2))
CONTACTS.addContact("r_sole_contact", CONTACT_6D_1)
CONTACTS.addContact("l_sole_contact", CONTACT_6D_2)
COSTS = crocoddyl.CostModelSum(ROBOT_STATE, ACTUATION.nu, False)
COSTS.addCost(
    "force",
    crocoddyl.CostModelContactForce(
        ROBOT_STATE,
        crocoddyl.FrameForce(ROBOT_MODEL.getFrameId('r_sole'),
                             pinocchio.Force.Random()), ACTUATION.nu), 1.)
MODEL = crocoddyl.DifferentialActionModelContactFwdDynamics(
    ROBOT_STATE, ACTUATION, CONTACTS, COSTS, 0., True)
DATA = MODEL.createData()

# Created DAM numdiff
MODEL_ND = crocoddyl.DifferentialActionModelNumDiff(MODEL)
dnum = MODEL_ND.createData()

x = ROBOT_STATE.rand()
Ejemplo n.º 29
0
# Create robot model and data
ROBOT_MODEL = example_robot_data.loadICub().model
ROBOT_DATA = ROBOT_MODEL.createData()

# Create differential action model and data; link the contact data
ROBOT_STATE = crocoddyl.StateMultibody(ROBOT_MODEL)
ACTUATION = crocoddyl.ActuationModelFloatingBase(ROBOT_STATE)
IMPULSES = crocoddyl.ImpulseModelMultiple(ROBOT_STATE)
IMPULSE_6D = crocoddyl.ImpulseModel6D(ROBOT_STATE,
                                      ROBOT_MODEL.getFrameId('r_sole'))
IMPULSE_3D = crocoddyl.ImpulseModel3D(ROBOT_STATE,
                                      ROBOT_MODEL.getFrameId('l_sole'))
IMPULSES.addImpulse("r_sole_impulse", IMPULSE_6D)
IMPULSES.addImpulse("l_sole_impulse", IMPULSE_3D)
COSTS = crocoddyl.CostModelSum(ROBOT_STATE, 0)

COSTS.addCost("impulse_com", crocoddyl.CostModelImpulseCoM(ROBOT_STATE), 1.)
MODEL = crocoddyl.ActionModelImpulseFwdDynamics(ROBOT_STATE, IMPULSES, COSTS,
                                                0., 0., True)
DATA = MODEL.createData()

# Created DAM numdiff
MODEL_ND = crocoddyl.ActionModelNumDiff(MODEL)
MODEL_ND.disturbance *= 10
dnum = MODEL_ND.createData()

x = ROBOT_STATE.rand()
u = pinocchio.utils.rand(0)  # (ACTUATION.nu)
MODEL.calc(DATA, x, u)
MODEL.calcDiff(DATA, x, u)
Ejemplo n.º 30
0
Pref = crocoddyl.FramePlacement(
    leftFootId, pinocchio.SE3(np.eye(3), np.array([0., 0.4, 0.])))
footTrackingCost1 = crocoddyl.CostModelFramePlacement(
    state, crocoddyl.ActivationModelWeightedQuad(footTrackingWeights**2), Pref,
    actuation.nu)
Pref = crocoddyl.FramePlacement(
    leftFootId, pinocchio.SE3(np.eye(3), np.array([0.3, 0.15, 0.35])))
footTrackingCost2 = crocoddyl.CostModelFramePlacement(
    state, crocoddyl.ActivationModelWeightedQuad(footTrackingWeights**2), Pref,
    actuation.nu)

# Cost for CoM reference
comTrack = crocoddyl.CostModelCoMPosition(state, comRef, actuation.nu)

# Create cost model per each action model. We divide the motion in 3 phases plus its terminal model
runningCostModel1 = crocoddyl.CostModelSum(state, actuation.nu)
runningCostModel2 = crocoddyl.CostModelSum(state, actuation.nu)
runningCostModel3 = crocoddyl.CostModelSum(state, actuation.nu)
terminalCostModel = crocoddyl.CostModelSum(state, actuation.nu)

# Then let's added the running and terminal cost functions
runningCostModel1.addCost("gripperPose", handTrackingCost, 1e2)
runningCostModel1.addCost("stateReg", xRegCost, 1e-3)
runningCostModel1.addCost("ctrlReg", uRegCost, 1e-4)
runningCostModel1.addCost("limitCost", limitCost, 1e3)

runningCostModel2.addCost("gripperPose", handTrackingCost, 1e2)
runningCostModel2.addCost("footPose", footTrackingCost1, 1e1)
runningCostModel2.addCost("stateReg", xRegCost, 1e-3)
runningCostModel2.addCost("ctrlReg", uRegCost, 1e-4)
runningCostModel2.addCost("limitCost", limitCost, 1e3)