Blender V4.3
WDLSSolver.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009 Ruben Smits
2 *
3 * SPDX-License-Identifier: LGPL-2.1-or-later */
4
9#include "WDLSSolver.hpp"
11
12namespace iTaSC {
13
14WDLSSolver::WDLSSolver() : m_lambda(0.5), m_epsilon(0.1)
15{
16 // maximum joint velocity
17 m_qmax = 50.0;
18}
19
22
23bool WDLSSolver::init(unsigned int nq, unsigned int nc, const std::vector<bool>& gc)
24{
25 m_ns = std::min(nc,nq);
26 m_AWq = e_zero_matrix(nc,nq);
27 m_WyAWq = e_zero_matrix(nc,nq);
28 m_WyAWqt = e_zero_matrix(nq,nc);
29 m_S = e_zero_vector(std::max(nc,nq));
30 m_Wy_ydot = e_zero_vector(nc);
31 if (nq > nc) {
32 m_transpose = true;
33 m_temp = e_zero_vector(nc);
34 m_U = e_zero_matrix(nc,nc);
35 m_V = e_zero_matrix(nq,nc);
36 m_WqV = e_zero_matrix(nq,nc);
37 } else {
38 m_transpose = false;
39 m_temp = e_zero_vector(nq);
40 m_U = e_zero_matrix(nc,nq);
41 m_V = e_zero_matrix(nq,nq);
42 m_WqV = e_zero_matrix(nq,nq);
43 }
44 return true;
45}
46
47bool WDLSSolver::solve(const e_matrix& A, const e_vector& Wy, const e_vector& ydot, const e_matrix& Wq, e_vector& qdot, e_scalar& nlcoef)
48{
49 double alpha, vmax, norm;
50 // Create the Weighted jacobian
51 m_AWq = A*Wq;
52 for (int i=0; i<Wy.size(); i++)
53 m_WyAWq.row(i) = Wy(i)*m_AWq.row(i);
54
55 // Compute the SVD of the weighted jacobian
56 int ret;
57 if (m_transpose) {
58 m_WyAWqt = m_WyAWq.transpose();
59 ret = KDL::svd_eigen_HH(m_WyAWqt,m_V,m_S,m_U,m_temp);
60 } else {
61 ret = KDL::svd_eigen_HH(m_WyAWq,m_U,m_S,m_V,m_temp);
62 }
63 if(ret<0)
64 return false;
65
66 m_WqV.noalias() = Wq*m_V;
67
68 //Wy*ydot
69 m_Wy_ydot = Wy.array() * ydot.array();
70 //S^-1*U'*Wy*ydot
71 e_scalar maxDeltaS = e_scalar(0.0);
72 e_scalar prevS = e_scalar(0.0);
73 e_scalar maxS = e_scalar(1.0);
74 e_scalar S, lambda;
75 qdot.setZero();
76 for(int i=0;i<m_ns;++i) {
77 S = m_S(i);
78 if (S <= KDL::epsilon)
79 break;
80 if (i > 0 && (prevS-S) > maxDeltaS) {
81 maxDeltaS = (prevS-S);
82 maxS = prevS;
83 }
84 lambda = (S < m_epsilon) ? (e_scalar(1.0)-KDL::sqr(S/m_epsilon))*m_lambda*m_lambda : e_scalar(0.0);
85 alpha = m_U.col(i).dot(m_Wy_ydot)*S/(S*S+lambda);
86 vmax = m_WqV.col(i).array().abs().maxCoeff();
87 norm = fabs(alpha*vmax);
88 if (norm > m_qmax) {
89 qdot += m_WqV.col(i)*(alpha*m_qmax/norm);
90 } else {
91 qdot += m_WqV.col(i)*alpha;
92 }
93 prevS = S;
94 }
95 if (maxDeltaS == e_scalar(0.0))
96 nlcoef = e_scalar(KDL::epsilon);
97 else
98 nlcoef = (maxS-maxDeltaS)/maxS;
99 return true;
100}
101
102}
SIMD_FORCE_INLINE btScalar norm() const
Return the norm (length) of the vector.
Definition btVector3.h:263
virtual bool init(unsigned int nq, unsigned int nc, const std::vector< bool > &gc)
virtual ~WDLSSolver()
virtual bool solve(const e_matrix &A, const e_vector &Wy, const e_vector &ydot, const e_matrix &Wq, e_vector &qdot, e_scalar &nlcoef)
#define e_vector
#define e_scalar
#define e_zero_vector
#define e_zero_matrix
#define e_matrix
ccl_device_inline float2 fabs(const float2 a)
INLINE Rall1d< T, V, S > sqr(const Rall1d< T, V, S > &arg)
Definition rall1d.h:351
double epsilon
default precision while comparing with Equal(..,..) functions. Initialized at 0.0000001.
Definition utility.cpp:22
int svd_eigen_HH(const Eigen::MatrixBase< MatrixA > &A, Eigen::MatrixBase< MatrixUV > &U, Eigen::MatrixBase< VectorS > &S, Eigen::MatrixBase< MatrixUV > &V, Eigen::MatrixBase< VectorS > &tmp, int maxiter=150)
return ret