AUV-Coop-Assembly
Master Thesis for Robotics Engineering. Cooperative peg-in-hole assembly with two underwater vehicles guided by vision
controller2_old.cpp
1 #include <ros/ros.h>
2 #include <Eigen/Core>
3 #include <Eigen/Geometry>
4 #include <tf/transform_listener.h>
5 #include <tf_conversions/tf_eigen.h>
6 #include "../header/publisher.h"
7 #include <cmat/cmat.h>
8 #include "../classes/task.h"
9 #include "../support/support.h"
10 #include "../support/defines.h"
11 
12 
13 //TODO fare il .h
14 int equality_icat(Task task, CMAT::Matrix &rhop, CMAT::Matrix &Q);
15 int inequality_icat(Task task, CMAT::Matrix &rhop, CMAT::Matrix &Q);
16 
17 int main(int argc, char **argv)
18 {
19  ROS_INFO("[CONTROLLER] Start");
20  ros::init(argc, argv, "controller");
21  ros::NodeHandle nh;
22 
23  std::string topicTwist = "/uwsim/g500_A/twist_command_A";
24  ros::Publisher pubTwist = nh.advertise<geometry_msgs::TwistStamped>(topicTwist,1);
25 
26 
27  Publisher pubClassTwist(pubTwist);
28  geometry_msgs::TwistStamped twist;
29  twist.twist.linear.x=0;
30  twist.twist.linear.y=0;
31  twist.twist.linear.z=0;
32  twist.twist.angular.x=0;
33  twist.twist.angular.y=0;
34  twist.twist.angular.z=0;
35 
36  // coord vehicle near the peg [-0.287, -0.062, 7.424]
37  //tf::Vector3 goalPoint = tf::Vector3(-5.287, -0.062, 7.424); //respect world
38  //tf::Transform goal = tf::Transform(tf::Matrix3x3(1, 0, 0, 0, 1, 0, 0, 0, 1), goalPoint);
39  //tf::Vector3 xDot = tf::Vector3(0, 0, 0);
40 
42 
43  double goalLine[3] = {-0.287, -0.062, 7.424};
44  CMAT::Vect3 goal_xyz(goalLine);
45  CMAT::RotMatrix goal_rot = CMAT::Matrix::Eye(3);
46  CMAT::TransfMatrix wTgoal (goal_rot, goal_xyz);
47 
48 
49  //TRANSFORM LISTENER ROBE
50  tf::TransformListener tfListener;
51  tf::StampedTransform wTv_tf;
52 
53  //Wait to transform to be ready (or fail if wait more than 3 sec)
54  tfListener.waitForTransform("world", "/girona500_A", ros::Time(0), ros::Duration(3.0));
55 
56 
57  //initialize task
58  Task vehicleReaching(6,10); //4DOF ARM
59 
60  ros::Rate r(1000); //1000Hz
61  while(ros::ok()){
62  try {
63 
64  tfListener.lookupTransform("world", "/girona500_A", ros::Time(0), wTv_tf);
65 
66  } catch (tf::TransformException &ex) {
67  ROS_ERROR("%s",ex.what());
68  ros::Duration(1.0).sleep();
69  continue;
70  }
71 
73  double vectDiag[6];
74  std::fill_n(vectDiag, 6, 1);
75  vehicleReaching.A.SetDiag(vectDiag);
76 
77 
79  // double* vector = SUPPORT::tfMat_to_double(wTv.getBasis());
80 
81  Eigen::MatrixXd jacobian_eigen(6,10);
82  jacobian_eigen = Eigen::MatrixXd::Zero(6,10);
83 
84  Eigen::Matrix3d wRv_Eigen;
85  tf::matrixTFToEigen(wTv_tf.getBasis(), wRv_Eigen);
86 
87  //matrix([1:6];[1:4]) deve restare zero
88  //matrix([1:3];[5:7]) parte linear
89  jacobian_eigen.block(0,4, 3,3) = wRv_Eigen;
90 
91  //matrix([4:6];[8:10]) parte angolare
92  //according to eigen doc, using these kind of specific function (and not
93  //(.block improves performance
94  jacobian_eigen.bottomRightCorner(3,3) = wRv_Eigen;
95 
96  // in pasto a cmat
97  //eigen unroll to vector for cmat function
98  vehicleReaching.J = CMAT::Matrix(6,TOT_DOF, jacobian_eigen.data());
99 
100 
102  //std::cout << wRv_Eigen << std::endl;
103  CMAT::TransfMatrix wTv_cmat = CONV::transfMatrix_tf2cmat(wTv_tf);
104  CMAT::Vect6 error = CMAT::CartError(wTgoal, wTv_cmat);
105  double k = 0.2;
106  vehicleReaching.reference = k * (error); //ang,lin
107 
108 
110  CMAT::Matrix yDot(TOT_DOF,1);
111  //yDot = [arm arm arm arm wx wy wz x y z]
112 
113  //pseudo easy
114  //yDot = vehicleReaching.J.RegPseudoInverse(0.001, 0.0001) * vehicleReaching.reference;
115 
116  CMAT::Matrix Q = CMAT::Matrix::Eye(TOT_DOF);
117 
118  inequality_icat(vehicleReaching, yDot, Q);
119 
120 
121  twist.twist.angular.x=yDot(5);
122  twist.twist.angular.y=yDot(6);
123  twist.twist.angular.z=yDot(7);
124  twist.twist.linear.x=yDot(8);
125  twist.twist.linear.y=yDot(9);
126  twist.twist.linear.z=yDot(10);
127 
128  pubClassTwist.publish(twist);
129 
130  ros::spinOnce();
131  r.sleep();
132  }
133 
134  return 0;
135 }
136 
137 
138 
139 //TODO passare il task o le cose singole?
140 //int Equalitytask(CMAT::Matrix J, CMAT::Matrix Q, CMAT::Matrix rhop, CMAT::Matrix xdot,) {
141 int equality_icat(Task task, CMAT::Matrix &rhop, CMAT::Matrix &Q) {
142 
143  //J jacobiana task
144  //Q inizializzata a eye(totDof) prima di fare tutto
145  //rhop il controllo cinematico che viene passato a tutti i lower priority task,
146  // che poi alla fine è yDot voluto
147  // xdot è reference del task
148  //mu double var globale che viene modificata da cmat pseudoinverse varie
149  //flag int analogo a mu
150  //NOTA: sono doppie: una per calcolare la W una per la barGpinv
151 
152  CMAT::Matrix I = CMAT::Matrix::Eye(TOT_DOF);
153  CMAT::Matrix barG, barGtransp, T, W, barGpinv;
154  // barG the actual Jacobian
155  barG = task.J * Q;
156  barGtransp = barG.Transpose();
157 
159  // T is the reg. matrix for the different levels of task priority
160  // takes into account that not all the controls are available
161  T = (I - Q).Transpose() * (I-Q);
162 
163  // compute W to solve the problem of discontinuity due to different priority levels
164  W = barG *
165  (barGtransp * barG + T)
166  .RegPseudoInverse(task.threshold, task.lambda, task.mu_W, task.flag_W) *
167  barGtransp;
168 
170  barGpinv = (barGtransp * barG)
171  .RegPseudoInverse(task.threshold, task.lambda, task.mu_G, task.flag_G);
172  rhop = rhop + Q * barGpinv * barGtransp * W * (task.reference - task.J * rhop);
173 
175 
176 
178  Q = Q * (I - barGpinv * barGtransp * barG);
179 
180 }
181 
182 int inequality_icat(Task task, CMAT::Matrix &rhop, CMAT::Matrix &Q) {
183 
184  CMAT::Matrix I = CMAT::Matrix::Eye(TOT_DOF);
185  CMAT::Matrix barG, barGtransp, barGtranspAA, T, H, W, barGpinv;
186  // barG the actual Jacobian
187  barG = task.J * Q;
188  barGtransp = barG.Transpose();
189  barGtranspAA = barGtransp * task.A * task.A;
190 
192  // T is the reg. matrix for the different levels of task priority
193  // takes into account that not all the controls are available
194  T = (I - Q).Transpose() * (I-Q);
195  // H is the reg. matrix for the activation, i.e. A*(I-A)
196  H = barGtransp *
197  (CMAT::Matrix::Eye(task.dimension) - task.A)*
198  task.A * barG;
199 
200  // compute W to solve the problem of discontinuity due to different priority levels
201  W = barG *
202  (barGtranspAA * barG + T + H)
203  .RegPseudoInverse(task.threshold, task.lambda, task.mu_W, task.flag_W) *
204  barGtranspAA;
205 
206  barGpinv = (barGtranspAA * barG + H)
207  .RegPseudoInverse(task.threshold, task.lambda, task.mu_G, task.flag_G);
208 
209 
211 
212 
214  rhop = rhop + Q * barGpinv * barGtranspAA * W * (task.reference - task.J * rhop);
215 
217  Q = Q * (I - barGpinv * barGtranspAA * barG);
218 
219 }
220 
221 
222 
223 
224 
225 
226 
227 
228 
229 
230 
231 
232 
233 
234 
235 
236 
ABSTRACT class task. Each task is a derived class of this class. It contains all the variable that th...
Definition: task.h:17