Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.11 KB | None | 0 0
  1. //Node that makes the robot goto to point
  2.  
  3. #include "ros/ros.h"
  4. #include "geometry_msgs/Twist.h"
  5. #include "geometry_msgs/Pose.h"
  6. #include "gazebo_msgs/ModelStates.h"
  7. #include <sstream>
  8. #include <tf/tf.h>
  9. #include <math.h>
  10.  
  11.  
  12. ros::Publisher velocity_publisher; //publishes the robot velocity
  13. ros::Subscriber pose_subscriber;  //subscribes the robot pose
  14. //gazebo_msgs::ModelStates modelstate;
  15.  
  16. double x;
  17. double y;
  18. double z;
  19. double w;
  20.  
  21. int state = 1;
  22.  
  23. const double PI = 3.14159265359;
  24.  
  25. using namespace std;
  26.  
  27. //method to move the robot straight with a certain speed and a certain direction
  28. void move(double speed, double distance, bool isForward);
  29.  
  30. //method to rotate the robot
  31. void rotate(double angular_speed, double angle, bool clockwise);
  32.  
  33. //method to convert angles from degrees to radians
  34. double degrees_to_radians(double angle_in_degrees);
  35.  
  36. //method for the orientation of the rotation
  37. void setDesiredOrientation (double desired_angle_radians);
  38.  
  39. //method that to update the robot position
  40. void ModelStatecallback(const gazebo_msgs::ModelStates::ConstPtr& msg);
  41.  
  42. //GoTo function
  43. void GoTo (double x_final, double y_final);
  44.  
  45.  
  46. //main function, calls the necessary complementary functions
  47. int main(int argc, char **argv)
  48. {
  49.  
  50.     //Initialize ROS node
  51.     ros::init(argc, argv, "moveto");
  52.     ros::NodeHandle n;
  53.  
  54.     double speed;
  55.     double distance;
  56.     bool isForward;
  57.  
  58.   double angular_speed;
  59.   double angle;
  60.   bool clockwise;
  61.  
  62.   double x_final;
  63.   double y_final;
  64.  
  65.  
  66.   velocity_publisher = n.advertise<geometry_msgs::Twist>("/cmd_vel", 10); //publisher for velocities
  67.   pose_subscriber = n.subscribe("/gazebo/model_states", 100, ModelStatecallback);  //subscriber to robot pose
  68.  
  69.   //test funcion move
  70.   /*cout<<"enter speed: ";
  71.     cin>>speed;
  72.     cout<<"enter distance: ";
  73.     cin>>distance;
  74.     cout<<"forward: ";
  75.   cin>>isForward;
  76.   move (speed, distance, isForward);*/
  77.  
  78.   //test void rotate
  79.   /*cout<<"enter angular speed: ";
  80.   cin>>angular_speed;
  81.   cout<<"enter angle: ";
  82.   cin>>angle;
  83.   cout<<"clockwise: ";
  84.   cin>>clockwise;
  85.   rotate (degrees_to_radians(angular_speed), degrees_to_radians(angle), clockwise);*/
  86.  
  87.   /*setDesiredOrientation (degrees_to_radians(120));
  88.   ros::Rate loop_rate(0.5);
  89.   loop_rate.sleep();
  90.   setDesiredOrientation (degrees_to_radians(-60));
  91.   loop_rate.sleep();
  92.   setDesiredOrientation (degrees_to_radians(0));*/
  93.  
  94.   cout << "xfinal: " << endl;
  95.   cin >> x_final;
  96.   cout << "yfinal: " << endl;
  97.   cin >> y_final;
  98.  
  99.   ros::Rate loop_rate (0.5);
  100.   GoTo (x_final, y_final);
  101.   loop_rate.sleep();
  102.  
  103.   ros::spin();
  104.   return 0;
  105. }
  106.  
  107. /*void move(double speed, double distance, bool isForward)  //makes the robot move on a straith line
  108. {
  109.     geometry_msgs::Twist vel_msg;
  110.    
  111.   //checking the direction of movement, notice that robt initial position is (0,0,0), with orientation in the y axis
  112.     if (isForward)
  113.         vel_msg.linear.y =-abs(speed);
  114.  
  115.     else
  116.         vel_msg.linear.y = abs(speed);
  117.  
  118.   //Set all vels to zero
  119.     vel_msg.linear.x=0;
  120.     vel_msg.linear.z=0;
  121.    
  122.     vel_msg.angular.x = 0;
  123.     vel_msg.angular.y = 0;
  124.     vel_msg.angular.z = 0;
  125.  
  126.   //Time variables to know when the robot will stop (distance=speed*time)
  127.     double t0 = ros::Time::now().toSec();
  128.   double current_distance=0.0;
  129.     ros::Rate loop_rate(10);
  130.  
  131.   //Cycle for the movement
  132.   do{
  133.         velocity_publisher.publish(vel_msg);
  134.         double t1=ros::Time::now().toSec();
  135.     current_distance = speed * (t1-t0);
  136.     //current_distance = distance - abs(modelstate.pose.position.y);
  137.     ros::spinOnce();
  138.         loop_rate.sleep();
  139.   }while (current_distance < distance);
  140.    
  141.   //When finished make velocity zero to stop the robot at the desired location
  142.   vel_msg.linear.y = 0;
  143.     velocity_publisher.publish(vel_msg);
  144.  
  145.   cout << x << y << z << w << endl;
  146.  
  147.   //cout<<"pos: "<<modelstate.pose.position.y << endl;
  148. }*/
  149.  
  150. /*void rotate(double angular_speed, double angle, bool clockwise) {
  151.  
  152.   geometry_msgs::Twist vel_msg;
  153.  
  154.   //Set linear vels to zero
  155.   vel_msg.linear.x=0;
  156.   vel_msg.linear.y=0;
  157.   vel_msg.linear.z=0;
  158.  
  159.   vel_msg.angular.x=0;
  160.   vel_msg.angular.y=0;
  161.  
  162.   if (clockwise)
  163.     vel_msg.angular.z = -abs(angular_speed);
  164.  
  165.   else
  166.     vel_msg.angular.z = abs(angular_speed);
  167.  
  168.   double current_angle = 0.0;
  169.   double t0 = ros::Time::now().toSec();
  170.   ros::Rate loop_rate(10);
  171.  
  172.   do{
  173.     velocity_publisher.publish(vel_msg);
  174.     double t1=ros::Time::now().toSec();
  175.     current_angle = angular_speed * (t1-t0);
  176.     //ros::spinOnce();
  177.     loop_rate.sleep();
  178.   }while (current_angle < angle);
  179.  
  180.   vel_msg.angular.z = 0;
  181.   velocity_publisher.publish(vel_msg);
  182.  
  183. }*/
  184.  
  185. double degrees_to_radians(double angle_in_degrees){
  186.   return angle_in_degrees * (PI/180);
  187.  
  188. }
  189.  
  190. void ModelStatecallback(const gazebo_msgs::ModelStates::ConstPtr& msg)
  191. {
  192.   x = msg->pose[1].position.x;
  193.   y = msg->pose[1].position.y;
  194.   z = msg->pose[1].position.z;
  195.   w = ((tf::getYaw(msg->pose[1].orientation)));
  196.   //cout << y << endl;
  197.  
  198. }
  199.  
  200. /*void setDesiredOrientation (double desired_angle_radians)
  201. {
  202.   double relative_angle_radians = desired_angle_radians - w;
  203.   bool clockwise = ((relative_angle_radians<0)?true:false);
  204.   cout << desired_angle_radians << "," << w << "," << relative_angle_radians << endl;
  205.   rotate (abs(relative_angle_radians)/4, abs(relative_angle_radians), clockwise);
  206. }*/
  207.  
  208.  
  209. void GoTo (double x_final, double y_final)
  210. {
  211.  
  212.   ros::Rate loop_rate(100);
  213.   geometry_msgs::Twist vel_msg;
  214.  
  215.   vel_msg.linear.x=0;
  216.   vel_msg.linear.z=0;
  217.   vel_msg.linear.y=0;
  218.   vel_msg.angular.x = 0;
  219.   vel_msg.angular.y = 0;
  220.   vel_msg.angular.z = 0;
  221.  
  222.   double dist;
  223.  
  224.   dist = sqrt ( pow (x_final - x, 2) + pow(y_final - y, 2) );
  225.  
  226.   //while(1) {cout << y << endl;}
  227.  
  228.   switch (state) {
  229.  
  230.  
  231.     case 1:   //move
  232.  
  233.  
  234.  
  235.      do{
  236.       dist = sqrt ( pow (x_final - x, 2) + pow(y_final - y, 2) );
  237.  
  238.       //I_dist = I_dist + error_dist*0.5;
  239.       vel_msg.linear.y = 0.25*dist;
  240.  
  241.       velocity_publisher.publish(vel_msg);
  242.  
  243.       ros::spinOnce();
  244.       loop_rate.sleep();
  245.  
  246.      cout << y << endl;
  247.     }while (dist < 0.5);
  248.  
  249.       if (dist < 0.5){state = 2;}
  250.  
  251.  
  252.  
  253.       break;
  254.  
  255.     case 2:
  256.     vel_msg.linear.y = 0;
  257.     velocity_publisher.publish(vel_msg);
  258.  
  259.  
  260.  
  261.   }
  262.  
  263.  
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement