setpoint_position.cpp
Go to the documentation of this file.
00001 
00009 /*
00010  * Copyright 2014 Vladimir Ermakov.
00011  *
00012  * This program is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License as published by
00014  * the Free Software Foundation; either version 3 of the License, or
00015  * (at your option) any later version.
00016  *
00017  * This program is distributed in the hope that it will be useful, but
00018  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00019  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
00020  * for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License along
00023  * with this program; if not, write to the Free Software Foundation, Inc.,
00024  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00025  */
00026 
00027 #include <mavros/mavros_plugin.h>
00028 #include <mavros/setpoint_mixin.h>
00029 #include <pluginlib/class_list_macros.h>
00030 
00031 #include <geometry_msgs/PoseStamped.h>
00032 
00033 namespace mavplugin {
00034 
00040 class SetpointPositionPlugin : public MavRosPlugin,
00041         private SetPositionTargetLocalNEDMixin<SetpointPositionPlugin>,
00042         private TFListenerMixin<SetpointPositionPlugin> {
00043 public:
00044         SetpointPositionPlugin() :
00045                 uas(nullptr),
00046                 tf_rate(10.0)
00047         { };
00048 
00049         void initialize(UAS &uas_,
00050                         ros::NodeHandle &nh,
00051                         diagnostic_updater::Updater &diag_updater)
00052         {
00053                 bool listen_tf;
00054 
00055                 uas = &uas_;
00056                 sp_nh = ros::NodeHandle(nh, "setpoint");
00057 
00058                 sp_nh.param("position/listen_tf", listen_tf, false);
00059                 sp_nh.param<std::string>("position/frame_id", frame_id, "local_origin");
00060                 sp_nh.param<std::string>("position/child_frame_id", child_frame_id, "setpoint");
00061                 sp_nh.param("position/tf_rate_limit", tf_rate, 50.0);
00062 
00063                 if (listen_tf) {
00064                         ROS_INFO_STREAM_NAMED("setpoint", "Listen to position setpoint transform " << frame_id
00065                                         << " -> " << child_frame_id);
00066                         tf_start("PositionSpTF", &SetpointPositionPlugin::send_setpoint_transform);
00067                 }
00068                 else {
00069                         setpoint_sub = sp_nh.subscribe("local_position", 10, &SetpointPositionPlugin::setpoint_cb, this);
00070                 }
00071         }
00072 
00073         const std::string get_name() const {
00074                 return "SetpointPosition";
00075         }
00076 
00077         const message_map get_rx_handlers() {
00078                 return { /* Rx disabled */ };
00079         }
00080 
00081 private:
00082         friend class SetPositionTargetLocalNEDMixin;
00083         friend class TFListenerMixin;
00084         UAS *uas;
00085 
00086         ros::NodeHandle sp_nh;
00087         ros::Subscriber setpoint_sub;
00088 
00089         std::string frame_id;
00090         std::string child_frame_id;
00091 
00092         double tf_rate;
00093 
00094         /* -*- mid-level helpers -*- */
00095 
00101         void send_setpoint_transform(const tf::Transform &transform, const ros::Time &stamp) {
00102                 // ENU frame
00103                 tf::Vector3 origin = transform.getOrigin();
00104                 tf::Quaternion q = transform.getRotation();
00105 
00106                 /* Documentation start from bit 1 instead 0,
00107                  * Ignore velocity and accel vectors, yaw rate
00108                  */
00109                 uint16_t ignore_all_except_xyzy = (1<<11)|(7<<6)|(7<<3);
00110 
00111                 // ENU->NED. Issue #49.
00112                 set_position_target_local_ned(stamp.toNSec() / 1000000,
00113                                 MAV_FRAME_LOCAL_NED,
00114                                 ignore_all_except_xyzy,
00115                                 origin.y(), origin.x(), -origin.z(),
00116                                 0.0, 0.0, 0.0,
00117                                 0.0, 0.0, 0.0,
00118                                 tf::getYaw(q), 0.0);
00119         }
00120 
00121         /* -*- callbacks -*- */
00122 
00123         /* common TF listener moved to mixin */
00124 
00125         void setpoint_cb(const geometry_msgs::PoseStamped::ConstPtr &req) {
00126                 tf::Transform transform;
00127                 poseMsgToTF(req->pose, transform);
00128                 send_setpoint_transform(transform, req->header.stamp);
00129         }
00130 };
00131 
00132 }; // namespace mavplugin
00133 
00134 PLUGINLIB_EXPORT_CLASS(mavplugin::SetpointPositionPlugin, mavplugin::MavRosPlugin)


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