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 <QCheckBox>
25 #include <QDialog>
26 #include <QDialogButtonBox>
27 #include <QLabel>
28 #include <QPushButton>
29 #include <QVBoxLayout>
30 
31 #include <VirtualRobot/Robot.h>
32 
35 
37 #include <RobotAPI/interface/core/RobotState.h>
39 
40 #include "ConnectDialog.h"
41 #include <QtWidgets/QLabel>
42 
43 
45 {
46 
47  robotvisu::SettingsWidget::SettingsWidget(QWidget* parent) : QWidget(parent)
48  {
49  _enabled = new QCheckBox("Enable Visu", this);
50  _enabled->setChecked(true);
51 
52  _collisionModel = new QCheckBox("Use Collision Model", this);
53  _collisionModel->setChecked(false);
54 
55 
56  setLayout(new QVBoxLayout());
57  layout()->addWidget(_enabled);
58  layout()->addWidget(_collisionModel);
59 
60 
61  connect(_enabled, &QCheckBox::toggled, this, &This::changed);
62  connect(_collisionModel, &QCheckBox::toggled, this, &This::changed);
63 
64  const int margin = 0;
65  setContentsMargins(margin, margin, margin, margin);
66  }
67 
68 
69  bool
71  {
72  return _enabled->isChecked();
73  }
74 
75 
76  bool
78  {
79  return _collisionModel->isChecked();
80  }
81 
82 
84  robotvisu::SettingsWidget* settings) :
85  _robotStateComponent(
86  std::make_unique<RobotStateComponentInterfacePrx>(robotStateComponent)),
87  _settings(settings)
88  {
89  ARMARX_CHECK_NOT_NULL(robotStateComponent);
90  _filename = robotStateComponent->getRobotFilename();
92  robotStateComponent, "", {}, VirtualRobot::RobotIO::RobotDescription::eStructure);
93  ARMARX_CHECK_NOT_NULL(_robot);
94  }
95 
96 
98  {
99  }
100 
101 
102  core::Pose
104  {
105  _synchronize(synchronize);
106  return core::Pose{_robot->getGlobalPose()};
107  }
108 
109 
110  std::map<std::string, float>
112  {
113  _synchronize(synchronize);
114  return _robot->getConfig()->getRobotNodeJointValueMap();
115  }
116 
117 
118  viz::Robot
119  robotvisu::Connection::vizRobot(const std::string& id, bool synchronize)
120  {
121  _synchronize(synchronize);
122  viz::Robot robot = viz::Robot(id)
123  .file("", _filename)
124  .pose(getGlobalPose(false))
125  .joints(getJointValues(false));
126 
127  if (_settings and _settings->useCollisionModel())
128  {
129  robot.useCollisionModel();
130  }
131  return robot;
132  }
133 
134 
137  {
138  ARMARX_CHECK_NOT_NULL(_robotStateComponent);
139  return *_robotStateComponent;
140  }
141 
142 
143  std::string
145  {
146  return getRobotStateComponent()->ice_getIdentity().name;
147  }
148 
149 
150  void
151  robotvisu::Connection::_synchronize(bool synchronize)
152  {
153  if (synchronize)
154  {
155  RemoteRobot::synchronizeLocalClone(_robot, getRobotStateComponent());
156  }
157  }
158 
159 
160  RobotVisuWidget::RobotVisuWidget(ManagedIceObject& component, QWidget* parent) :
161  QWidget(parent), _component(component)
162  {
163  _statusLabel = new QLabel("Not connected", this);
164  _connectButton = new QPushButton("Connect ...", this);
165 
166  _settings = new robotvisu::SettingsWidget();
167  _settings->setEnabled(false);
168 
169 
170  QBoxLayout* layout = new QVBoxLayout(this);
171  setLayout(layout);
172 
173  layout->addWidget(_statusLabel);
174  layout->addWidget(_connectButton);
175  layout->addWidget(_settings);
176 
177 
178  connect(_connectButton, &QPushButton::pressed, this, &This::connectToRobot);
179 
180  connect(_settings, &robotvisu::SettingsWidget::changed, this, &This::settingsChanged);
181 
182  connect(this,
184  this,
185  [this]()
186  {
187  QString name = QString::fromStdString(connection().getConnectedName());
188  _statusLabel->setText("Connected to '" + name + "'");
189  });
190  connect(this, &This::connected, this, [this]() { _settings->setEnabled(true); });
191  }
192 
193 
194  bool
196  {
197  return isConnected() and _settings->isEnabled();
198  }
199 
200 
201  bool
203  {
204  return _connection.has_value();
205  }
206 
207 
210  {
211  return _connection.value();
212  }
213 
214 
215  void
217  {
219  "*RobotStateComponent", _component, this);
220  switch (dialog.exec())
221  {
222  case QDialog::Accepted:
223  break;
224  case QDialog::Rejected:
225  return;
226  }
227 
228  auto robotStateComponent = dialog.getProxy();
229  if (robotStateComponent)
230  {
231  _connection.emplace(robotStateComponent, _settings);
232  emit connected();
233  }
234  }
235 
236 } // 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:119
RemoteRobot.h
armarx::navigation::qt_plugins::location_graph_editor::RobotVisuWidget::settingsChanged
void settingsChanged()
armarx::navigation::qt_plugins::location_graph_editor::RobotVisuWidget::connectToRobot
void connectToRobot()
Definition: RobotVisuWidget.cpp:216
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:195
armarx::navigation::qt_plugins::location_graph_editor::RobotVisuWidget::connection
robotvisu::Connection & connection()
Definition: RobotVisuWidget.cpp:209
armarx::navigation::core::Pose
Eigen::Isometry3f Pose
Definition: basic_types.h:31
armarx::RemoteRobot::synchronizeLocalClone
static bool synchronizeLocalClone(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx)
Definition: RemoteRobot.cpp:448
armarx::ProxyType::component
@ component
armarx::navigation::qt_plugins::location_graph_editor::robotvisu::Connection
Definition: RobotVisuWidget.h:92
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:83
armarx::navigation::qt_plugins::location_graph_editor::robotvisu::SettingsWidget::SettingsWidget
SettingsWidget(QWidget *parent=nullptr)
Definition: RobotVisuWidget.cpp:47
ConnectDialog.h
armarx::navigation::qt_plugins::location_graph_editor::robotvisu::SettingsWidget::useCollisionModel
bool useCollisionModel() const
Definition: RobotVisuWidget.cpp:77
armarx::navigation::qt_plugins::location_graph_editor::robotvisu::Connection::getJointValues
std::map< std::string, float > getJointValues(bool synchronize=true)
Definition: RobotVisuWidget.cpp:111
armarx::navigation::qt_plugins::location_graph_editor::RobotVisuWidget::isConnected
bool isConnected() const
Definition: RobotVisuWidget.cpp:202
Elements.h
armarx::navigation::qt_plugins::location_graph_editor::robotvisu::SettingsWidget::changed
void changed()
armarx::navigation::qt_plugins::location_graph_editor::robotvisu::SettingsWidget
Definition: RobotVisuWidget.h:68
RobotVisuWidget.h
ManagedIceObject.h
armarx::armem::human::Robot
@ Robot
Definition: util.h:14
armarx::navigation::qt_plugins::location_graph_editor::RobotVisuWidget::RobotVisuWidget
RobotVisuWidget(ManagedIceObject &component, QWidget *parent=nullptr)
Definition: RobotVisuWidget.cpp:160
armarx::viz::Robot::useCollisionModel
Robot & useCollisionModel()
Definition: Robot.h:22
armarx::navigation::qt_plugins::location_graph_editor::robotvisu::Connection::~Connection
~Connection()
Definition: RobotVisuWidget.cpp:97
armarx::navigation::qt_plugins::location_graph_editor::ConnectDialog
Definition: ConnectDialog.h:36
armarx::navigation::qt_plugins::location_graph_editor::robotvisu::Connection::getConnectedName
std::string getConnectedName() const
Definition: RobotVisuWidget.cpp:144
armarx::viz::Robot
Definition: Robot.h:9
armarx::navigation::qt_plugins::location_graph_editor
Definition: GuiGraph.cpp:29
ExpressionException.h
armarx::navigation::qt_plugins::location_graph_editor::ConnectDialog::getProxy
ProxyT getProxy()
Definition: ConnectDialog.h:63
armarx::navigation::qt_plugins::location_graph_editor::robotvisu::Connection::getGlobalPose
core::Pose getGlobalPose(bool synchronize=true)
Definition: RobotVisuWidget.cpp:103
armarx::ManagedIceObject
The ManagedIceObject is the base class for all ArmarX objects.
Definition: ManagedIceObject.h:163
armarx::RemoteRobot::createLocalClone
VirtualRobot::RobotPtr createLocalClone()
Clones the structure of this remote robot to a local instance.
Definition: RemoteRobot.cpp:328
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:70
IceInternal::ProxyHandle<::IceProxy::armarx::RobotStateComponentInterface >
armarx::navigation::qt_plugins::location_graph_editor::RobotVisuWidget::connected
void connected()