rdl_mathutils.h
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 #ifndef __RDL_MATHUTILS_H__
12 #define __RDL_MATHUTILS_H__
13 
18 #include <assert.h>
19 #include <cmath>
20 
23 
24 namespace RobotDynamics
25 {
26 struct Model;
27 
28 namespace Math
29 {
35 {
42 };
43 
44 extern Vector3d Vector3dZero;
46 extern Matrix3d Matrix3dZero;
48 
52 
53 inline VectorNd vectorFromPtr(unsigned int n, double* ptr)
54 {
55  // TODO: use memory mapping operators for Eigen
56  VectorNd result(n);
57 
58  for (unsigned int i = 0; i < n; i++)
59  {
60  result[i] = ptr[i];
61  }
62 
63  return result;
64 }
65 
66 inline MatrixNd matrixFromPtr(unsigned int rows, unsigned int cols, double* ptr, bool row_major = true)
67 {
68  MatrixNd result(rows, cols);
69 
70  if (row_major)
71  {
72  for (unsigned int i = 0; i < rows; i++)
73  {
74  for (unsigned int j = 0; j < cols; j++)
75  {
76  result(i, j) = ptr[i * cols + j];
77  }
78  }
79  }
80  else
81  {
82  for (unsigned int i = 0; i < rows; i++)
83  {
84  for (unsigned int j = 0; j < cols; j++)
85  {
86  result(i, j) = ptr[i + j * rows];
87  }
88  }
89  }
90 
91  return result;
92 }
93 
96 
98 Matrix3d parallel_axis(const Matrix3d& inertia, double mass, const Vector3d& com);
99 
110 SpatialMatrix Xtrans_mat(const Vector3d& displacement);
111 
119 SpatialMatrix Xrotz_mat(const double& zrot);
120 
128 SpatialMatrix Xroty_mat(const double& yrot);
129 
137 SpatialMatrix Xrotx_mat(const double& xrot);
138 
148 SpatialMatrix XtransRotZYXEuler(const Vector3d& displacement, const Vector3d& zyx_euler);
149 
150 inline Matrix3d rotx(const double& xrot)
151 {
152  double s, c;
153 
154  s = sin(xrot);
155  c = cos(xrot);
156  return Matrix3d(1., 0., 0., 0., c, s, 0., -s, c);
157 }
158 
159 inline Matrix3d roty(const double& yrot)
160 {
161  double s, c;
162 
163  s = sin(yrot);
164  c = cos(yrot);
165  return Matrix3d(c, 0., -s, 0., 1., 0., s, 0., c);
166 }
167 
168 inline Matrix3d rotz(const double& zrot)
169 {
170  double s, c;
171 
172  s = sin(zrot);
173  c = cos(zrot);
174  return Matrix3d(c, s, 0., -s, c, 0., 0., 0., 1.);
175 }
176 
177 inline Matrix3d rotxdot(const double& x, const double& xdot)
178 {
179  double s, c;
180 
181  s = sin(x);
182  c = cos(x);
183  return Matrix3d(0., 0., 0., 0., -s * xdot, c * xdot, 0., -c * xdot, -s * xdot);
184 }
185 
186 inline Matrix3d rotydot(const double& y, const double& ydot)
187 {
188  double s, c;
189 
190  s = sin(y);
191  c = cos(y);
192  return Matrix3d(-s * ydot, 0., -c * ydot, 0., 0., 0., c * ydot, 0., -s * ydot);
193 }
194 
195 inline Matrix3d rotzdot(const double& z, const double& zdot)
196 {
197  double s, c;
198 
199  s = sin(z);
200  c = cos(z);
201  return Matrix3d(-s * zdot, c * zdot, 0., -c * zdot, -s * zdot, 0., 0., 0., 0.);
202 }
203 
204 inline Vector3d angular_velocity_from_angle_rates(const Vector3d& zyx_angles, const Vector3d& zyx_angle_rates)
205 {
206  double sy = sin(zyx_angles[1]);
207  double cy = cos(zyx_angles[1]);
208  double sx = sin(zyx_angles[2]);
209  double cx = cos(zyx_angles[2]);
210 
211  return Vector3d(zyx_angle_rates[2] - sy * zyx_angle_rates[0], cx * zyx_angle_rates[1] + sx * cy * zyx_angle_rates[0],
212  -sx * zyx_angle_rates[1] + cx * cy * zyx_angle_rates[0]);
213 }
214 
215 inline Vector3d global_angular_velocity_from_rates(const Vector3d& zyx_angles, const Vector3d& zyx_rates)
216 {
217  Matrix3d RzT = rotz(zyx_angles[0]).transpose();
218  Matrix3d RyT = roty(zyx_angles[1]).transpose();
219 
220  return Vector3d(Vector3d(0., 0., zyx_rates[0]) + RzT * Vector3d(0., zyx_rates[1], 0.) + RzT * RyT * Vector3d(zyx_rates[2], 0., 0.));
221 }
222 
223 inline Vector3d angular_acceleration_from_angle_rates(const Vector3d& zyx_angles, const Vector3d& zyx_angle_rates, const Vector3d& zyx_angle_rates_dot)
224 {
225  double sy = sin(zyx_angles[1]);
226  double cy = cos(zyx_angles[1]);
227  double sx = sin(zyx_angles[2]);
228  double cx = cos(zyx_angles[2]);
229  double xdot = zyx_angle_rates[2];
230  double ydot = zyx_angle_rates[1];
231  double zdot = zyx_angle_rates[0];
232  double xddot = zyx_angle_rates_dot[2];
233  double yddot = zyx_angle_rates_dot[1];
234  double zddot = zyx_angle_rates_dot[0];
235 
236  return Vector3d(xddot - (cy * ydot * zdot + sy * zddot), -sx * xdot * ydot + cx * yddot + cx * xdot * cy * zdot + sx * (-sy * ydot * zdot + cy * zddot),
237  -cx * xdot * ydot - sx * yddot - sx * xdot * cy * zdot + cx * (-sy * ydot * zdot + cy * zddot));
238 }
239 
240 void SparseFactorizeLTL(Model& model, Math::MatrixNd& H);
241 
242 void SparseMultiplyHx(Model& model, Math::MatrixNd& L);
243 
244 void SparseMultiplyLx(Model& model, Math::MatrixNd& L);
245 
246 void SparseMultiplyLTx(Model& model, Math::MatrixNd& L);
247 
248 void SparseSolveLx(Model& model, Math::MatrixNd& L, Math::VectorNd& x);
249 
251 } // namespace Math
252 } // namespace RobotDynamics
253 
254 /* __RDL_MATHUTILS_H__ */
255 #endif // ifndef __RDL_MATHUTILS_H__
MatrixNd matrixFromPtr(unsigned int rows, unsigned int cols, double *ptr, bool row_major=true)
Definition: rdl_mathutils.h:66
SpatialVector SpatialVectorZero
SpatialMatrix SpatialMatrixZero
Vector3d angular_velocity_from_angle_rates(const Vector3d &zyx_angles, const Vector3d &zyx_angle_rates)
void SparseMultiplyLTx(Model &model, Math::MatrixNd &L)
Vector3d global_angular_velocity_from_rates(const Vector3d &zyx_angles, const Vector3d &zyx_rates)
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.
SpatialMatrix XtransRotZYXEuler(const Vector3d &displacement, const Vector3d &zyx_euler)
Creates a spatial transformation for given parameters.
LinearSolver
Available solver methods for the linear systems.
Definition: rdl_mathutils.h:34
Matrix3d roty(const double &yrot)
Vector3d angular_acceleration_from_angle_rates(const Vector3d &zyx_angles, const Vector3d &zyx_angle_rates, const Vector3d &zyx_angle_rates_dot)
Quaternion that are used for singularity free joints.
Definition: Quaternion.h:25
Matrix3d rotzdot(const double &z, const double &zdot)
Eigen::VectorXd VectorNd
Definition: rdl_eigenmath.h:25
Matrix3d rotydot(const double &y, const double &ydot)
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
Matrix3d rotx(const double &xrot)
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.
Matrix3d rotz(const double &zrot)
VectorNd vectorFromPtr(unsigned int n, double *ptr)
Definition: rdl_mathutils.h:53
Namespace for all structures of the RobotDynamics library.
Definition: Body.h:21
Matrix3d rotxdot(const double &x, const double &xdot)


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