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 <math.h>
00014
00015 #include <micros_mars_task_alloc/Heading.h>
00016 #include <micros_mars_task_alloc/Force.h>
00017
00018 namespace micros_mars_task_alloc{
00019 using namespace std;
00020
00021 class Avoid : public nodelet::Nodelet
00022 {
00023 public:
00024 Avoid(): magnitude_(0.0), direction_(0.0), random_factor_(0.8), pi_(std::acos(-1)){}
00025 ~Avoid(){}
00026
00027 virtual void onInit();
00028
00029 void callback_0(const micros_mars_task_alloc::HeadingConstPtr& msg);
00030 void callback_1(const micros_mars_task_alloc::ForceConstPtr& msg);
00031
00032
00033 private:
00034 ros::NodeHandle nh_;
00035 ros::Publisher pub_;
00036 ros::Subscriber sub_0_;
00037 ros::Subscriber sub_1_;
00038
00039 float magnitude_;
00040 float direction_;
00041 float pi_;
00042 float random_factor_;
00043 };
00044
00045 void Avoid::onInit()
00046 {
00047 nh_ = getPrivateNodeHandle();
00048 sub_0_ = nh_.subscribe("/wander/heading", 10, &Avoid::callback_0, this);
00049 sub_1_ = nh_.subscribe("/feelforce/force", 10, &Avoid::callback_1, this);
00050 pub_ = nh_.advertise<micros_mars_task_alloc::Heading>("suppressor/heading", 10);
00051
00052 }
00053
00054 void Avoid::callback_0(const micros_mars_task_alloc::HeadingConstPtr& msg)
00055 {
00056 micros_mars_task_alloc::HeadingPtr avoid_heading_ptr(new micros_mars_task_alloc::Heading);
00057
00058 float x0, y0, x1, y1, x_average, y_average;
00059 x0 = 0.0;
00060 y0 = 0.0;
00061 x1 = 0.0;
00062 y1 = 0.0;
00063 x_average = 0.0;
00064 y_average = 0.0;
00065
00066 x0 = float(double((msg -> distance)) * std::cos(double(msg -> angle)));
00067 y0 = float(double((msg -> distance)) * std::sin(double(msg -> angle)));
00068
00069 x1 = float(double(magnitude_) * std::cos(double(direction_)));
00070 y1 = float(double(magnitude_) * std::sin(double(direction_)));
00071
00072 x_average = random_factor_ * x0 + (1 - random_factor_) * x1;
00073 y_average = random_factor_ * y0 + (1 - random_factor_) * y1;
00074
00075 avoid_heading_ptr -> distance = float(std::sqrt(double(x_average * x_average + y_average * y_average)));
00076 avoid_heading_ptr -> angle = float(std::fmod( (std::atan2(double(y_average), double(x_average)) + double(2 * pi_)), double(2 * pi_) ));
00077
00078 pub_.publish(avoid_heading_ptr);
00079 cout << "The avoid heading message was published successfully!" << endl;
00080 }
00081
00082 void Avoid::callback_1(const micros_mars_task_alloc::ForceConstPtr& msg)
00083 {
00084 magnitude_ = msg -> magnitude;
00085 direction_ = msg -> direction;
00086 }
00087
00088 }
00089 PLUGINLIB_EXPORT_CLASS(micros_mars_task_alloc::Avoid, nodelet::Nodelet)