00001
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <mavros/mavros_plugin.h>
00029 #include <pluginlib/class_list_macros.h>
00030
00031 #include <geometry_msgs/PolygonStamped.h>
00032
00033 namespace mavplugin {
00034
00040 class SafetyAreaPlugin : public MavRosPlugin {
00041 public:
00042 SafetyAreaPlugin() :
00043 uas(nullptr)
00044 { };
00045
00046 void initialize(UAS &uas_,
00047 ros::NodeHandle &nh,
00048 diagnostic_updater::Updater &diag_updater)
00049 {
00050 bool manual_def = false;
00051 double p1x, p1y, p1z,
00052 p2x, p2y, p2z;
00053
00054 uas = &uas_;
00055 safety_nh = ros::NodeHandle(nh, "safety_area");
00056
00057 if (safety_nh.getParam("p1/x", p1x) &&
00058 safety_nh.getParam("p1/y", p1y) &&
00059 safety_nh.getParam("p1/z", p1z)) {
00060 manual_def = true;
00061 ROS_DEBUG_NAMED("safetyarea", "SA: Manual set: P1(%f %f %f)",
00062 p1x, p1y, p1z);
00063 }
00064
00065 if (manual_def &&
00066 safety_nh.getParam("p2/x", p2x) &&
00067 safety_nh.getParam("p2/y", p2y) &&
00068 safety_nh.getParam("p2/z", p2z)) {
00069 manual_def = true;
00070 ROS_DEBUG_NAMED("safetyarea", "SA: Manual set: P2(%f %f %f)",
00071 p2x, p2y, p2z);
00072 }
00073 else
00074 manual_def = false;
00075
00076 if (manual_def)
00077 send_safety_set_allowed_area(
00078 p1x, p1y, p1z,
00079 p2x, p2y, p2z);
00080
00081 safetyarea_sub = safety_nh.subscribe("set", 10, &SafetyAreaPlugin::safetyarea_cb, this);
00082 }
00083
00084 const std::string get_name() const {
00085 return "SafetyArea";
00086 }
00087
00088 const message_map get_rx_handlers() {
00089 return { };
00093 }
00094
00095 private:
00096 UAS *uas;
00097
00098 ros::NodeHandle safety_nh;
00099 ros::Subscriber safetyarea_sub;
00100
00101
00102
00103 void safety_set_allowed_area(
00104 uint8_t coordinate_frame,
00105 float p1x, float p1y, float p1z,
00106 float p2x, float p2y, float p2z) {
00107 mavlink_message_t msg;
00108 mavlink_msg_safety_set_allowed_area_pack_chan(UAS_PACK_CHAN(uas), &msg,
00109 UAS_PACK_TGT(uas),
00110 coordinate_frame,
00111 p1x, p1y, p1z,
00112 p2x, p2y, p2z);
00113 UAS_FCU(uas)->send_message(&msg);
00114 }
00115
00116
00117
00124 void send_safety_set_allowed_area(float p1x, float p1y, float p1z,
00125 float p2x, float p2y, float p2z) {
00126
00127 ROS_INFO_NAMED("safetyarea", "SA: Set safty area: P1(%f %f %f) P2(%f %f %f)",
00128 p1x, p1y, p1z,
00129 p2x, p2y, p2z);
00130
00131 safety_set_allowed_area(
00132 MAV_FRAME_LOCAL_NED,
00133 p1y, p1x, -p1z,
00134 p2y, p2x, -p2z);
00135 }
00136
00137
00138
00139 void safetyarea_cb(const geometry_msgs::PolygonStamped::ConstPtr &req) {
00140 if (req->polygon.points.size() != 2) {
00141 ROS_ERROR_NAMED("safetyarea", "SA: Polygon should contain only two points");
00142 return;
00143 }
00144
00145 send_safety_set_allowed_area(
00146 req->polygon.points[0].x,
00147 req->polygon.points[0].y,
00148 req->polygon.points[0].z,
00149 req->polygon.points[1].x,
00150 req->polygon.points[1].y,
00151 req->polygon.points[1].z);
00152 }
00153 };
00154
00155 };
00156
00157 PLUGINLIB_EXPORT_CLASS(mavplugin::SafetyAreaPlugin, mavplugin::MavRosPlugin)