RobotVisuWidget.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @author Rainer Kartmann ( rainer dot kartmann at kit dot edu )
17  * @date 2021
18  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
19  * GNU General Public License
20  */
21 
22 #include "RobotVisuWidget.h"
23 
24 #include <map>
25 #include <memory>
26 #include <string>
27 
28 #include <QCheckBox>
29 #include <QDialog>
30 #include <QDialogButtonBox>
31 #include <QLabel>
32 #include <QPushButton>
33 #include <QVBoxLayout>
34 #include <QtWidgets/QLabel>
35 #include <qboxlayout.h>
36 #include <qdialog.h>
37 #include <qobject.h>
38 #include <qobjectdefs.h>
39 #include <qwidget.h>
40 
41 #include <VirtualRobot/Robot.h>
42 
45 
47 #include <RobotAPI/interface/core/RobotState.h>
49 
51 
52 #include "ConnectDialog.h"
53 
55 {
56 
57  robotvisu::SettingsWidget::SettingsWidget(QWidget* parent) : QWidget(parent)
58  {
59  _enabled = new QCheckBox("Enable Visu", this);
60  _enabled->setChecked(true);
61 
62  _collisionModel = new QCheckBox("Use Collision Model", this);
63  _collisionModel->setChecked(false);
64 
65 
66  setLayout(new QVBoxLayout());
67  layout()->addWidget(_enabled);
68  layout()->addWidget(_collisionModel);
69 
70 
71  connect(_enabled, &QCheckBox::toggled, this, &This::changed);
72  connect(_collisionModel, &QCheckBox::toggled, this, &This::changed);
73 
74  const int margin = 0;
75  setContentsMargins(margin, margin, margin, margin);
76  }
77 
78  bool
80  {
81  return _enabled->isChecked();
82  }
83 
84  bool
86  {
87  return _collisionModel->isChecked();
88  }
89 
91  robotvisu::SettingsWidget* settings) :
92  _robotStateComponent(
93  std::make_unique<RobotStateComponentInterfacePrx>(robotStateComponent)),
94  _settings(settings)
95  {
96  ARMARX_CHECK_NOT_NULL(robotStateComponent);
97  _filename = robotStateComponent->getRobotFilename();
99  robotStateComponent, "", {}, VirtualRobot::RobotIO::RobotDescription::eStructure);
100  ARMARX_CHECK_NOT_NULL(_robot);
101  }
102 
104  {
105  }
106 
107  core::Pose
109  {
110  _synchronize(synchronize);
111  return core::Pose{_robot->getGlobalPose()};
112  }
113 
114  std::map<std::string, float>
116  {
117  _synchronize(synchronize);
118  return _robot->getConfig()->getRobotNodeJointValueMap();
119  }
120 
121  viz::Robot
122  robotvisu::Connection::vizRobot(const std::string& id, bool synchronize)
123  {
124  _synchronize(synchronize);
125  viz::Robot robot = viz::Robot(id).file("", _filename).pose(getGlobalPose(false));
126  // .joints(getJointValues(false)); not working for RobotStateComponent ...
127 
128  if (_settings and _settings->useCollisionModel())
129  {
130  robot.useCollisionModel();
131  }
132  return robot;
133  }
134 
137  {
138  ARMARX_CHECK_NOT_NULL(_robotStateComponent);
139  return *_robotStateComponent;
140  }
141 
142  std::string
144  {
145  return getRobotStateComponent()->ice_getIdentity().name;
146  }
147 
148  void
149  robotvisu::Connection::_synchronize(bool synchronize)
150  {
151  if (synchronize)
152  {
153  RemoteRobot::synchronizeLocalClone(_robot, getRobotStateComponent());
154  }
155  }
156 
157  RobotVisuWidget::RobotVisuWidget(ManagedIceObject& component, QWidget* parent) :
158  QWidget(parent), _component(component)
159  {
160  _statusLabel = new QLabel("Not connected", this);
161  _connectButton = new QPushButton("Connect ...", this);
162 
163  _settings = new robotvisu::SettingsWidget();
164  _settings->setEnabled(false);
165 
166 
167  QBoxLayout* layout = new QVBoxLayout(this);
168  setLayout(layout);
169 
170  layout->addWidget(_statusLabel);
171  layout->addWidget(_connectButton);
172  layout->addWidget(_settings);
173 
174 
175  connect(_connectButton, &QPushButton::pressed, this, &This::connectToRobot);
176 
177  connect(_settings, &robotvisu::SettingsWidget::changed, this, &This::settingsChanged);
178 
179  connect(this,
181  this,
182  [this]()
183  {
184  QString name = QString::fromStdString(connection().getConnectedName());
185  _statusLabel->setText("Connected to '" + name + "'");
186  });
187  connect(this, &This::connected, this, [this]() { _settings->setEnabled(true); });
188  }
189 
190  bool
192  {
193  return isConnected() and _settings->isEnabled();
194  }
195 
196  bool
198  {
199  return _connection.has_value();
200  }
201 
204  {
205  return _connection.value();
206  }
207 
208  void
210  {
212  "*RobotStateComponent", _component, this);
213  switch (dialog.exec())
214  {
215  case QDialog::Accepted:
216  break;
217  case QDialog::Rejected:
218  return;
219  }
220 
221  auto robotStateComponent = dialog.getProxy();
222  if (robotStateComponent)
223  {
224  _connection.emplace(robotStateComponent, _settings);
225  emit connected();
226  }
227  }
228 
229 } // namespace armarx::navigation::qt_plugins::location_graph_editor
armarx::navigation::qt_plugins::location_graph_editor::robotvisu::Connection::vizRobot
viz::Robot vizRobot(const std::string &id, bool synchronize=true)
Definition: RobotVisuWidget.cpp:122
RemoteRobot.h
armarx::navigation::qt_plugins::location_graph_editor::RobotVisuWidget::settingsChanged
void settingsChanged()
Robot.h
armarx::navigation::qt_plugins::location_graph_editor::RobotVisuWidget::connectToRobot
void connectToRobot()
Definition: RobotVisuWidget.cpp:209
armarx::navigation::qt_plugins::location_graph_editor::RobotVisuWidget::isEnabled
bool isEnabled() const
Indicates whether the connection is established and visu is enabled.
Definition: RobotVisuWidget.cpp:191
armarx::navigation::qt_plugins::location_graph_editor::RobotVisuWidget::connection
robotvisu::Connection & connection()
Definition: RobotVisuWidget.cpp:203
armarx::navigation::core::Pose
Eigen::Isometry3f Pose
Definition: basic_types.h:31
basic_types.h
armarx::RemoteRobot::synchronizeLocalClone
static bool synchronizeLocalClone(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx)
Definition: RemoteRobot.cpp:522
armarx::ProxyType::component
@ component
armarx::navigation::qt_plugins::location_graph_editor::robotvisu::Connection
Definition: RobotVisuWidget.h:98
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::navigation::qt_plugins::location_graph_editor::robotvisu::Connection::Connection
Connection(RobotStateComponentInterfacePrx robotStateComponent, robotvisu::SettingsWidget *settings)
Definition: RobotVisuWidget.cpp:90
armarx::navigation::qt_plugins::location_graph_editor::robotvisu::SettingsWidget::SettingsWidget
SettingsWidget(QWidget *parent=nullptr)
Definition: RobotVisuWidget.cpp:57
ConnectDialog.h
armarx::navigation::qt_plugins::location_graph_editor::robotvisu::SettingsWidget::useCollisionModel
bool useCollisionModel() const
Definition: RobotVisuWidget.cpp:85
armarx::navigation::qt_plugins::location_graph_editor::robotvisu::Connection::getJointValues
std::map< std::string, float > getJointValues(bool synchronize=true)
Definition: RobotVisuWidget.cpp:115
armarx::navigation::qt_plugins::location_graph_editor::RobotVisuWidget::isConnected
bool isConnected() const
Definition: RobotVisuWidget.cpp:197
armarx::navigation::qt_plugins::location_graph_editor::robotvisu::SettingsWidget::changed
void changed()
armarx::navigation::qt_plugins::location_graph_editor::robotvisu::SettingsWidget
Definition: RobotVisuWidget.h:75
RobotVisuWidget.h
ManagedIceObject.h
armarx::armem::human::Robot
@ Robot
Definition: util.h:17
armarx::navigation::qt_plugins::location_graph_editor::RobotVisuWidget::RobotVisuWidget
RobotVisuWidget(ManagedIceObject &component, QWidget *parent=nullptr)
Definition: RobotVisuWidget.cpp:157
armarx::viz::Robot::useCollisionModel
Robot & useCollisionModel()
Definition: Robot.h:34
armarx::navigation::qt_plugins::location_graph_editor::robotvisu::Connection::~Connection
~Connection()
Definition: RobotVisuWidget.cpp:103
armarx::navigation::qt_plugins::location_graph_editor::ConnectDialog
Definition: ConnectDialog.h:40
armarx::navigation::qt_plugins::location_graph_editor::robotvisu::Connection::getConnectedName
std::string getConnectedName() const
Definition: RobotVisuWidget.cpp:143
armarx::viz::Robot
Definition: Robot.h:10
armarx::navigation::qt_plugins::location_graph_editor
Definition: GuiGraph.cpp:34
ExpressionException.h
armarx::navigation::qt_plugins::location_graph_editor::ConnectDialog::getProxy
ProxyT getProxy()
Definition: ConnectDialog.h:66
armarx::navigation::qt_plugins::location_graph_editor::robotvisu::Connection::getGlobalPose
core::Pose getGlobalPose(bool synchronize=true)
Definition: RobotVisuWidget.cpp:108
armarx::ManagedIceObject
The ManagedIceObject is the base class for all ArmarX objects.
Definition: ManagedIceObject.h:162
armarx::RemoteRobot::createLocalClone
VirtualRobot::RobotPtr createLocalClone()
Clones the structure of this remote robot to a local instance.
Definition: RemoteRobot.cpp:381
std
Definition: Application.h:66
armarx::navigation::qt_plugins::location_graph_editor::robotvisu::Connection::getRobotStateComponent
RobotStateComponentInterfacePrx getRobotStateComponent() const
Definition: RobotVisuWidget.cpp:136
armarx::navigation::qt_plugins::location_graph_editor::robotvisu::SettingsWidget::isEnabled
bool isEnabled() const
Definition: RobotVisuWidget.cpp:79
IceInternal::ProxyHandle<::IceProxy::armarx::RobotStateComponentInterface >
armarx::navigation::qt_plugins::location_graph_editor::RobotVisuWidget::connected
void connected()