task-com-equality.cpp 4.56 KB
Newer Older
1
//
2
// Copyright (c) 2017-2020 CNRS, Inria
3
//
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
      m_mask.resize(3);
      m_mask.fill(1.);
      setMask(m_mask);      
    }


    void TaskComEquality::setMask(math::ConstRefVector mask)
    {
      assert(mask.size() == 3);
      TaskMotion::setMask(mask);
      int n = dim();
      m_constraint.resize(n, m_robot.nv());
      m_p_error_masked_vec.resize(n);
      m_v_error_masked_vec.resize(n);
      m_drift_masked.resize(n);
      m_a_des_masked.resize(n);
57
58
59
60
    }

    int TaskComEquality::dim() const
    {
61
      return int(m_mask.sum());
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    }

    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
85
86
87
88
89
90
91
    const TrajectorySample & TaskComEquality::getReference() const
    {
      return m_ref;
    }

    const Vector & TaskComEquality::getDesiredAcceleration() const
    {
92
      return m_a_des_masked;
andreadelprete's avatar
andreadelprete committed
93
94
95
96
    }

    Vector TaskComEquality::getAcceleration(ConstRefVector dv) const
    {
97
      return m_constraint.matrix()*dv - m_drift_masked;
andreadelprete's avatar
andreadelprete committed
98
99
    }

100
    const Vector & TaskComEquality::position_error() const
101
    {
102
      return m_p_error_masked_vec;
103
104
    }

105
    const Vector & TaskComEquality::velocity_error() const
106
    {
107
      return m_v_error_masked_vec;
108
109
110
111
    }

    const Vector & TaskComEquality::position() const
    {
112
      return m_p_com;
113
114
115
116
    }

    const Vector & TaskComEquality::velocity() const
    {
117
      return m_v_com;
118
119
120
121
    }

    const Vector & TaskComEquality::position_ref() const
    {
122
      return m_ref.pos;
123
124
125
126
    }

    const Vector & TaskComEquality::velocity_ref() const
    {
127
      return m_ref.vel;
128
129
130
131
132
    }

    const ConstraintBase & TaskComEquality::getConstraint() const
    {
      return m_constraint;
133
134
    }

Olivier Stasse's avatar
Olivier Stasse committed
135
136
137
    const ConstraintBase & TaskComEquality::compute(const double ,
                                                    ConstRefVector ,
                                                    ConstRefVector ,
138
                                                    Data & data)
139
    {
140
      m_robot.com(data, m_p_com, m_v_com, m_drift);
141
142

      // Compute errors
143
144
      m_p_error = m_p_com - m_ref.pos;
      m_v_error = m_v_com - m_ref.vel;
andreadelprete's avatar
andreadelprete committed
145
146
147
      m_a_des = - m_Kp.cwiseProduct(m_p_error)
                - m_Kd.cwiseProduct(m_v_error)
                + m_ref.acc;
148

149
150
      m_p_error_vec = m_p_error;
      m_v_error_vec = m_v_error;
151
      m_a_des_vec = m_a_des;
152
153
154
155
156
#ifndef NDEBUG
//      std::cout<<m_name<<" errors: "<<m_p_error.norm()<<" "
//        <<m_v_error.norm()<<std::endl;
#endif

157
158
159
      // Get CoM jacobian
      const Matrix3x & Jcom = m_robot.Jcom(data);

160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
      int idx = 0;
      for (int i = 0; i < 3; i++) {
        if (m_mask(i) != 1.) continue;

        m_constraint.matrix().row(idx) = Jcom.row(i);
        m_constraint.vector().row(idx) = (m_a_des - m_drift).row(i);
       
        m_a_des_masked(idx)            = m_a_des(i);
        m_drift_masked(idx)            = m_drift(i);
        m_p_error_masked_vec(idx)      = m_p_error_vec(i);
        m_v_error_masked_vec(idx)      = m_v_error_vec(i);

        idx += 1;
      }

175
176
      return m_constraint;
    }
177

178
179
  }
}