AUV-Coop-Assembly
Master Thesis for Robotics Engineering. Cooperative peg-in-hole assembly with two underwater vehicles guided by vision
forceInsertTask.cpp
1 #include "header/forceInsertTask.h"
2 
9 ForceInsertTask::ForceInsertTask(int dim, bool eqType, std::string robotName)
10  : Task(dim, eqType, robotName, "FORCE_INSERTION"){
11  switch (dimension){
12  case 1:
13  gain = 0.09;
14  break;
15  case 2:
16  gain = 0.01;
17  gainAng = 0.01;
18  break;
19  case 3:
20  gain = 0.001;
21  break;
22  case 6:
23  gain = 0.001;
24  gainAng = 0.001;
25  break;
26  }
27 
28  integralFor << 0,0,0;
29 
30 }
31 
32 int ForceInsertTask::updateMatrices(Infos* const robInfo){
33 
34  Eigen::Vector3d force = robInfo->robotSensor.forcePegTip;
35  Eigen::Vector3d torque = robInfo->robotSensor.torquePegTip;
36  force(0) *= 0.3; //forces on x are always too high, this help considering not so much the forces on x
37  Eigen::Vector3d w_force = robInfo->transforms.wTt_eigen.topLeftCorner<3,3>() * force;
38  Eigen::Vector3d w_torque = robInfo->transforms.wTt_eigen.topLeftCorner<3,3>() * torque;
39 
40  setActivation(w_force, w_torque);
41  setJacobian(robInfo->robotState.w_Jtool_robot, w_force, w_torque);
42  setReference(w_force, w_torque);
43 
44  return 0;
45 
46 }
47 
54 void ForceInsertTask::setActivation(Eigen::Vector3d force, Eigen::Vector3d torque){
55  if (eqType){
56  double vectDiag[dimension];
57  std::fill_n(vectDiag, dimension, 1);
58  this->A.SetDiag(vectDiag);
59 
60  } else {
61 
62  double negValueForActMax = -1;
63  double posValueForActMax = 2;
64  double negValueForAct = -0.001;
65  double posValueForAct = 0.001;
66 
67  switch (dimension){
68  case 1:{
69  double forceNorm = force.norm();
70  A(1,1) = CMAT::DecreasingBellShapedFunction(negValueForActMax, negValueForAct,
71  0, 1, forceNorm) +
72  CMAT::IncreasingBellShapedFunction(posValueForAct, posValueForActMax ,
73  0, 1, forceNorm);
74  break;
75  }
76  case 2:{
77  double forceNorm = force.norm();
78  double torqueNorm = torque.norm();
79 
80  //with norms decreasing not necessary because they are always positive
81  A(1,1) = CMAT::IncreasingBellShapedFunction(posValueForAct, posValueForActMax ,
82  0, 1, forceNorm);
83 
84  A(2,2) = CMAT::IncreasingBellShapedFunction(posValueForAct, posValueForActMax ,
85  0, 1, torqueNorm);
86 
87  break;
88 
89  }
90  case 3: {
91  for (int i=1; i<=dimension; i++){
92  A(i,i) = CMAT::DecreasingBellShapedFunction(negValueForActMax, negValueForAct,
93  0, 1, force(i-1)) +
94  CMAT::IncreasingBellShapedFunction(posValueForAct, posValueForActMax,
95  0, 1, force(i-1));
96  }
97  break;
98  }
99  case 6: {
100  for (int i=1; i<=3; i++){
101  A(i,i) = CMAT::DecreasingBellShapedFunction(negValueForActMax, negValueForAct,
102  0, 1, force(i-1)) +
103  CMAT::IncreasingBellShapedFunction(posValueForAct, posValueForActMax,
104  0, 1, force(i-1));
105  }
106 
107  for (int i=4; i<=6; i++){
108  A(i,i) = CMAT::DecreasingBellShapedFunction(negValueForActMax, negValueForAct,
109  0, 1, torque(i-4)) +
110  CMAT::IncreasingBellShapedFunction(posValueForAct, posValueForActMax,
111  0, 1, torque(i-4));
112  }
113  break;
114  }
115  }
116  }
117 }
118 
119 void ForceInsertTask::setJacobian(Eigen::Matrix<double, 6, TOT_DOF> w_J_robot,
120  Eigen::Vector3d force, Eigen::Vector3d torque){
121 
122  switch (dimension){
123 
124  case 1:{
125 
126  Eigen::Vector3d normalForce;
127  double normForce = force.norm();
128  if (normForce > 0.001) {
129  normalForce = force/normForce;
130  } else {
131  normalForce << 0,0,0;
132  }
133 
134  //not consider vehicle part. Only arm moves to nullify forces
135  w_J_robot.topRows<3>().topRightCorner<3,6>() = Eigen::MatrixXd::Zero(3, 6);
136  J = CONV::matrix_eigen2cmat(
137  (normalForce.transpose()) * (w_J_robot.topRows<3>())); //toprows: only linear part
138 
139 
140  break;
141  }
142 
143  case 2:{
144 
145  Eigen::MatrixXd jacobTemp = Eigen::MatrixXd::Zero(dimension, dof);
146 
148  Eigen::Vector3d normalForce;
149  double normForce = force.norm();
150  if (normForce > 0) {
151  normalForce = force/normForce;
152  } else {
153  normalForce << 0,0,0;
154  }
155 
156  //not consider vehicle part. Only arm moves to nullify forces
157  //w_J_robot.rightCols<6>() = Eigen::MatrixXd::Zero(6, 6);
158  jacobTemp.topRows<1>() = - (normalForce.transpose()) * (w_J_robot.topRows<3>());
159 
161  Eigen::Vector3d normalTorque;
162  double normTorque = torque.norm();
163  if (normTorque > 0) {
164  normalTorque = torque/normTorque;
165  } else {
166  normalTorque << 0,0,0;
167  }
168 
169  jacobTemp.bottomRows<1>() = - (normalTorque.transpose()) * (w_J_robot.bottomRows<3>());
170 
172  J = CONV::matrix_eigen2cmat(jacobTemp);
173 
174  break;
175  }
176 
177  case 3:{
178  J = CONV::matrix_eigen2cmat(w_J_robot.topRows<3>());
179  break;
180  }
181  case 6: {
182  J = CONV::matrix_eigen2cmat(w_J_robot);
183  break;
184  }
185 
186  } // END SWITCH
187 
188 }
189 
190 
191 void ForceInsertTask::setReference(Eigen::Vector3d force, Eigen::Vector3d torque){
192 
193  switch(dimension){
194  case 1:{
195  error(1) = 0 - force.norm();
196  reference = gain * error;
197  reference = FRM::saturateCmat(reference, 0.05);
198 
199  break;
200  }
201 
202  case 2:{
203  error(1) = - force.norm(); //for old logs, not used anymore?
204  //integralFor = (force * 0.1) + integralFor;
205  reference(1) = gain*(error(1)); // - 0.005 * (integralFor.norm());
206  reference(1) = FRM::saturateScalar(reference(1), 0.04);
207 
208 
209  error(2) = - torque.norm();
210  reference(2) = gainAng * error(2);
211  reference(2) = FRM::saturateScalar(reference(2), 0.04);
212 
213  break;
214  }
215 
216  case 3:{
217  error = -1 * CONV::matrix_eigen2cmat(force); //-1 because it is 0 - force
218  reference = gain * error;
219  reference = FRM::saturateCmat(reference, 0.1);
220 
221  break;
222  }
223 
224  case 6: {
225 
226  CMAT::Vect3 force_cmat = CONV::matrix_eigen2cmat(force);
227  CMAT::Vect3 torque_cmat = CONV::matrix_eigen2cmat(torque);
228  error(1) = ( - force_cmat(1));
229  error(2) = ( - force_cmat(2));
230  error(3) = ( - force_cmat(3));
231  error(4) = ( - torque_cmat(1));
232  error(5) = ( - torque_cmat(2));
233  error(6) = ( - torque_cmat(3));
234  reference(1) = gain * ( - force_cmat(1));
235  reference(2) = gain * ( - force_cmat(2));
236  reference(3) = gain * ( - force_cmat(3));
237  reference(4) = gainAng * ( - torque_cmat(1));
238  reference(5) = gainAng * ( - torque_cmat(2));
239  reference(6) = gainAng * ( - torque_cmat(3));
240  reference = FRM::saturateCmat(reference, 0.001);
241  break;
242  }
243  default:
244  std::cerr << "[" << taskName << "] DIMENSION OF TASK IS WRONG\n";
245  }
246 
247 
248 }
249 
250 
ForceInsertTask(int dimension, bool eqType, std::string robotName)
ForceInsertTask::ForceInsertTask.
Definition: infos.h:117
ABSTRACT class task. Each task is a derived class of this class. It contains all the variable that th...
Definition: task.h:17