task-com-equality.cpp 3.74 KB
Newer Older
1
2
3
//
// Copyright (c) 2017 CNRS
//
jcarpent's avatar
jcarpent committed
4
5
// This file is part of tsid
// tsid is free software: you can redistribute it
6
7
8
// and/or modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation, either version
// 3 of the License, or (at your option) any later version.
jcarpent's avatar
jcarpent committed
9
// tsid is distributed in the hope that it will be
10
11
12
13
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Lesser Public License for more details. You should have
// received a copy of the GNU Lesser General Public License along with
jcarpent's avatar
jcarpent committed
14
// tsid If not, see
15
16
17
// <http://www.gnu.org/licenses/>.
//

jcarpent's avatar
jcarpent committed
18
19
#include "tsid/tasks/task-com-equality.hpp"
#include "tsid/robots/robot-wrapper.hpp"
20

jcarpent's avatar
jcarpent committed
21
namespace tsid
22
23
24
{
  namespace tasks
  {
25
26
    using namespace math;
    using namespace trajectories;
27
    using namespace pinocchio;
28
29
30
31
32
33
34
35

    TaskComEquality::TaskComEquality(const std::string & name,
                                     RobotWrapper & robot):
      TaskMotion(name, robot),
      m_constraint(name, 3, robot.nv())
    {
      m_Kp.setZero(3);
      m_Kd.setZero(3);
36
37
38
39
40
      m_p_error_vec.setZero(3);
      m_v_error_vec.setZero(3);
      m_p_com.setZero(3);
      m_v_com.setZero(3);
      m_a_des_vec.setZero(3);
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
    }

    int TaskComEquality::dim() const
    {
      //return self._mask.sum ()
      return 3;
    }

    const Vector3 & TaskComEquality::Kp(){ return m_Kp; }

    const Vector3 & TaskComEquality::Kd(){ return m_Kd; }

    void TaskComEquality::Kp(ConstRefVector Kp)
    {
      assert(Kp.size()==3);
      m_Kp = Kp;
    }

    void TaskComEquality::Kd(ConstRefVector Kd)
    {
      assert(Kd.size()==3);
      m_Kd = Kd;
    }

    void TaskComEquality::setReference(const TrajectorySample & ref)
    {
      m_ref = ref;
    }

andreadelprete's avatar
andreadelprete committed
70
71
72
73
74
75
76
    const TrajectorySample & TaskComEquality::getReference() const
    {
      return m_ref;
    }

    const Vector & TaskComEquality::getDesiredAcceleration() const
    {
77
      return m_a_des_vec;
andreadelprete's avatar
andreadelprete committed
78
79
80
81
82
83
84
    }

    Vector TaskComEquality::getAcceleration(ConstRefVector dv) const
    {
      return m_constraint.matrix()*dv - m_drift;
    }

85
    const Vector & TaskComEquality::position_error() const
86
    {
87
      return m_p_error_vec;
88
89
    }

90
    const Vector & TaskComEquality::velocity_error() const
91
    {
92
93
94
95
96
      return m_v_error_vec;
    }

    const Vector & TaskComEquality::position() const
    {
97
      return m_p_com;
98
99
100
101
    }

    const Vector & TaskComEquality::velocity() const
    {
102
      return m_v_com;
103
104
105
106
    }

    const Vector & TaskComEquality::position_ref() const
    {
107
      return m_ref.pos;
108
109
110
111
    }

    const Vector & TaskComEquality::velocity_ref() const
    {
112
      return m_ref.vel;
113
114
115
116
117
    }

    const ConstraintBase & TaskComEquality::getConstraint() const
    {
      return m_constraint;
118
119
    }

Olivier Stasse's avatar
Olivier Stasse committed
120
121
122
    const ConstraintBase & TaskComEquality::compute(const double ,
                                                    ConstRefVector ,
                                                    ConstRefVector ,
123
                                                    Data & data)
124
    {
125
      m_robot.com(data, m_p_com, m_v_com, m_drift);
126
127

      // Compute errors
128
129
      m_p_error = m_p_com - m_ref.pos;
      m_v_error = m_v_com - m_ref.vel;
andreadelprete's avatar
andreadelprete committed
130
131
132
      m_a_des = - m_Kp.cwiseProduct(m_p_error)
                - m_Kd.cwiseProduct(m_v_error)
                + m_ref.acc;
133

134
135
      m_p_error_vec = m_p_error;
      m_v_error_vec = m_v_error;
136
      m_a_des_vec = m_a_des;
137
138
139
140
141
#ifndef NDEBUG
//      std::cout<<m_name<<" errors: "<<m_p_error.norm()<<" "
//        <<m_v_error.norm()<<std::endl;
#endif

142
143
144
145
      // Get CoM jacobian
      const Matrix3x & Jcom = m_robot.Jcom(data);

      m_constraint.setMatrix(Jcom);
andreadelprete's avatar
andreadelprete committed
146
      m_constraint.setVector(m_a_des - m_drift);
147
148
      return m_constraint;
    }
149

150
151
  }
}