task-com-equality.cpp 3.28 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// Copyright (c) 2017 CNRS
//
// This file is part of PinInvDyn
// PinInvDyn is free software: you can redistribute it
// 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.
// PinInvDyn is distributed in the hope that it will be
// 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
// PinInvDyn If not, see
// <http://www.gnu.org/licenses/>.
//

#include <pininvdyn/tasks/task-com-equality.hpp>

namespace pininvdyn
{
  namespace tasks
  {
    using namespace pininvdyn::math;
    using namespace se3;

    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);
    }

    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;
    }

63
    const Vector & TaskComEquality::position_error() const
64
    {
65
      return m_p_error_vec;
66
67
    }

68
    const Vector & TaskComEquality::velocity_error() const
69
    {
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
      return m_v_error_vec;
    }

    const Vector & TaskComEquality::position() const
    {
      return m_p_error_vec;
    }

    const Vector & TaskComEquality::velocity() const
    {
      return m_p_error_vec;
    }

    const Vector & TaskComEquality::position_ref() const
    {
      return m_p_error_vec;
    }

    const Vector & TaskComEquality::velocity_ref() const
    {
      return m_p_error_vec;
    }

    const ConstraintBase & TaskComEquality::getConstraint() const
    {
      return m_constraint;
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
    }

    const ConstraintBase & TaskComEquality::compute(const double t,
                                                    ConstRefVector q,
                                                    ConstRefVector v,
                                                    Data & data)
    {
      Vector3 p_com, v_com, drift;
      m_robot.com(data, p_com, v_com, drift);

      // Compute errors
      m_p_error = p_com - m_ref.pos;
      m_v_error = v_com - m_ref.vel;
      Vector3 m_a_des = - m_Kp.cwiseProduct(m_p_error)
                        - m_Kd.cwiseProduct(m_v_error)
                        + m_ref.acc;

113
114
115
116
117
118
119
      m_p_error_vec = m_p_error;
      m_v_error_vec = m_v_error;
#ifndef NDEBUG
//      std::cout<<m_name<<" errors: "<<m_p_error.norm()<<" "
//        <<m_v_error.norm()<<std::endl;
#endif

120
121
122
123
124
125
126
127
128
129
      // Get CoM jacobian
      const Matrix3x & Jcom = m_robot.Jcom(data);

      m_constraint.setMatrix(Jcom);
      m_constraint.setVector(m_a_des - drift);
      return m_constraint;
    }
    
  }
}