safety_area.cpp
Go to the documentation of this file.
00001 
00010 /*
00011  * Copyright 2014 Nuno Marques.
00012  *
00013  * This program is free software; you can redistribute it and/or modify
00014  * it under the terms of the GNU General Public License as published by
00015  * the Free Software Foundation; either version 3 of the License, or
00016  * (at your option) any later version.
00017  *
00018  * This program is distributed in the hope that it will be useful, but
00019  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00020  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
00021  * for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License along
00024  * with this program; if not, write to the Free Software Foundation, Inc.,
00025  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 { /* Rx disabled */ };
00093         }
00094 
00095 private:
00096         UAS *uas;
00097 
00098         ros::NodeHandle safety_nh;
00099         ros::Subscriber safetyarea_sub;
00100 
00101         /* -*- low-level send -*- */
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         /* -*- mid-level helpers -*- */
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         /* -*- callbacks -*- */
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 }; // namespace mavplugin
00156 
00157 PLUGINLIB_EXPORT_CLASS(mavplugin::SafetyAreaPlugin, mavplugin::MavRosPlugin)


mavros
Author(s): Vladimir Ermakov
autogenerated on Wed Aug 26 2015 12:29:13