AUV-Coop-Assembly
Master Thesis for Robotics Engineering. Cooperative peg-in-hole assembly with two underwater vehicles guided by vision
controller_old.cpp
1 #include <ros/ros.h>
2 #include <tf/transform_listener.h>
3 #include "../header/publisher.h"
4 #include <eigen3/Eigen/Geometry>
5 #include <cmat/cmat.h>
6 
7 typedef Eigen::Matrix<double, 3, 3> JacobianVehiclePose;
8 
9 int main(int argc, char **argv)
10 {
11  ROS_INFO("[CONTROLLER] Start");
12  ros::init(argc, argv, "controller");
13  ros::NodeHandle nh;
14 
15  std::string topicTwist = "/uwsim/g500_A/twist_command_A";
16  ros::Publisher pubTwist = nh.advertise<geometry_msgs::TwistStamped>(topicTwist,1);
17 
18 
19  Publisher pubClassTwist(pubTwist);
20  geometry_msgs::TwistStamped twist;
21  twist.twist.linear.x=0;
22  twist.twist.linear.y=0;
23  twist.twist.linear.z=0;
24  twist.twist.angular.x=0;
25  twist.twist.angular.y=0;
26  twist.twist.angular.z=0;
27 
28  // coord vehicle near the peg [-0.287, -0.062, 7.424]
29  tf::Vector3 goalPoint = tf::Vector3(-5.287, -0.062, 7.424); //respect world
30 
31  tf::Transform goal = tf::Transform(tf::Matrix3x3(1, 0, 0, 0, 1, 0, 0, 0, 1), goalPoint);
32 
33  tf::Vector3 yDot = tf::Vector3(0, 0, 0);
34  tf::Vector3 xDot = tf::Vector3(0, 0, 0);
35 
36  //TRANSFORM LISTENER ROBE
37  tf::TransformListener tfListener;
38  tf::StampedTransform wTv;
39 
40  //Wait to transform to be ready (or fail if wait more than 3 sec)
41  tfListener.waitForTransform("world", "/girona500_A", ros::Time(0), ros::Duration(3.0));
42 
43  ros::Rate r(1000); //1Hz
44  while(ros::ok()){
45  try {
46 
47  tfListener.lookupTransform("world", "/girona500_A", ros::Time(0), wTv);
48 
49  } catch (tf::TransformException &ex) {
50  ROS_ERROR("%s",ex.what());
51  ros::Duration(1.0).sleep();
52  continue;
53  }
54 
55  //simple difference controller
56  //double k = 0.05;
57  //yDot = k * (goalPoint - wTv.getOrigin());
58 
60  //reference
61  double k = 0.2;
62  xDot = k * (goalPoint - wTv.getOrigin());
63 
64  double vect[16];
65  vect[0] = wTv.getBasis().getRow(0).getX();
66  vect[0] = wTv.getBasis().getRow(0).getY();
67 
68  //cmat_wTv.SetRotMatrix(wTv.getBasis());
69  //cmat_wTv.Matrix[1] = 1;
70  //cmat_wTv.GetTrasl().PrintMtx();
71  //JacobianVehiclePose Jveh = JacobianVehiclePose();
72  //Jveh.
73  tf::Matrix3x3 jacobianVehiclePose = wTv.getBasis();
74 
75  yDot = (jacobianVehiclePose.inverse() * xDot);
76 
77 
78  twist.twist.linear.x=yDot.getX();
79  twist.twist.linear.y=yDot.getY();
80  twist.twist.linear.z=yDot.getZ();
81 
82  pubClassTwist.publish(twist);
83 
84  ros::spinOnce();
85  r.sleep();
86  }
87 
88  return 0;
89 }