turn.cpp
Go to the documentation of this file.
00001 /*
00002 Author: Minglong Li
00003 Affiliation: State Key Laboratory of High Performance Computing (HPCL)
00004              College of Computer, National University of Defense Technology
00005 Email: minglong_l@163.com
00006 Created on: July 16th, 2016
00007 */
00008 #include <ros/ros.h>
00009 #include <nodelet/nodelet.h>
00010 #include <iostream>
00011 #include <pluginlib/class_list_macros.h>
00012 
00013 #include <std_msgs/Bool.h>
00014 #include <geometry_msgs/Twist.h>
00015 #include <micros_mars_task_alloc/Heading.h>
00016 
00017 namespace micros_mars_task_alloc {
00018 using namespace std;
00019 
00020 class Turn : public nodelet::Nodelet
00021 {
00022 public:
00023     Turn(): pi_(std::acos(-1)), reset_flag_(true), angular_velocity_(1.0), turn_frequency_(50){}
00024     ~Turn(){}
00025 
00026     virtual void onInit();
00027     void turn_CB(const micros_mars_task_alloc::HeadingConstPtr& msg);
00028     void reset_CB(const std_msgs::BoolConstPtr& msg);
00029     
00030 private:
00031     float pi_;
00032     bool reset_flag_;
00033     float angular_velocity_;
00034     float turn_frequency_;
00035     
00036     ros::NodeHandle nh_;
00037     ros::Subscriber sub_0_;
00038     ros::Subscriber sub_1_;
00039     ros::Publisher pub_twist_;
00040     ros::Publisher pub_heading_;
00041     
00042     
00043     
00044 };
00045 
00046 void Turn::onInit()
00047 {
00048     nh_ = getPrivateNodeHandle();
00049     cout << "Initialising nodelet ..." << endl;
00050     sub_0_ = nh_.subscribe("/runaway/turn/heading", 10, &Turn::turn_CB, this);//the topic is /turn/runaway/heading
00051     sub_1_ = nh_.subscribe("/turn/reset", 10, &Turn::reset_CB, this);
00052     pub_twist_ = nh_.advertise<geometry_msgs::Twist>("/robot0/cmd_vel", 10);//the topic here is a absolute topic
00053     pub_heading_ = nh_.advertise<micros_mars_task_alloc::Heading>("/turn/heading", 10);//the topic here is a relative topic
00054 }
00055 
00056 void Turn::turn_CB(const micros_mars_task_alloc::HeadingConstPtr& msg)
00057 {
00058     float turn_angle = msg -> angle;
00059     float count;
00060     ros::Rate loop_rate(turn_frequency_);
00061     cout << "msg->angle in turn module: " << msg->angle << endl;
00062     cout << "msg->distance in turn module: " << msg->distance << endl;
00063     cout << "reset_flat_: " << reset_flag_ << endl;
00064     if(isnan(turn_angle))
00065     {
00066         turn_angle = 1.57;//pi/2
00067     } 
00068     if(reset_flag_)
00069     {
00070         cout << "turn reset start" << endl;
00071         geometry_msgs::TwistPtr twist_ptr(new geometry_msgs::Twist);
00072         if (turn_angle == 0.0 || (turn_angle > 0.0 && turn_angle < pi_) || turn_angle == pi_)
00073         {
00074             cout << "turn left start" << endl;
00075             twist_ptr -> angular.z = angular_velocity_;//TODO: angular.z right???????????????????
00076             count = 0;
00077             cout << "turn angle is: " << turn_angle << endl;
00078             while(ros::ok())
00079             {
00080                 pub_twist_.publish(twist_ptr);
00081                 count += 1;
00082                 loop_rate.sleep();
00083                 if ((1.0 / turn_frequency_) * count * angular_velocity_ > turn_angle)
00084                 {
00085                     twist_ptr -> angular.z = 0;
00086                     pub_twist_.publish(twist_ptr);
00087                     break;
00088                 }
00089             }
00090             count = 0;
00091         }
00092         else
00093         {
00094             cout << "turn right start" << endl; 
00095             twist_ptr -> angular.z = (-1.0) * angular_velocity_;
00096             count = 0;
00097             cout << "turn angle is: " << turn_angle << endl;
00098             cout << "(1.0 / turn_frequency_) * count * angular_velocity_ : " << (1.0 / turn_frequency_) * count * angular_velocity_ << endl;
00099             cout << "(2 * pi_ - turn_angle): " << (2 * pi_ - turn_angle) << endl;
00100             while(ros::ok())
00101             {
00102                 pub_twist_.publish(twist_ptr);
00103                 count += 1;
00104                 loop_rate.sleep();
00105                 if ((1.0 / turn_frequency_) * count * angular_velocity_ > (2 * pi_ - turn_angle))
00106                     twist_ptr -> angular.z = 0;
00107                     pub_twist_.publish(twist_ptr);
00108                     break;
00109             }
00110             count = 0;
00111         }
00112         cout << "turn reset stop" << endl;
00113         float temp_distance = msg->distance;
00114         micros_mars_task_alloc::HeadingPtr heading_ptr2(new micros_mars_task_alloc::Heading);
00115         heading_ptr2->distance = temp_distance;
00116         pub_heading_.publish(heading_ptr2);
00117         cout << "heading message was published successfully to the forward module." << endl;
00118         reset_flag_ = false;
00119     }
00120 }
00121 
00122 void Turn::reset_CB(const std_msgs::BoolConstPtr& msg)
00123 {
00124     reset_flag_ = msg -> data;
00125 }
00126 }//namespace micros_mars_task_alloc
00127 PLUGINLIB_EXPORT_CLASS(micros_mars_task_alloc::Turn, nodelet::Nodelet)


micros_mars_task_alloc
Author(s): Minglong Li , Xiaodong Yi , Yanzhen Wang , Zhongxuan Cai
autogenerated on Mon Jul 1 2019 19:55:03