rdl_mathutils.cc
Go to the documentation of this file.
1 /*
2  * Original Copyright (c) 2011-2016 Martin Felis <martin.felis@iwr.uni-heidelberg.de>
3  *
4  *
5  * RDL - Robot Dynamics Library
6  * Modifications Copyright (c) 2017 Jordan Lack <jlack1987@gmail.com>
7  *
8  * Licensed under the zlib license. See LICENSE for more details.
9  */
10 
11 #include <cmath>
12 #include <limits>
13 
14 #include <iostream>
15 #include <assert.h>
16 
18 #include <rdl_dynamics/Model.h>
19 
20 namespace RobotDynamics
21 {
22  namespace Math
23  {
24 
25  Vector3d Vector3dZero(0., 0., 0.);
26  Matrix3d Matrix3dIdentity(1., 0., 0., 0., 1., 0., 0., 0., 1);
27  Matrix3d Matrix3dZero(0., 0., 0., 0., 0., 0., 0., 0., 0.);
28  Quaternion QuaternionIdentity(0., 0., 0., 1);
29 
30  SpatialVector SpatialVectorZero(0., 0., 0., 0., 0., 0.);
31 
32  SpatialMatrix SpatialMatrixIdentity(1., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 1.);
33 
34  SpatialMatrix SpatialMatrixZero(0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.);
35 
37  {
38  x.setZero();
39 
40  // We can only solve quadratic systems
41  assert (A.rows() == A.cols());
42 
43  unsigned int n = A.rows();
44  // cppcheck-suppress variableScope
45  unsigned int pi;
46 
47  // the pivots
48  size_t *pivot = new size_t[n];
49 
50  // temporary result vector which contains the pivoted result
51  VectorNd px(x);
52 
53  unsigned int i, j, k;
54 
55  for(i = 0; i < n; i++)
56  {
57  pivot[i] = i;
58  }
59 
60  for(j = 0; j < n; j++)
61  {
62  pi = j;
63  double pv = fabs(A(j, pivot[j]));
64 
65  // find the pivot
66  for(k = j; k < n; k++)
67  {
68  double pt = fabs(A(j, pivot[k]));
69  if(pt > pv)
70  {
71  pv = pt;
72  pi = k;
73  unsigned int p_swap = pivot[j];
74  pivot[j] = pivot[pi];
75  pivot[pi] = p_swap;
76  }
77  }
78 
79  for(i = j + 1; i < n; i++)
80  {
81  if(fabs(A(j, pivot[j])) <= std::numeric_limits<double>::epsilon())
82  {
83  std::cerr << "Error: pivoting failed for matrix A = " << std::endl;
84  std::cerr << "A = " << std::endl << A << std::endl;
85  std::cerr << "b = " << b << std::endl;
86  }
87  // assert (fabs(A(j,pivot[j])) > std::numeric_limits<double>::epsilon());
88  double d = A(i, pivot[j]) / A(j, pivot[j]);
89 
90  b[i] -= b[j] * d;
91 
92  for(k = j; k < n; k++)
93  {
94  A(i, pivot[k]) -= A(j, pivot[k]) * d;
95  }
96  }
97  }
98 
99  // warning: i is an unsigned int, therefore a for loop of the
100  // form "for (i = n - 1; i >= 0; i--)" might end up in getting an invalid
101  // value for i!
102  i = n;
103  do
104  {
105  i--;
106 
107  for(j = i + 1; j < n; j++)
108  {
109  px[i] += A(i, pivot[j]) * px[j];
110  }
111  px[i] = (b[i] - px[i]) / A(i, pivot[i]);
112 
113  } while(i > 0);
114 
115  // Unswapping
116  for(i = 0; i < n; i++)
117  {
118  x[pivot[i]] = px[i];
119  }
120 
121  delete[] pivot;
122 
123  return true;
124  }
125 
126  Matrix3d parallel_axis(const Matrix3d &inertia, double mass, const Vector3d &com)
127  {
128  Matrix3d com_cross = toTildeForm(com);
129 
130  return inertia + mass * com_cross * com_cross.transpose();
131  }
132 
134  {
135  return SpatialMatrix(1., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., r[2], -r[1], 1., 0., 0., -r[2], 0., r[0], 0., 1., 0., r[1], -r[0], 0., 0., 0., 1.);
136  }
137 
138  SpatialMatrix Xrotx_mat(const double &xrot)
139  {
140  double s, c;
141  s = sin(xrot);
142  c = cos(xrot);
143 
144  return SpatialMatrix(1., 0., 0., 0., 0., 0., 0., c, s, 0., 0., 0., 0., -s, c, 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., c, s, 0., 0., 0., 0., -s, c);
145  }
146 
147  SpatialMatrix Xroty_mat(const double &yrot)
148  {
149  double s, c;
150  s = sin(yrot);
151  c = cos(yrot);
152 
153  return SpatialMatrix(c, 0., -s, 0., 0., 0., 0., 1., 0., 0., 0., 0., s, 0., c, 0., 0., 0., 0., 0., 0., c, 0., -s, 0., 0., 0., 0., 1., 0., 0., 0., 0., s, 0., c);
154  }
155 
156  SpatialMatrix Xrotz_mat(const double &zrot)
157  {
158  double s, c;
159  s = sin(zrot);
160  c = cos(zrot);
161 
162  return SpatialMatrix(c, s, 0., 0., 0., 0., -s, c, 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., c, s, 0., 0., 0., 0., -s, c, 0., 0., 0., 0., 0., 0., 1.);
163  }
164 
165  SpatialMatrix XtransRotZYXEuler(const Vector3d &displacement, const Vector3d &zyx_euler)
166  {
167  return Xrotz_mat(zyx_euler[0]) * Xroty_mat(zyx_euler[1]) * Xrotx_mat(zyx_euler[2]) * Xtrans_mat(displacement);
168  }
169 
171  {
172  for(unsigned int i = 0; i < model.qdot_size; i++)
173  {
174  for(unsigned int j = i + 1; j < model.qdot_size; j++)
175  {
176  H(i, j) = 0.;
177  }
178  }
179 
180  for(unsigned int k = model.qdot_size; k > 0; k--)
181  {
182  H(k - 1, k - 1) = sqrt(H(k - 1, k - 1));
183  unsigned int i = model.lambda_q[k];
184  while(i != 0)
185  {
186  H(k - 1, i - 1) = H(k - 1, i - 1) / H(k - 1, k - 1);
187  i = model.lambda_q[i];
188  }
189 
190  i = model.lambda_q[k];
191  while(i != 0)
192  {
193  unsigned int j = i;
194  while(j != 0)
195  {
196  H(i - 1, j - 1) = H(i - 1, j - 1) - H(k - 1, i - 1) * H(k - 1, j - 1);
197  j = model.lambda_q[j];
198  }
199  i = model.lambda_q[i];
200  }
201  }
202  }
203 
205  {
206  assert (0 && !"Not yet implemented!");
207  }
208 
210  {
211  assert (0 && !"Not yet implemented!");
212  }
213 
215  {
216  assert (0 && !"Not yet implemented!");
217  }
218 
220  {
221  for(unsigned int i = 1; i <= model.qdot_size; i++)
222  {
223  unsigned int j = model.lambda_q[i];
224  while(j != 0)
225  {
226  x[i - 1] = x[i - 1] - L(i - 1, j - 1) * x[j - 1];
227  j = model.lambda_q[j];
228  }
229  x[i - 1] = x[i - 1] / L(i - 1, i - 1);
230  }
231  }
232 
234  {
235  for(int i = model.qdot_size; i > 0; i--)
236  {
237  x[i - 1] = x[i - 1] / L(i - 1, i - 1);
238  unsigned int j = model.lambda_q[i];
239  while(j != 0)
240  {
241  x[j - 1] = x[j - 1] - L(i - 1, j - 1) * x[i - 1];
242  j = model.lambda_q[j];
243  }
244  }
245  }
246  } /* Math */
247 } /* RobotDynamics */
std::vector< unsigned int > lambda_q
Definition: Model.h:159
SpatialVector SpatialVectorZero
SpatialMatrix SpatialMatrixZero
void SparseMultiplyLTx(Model &model, Math::MatrixNd &L)
SpatialMatrix Xrotx_mat(const double &xrot)
Creates a rotational transformation around the X-axis.
Matrix3d Matrix3dIdentity
void SparseMultiplyHx(Model &model, Math::MatrixNd &L)
bool linSolveGaussElimPivot(MatrixNd A, VectorNd b, VectorNd &x)
Solves a linear system using gaussian elimination with pivoting.
void SparseMultiplyLx(Model &model, Math::MatrixNd &L)
SpatialMatrix Xtrans_mat(const Vector3d &displacement)
Creates a transformation of a linear displacement.
static Matrix3d toTildeForm(const Point3d &p)
Definition: Point3.hpp:370
SpatialMatrix XtransRotZYXEuler(const Vector3d &displacement, const Vector3d &zyx_euler)
Creates a spatial transformation for given parameters.
Eigen::VectorXd VectorNd
Definition: rdl_eigenmath.h:25
SpatialMatrix Xroty_mat(const double &yrot)
Creates a rotational transformation around the Y-axis.
Contains all information about the rigid body model.
Definition: Model.h:121
void SparseFactorizeLTL(Model &model, Math::MatrixNd &H)
Eigen::MatrixXd MatrixNd
Definition: rdl_eigenmath.h:26
SpatialMatrix SpatialMatrixIdentity
Quaternion QuaternionIdentity
void SparseSolveLTx(Model &model, Math::MatrixNd &L, Math::VectorNd &x)
Matrix3d parallel_axis(const Matrix3d &inertia, double mass, const Vector3d &com)
Translates the inertia matrix to a new center.
void SparseSolveLx(Model &model, Math::MatrixNd &L, Math::VectorNd &x)
SpatialMatrix Xrotz_mat(const double &zrot)
Creates a rotational transformation around the Z-axis.
Namespace for all structures of the RobotDynamics library.
Definition: Body.h:21
unsigned int qdot_size
The size of the.
Definition: Model.h:187


rdl_dynamics
Author(s):
autogenerated on Tue Apr 20 2021 02:25:27