Go to the documentation of this file.00001
00002
00003
00004
00005
00006
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 Forward : public nodelet::Nodelet
00021 {
00022 public:
00023 Forward(): halt_flag_(false), angular_velocity_(1.0), linear_velocity_(0.5), idle_time_(0.5){}
00024 ~Forward(){}
00025
00026 virtual void onInit();
00027 void forward_CB(const micros_mars_task_alloc::HeadingConstPtr& msg);
00028 void halt_CB(const std_msgs::BoolConstPtr& msg);
00029
00030 private:
00031 bool halt_flag_;
00032 float linear_velocity_;
00033 float angular_velocity_;
00034 double idle_time_;
00035
00036
00037
00038 ros::NodeHandle nh_;
00039 ros::Subscriber sub_0_;
00040 ros::Subscriber sub_1_;
00041 ros::Publisher pub_twist_;
00042 ros::Publisher pub_turn_reset_;
00043 ros::Publisher pub_busy_;
00044
00045 };
00046
00047 void Forward::onInit()
00048 {
00049 nh_ = getPrivateNodeHandle();
00050 cout << "Initialising nodelet ..." << endl;
00051 sub_0_ = nh_.subscribe("/turn/heading", 10, &Forward::forward_CB, this);
00052 sub_1_ = nh_.subscribe("/collide/halt", 10, &Forward::halt_CB, this);
00053
00054 pub_twist_ = nh_.advertise<geometry_msgs::Twist>("/robot0/cmd_vel", 10);
00055 pub_turn_reset_ = nh_.advertise<std_msgs::Bool>("/turn/reset", 10);
00056 pub_busy_ = nh_.advertise<std_msgs::Bool>("/robot/busy", 10);
00057 }
00058
00059 void Forward::forward_CB(const micros_mars_task_alloc::HeadingConstPtr& msg)
00060 {
00061 cout << "forward_CB start!" << endl;
00062 cout << "halt_flag_: " << halt_flag_ << endl;
00063 ros::Rate rate(10);
00064 geometry_msgs::TwistPtr twist_ptr(new geometry_msgs::Twist);
00065 std_msgs::BoolPtr bool_ptr(new std_msgs::Bool);
00066 std_msgs::BoolPtr busy_ptr(new std_msgs::Bool);
00067
00068 double forward_count = 0;
00069 cout << "while() start" << endl;
00070 float distance = msg->distance;
00071 if(isnan(msg-> distance))
00072 {
00073 distance = 0.5;
00074 }
00075 while(ros::ok())
00076 {
00077 twist_ptr -> linear.x = linear_velocity_;
00078 pub_twist_.publish(twist_ptr);
00079 forward_count += 1;
00080 cout << "msg->angle in forward module: " << msg->angle << endl;
00081 cout << "msg->distance in forward moudle: " << msg->distance << endl;
00082 cout << "linear_velocity_ * 0.1 * forward_count in forwarde module: " << linear_velocity_ * 0.1 * forward_count << endl;
00083 if (halt_flag_)
00084 {
00085 twist_ptr -> linear.x = 0.0;
00086 pub_twist_.publish(twist_ptr);
00087 bool_ptr -> data = true;
00088 pub_turn_reset_.publish(bool_ptr);
00089 cout << "turn_reset 'true' was published successfully." << endl;
00090 break;
00091 }
00092 else if(linear_velocity_ * 0.1 * forward_count > distance)
00093 {
00094 cout << " 'else if' module start!" << endl;
00095 forward_count = 0;
00096 twist_ptr->linear.x = 0.0;
00097 pub_twist_.publish(twist_ptr);
00098 cout << "robot stop, the robot is idle" << endl;
00099
00100 busy_ptr ->data = false;
00101 pub_busy_.publish(busy_ptr);
00102 ros::Duration(idle_time_).sleep();
00103
00104 bool_ptr -> data = true;
00105 pub_turn_reset_.publish(bool_ptr);
00106 cout << "turn reset was published successfully in forwardCB";
00107 break;
00108 }
00109 rate.sleep();
00110 }
00111 cout << "while() end" << endl;
00112 }
00113
00114 void Forward::halt_CB(const std_msgs::BoolConstPtr& msg)
00115 {
00116 halt_flag_ = msg -> data;
00117 if(halt_flag_)
00118 {
00119 std_msgs::BoolPtr bool_ptr(new std_msgs::Bool);
00120
00121 bool_ptr -> data = true;
00122 pub_turn_reset_.publish(bool_ptr);
00123 cout << "turn reset message was published successfully in halt_CB"<< endl;
00124 }
00125 }
00126 }
00127 PLUGINLIB_EXPORT_CLASS(micros_mars_task_alloc::Forward, nodelet::Nodelet)