local_position.cpp
Go to the documentation of this file.
00001 
00010 /*
00011  * Copyright 2014 Vladimir Ermakov.
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 <tf/transform_broadcaster.h>
00032 #include <geometry_msgs/PoseStamped.h>
00033 
00034 namespace mavplugin {
00035 
00041 class LocalPositionPlugin : public MavRosPlugin {
00042 public:
00043         LocalPositionPlugin() :
00044                 uas(nullptr),
00045                 send_tf(false)
00046         { };
00047 
00048         void initialize(UAS &uas_,
00049                         ros::NodeHandle &nh,
00050                         diagnostic_updater::Updater &diag_updater)
00051         {
00052                 uas = &uas_;
00053 
00054                 pos_nh = ros::NodeHandle(nh, "position");
00055 
00056                 pos_nh.param("local/send_tf", send_tf, true);
00057                 pos_nh.param<std::string>("local/frame_id", frame_id, "local_origin");
00058                 pos_nh.param<std::string>("local/child_frame_id", child_frame_id, "fcu");
00059 
00060                 local_position = pos_nh.advertise<geometry_msgs::PoseStamped>("local", 10);
00061         }
00062 
00063         std::string const get_name() const {
00064                 return "LocalPosition";
00065         }
00066 
00067         const message_map get_rx_handlers() {
00068                 return {
00069                         MESSAGE_HANDLER(MAVLINK_MSG_ID_LOCAL_POSITION_NED, &LocalPositionPlugin::handle_local_position_ned)
00070                 };
00071         }
00072 
00073 private:
00074         UAS *uas;
00075 
00076         ros::NodeHandle pos_nh;
00077         ros::Publisher local_position;
00078         tf::TransformBroadcaster tf_broadcaster;
00079 
00080         std::string frame_id;           
00081         std::string child_frame_id;     
00082         bool send_tf;
00083 
00084         void handle_local_position_ned(const mavlink_message_t *msg, uint8_t sysid, uint8_t compid) {
00085                 mavlink_local_position_ned_t pos_ned;
00086                 mavlink_msg_local_position_ned_decode(msg, &pos_ned);
00087 
00088                 ROS_DEBUG_THROTTLE_NAMED(10, "position", "Local position NED: boot_ms:%06d "
00089                                 "position:(%1.3f %1.3f %1.3f) speed:(%1.3f %1.3f %1.3f)",
00090                                 pos_ned.time_boot_ms,
00091                                 pos_ned.x, pos_ned.y, pos_ned.z,
00092                                 pos_ned.vx, pos_ned.vy, pos_ned.vz);
00093 
00094                 /* TODO: check convertion to ENU
00095                  * I think XZY is not body-fixed, but orientation does.
00096                  * Perhaps this adds additional errorprone to us.
00097                  * Need more tests. Issue #49.
00098                  *
00099                  * orientation in ENU, body-fixed
00100                  */
00101                 tf::Transform transform;
00102                 transform.setOrigin(tf::Vector3(pos_ned.y, pos_ned.x, -pos_ned.z));
00103                 transform.setRotation(uas->get_attitude_orientation());
00104 
00105                 geometry_msgs::PoseStampedPtr pose = boost::make_shared<geometry_msgs::PoseStamped>();
00106 
00107                 tf::poseTFToMsg(transform, pose->pose);
00108                 pose->header.frame_id = frame_id;
00109                 pose->header.stamp = ros::Time::now();
00110 
00111                 if (send_tf)
00112                         tf_broadcaster.sendTransform(
00113                                         tf::StampedTransform(
00114                                                 transform,
00115                                                 pose->header.stamp,
00116                                                 frame_id, child_frame_id));
00117 
00118                 local_position.publish(pose);
00119         }
00120 };
00121 
00122 }; // namespace mavplugin
00123 
00124 PLUGINLIB_EXPORT_CLASS(mavplugin::LocalPositionPlugin, mavplugin::MavRosPlugin)
00125 


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