Go to the documentation of this file.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 <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
00095
00096
00097
00098
00099
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 };
00123
00124 PLUGINLIB_EXPORT_CLASS(mavplugin::LocalPositionPlugin, mavplugin::MavRosPlugin)
00125