Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.04 KB | None | 0 0
  1. #include "ros/ros.h"
  2. #include "ros/rate.h"
  3. #include "nav_msgs/Odometry.h"
  4. #include "tf2_ros/transform_listener.h"
  5. #include "tf/tf.h"
  6. #include "geometry_msgs/PoseStamped.h"
  7. #include <math.h>
  8. #include <stdlib.h>
  9. #define PI 3.14159265358979323846
  10. const float delta = PI / 8;
  11. const float near_Dist = 0.5;
  12. const float stop_Dist = 0.15;
  13.  
  14. float destinationLocation[2];
  15. float currentLocation[2];
  16. bool flag;
  17. double curr_Yaw = 0.0;
  18. double dest_Yaw = 0.0;
  19.  
  20. void goalCallback(const geometry_msgs::PoseStamped &recvDest)
  21. {
  22.     destinationLocation[0] = recvDest.pose.position.x;
  23.     destinationLocation[1] = recvDest.pose.position.y;
  24.     flag = false;
  25. }
  26.  
  27. // funkcija povratnog poziva
  28. void poseMessageReceived (const nav_msgs::Odometry::ConstPtr& msg)
  29. {
  30.     currentLocation[0] = msg->pose.pose.position.x;
  31.     currentLocation[1] = msg->pose.pose.position.y;
  32.    
  33.     tf::Quaternion q(
  34.       msg->pose.pose.orientation.x,
  35.       msg->pose.pose.orientation.y,
  36.       msg->pose.pose.orientation.z,
  37.       msg->pose.pose.orientation.w
  38.     );
  39.  
  40.     tf::Matrix3x3 m(q);
  41.  
  42.     double roll, pitch, yaw;
  43.     m.getRPY(roll, pitch, yaw);
  44.     curr_Yaw = yaw;
  45. }
  46.  
  47. int main (int argc, char** argv) {
  48.   // Inicijaliziraj ROS i postani čvor
  49.   ros::init (argc, argv, "zadaca2");
  50.   ros::NodeHandle nh;
  51.  
  52.   // Kreiraj pretplatnički objekt
  53.   // Parametri su: ime_teme, veličina_spremnika, funkcija_povratnog_poziva
  54.   ros::Subscriber subGoal = nh.subscribe("move_base_simple/goal", 1000, &goalCallback);
  55.   ros::Subscriber subOdom = nh.subscribe("odom", 1000, &poseMessageReceived);
  56.  
  57.   ros::Publisher pub = nh.advertise<geometry_msgs::Twist>("cmd_vel", 1000);
  58.   ros::Rate rate(20);
  59.   geometry_msgs::Twist movement;
  60.   dest_Yaw = 0.0;
  61.   float past_Distance = 0.0;
  62.   float curr_Distance = 0.0;
  63.   flag = true;
  64.  
  65.   while(nh.ok()){
  66.  
  67.     if(flag){
  68.       rate.sleep();
  69.       ros::spinOnce();
  70.       continue;
  71.     }else{
  72.         double dx, dy;
  73.         dx = destinationLocation[0] - currentLocation[0];
  74.         dy = destinationLocation[1] - currentLocation[1];
  75.         dest_Yaw = std::atan2(dy, dx);
  76.         float diff_Yaw =  dest_Yaw - curr_Yaw;
  77.                 ROS_INFO("yaw diff 1: %.2f", dest_Yaw - curr_Yaw);
  78.         while(diff_Yaw > PI) diff_Yaw -= 2*PI;
  79.         while(diff_Yaw < -PI) diff_Yaw += 2*PI;
  80.                 ROS_INFO("yaw diff 2: %.2f", dest_Yaw - curr_Yaw);
  81.  
  82.  
  83.         past_Distance = curr_Distance;
  84.         curr_Distance = std::sqrt(std::pow(dx, 2) + std::pow(dy, 2));
  85.         movement.linear.y = 0.0;
  86.         movement.linear.z = 0.0;
  87.         movement.angular.x = 0.0;
  88.         movement.angular.y = 0.0;
  89.  
  90.         //Okreni se u mjestu ukoliko je razlika kuteva veća od "delta"
  91.         if(diff_Yaw > delta && curr_Distance > stop_Dist)
  92.         {
  93.           movement.linear.x = 0.0;
  94.           ROS_INFO("yaw > 0.015");
  95.           movement.angular.z = 0.5 * std::abs(diff_Yaw);
  96.         }
  97.         else if (diff_Yaw < -delta && curr_Distance > stop_Dist)
  98.         {
  99.           movement.linear.x = 0.0;
  100.           ROS_INFO("yaw < 0.015");
  101.           movement.angular.z = -0.5 * std::abs(diff_Yaw);
  102.         }
  103.         //Ukoliko razlika kuteva nije veća od "delta"
  104.         else
  105.         {
  106.           if(curr_Distance > near_Dist)
  107.           {
  108.             movement.linear.x = 0.4;
  109.             ROS_INFO("DISTANCE > THRESHOLD_DISTANCE");
  110.             movement.angular.z = (std::abs(diff_Yaw)<=2) ? 0.5 * (diff_Yaw) : 1;
  111.           }
  112.           else if(curr_Distance > stop_Dist)
  113.           {
  114.             movement.linear.x = 0.2;
  115.             ROS_INFO("DISTANCE > STOP_DISTANCE");
  116.             movement.angular.z = (std::abs(diff_Yaw)<=2) ? 0.5 * (diff_Yaw) : 1;
  117.           }
  118.           else
  119.           {
  120.             ROS_INFO("Current distance good. STOP.");
  121.             movement.linear.x = 0.0;
  122.             movement.angular.z = 0.0;
  123.           }
  124.         }
  125.  
  126.         ROS_INFO("X: %.2f, Y: %.2f", currentLocation[0], currentLocation[1]);
  127.         ROS_INFO("yaw diff: %.2f", dest_Yaw - curr_Yaw);
  128.         ROS_INFO("Dest.yaw: %.2f, current.yaw: %.2f", dest_Yaw, curr_Yaw);
  129.         ROS_INFO("DISTANCE: %.2f, SPEED: %.2f\n", curr_Distance, movement.linear.x);
  130.  
  131.         pub.publish(movement);
  132.         rate.sleep();
  133.  
  134.         ros::spinOnce();
  135.     }
  136. }
  137.  
  138.   return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement