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
106
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
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
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
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
The ManagedIceObject is the base class for all ArmarX objects.
VirtualRobot::RobotPtr createLocalClone()
Clones the structure of this remote robot to a local instance.
static bool synchronizeLocalClone(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx)
RobotVisuWidget(ManagedIceObject &component, QWidget *parent=nullptr)
bool isEnabled() const
Indicates whether the connection is established and visu is enabled.
Connection(RobotStateComponentInterfacePrx robotStateComponent, robotvisu::SettingsWidget *settings)
std::map< std::string, float > getJointValues(bool synchronize=true)
viz::Robot vizRobot(const std::string &id, bool synchronize=true)
DerivedT & pose(Eigen::Matrix4f const &pose)
Definition ElementOps.h:176
Robot & useCollisionModel()
Definition Robot.h:34
Robot & file(std::string const &project, std::string const &filename)
Definition Robot.h:16
#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...
Eigen::Isometry3f Pose
Definition basic_types.h:31
::IceInternal::ProxyHandle<::IceProxy::armarx::RobotStateComponentInterface > RobotStateComponentInterfacePrx