AUV-Coop-Assembly
Master Thesis for Robotics Engineering. Cooperative peg-in-hole assembly with two underwater vehicles guided by vision
formulas.cpp
1 #include "header/formulas.h"
2 
3 Eigen::Matrix3d FRM::skewMat(Eigen::Vector3d vect){
4  Eigen::Matrix3d skew;
5 
6  skew << 0, -vect(2), vect(1),
7  vect(2), 0, -vect(0),
8  -vect(1), vect(0), 0;
9 
10  return skew;
11 
12 }
19 Eigen::Vector3d FRM::reducedVersorLemma(Eigen::Vector3d a, Eigen::Vector3d b){
20 
21  Eigen::Vector3d misalign;
22 
23  Eigen::Vector3d vsinth = a.cross(b);
24  double costh = a.dot(b);
25 
26  double sinth = vsinth.norm();
27 
28  if (sinth > 0.00000000001){
29  double theta = atan2(sinth, costh);
30  misalign = (vsinth * (theta/sinth));
31 
32  } else {
33  misalign << 0, 0, 0;
34  }
35 
36  return misalign;
37 
38 
39 }
40 
41 Eigen::Matrix4d FRM::invertTransf(Eigen::Matrix4d mat){
42 
43  Eigen::Matrix4d inv = Eigen::Matrix4d::Identity();
44 
45  // inverse of rot part is the transpose
46  Eigen::Matrix3d rot_t = mat.topLeftCorner<3,3>().transpose();
47  inv.topLeftCorner<3,3>() = rot_t;
48 
49  inv.topRightCorner<3,1>() = - rot_t * mat.topRightCorner<3,1>();
50 
51  return inv;
52 
53 
54 }
55 
56 Eigen::MatrixXd FRM::pseudoInverse(Eigen::MatrixXd mat, double tolerance){
57 
58  Eigen::JacobiSVD<Eigen::MatrixXd> svd(mat, Eigen::ComputeFullU | Eigen::ComputeFullV);
59  Eigen::MatrixXd S_inv(mat.cols(), mat.rows());
60  Eigen::MatrixXd singularValues = svd.singularValues();
61  S_inv = Eigen::MatrixXd::Zero(mat.cols(), mat.rows());
62  for (int i=0; i< singularValues.size(); ++i){
63  if (singularValues(i) > tolerance) {
64  S_inv(i, i) = 1 / singularValues(i);
65 
66  } else {
67  S_inv(i, i) = 0;
68  }
69  }
70 
71  return (svd.matrixV() * S_inv * svd.matrixU().transpose());
72 }
73 
84 Eigen::VectorXd FRM::saturateVectorEigen(Eigen::VectorXd vector, double threshold){
85 
86  if (threshold <0.0){
87  std::cerr << "FRM::saturateVectorEigen: ERROR: the threshold must be positive";
88  return vector;
89  }
90  Eigen::VectorXd out = vector;
91 
92  //take abs value
93  for (int i =0; i< vector.rows(); i++){
94  if (vector(i) < 0.0){
95  vector(i) *= -1;
96  }
97  }
98 
99  double maxCoeff = vector.maxCoeff();
100  if ( maxCoeff > threshold){ //if so, scale vector
101 
102  out = out/maxCoeff*threshold;
103 
104  }
105 
106  return out;
107 }
108 
109 std::vector<double> FRM::saturateVectorStd(std::vector<double> vector, double threshold){
110 
111  if (threshold <0.0){
112  std::cerr << "FRM::saturateVectorStd: ERROR: the threshold must be positive";
113  return vector;
114  }
115  std::vector<double> out = vector;
116 
117  //take abs value
118  for (int i =0; i< vector.size(); i++){
119  if (vector.at(i) < 0.0){
120  vector.at(i) *= -1;
121  }
122  }
123 
124  double maxCoeff = *(std::max_element(vector.begin(), vector.end()));
125  if ( maxCoeff > threshold){ //if so, scale vector
126 
127  for (int i=0; i<out.size(); i++){
128  out.at(i) = (out.at(i))/maxCoeff*threshold;
129 
130  }
131 
132  }
133 
134  return out;
135 }
136 
137 
138 CMAT::Matrix FRM::saturateCmat(CMAT::Matrix mat, double threshold){
139  if (threshold <0.0){
140  std::cerr << "FRM::saturateCmat: ERROR: the threshold must be positive";
141  return mat;
142  }
143 
144  CMAT::Matrix out = mat;
145 
146  //take abs value & get max
147  double maxCoeff = 0.0;
148  for (int i=1; i<= mat.GetNumColumns(); i++){
149  for (int j=1; j<= mat.GetNumRows(); j++){
150  if (mat(j,i) < 0.0){
151  mat(j,i) *= -1;
152  }
153  if (mat(j,i) > maxCoeff){
154  maxCoeff = mat(j,i);
155  }
156  }
157  }
158 
159  if ( maxCoeff > threshold){ //if so, scale vector
160  out = out/maxCoeff*threshold;
161  }
162 
163  return out;
164 }
165 
166 double FRM::saturateScalar(double scalar, double threshold){
167 
168  if (threshold <0.0){
169  std::cerr << "FRM::saturateScalar: ERROR: the threshold must be positive";
170  return scalar;
171  }
172 
173  if (scalar < 0){
174 
175  if (scalar < (-threshold) ) {
176  scalar = -threshold;
177  }
178 
179  } else {
180  if (scalar > threshold){
181  scalar = threshold;
182  }
183  }
184 
185  return scalar;
186 
187 }
188 
189 Eigen::Matrix3d FRM::eul2Rot(std::vector<double> eulRad){
190 
191  Eigen::Matrix3d R_x, R_y, R_z;
192 
193  R_x << 1, 0, 0,
194  0, cos(eulRad.at(0)), -sin(eulRad.at(0)),
195  0, sin(eulRad.at(0)), cos(eulRad.at(0));
196 
197  R_y << cos(eulRad.at(1)), 0, sin(eulRad.at(1)),
198  0, 1, 0,
199  -sin(eulRad.at(1)), 0, cos(eulRad.at(1));
200 
201  R_z << cos(eulRad.at(2)), -sin(eulRad.at(2)), 0,
202  sin(eulRad.at(2)), cos(eulRad.at(2)), 0,
203  0, 0, 1;
204 
205 
206  return (R_x * R_y * R_z);
207 
208 
209 
210 
211 }
212 
213