Robot Dynamics Library
rdl_mathutils.hpp
Go to the documentation of this file.
1 // Copyright (c) 2017 Jordan Lack <jlack1987@gmail.com>
2 // Copyright (c) 2011-2016 Martin Felis <martin.felis@iwr.uni-heidelberg.de>
3 // RDL - Robot Dynamics Library
4 // Licensed under the zlib license. See LICENSE for more details.
5 
6 #ifndef __RDL_MATHUTILS_H__
7 #define __RDL_MATHUTILS_H__
8 
13 #include <assert.h>
14 #include <cmath>
15 
17 
18 namespace RobotDynamics
19 {
20 struct Model;
21 
22 namespace Math
23 {
29 {
36 };
37 
42 
46 
47 inline VectorNd vectorFromPtr(unsigned int n, double* ptr)
48 {
49  // TODO: use memory mapping operators for Eigen
50  VectorNd result(n);
51 
52  for (unsigned int i = 0; i < n; i++)
53  {
54  result[i] = ptr[i];
55  }
56 
57  return result;
58 }
59 
60 inline MatrixNd matrixFromPtr(unsigned int rows, unsigned int cols, double* ptr, bool row_major = true)
61 {
62  MatrixNd result(rows, cols);
63 
64  if (row_major)
65  {
66  for (unsigned int i = 0; i < rows; i++)
67  {
68  for (unsigned int j = 0; j < cols; j++)
69  {
70  result(i, j) = ptr[i * cols + j];
71  }
72  }
73  }
74  else
75  {
76  for (unsigned int i = 0; i < rows; i++)
77  {
78  for (unsigned int j = 0; j < cols; j++)
79  {
80  result(i, j) = ptr[i + j * rows];
81  }
82  }
83  }
84 
85  return result;
86 }
87 
90 
92 Matrix3d parallel_axis(const Matrix3d& inertia, double mass, const Vector3d& com);
93 
100 inline Vector3d angular_velocity_from_zyx_angle_rates(const Vector3d& zyx_angles, const Vector3d& zyx_angle_rates)
101 {
102  double sy = sin(zyx_angles[1]);
103  double cy = cos(zyx_angles[1]);
104  double sx = sin(zyx_angles[2]);
105  double cx = cos(zyx_angles[2]);
106 
107  return Vector3d(zyx_angle_rates[2] - sy * zyx_angle_rates[0], cx * zyx_angle_rates[1] + sx * cy * zyx_angle_rates[0],
108  -sx * zyx_angle_rates[1] + cx * cy * zyx_angle_rates[0]);
109 }
110 
117 inline Vector3d angular_velocity_from_xyz_angle_rates(const Vector3d& xyz_angles, const Vector3d& xyz_angle_rates)
118 {
119  double s1 = sin(xyz_angles[1]);
120  double c1 = cos(xyz_angles[1]);
121  double s2 = sin(xyz_angles[2]);
122  double c2 = cos(xyz_angles[2]);
123 
124  return Vector3d(c2 * c1 * xyz_angle_rates[0] + s2 * xyz_angle_rates[1], -s2 * c1 * xyz_angle_rates[0] + c2 * xyz_angle_rates[1],
125  s1 * xyz_angle_rates[0] + xyz_angle_rates[2]);
126 }
127 
136 inline Matrix3d angular_velocity_to_zyx_angle_rates_jacobian(const Vector3d& zyx_angles, double singularity_threshold = 1.e-10)
137 {
138  double c1 = cos(zyx_angles[1]);
139  double t1 = tan(zyx_angles[1]);
140  double s2 = sin(zyx_angles[2]);
141  double c2 = cos(zyx_angles[2]);
142 
143  if (c1 < singularity_threshold)
144  {
145  errno = EDOM;
146  std::cerr << "Domain error - unable to convert angular velocities to ZYX euler rates with pitch = pi/2" << std::endl;
147  return Matrix3dZero;
148  }
149 
150  return Matrix3d(0., s2 / c1, c2 / c1, 0., c2, -s2, 1., s2 * t1, c2 * t1);
151 }
152 
161 inline Matrix3d angular_velocity_to_xyz_angle_rates_jacobian(const Vector3d& xyz_angles, double singularity_threshold = 1.e-10)
162 {
163  double c1 = cos(xyz_angles[1]);
164  double t1 = tan(xyz_angles[1]);
165  double s2 = sin(xyz_angles[2]);
166  double c2 = cos(xyz_angles[2]);
167 
168  if (c1 < singularity_threshold)
169  {
170  errno = EDOM;
171  std::cerr << "Domain error - unable to convert angular velocities to XYZ euler rates with pitch = pi/2" << std::endl;
172  return Matrix3dZero;
173  }
174 
175  return Matrix3d(c2 / c1, -s2 / c1, 0., s2, c2, 0., -c2 * t1, s2 * t1, 1.);
176 }
177 
186 inline Vector3d angular_velocity_to_zyx_angle_rates(const Vector3d& zyx_angles, const Vector3d& angular_velocity, double singularity_threshold = 1.e-10)
187 {
188  double c1 = cos(zyx_angles[1]);
189  double t1 = tan(zyx_angles[1]);
190  double s2 = sin(zyx_angles[2]);
191  double c2 = cos(zyx_angles[2]);
192 
193  if (c1 < singularity_threshold)
194  {
195  errno = EDOM;
196  std::cerr << "unable to compute angle rates from angular velocity due to singularity" << std::endl;
197  return Vector3dZero;
198  }
199 
200  return Vector3d((s2 / c1) * angular_velocity[1] + (c2 / c1) * angular_velocity[2], c2 * angular_velocity[1] - s2 * angular_velocity[2],
201  angular_velocity[0] + s2 * t1 * angular_velocity[1] + c2 * t1 * angular_velocity[2]);
202 }
203 
212 inline Vector3d angular_velocity_to_xyz_angle_rates(const Vector3d& xyz_angles, const Vector3d& angular_velocity, double singularity_threshold = 1.e-10)
213 {
214  double c1 = cos(xyz_angles[1]);
215  double t1 = tan(xyz_angles[1]);
216  double s2 = sin(xyz_angles[2]);
217  double c2 = cos(xyz_angles[2]);
218 
219  if (c1 < singularity_threshold)
220  {
221  errno = EDOM;
222  std::cerr << "unable to compute jacobian matrix for XYZ angles" << std::endl;
223  return Vector3dZero;
224  }
225 
226  return Vector3d((c2 / c1) * angular_velocity[0] - (s2 / c1) * angular_velocity[1], s2 * angular_velocity[0] + c2 * angular_velocity[1],
227  -c2 * t1 * angular_velocity[0] + s2 * t1 * angular_velocity[1] + angular_velocity[2]);
228 }
229 
230 inline Vector3d global_angular_velocity_from_rates(const Vector3d& zyx_angles, const Vector3d& zyx_rates)
231 {
232  double s0, c0, s1, c1;
233  s0 = sin(zyx_angles[0]);
234  c0 = cos(zyx_angles[0]);
235  s1 = sin(zyx_angles[1]);
236  c1 = cos(zyx_angles[1]);
237 
238  Matrix3d RzT(c0, s0, 0., -s0, c0, 0., 0., 0., 1.);
239  RzT.transposeInPlace();
240  Matrix3d RyT(c1, 0., -s1, 0., 1., 0., s1, 0., c1);
241  RyT.transposeInPlace();
242 
243  return Vector3d(Vector3d(0., 0., zyx_rates[0]) + RzT * Vector3d(0., zyx_rates[1], 0.) + RzT * RyT * Vector3d(zyx_rates[2], 0., 0.));
244 }
245 
254 inline Vector3d angular_acceleration_from_zyx_angle_rates(const Vector3d& zyx_angles, const Vector3d& zyx_angle_rates, const Vector3d& zyx_angle_rates_dot)
255 {
256  double sy = sin(zyx_angles[1]);
257  double cy = cos(zyx_angles[1]);
258  double sx = sin(zyx_angles[2]);
259  double cx = cos(zyx_angles[2]);
260  double xdot = zyx_angle_rates[2];
261  double ydot = zyx_angle_rates[1];
262  double zdot = zyx_angle_rates[0];
263  double xddot = zyx_angle_rates_dot[2];
264  double yddot = zyx_angle_rates_dot[1];
265  double zddot = zyx_angle_rates_dot[0];
266 
267  return Vector3d(xddot - (cy * ydot * zdot + sy * zddot), -sx * xdot * ydot + cx * yddot + cx * xdot * cy * zdot + sx * (-sy * ydot * zdot + cy * zddot),
268  -cx * xdot * ydot - sx * yddot - sx * xdot * cy * zdot + cx * (-sy * ydot * zdot + cy * zddot));
269 }
270 
279 inline Vector3d angular_acceleration_from_xyz_angle_rates(const Vector3d& xyz_angles, const Vector3d& xyz_angle_rates, const Vector3d& xyz_angle_rates_dot)
280 {
281  double s1 = sin(xyz_angles[1]);
282  double c1 = cos(xyz_angles[1]);
283  double s2 = sin(xyz_angles[2]);
284  double c2 = cos(xyz_angles[2]);
285 
286  double xdot = xyz_angle_rates[0];
287  double ydot = xyz_angle_rates[1];
288  double zdot = xyz_angle_rates[2];
289 
290  double xddot = xyz_angle_rates_dot[0];
291  double yddot = xyz_angle_rates_dot[1];
292  double zddot = xyz_angle_rates_dot[2];
293  return Vector3d(c2 * c1 * xddot + s2 * yddot + (-s2 * c1 * zdot - c2 * s1 * ydot) * xdot + c2 * zdot * ydot,
294  -s2 * c1 * xddot + c2 * yddot + (-c2 * c1 * zdot + s2 * s1 * ydot) * xdot - s2 * zdot * ydot, s1 * xddot + zddot + c1 * ydot * xdot);
295 }
296 
297 void SparseFactorizeLTL(Model& model, Math::MatrixNd& H);
298 
300 
302 
304 
305 void SparseSolveLx(Model& model, Math::MatrixNd& L, Math::VectorNd& x);
306 
308 } // namespace Math
309 } // namespace RobotDynamics
310 
311 /* __RDL_MATHUTILS_H__ */
312 #endif // ifndef __RDL_MATHUTILS_H__
Definition: rdl_eigenmath.hpp:104
Quaternion that are used for singularity free joints.
Definition: rdl_eigenmath.hpp:614
Definition: rdl_eigenmath.hpp:354
Definition: rdl_eigenmath.hpp:187
Definition: rdl_eigenmath.hpp:54
bool linSolveGaussElimPivot(MatrixNd A, VectorNd b, VectorNd &x)
Solves a linear system using gaussian elimination with pivoting.
Definition: rdl_mathutils.cpp: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.)
Definition: rdl_mathutils.hpp:44
void SparseMultiplyLTx(Model &model, Math::MatrixNd &L)
SpatialVector SpatialVectorZero(0., 0., 0., 0., 0., 0.)
Definition: rdl_mathutils.hpp:43
Matrix3d angular_velocity_to_zyx_angle_rates_jacobian(const Vector3d &zyx_angles, double singularity_threshold=1.e-10)
compute the 3x3 jacobian matrix that when multiplied by the angular velocity, ordered (wx,...
Definition: rdl_mathutils.hpp:136
LinearSolver
Available solver methods for the linear systems.
Definition: rdl_mathutils.hpp:29
@ LinearSolverLLT
Definition: rdl_mathutils.hpp:34
@ LinearSolverHouseholderQR
Definition: rdl_mathutils.hpp:33
@ LinearSolverLast
Definition: rdl_mathutils.hpp:35
@ LinearSolverPartialPivLU
Definition: rdl_mathutils.hpp:31
@ LinearSolverUnknown
Definition: rdl_mathutils.hpp:30
@ LinearSolverColPivHouseholderQR
Definition: rdl_mathutils.hpp:32
Quaternion QuaternionIdentity(0., 0., 0., 1)
Definition: rdl_mathutils.hpp:41
Vector3d angular_acceleration_from_zyx_angle_rates(const Vector3d &zyx_angles, const Vector3d &zyx_angle_rates, const Vector3d &zyx_angle_rates_dot)
calculate angular acceleration from zyx angles, angle rates, and angle accelerations
Definition: rdl_mathutils.hpp:254
Matrix3d Matrix3dIdentity(1., 0., 0., 0., 1., 0., 0., 0., 1)
Definition: rdl_mathutils.hpp:39
void SparseFactorizeLTL(Model &model, Math::MatrixNd &H)
Definition: rdl_mathutils.cpp:129
MatrixNd matrixFromPtr(unsigned int rows, unsigned int cols, double *ptr, bool row_major=true)
Definition: rdl_mathutils.hpp:60
void SparseSolveLTx(Model &model, Math::MatrixNd &L, Math::VectorNd &x)
Definition: rdl_mathutils.cpp:177
Vector3d angular_velocity_to_zyx_angle_rates(const Vector3d &zyx_angles, const Vector3d &angular_velocity, double singularity_threshold=1.e-10)
convert angular velocity to zyx angle rates
Definition: rdl_mathutils.hpp:186
Vector3d Vector3dZero(0., 0., 0.)
Definition: rdl_mathutils.hpp:38
Vector3d angular_velocity_from_xyz_angle_rates(const Vector3d &xyz_angles, const Vector3d &xyz_angle_rates)
convert xyz euler angles and angle rates to angular velocity
Definition: rdl_mathutils.hpp:117
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.)
Definition: rdl_mathutils.hpp:45
void SparseMultiplyHx(Model &model, Math::MatrixNd &L)
Vector3d angular_acceleration_from_xyz_angle_rates(const Vector3d &xyz_angles, const Vector3d &xyz_angle_rates, const Vector3d &xyz_angle_rates_dot)
calculate angular acceleration from zyx angles, angle rates, and angle accelerations
Definition: rdl_mathutils.hpp:279
Eigen::VectorXd VectorNd
Definition: rdl_eigenmath.hpp:20
void SparseSolveLx(Model &model, Math::MatrixNd &L, Math::VectorNd &x)
Definition: rdl_mathutils.cpp:163
Vector3d global_angular_velocity_from_rates(const Vector3d &zyx_angles, const Vector3d &zyx_rates)
Definition: rdl_mathutils.hpp:230
VectorNd vectorFromPtr(unsigned int n, double *ptr)
Definition: rdl_mathutils.hpp:47
Vector3d angular_velocity_to_xyz_angle_rates(const Vector3d &xyz_angles, const Vector3d &angular_velocity, double singularity_threshold=1.e-10)
convert angular velocity to xyz angle rates
Definition: rdl_mathutils.hpp:212
Matrix3d angular_velocity_to_xyz_angle_rates_jacobian(const Vector3d &xyz_angles, double singularity_threshold=1.e-10)
compute the 3x3 jacobian matrix that when multiplied by the angular velocity, ordered (wx,...
Definition: rdl_mathutils.hpp:161
Matrix3d Matrix3dZero(0., 0., 0., 0., 0., 0., 0., 0., 0.)
Definition: rdl_mathutils.hpp:40
Vector3d angular_velocity_from_zyx_angle_rates(const Vector3d &zyx_angles, const Vector3d &zyx_angle_rates)
convert zyx euler angles and angle rates to angular velocity
Definition: rdl_mathutils.hpp:100
void SparseMultiplyLx(Model &model, Math::MatrixNd &L)
Matrix3d parallel_axis(const Matrix3d &inertia, double mass, const Vector3d &com)
Translates the inertia matrix to a new center.
Definition: rdl_mathutils.cpp:122
Eigen::MatrixXd MatrixNd
Definition: rdl_eigenmath.hpp:21
Namespace for all structures of the RobotDynamics library.
Definition: examples.hpp:19
Contains all information about the rigid body model.
Definition: Model.hpp:112