3dr_radio.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 <pluginlib/class_list_macros.h>
00029 
00030 #include <mavros/RadioStatus.h>
00031 
00032 namespace mavplugin {
00033 
00034 class TDRRadioStatus : public diagnostic_updater::DiagnosticTask
00035 {
00036 public:
00037         TDRRadioStatus(const std::string name, uint8_t _low_rssi) :
00038                 diagnostic_updater::DiagnosticTask(name),
00039                 data_received(false),
00040                 low_rssi(_low_rssi),
00041                 last_rst{}
00042         { }
00043 
00044 
00045         template <typename msgT>
00046         void set(msgT &rst) {
00047                 lock_guard lock(mutex);
00048                 data_received = true;
00049 #define RST_COPY(field) last_rst.field = rst.field
00050                 RST_COPY(rssi);
00051                 RST_COPY(remrssi);
00052                 RST_COPY(txbuf);
00053                 RST_COPY(noise);
00054                 RST_COPY(remnoise);
00055                 RST_COPY(rxerrors);
00056                 RST_COPY(fixed);
00057 #undef RST_COPY
00058         }
00059 
00063         void run(diagnostic_updater::DiagnosticStatusWrapper &stat) {
00064                 lock_guard lock(mutex);
00065 
00066                 if (!data_received)
00067                         stat.summary(2, "No data");
00068                 else if (last_rst.rssi < low_rssi)
00069                         stat.summary(1, "Low RSSI");
00070                 else if (last_rst.remrssi < low_rssi)
00071                         stat.summary(1, "Low remote RSSI");
00072                 else
00073                         stat.summary(0, "Normal");
00074 
00075                 float rssi_dbm = (last_rst.rssi / 1.9) - 127;
00076                 float remrssi_dbm = (last_rst.remrssi / 1.9) - 127;
00077 
00078                 stat.addf("RSSI", "%u", last_rst.rssi);
00079                 stat.addf("RSSI (dBm)", "%.1f", rssi_dbm);
00080                 stat.addf("Remote RSSI", "%u", last_rst.remrssi);
00081                 stat.addf("Remote RSSI (dBm)", "%.1f", remrssi_dbm);
00082                 stat.addf("Tx buffer (%)", "%u", last_rst.txbuf);
00083                 stat.addf("Noice level", "%u", last_rst.noise);
00084                 stat.addf("Remote noice level", "%u", last_rst.remnoise);
00085                 stat.addf("Rx errors", "%u", last_rst.rxerrors);
00086                 stat.addf("Fixed", "%u", last_rst.fixed);
00087         }
00088 
00089 private:
00090         std::recursive_mutex mutex;
00091         mavlink_radio_status_t last_rst;
00092         bool data_received;
00093         const uint8_t low_rssi;
00094 };
00095 
00096 
00100 class TDRRadioPlugin : public MavRosPlugin {
00101 public:
00102         TDRRadioPlugin() :
00103                 tdr_diag("3DR Radio", 40),
00104                 has_radio_status(false)
00105         { }
00106 
00107         void initialize(UAS &uas,
00108                         ros::NodeHandle &nh,
00109                         diagnostic_updater::Updater &diag_updater)
00110         {
00111                 diag_updater.add(tdr_diag);
00112                 status_pub = nh.advertise<mavros::RadioStatus>("radio_status", 10);
00113         }
00114 
00115         std::string const get_name() const {
00116                 return "3DRRadio";
00117         }
00118 
00119         const message_map get_rx_handlers() {
00120                 return {
00121                         MESSAGE_HANDLER(MAVLINK_MSG_ID_RADIO_STATUS, &TDRRadioPlugin::handle_radio_status),
00122 #ifdef MAVLINK_MSG_ID_RADIO
00123                         MESSAGE_HANDLER(MAVLINK_MSG_ID_RADIO, &TDRRadioPlugin::handle_radio),
00124 #endif
00125                 };
00126         }
00127 
00128 private:
00129         TDRRadioStatus tdr_diag;
00130         bool has_radio_status;
00131 
00132         ros::Publisher status_pub;
00133 
00134         /* -*- message handlers -*- */
00135 
00136         void handle_radio_status(const mavlink_message_t *msg, uint8_t sysid, uint8_t compid) {
00137                 mavlink_radio_status_t rst;
00138                 mavlink_msg_radio_status_decode(msg, &rst);
00139                 has_radio_status = true;
00140                 handle_message(rst, sysid, compid);
00141         }
00142 
00143 #ifdef MAVLINK_MSG_ID_RADIO
00144         void handle_radio(const mavlink_message_t *msg, uint8_t sysid, uint8_t compid) {
00145                 if (has_radio_status)
00146                         return;
00147 
00148                 // actually the same data, but from earlier modems
00149                 mavlink_radio_t rst;
00150                 mavlink_msg_radio_decode(msg, &rst);
00151                 handle_message(rst, sysid, compid);
00152         }
00153 #endif
00154 
00155         template<typename msgT>
00156         void handle_message(msgT &rst, uint8_t sysid, uint8_t compid) {
00157                 if (sysid != '3' || compid != 'D')
00158                         ROS_WARN_THROTTLE_NAMED(30, "radio", "RADIO_STATUS not from 3DR modem?");
00159 
00160                 tdr_diag.set(rst);
00161 
00162                 mavros::RadioStatusPtr msg = boost::make_shared<mavros::RadioStatus>();
00163 
00164 #define RST_COPY(field) msg->field = rst.field
00165                 RST_COPY(rssi);
00166                 RST_COPY(remrssi);
00167                 RST_COPY(txbuf);
00168                 RST_COPY(noise);
00169                 RST_COPY(remnoise);
00170                 RST_COPY(rxerrors);
00171                 RST_COPY(fixed);
00172 #undef RST_COPY
00173 
00174                 msg->header.stamp = ros::Time::now();
00175                 status_pub.publish(msg);
00176         }
00177 };
00178 
00179 }; // namespace mavplugin
00180 
00181 PLUGINLIB_EXPORT_CLASS(mavplugin::TDRRadioPlugin, mavplugin::MavRosPlugin)
00182 


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