Skip to content
Snippets Groups Projects
Target.py 1.55 KiB
Newer Older
Ale's avatar
Ale committed
import numpy as np
from .ProblemData import ProblemData
import pinocchio as pin

Ale's avatar
Ale committed
class Target:
Fanny Risbourg's avatar
Fanny Risbourg committed
    def __init__(self, pd: ProblemData):
Ale's avatar
Ale committed
        self.pd = pd
        self.dt = pd.dt

        self.gait = ([] + \
                    [[0, 0, 0, 0]] * pd.init_steps + \
                    [[0, 0, 0, 0]] * pd.target_steps )
Ale's avatar
Ale committed

        self.T = pd.T
Fanny Risbourg's avatar
Fanny Risbourg committed
        self.contactSequence = [self.patternToId(p) for p in self.gait]
Ale's avatar
Ale committed

        self.target = {pd.rfFootId: []}
Ale's avatar
Ale committed
        q = pd.q0_reduced
        v = pd.v0_reduced
Ale's avatar
Ale committed
        pin.forwardKinematics(pd.model, pd.rdata, q, v)
        pin.updateFramePlacements(pd.model, pd.rdata)
        self.FR_foot0 = pd.rdata.oMf[pd.rfFootId].translation.copy()
Ale's avatar
Ale committed
        self.A = np.array([0, 0.03, 0.03])
        self.offset = np.array([0.05, -0.02, 0.06])
Ale's avatar
Ale committed
        self.freq = np.array([0, 0.5 , 0.5 ])
Ale's avatar
Ale committed
        self.phase = np.array([0, np.pi / 2, 0])
Ale's avatar
Ale committed

    def patternToId(self, gait):
Fanny Risbourg's avatar
Fanny Risbourg committed
        return tuple(self.pd.allContactIds[i] for i, c in enumerate(gait) if c == 1)

Ale's avatar
Ale committed
    def shift_gait(self):
        self.gait.pop(0)
        self.gait += [self.gait[-1]]
Fanny Risbourg's avatar
Fanny Risbourg committed
        self.contactSequence = [self.patternToId(p) for p in self.gait]
Ale's avatar
Ale committed

    def update(self, t):
        target = []
Ale's avatar
Ale committed
        for n in range(self.T):
Fanny Risbourg's avatar
Fanny Risbourg committed
            target += [
                self.FR_foot0
                + self.offset
                + self.A
                * np.sin(2 * np.pi * self.freq * (n + t) * self.dt + self.phase)
            ]
Ale's avatar
Ale committed
        self.target[self.pd.rfFootId] = np.array(target)
Ale's avatar
Ale committed
    def evaluate_in_t(self, t):
Fanny Risbourg's avatar
Fanny Risbourg committed
        return {e: self.target[e][t] for e in self.target}