task-com-equality.cpp 3.65 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//
// 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>
19
#include "pininvdyn/robots/robot-wrapper.hpp"
20
21
22
23
24

namespace pininvdyn
{
  namespace tasks
  {
25
26
    using namespace math;
    using namespace trajectories;
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
63
64
    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;
    }

andreadelprete's avatar
andreadelprete committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    const TrajectorySample & TaskComEquality::getReference() const
    {
      return m_ref;
    }

    const Vector & TaskComEquality::getDesiredAcceleration() const
    {
      return m_a_des;
    }

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

80
    const Vector & TaskComEquality::position_error() const
81
    {
82
      return m_p_error_vec;
83
84
    }

85
    const Vector & TaskComEquality::velocity_error() const
86
    {
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
      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;
113
114
115
116
117
    }

    const ConstraintBase & TaskComEquality::compute(const double t,
                                                    ConstRefVector q,
                                                    ConstRefVector v,
andreadelprete's avatar
andreadelprete committed
118
                                                    const Data & data)
119
    {
andreadelprete's avatar
andreadelprete committed
120
121
      Vector3 p_com, v_com;
      m_robot.com(data, p_com, v_com, m_drift);
122
123
124
125

      // Compute errors
      m_p_error = p_com - m_ref.pos;
      m_v_error = v_com - m_ref.vel;
andreadelprete's avatar
andreadelprete committed
126
127
128
      m_a_des = - m_Kp.cwiseProduct(m_p_error)
                - m_Kd.cwiseProduct(m_v_error)
                + m_ref.acc;
129

130
131
132
133
134
135
136
      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

137
138
139
140
      // Get CoM jacobian
      const Matrix3x & Jcom = m_robot.Jcom(data);

      m_constraint.setMatrix(Jcom);
andreadelprete's avatar
andreadelprete committed
141
      m_constraint.setVector(m_a_des - m_drift);
142
143
144
145
146
      return m_constraint;
    }
    
  }
}