motion_planning_frame_states.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2013, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 /* Author: Mario Prats, Ioan Sucan */
00031 
00032 #include <moveit/warehouse/state_storage.h>
00033 #include <moveit/motion_planning_rviz_plugin/motion_planning_frame.h>
00034 #include <moveit/motion_planning_rviz_plugin/motion_planning_display.h>
00035 #include <moveit/robot_state/conversions.h>
00036 
00037 #include <QMessageBox>
00038 #include <QInputDialog>
00039 
00040 #include "ui_motion_planning_rviz_plugin_frame.h"
00041 
00042 namespace moveit_rviz_plugin
00043 {
00044 
00045 void MotionPlanningFrame::populateRobotStatesList(void)
00046 {
00047   ui_->list_states->clear();
00048   for (RobotStateMap::iterator it = robot_states_.begin(); it != robot_states_.end(); ++it)
00049   {
00050     QListWidgetItem *item = new QListWidgetItem(QString(it->first.c_str()));
00051     ui_->list_states->addItem(item);
00052   }
00053 }
00054 
00055 void MotionPlanningFrame::loadStateButtonClicked()
00056 {
00057   if (robot_state_storage_)
00058   {
00059     bool ok;
00060     QString text = QInputDialog::getText(this, tr("Robot states to load"), tr("Pattern:"), QLineEdit::Normal, ".*", &ok);
00061     if (ok && ! text.isEmpty())
00062     {
00063       //First clear the current list
00064       clearStatesButtonClicked();
00065 
00066       std::vector<std::string> names;
00067       try
00068       {
00069         robot_state_storage_->getKnownRobotStates(text.toStdString(), names);
00070       }
00071       catch (std::runtime_error &ex)
00072       {
00073         QMessageBox::warning(this, "Cannot query the database", QString("Wrongly formatted regular expression for robot states: ").append(ex.what()));
00074         return;
00075       }
00076 
00077       for ( std::size_t i = 0 ; i < names.size() ; ++i )
00078       {
00079         moveit_warehouse::RobotStateWithMetadata rs;
00080         bool got_state = false;
00081         try
00082         {
00083           got_state = robot_state_storage_->getRobotState(rs, names[i]);
00084         }
00085         catch(std::runtime_error &ex)
00086         {
00087           ROS_ERROR("%s", ex.what());
00088         }
00089         if (!got_state)
00090           continue;
00091 
00092         //Overwrite if exists.
00093         if (robot_states_.find(names[i]) != robot_states_.end())
00094         {
00095           robot_states_.erase(names[i]);
00096         }
00097 
00098         //Store the current start state
00099         robot_states_.insert(RobotStatePair(names[i], *rs));
00100       }
00101       populateRobotStatesList();
00102     }
00103   }
00104   else
00105   {
00106     QMessageBox::warning(this, "Warning", "Not connected to a database.");
00107   }
00108 }
00109 
00110 void MotionPlanningFrame::saveRobotStateButtonClicked(const robot_state::RobotState &state)
00111 {
00112   bool ok = false;
00113 
00114   std::stringstream ss;
00115   ss << planning_display_->getRobotModel()->getName().c_str() << "_state_" << std::setfill('0') << std::setw(4) << robot_states_.size();
00116 
00117   QString text = QInputDialog::getText(this, tr("Choose a name"), tr("State name:"), QLineEdit::Normal, QString(ss.str().c_str()), &ok);
00118 
00119   std::string name;
00120   if (ok)
00121   {
00122     if (!text.isEmpty())
00123     {
00124       name = text.toStdString();
00125       if (robot_states_.find(name) != robot_states_.end())
00126         QMessageBox::warning(this, "Name already exists", QString("The name '").append(name.c_str()).
00127                              append("' already exists. Not creating state."));
00128       else
00129       {
00130         //Store the current start state
00131         moveit_msgs::RobotState msg;
00132         robot_state::robotStateToRobotStateMsg(state, msg);
00133         robot_states_.insert(RobotStatePair(name, msg));
00134 
00135         //Save to the database if connected
00136         if (robot_state_storage_)
00137         {
00138           try
00139           {
00140             robot_state_storage_->addRobotState(msg, name);
00141           }
00142           catch (std::runtime_error &ex)
00143           {
00144             ROS_ERROR("Cannot save robot state on the database: %s", ex.what());
00145           }
00146         }
00147         else
00148         {
00149           QMessageBox::warning(this, "Warning", "Not connected to a database. The state will be created but not stored");
00150         }
00151       }
00152     }
00153     else
00154       QMessageBox::warning(this, "Start state not saved", "Cannot use an empty name for a new start state.");
00155   }
00156   populateRobotStatesList();
00157 }
00158 
00159 void MotionPlanningFrame::saveStartStateButtonClicked()
00160 {
00161   saveRobotStateButtonClicked(*planning_display_->getQueryStartState());
00162 }
00163 
00164 void MotionPlanningFrame::saveGoalStateButtonClicked()
00165 {
00166   saveRobotStateButtonClicked(*planning_display_->getQueryGoalState());
00167 }
00168 
00169 void MotionPlanningFrame::setAsStartStateButtonClicked()
00170 {
00171   QListWidgetItem *item = ui_->list_states->currentItem();
00172 
00173   if (item)
00174   {
00175     robot_state::RobotState robot_state(*planning_display_->getQueryStartState());
00176     robot_state::robotStateMsgToRobotState(robot_states_[item->text().toStdString()], robot_state);
00177     planning_display_->setQueryStartState(robot_state);
00178   }
00179 }
00180 
00181 void MotionPlanningFrame::setAsGoalStateButtonClicked()
00182 {
00183   QListWidgetItem *item = ui_->list_states->currentItem();
00184 
00185   if (item)
00186   {
00187     robot_state::RobotState robot_state(*planning_display_->getQueryGoalState());
00188     robot_state::robotStateMsgToRobotState(robot_states_[item->text().toStdString()], robot_state);
00189     planning_display_->setQueryGoalState(robot_state);
00190   }
00191 }
00192 
00193 void MotionPlanningFrame::removeStateButtonClicked()
00194 {
00195   if (robot_state_storage_)
00196   {
00197     //Warn the user
00198     QMessageBox msgBox;
00199     msgBox.setText("All the selected states will be removed from the database");
00200     msgBox.setInformativeText("Do you want to continue?");
00201     msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
00202     msgBox.setDefaultButton(QMessageBox::No);
00203     int ret = msgBox.exec();
00204 
00205     switch (ret)
00206     {
00207       case QMessageBox::Yes:
00208       {
00209         QList<QListWidgetItem*> found_items =  ui_->list_states->selectedItems();
00210         for (std::size_t i = 0; i < found_items.size() ; ++i)
00211         {
00212           const std::string &name = found_items[i]->text().toStdString();
00213           try
00214           {
00215             robot_state_storage_->removeRobotState(name);
00216             robot_states_.erase(name);
00217           }
00218           catch (std::runtime_error &ex)
00219           {
00220             ROS_ERROR("%s", ex.what());
00221           }
00222         }
00223         break;
00224       }
00225     }
00226   }
00227   populateRobotStatesList();
00228 }
00229 
00230 void MotionPlanningFrame::clearStatesButtonClicked()
00231 {
00232   robot_states_.clear();
00233   populateRobotStatesList();
00234 }
00235 
00236 } //namespace


visualization
Author(s): Ioan Sucan , Dave Coleman
autogenerated on Mon Oct 6 2014 02:34:03