px4flow.cpp
Go to the documentation of this file.
00001 
00010 /*
00011  * Copyright 2014 M.H.Kabir.
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 <mavros_extras/OpticalFlow.h>
00032 
00033 namespace mavplugin {
00034 
00041 class PX4FlowPlugin : public MavRosPlugin {
00042 public:
00043         PX4FlowPlugin() :
00044                 uas(nullptr)
00045         { };
00046 
00047         void initialize(UAS &uas_,
00048                         ros::NodeHandle &nh,
00049                         diagnostic_updater::Updater &diag_updater)
00050         {
00051                 bool mode_tx;
00052                 uas = &uas_;
00053 
00054                 nh.param("optical_flow_tx", mode_tx, false);
00055                 if (!mode_tx)
00056                         flow_pub = nh.advertise<mavros_extras::OpticalFlow>("optical_flow", 10);
00057                 else
00058                         flow_sub = nh.subscribe("optical_flow", 10, &PX4FlowPlugin::send_flow_cb, this);
00059         }
00060 
00061         const std::string get_name() const {
00062                 return "PX4Flow";
00063         }
00064 
00065         const message_map get_rx_handlers() {
00066                 return {
00067                         MESSAGE_HANDLER(MAVLINK_MSG_ID_OPTICAL_FLOW, &PX4FlowPlugin::handle_optical_flow)
00068                 };
00069         }
00070 
00071 private:
00072         UAS *uas;
00073 
00074         ros::Publisher flow_pub;
00075         ros::Subscriber flow_sub;
00076 
00077         void handle_optical_flow(const mavlink_message_t *msg, uint8_t sysid, uint8_t compid) {
00078                 if (flow_pub.getNumSubscribers() == 0)
00079                         return;
00080 
00081                 mavlink_optical_flow_t flow;
00082                 mavlink_msg_optical_flow_decode(msg, &flow);
00083 
00084                 mavros_extras::OpticalFlowPtr flow_msg =
00085                         boost::make_shared<mavros_extras::OpticalFlow>();
00086 
00087                 // Note: for ENU->NED conversion i swap x & y.
00088                 flow_msg->header.stamp = ros::Time::now();
00089                 flow_msg->flow_x = flow.flow_y;
00090                 flow_msg->flow_y = flow.flow_x;
00091                 flow_msg->flow_comp_m_x  = flow.flow_comp_m_y;
00092                 flow_msg->flow_comp_m_y  = flow.flow_comp_m_x;
00093                 flow_msg->quality = flow.quality;
00094                 flow_msg->ground_distance = flow.ground_distance;
00095 
00096                 flow_pub.publish(flow_msg);
00097 
00098                 /* Optional TODO: send ground_distance in sensor_msgs/Range
00099                  *                with data filled by spec on used sonar.
00100                  */
00101         }
00102 
00103         void optical_flow(uint64_t time_usec, uint8_t sensor_id,
00104                         uint16_t flow_x, uint16_t flow_y,
00105                         float flow_comp_m_x, float flow_comp_m_y,
00106                         uint8_t quality,
00107                         float ground_distance) {
00108                 mavlink_message_t msg;
00109                 mavlink_msg_optical_flow_pack_chan(UAS_PACK_CHAN(uas), &msg,
00110                                 time_usec, sensor_id,
00111                                 flow_x, flow_y,
00112                                 flow_comp_m_x, flow_comp_m_y,
00113                                 quality, ground_distance);
00114                 UAS_FCU(uas)->send_message(&msg);
00115         }
00116 
00117         /* -*- ROS callbacks -*- */
00118 
00119         void send_flow_cb(const mavros_extras::OpticalFlow::ConstPtr flow_msg) {
00120                 optical_flow(flow_msg->header.stamp.toNSec() / 1000,
00121                                 0, /* maybe we need parameter? */
00122                                 flow_msg->flow_y,
00123                                 flow_msg->flow_x,
00124                                 flow_msg->flow_comp_m_y,
00125                                 flow_msg->flow_comp_m_x,
00126                                 flow_msg->quality,
00127                                 flow_msg->ground_distance);
00128         }
00129 };
00130 
00131 }; // namespace mavplugin
00132 
00133 PLUGINLIB_EXPORT_CLASS(mavplugin::PX4FlowPlugin, mavplugin::MavRosPlugin)


mavros_extras
Author(s): Vladimir Ermakov
autogenerated on Wed Aug 26 2015 12:29:20