my_plugin.cpp
Go to the documentation of this file.
00001 /*
00002  *  This file is part of ucl_drone 2016.
00003  *  For more information, refer
00004  *  to the corresponding header file.
00005  *
00006  *  \author Arnaud Jacques
00007  *  \date 2016
00008  *
00009  */
00010 
00011 #include "ucl_drone_gui/my_plugin.h"
00012 #include <pluginlib/class_list_macros.h>
00013 #include <QStringList>
00014 
00015 namespace ucl_drone_gui
00016 {
00017 MyPlugin::MyPlugin() : rqt_gui_cpp::Plugin(), widget_(0)
00018 {
00019   // Constructor is called first before initPlugin function, needless to say.
00020 
00021   // give QObjects reasonable names
00022   setObjectName("ucl_drone_gui");
00023 }
00024 
00025 void MyPlugin::initPlugin(qt_gui_cpp::PluginContext& context)
00026 {
00027   // access standalone command line arguments
00028   QStringList argv = context.argv();
00029   // create QWidget
00030   widget_ = new QWidget();
00031   // extend the widget with all attributes and children from UI file
00032   ui_.setupUi(widget_);
00033   // add widget to the user interface
00034   context.addWidget(widget_);
00035 
00036   roles_channel = getNodeHandle().resolveName("/drones_roles");
00037   roles_sub = getNodeHandle().subscribe(roles_channel, 1, &MyPlugin::rolesCb, this);
00038 
00039   // connect each button defined in my_plugin.ui to a method of  the current MyPlugin object
00040   connect(ui_.DroneSelect, SIGNAL(currentIndexChanged(const QString&)), this,
00041           SLOT(drone_select(const QString&)));
00042   connect(ui_.EmergencyButton, SIGNAL(clicked()), this, SLOT(emergency_publish()));
00043   connect(ui_.LandButton, SIGNAL(clicked()), this, SLOT(land_publish()));
00044   connect(ui_.TakeOffButton, SIGNAL(clicked()), this, SLOT(takeoff_publish()));
00045   connect(ui_.ResetPoseButton, SIGNAL(clicked()), this, SLOT(reset_pose_publish()));
00046   connect(ui_.addButton, SIGNAL(clicked()), this, SLOT(add_drone_to_list()));
00047   connect(ui_.removeButton, SIGNAL(clicked()), this, SLOT(remove_drone_from_list()));
00048 }
00049 
00050 void MyPlugin::add_drone_to_list()
00051 {
00052   QString text = ui_.addEdit->text();
00053   if (text.isEmpty())
00054   {
00055     return;
00056   }
00057   if (drones_list.contains(text))
00058   {
00059     return;
00060   }
00061   drones_list += text;
00062   QStringList list = (QStringList() << text);
00063   ui_.DroneSelect->addItems(list);
00064   int i = drones_list.size() - 1;
00065   init_emergency_pub(i, true);
00066   init_land_pub(i, true);
00067   init_takeoff_pub(i, true);
00068   init_reset_pose_pub(i, true);
00069 }
00070 
00071 void MyPlugin::remove_drone_from_list()
00072 {
00073   int i = ui_.DroneSelect->currentIndex();
00074   if (i < 0)
00075   {
00076     return;
00077   }
00078   drones_list.removeAt(i);
00079   ui_.DroneSelect->removeItem(i);
00080 
00081   emergency_pub.erase(emergency_pub.begin() + i);
00082   land_pub.erase(land_pub.begin() + i);
00083   takeoff_pub.erase(takeoff_pub.begin() + i);
00084   reset_pose_pub.erase(reset_pose_pub.begin() + i);
00085 }
00086 
00087 void MyPlugin::drone_select(const QString& str)
00088 {
00089   ROS_INFO("MyPlugin::drone_select");
00090   ui_.BatteryPercentageLabel->setText("N/A%");
00091   navdata_sub.shutdown();
00092   std::string name = str.toUtf8().constData();
00093   std::string navdata_sub_channel = getNodeHandle().resolveName("/" + name + "/navdata");
00094   navdata_sub = getNodeHandle().subscribe(navdata_sub_channel, 1, &MyPlugin::navdataCb, this);
00095 }
00096 
00097 void MyPlugin::emergency_publish()
00098 {
00099   ROS_INFO("MyPlugin::emergency_publish");
00100   std_msgs::Empty msg;
00101   int i = ui_.DroneSelect->currentIndex();
00102   if (i < 0)
00103   {
00104     return;
00105   }
00106   emergency_pub[i].publish(msg);
00107 }
00108 
00109 void MyPlugin::land_publish()
00110 {
00111   ROS_INFO("MyPlugin::land_publish");
00112   std_msgs::Empty msg;
00113   int i = ui_.DroneSelect->currentIndex();
00114   if (i < 0)
00115   {
00116     return;
00117   }
00118   land_pub[i].publish(msg);
00119 }
00120 
00121 void MyPlugin::takeoff_publish()
00122 {
00123   ROS_INFO("MyPlugin::takeoff_publish");
00124   std_msgs::Empty msg;
00125   int i = ui_.DroneSelect->currentIndex();
00126   if (i < 0)
00127   {
00128     return;
00129   }
00130   takeoff_pub[i].publish(msg);
00131 }
00132 
00133 void MyPlugin::reset_pose_publish()
00134 {
00135   ROS_INFO("MyPlugin::reset_pose_publish");
00136   std_msgs::Empty msg;
00137   int i = ui_.DroneSelect->currentIndex();
00138   if (i < 0)
00139   {
00140     return;
00141   }
00142   reset_pose_pub[i].publish(msg);
00143 }
00144 
00145 void MyPlugin::rolesCb(const ucl_drone::DroneRoles::ConstPtr drones_rolesPtr)
00146 {
00147   ROS_INFO("MyPlugin::rolesCb");
00148   if (ui_.DroneSelect->count() == 0)
00149   {
00150     emergency_pub.resize(drones_rolesPtr->roles.size());
00151     land_pub.resize(drones_rolesPtr->roles.size());
00152     takeoff_pub.resize(drones_rolesPtr->roles.size());
00153     reset_pose_pub.resize(drones_rolesPtr->roles.size());
00154     for (int i = 0; i < drones_rolesPtr->roles.size(); i++)
00155     {
00156       std::string name = drones_rolesPtr->roles[i].name;
00157       drones_list += (QStringList() << QString::fromUtf8(name.data(), name.size()));
00158     }
00159     drones_list.removeDuplicates();
00160     init_emergency_pub();
00161     init_land_pub();
00162     init_takeoff_pub();
00163     init_reset_pose_pub();
00164     ui_.DroneSelect->addItems(drones_list);
00165   }
00166 }
00167 
00168 void MyPlugin::init_emergency_pub(int i, bool only)
00169 {
00170   ROS_INFO("MyPlugin::init_emergency_pub");
00171   if (drones_list.size() <= i)
00172     return;
00173   emergency_pub.resize(drones_list.size());
00174   std::string name = drones_list.at(i).toUtf8().constData();
00175   std::string emergency_pub_channel = getNodeHandle().resolveName("/" + name + "/emergency_toggle");
00176   emergency_pub[i] = getNodeHandle().advertise< std_msgs::Empty >(emergency_pub_channel, 1);
00177   if (!only && i + 1 < drones_list.size())
00178   {
00179     this->init_emergency_pub(i + 1, false);
00180   }
00181 }
00182 
00183 void MyPlugin::init_land_pub(int i, bool only)
00184 {
00185   ROS_INFO("MyPlugin::init_land_pub");
00186   if (drones_list.size() <= i)
00187     return;
00188   land_pub.resize(drones_list.size());
00189   std::string name = drones_list.at(i).toUtf8().constData();
00190   std::string land_pub_channel = getNodeHandle().resolveName("/" + name + "/land");
00191   land_pub[i] = getNodeHandle().advertise< std_msgs::Empty >(land_pub_channel, 1);
00192   if (!only && i + 1 < drones_list.size())
00193   {
00194     this->init_land_pub(i + 1, false);
00195   }
00196 }
00197 
00198 void MyPlugin::init_takeoff_pub(int i, bool only)
00199 {
00200   ROS_INFO("MyPlugin::init_takeoff_pub");
00201   if (drones_list.size() <= i)
00202     return;
00203   takeoff_pub.resize(drones_list.size());
00204   std::string name = drones_list.at(i).toUtf8().constData();
00205   std::string takeoff_pub_channel = getNodeHandle().resolveName("/" + name + "/takeoff");
00206   takeoff_pub[i] = getNodeHandle().advertise< std_msgs::Empty >(takeoff_pub_channel, 1);
00207   if (!only && i + 1 < drones_list.size())
00208   {
00209     this->init_takeoff_pub(i + 1, false);
00210   }
00211 }
00212 
00213 void MyPlugin::init_reset_pose_pub(int i, bool only)
00214 {
00215   ROS_INFO("MyPlugin::init_reset_pose_pub");
00216   if (drones_list.size() <= i)
00217     return;
00218   reset_pose_pub.resize(drones_list.size());
00219   std::string name = drones_list.at(i).toUtf8().constData();
00220   std::string reset_pose_pub_channel = getNodeHandle().resolveName("/" + name + "/reset_pose");
00221   reset_pose_pub[i] = getNodeHandle().advertise< std_msgs::Empty >(reset_pose_pub_channel, 1);
00222   if (!only && i + 1 < drones_list.size())
00223   {
00224     this->init_reset_pose_pub(i + 1, false);
00225   }
00226 }
00227 
00228 void MyPlugin::navdataCb(const ardrone_autonomy::Navdata::ConstPtr navdata)
00229 {
00230   int percent = navdata->batteryPercent;
00231   // ROS_INFO("MyPlugin::navdataCb %d", percent);
00232   QString s = QString::number(percent) + "%";
00233   ui_.BatteryPercentageLabel->setText(s);
00234 }
00235 
00236 void MyPlugin::shutdownPlugin()
00237 {
00238   roles_sub.shutdown();
00239   navdata_sub.shutdown();
00240   for (int i = 0; i < drones_list.size(); i++)
00241   {
00242     emergency_pub[i].shutdown();
00243     land_pub[i].shutdown();
00244     takeoff_pub[i].shutdown();
00245     reset_pose_pub[i].shutdown();
00246   }
00247   // TODO unregister all subscribers, publishers here
00248 }
00249 
00250 void MyPlugin::saveSettings(qt_gui_cpp::Settings& plugin_settings,
00251                             qt_gui_cpp::Settings& instance_settings) const
00252 {
00253   // TODO save intrinsic configuration, usually using:
00254   QString k1("drones_list");
00255   instance_settings.setValue(k1, drones_list);
00256   QString k2("drone_selected");
00257   instance_settings.setValue(k2, ui_.DroneSelect->currentIndex());
00258 }
00259 
00260 void MyPlugin::restoreSettings(const qt_gui_cpp::Settings& plugin_settings,
00261                                const qt_gui_cpp::Settings& instance_settings)
00262 {
00263   // TODO restore intrinsic configuration, usually using:
00264   // v = instance_settings.value(k);
00265 
00266   QString k1("drones_list");
00267   drones_list = instance_settings.value(k1).toStringList();
00268   init_emergency_pub();
00269   init_land_pub();
00270   init_takeoff_pub();
00271   init_reset_pose_pub();
00272   ui_.DroneSelect->addItems(drones_list);
00273   QString k2("drone_selected");
00274   int drones_selected = instance_settings.value(k2).toInt();
00275   ui_.DroneSelect->setCurrentIndex(drones_selected);
00276 }
00277 
00278 /*bool hasConfiguration() const
00279 {
00280   return true;
00281 }
00282 
00283 void triggerConfiguration()
00284 {
00285   // Usually used to open a dialog to offer the user a set of configuration
00286 }*/
00287 
00288 }  // namespace
00289 PLUGINLIB_DECLARE_CLASS(ucl_drone_gui, MyPlugin, ucl_drone_gui::MyPlugin, rqt_gui_cpp::Plugin)


ucl_drone_gui
Author(s):
autogenerated on Sat Jun 8 2019 20:52:06