PlatformUnitGuiPlugin.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #include "PlatformUnitGuiPlugin.h"
26 #include <RobotAPI/gui-plugins/PlatformUnitPlugin/ui_PlatformUnitConfigDialog.h>
28 
29 #include <Eigen/Geometry>
30 
31 // Qt headers
32 #include <Qt>
33 #include <QtGlobal>
34 #include <QPushButton>
35 #include <QLabel>
36 #include <QLineEdit>
37 #include <QHBoxLayout>
38 
39 //std
40 #include <RobotAPI/interface/core/RobotLocalization.h>
41 #include <SimoxUtility/math/convert/mat3f_to_rpy.h>
42 #include <memory>
43 #include <cmath>
44 
45 using namespace armarx;
46 
47 
48 
50 {
51  addWidget<PlatformUnitWidget>();
52 }
53 
54 
56  platformUnitProxyName("PlatformUnit"), // overwritten in loadSettings() anyway?
57  platformName("Platform"),
58  speedCtrl {nullptr},
59  rotaCtrl {nullptr},
60  ctrlEvaluationTimer {},
61  platformRotation {0},
62  platformMoves {false}
63 {
64  // init gui
65  ui.setupUi(getWidget());
66  //init joystick controls
67  std::unique_ptr<JoystickControlWidget> speed{new JoystickControlWidget{}};
68  speedCtrl = speed.get();
69  speedCtrl->setSteps(0);
70  //use upper semicircle for rotation
71  std::unique_ptr<JoystickControlWidget> rotat{new JoystickControlWidget{false}};
72  rotaCtrl = rotat.get();
73  rotaCtrl->setSteps(0);
74  //add joystick controls
75  ui.gridLayout_2->addWidget(rotat.release(), 2, 0, 1, 2);
76  ui.gridLayout_3->addWidget(speed.release(), 2, 0, 1, 2);
77 
78  ctrlEvaluationTimer.setSingleShot(false);
79  keyboardVelocityTimer.setInterval(50);
80  stopPlatformTimer.setInterval(100);
81  connect(&stopPlatformTimer, SIGNAL(timeout()), this, SLOT(stopControlTimer()));
82  connect(&stopPlatformTimer, SIGNAL(timeout()), this, SLOT(stopPlatform()));
83 
84 
85  connect(getWidget().data(), SIGNAL(commandKeyPressed(int)), this, SLOT(controlPlatformWithKeyboard(int)));
86  connect(getWidget().data(), SIGNAL(commandKeyReleased(int)), this, SLOT(stopPlatformWithKeyboard(int)));
87 }
88 
89 
91 {
92  usingProxy(platformUnitProxyName);
93  usingTopic("GlobalRobotPoseLocalization");
94  // ARMARX_INFO << "Listening on Topic: " << platformName + "State";
95 
96 }
97 
98 
100 {
101  platformUnitProxy = getProxy<PlatformUnitInterfacePrx>(platformUnitProxyName);
102  connectSlots();
103 }
104 
105 
107 {
109 }
110 
111 
113 {
114 }
115 
116 
117 QPointer<QDialog> PlatformUnitWidget::getConfigDialog(QWidget* parent)
118 {
119  if (!dialog)
120  {
121  dialog = new PlatformUnitConfigDialog(parent);
122  }
123 
124  dialog->ui->editPlatformName->setText(QString::fromStdString(platformName));
125  return qobject_cast<PlatformUnitConfigDialog*>(dialog);
126 }
127 
128 
130 {
131  platformUnitProxyName = dialog->finder->getSelectedProxyName().toStdString();
132  platformName = dialog->ui->editPlatformName->text().toStdString();
133 }
134 
135 
136 void PlatformUnitWidget::loadSettings(QSettings* settings)
137 {
138  platformUnitProxyName = settings->value("platformUnitProxyName", QString::fromStdString(platformUnitProxyName)).toString().toStdString();
139  platformName = settings->value("platformName", QString::fromStdString(platformName)).toString().toStdString();
140 }
141 
142 
143 void PlatformUnitWidget::saveSettings(QSettings* settings)
144 {
145  settings->setValue("platformUnitProxyName", QString::fromStdString(platformUnitProxyName));
146  settings->setValue("platformName", QString::fromStdString(platformName));
147 }
148 
149 
151 {
152  connect(ui.buttonMoveToPosition, SIGNAL(clicked()), this, SLOT(moveTo()), Qt::UniqueConnection);
153  connect(&ctrlEvaluationTimer, SIGNAL(timeout()), this, SLOT(controlTimerTick()), Qt::UniqueConnection);
154  connect(&keyboardVelocityTimer, SIGNAL(timeout()), this, SLOT(keyboardVelocityControl()), Qt::UniqueConnection);
155  connect(speedCtrl, SIGNAL(pressed()), this, SLOT(startControlTimer()), Qt::UniqueConnection);
156  connect(speedCtrl, SIGNAL(pressed()), &keyboardVelocityTimer, SLOT(stop()), Qt::UniqueConnection);
157  connect(rotaCtrl, SIGNAL(pressed()), this, SLOT(startControlTimer()), Qt::UniqueConnection);
158  connect(rotaCtrl, SIGNAL(pressed()), &keyboardVelocityTimer, SLOT(stop()), Qt::UniqueConnection);
159  connect(speedCtrl, SIGNAL(released()), this, SLOT(stopPlatform()), Qt::UniqueConnection);
160  connect(speedCtrl, SIGNAL(released()), this, SLOT(stopControlTimer()), Qt::UniqueConnection);
161  connect(rotaCtrl, SIGNAL(released()), this, SLOT(stopPlatform()), Qt::UniqueConnection);
162  connect(rotaCtrl, SIGNAL(released()), this, SLOT(stopControlTimer()), Qt::UniqueConnection);
163  connect(ui.buttonStopPlatform, SIGNAL(pressed()), this, SLOT(stopPlatform()), Qt::UniqueConnection);
164 }
165 
166 
168 {
169  ARMARX_LOG << "Moving Platform";
170  ::Ice::Float positionX = ui.editTargetPositionX->text().toFloat();
171  ::Ice::Float positionY = ui.editTargetPositionY->text().toFloat();
172  ::Ice::Float rotation = ui.editTargetRotation->text().toFloat();
173  ::Ice::Float posAcc = 10.0f;
174  ::Ice::Float rotAcc = 0.1f;
175  platformUnitProxy->moveTo(positionX, positionY, rotation, posAcc, rotAcc);
176 }
177 
178 
179 void PlatformUnitWidget::setNewPlatformPoseLabels(float x, float y, float alpha)
180 {
181  ui.labelCurrentPositionX->setText(QString::number(x));
182  ui.labelCurrentPositionY->setText(QString::number(y));
183  ui.labelCurrentRotation->setText(QString::number(alpha));
184 }
185 
186 
187 void PlatformUnitWidget::setNewTargetPoseLabels(float x, float y, float alpha)
188 {
189  ui.editTargetPositionX->setText(QString::number(x));
190  ui.editTargetPositionY->setText(QString::number(y));
191  ui.editTargetRotation->setText(QString::number(alpha));
192 }
193 
194 
196 {
197  ctrlEvaluationTimer.start(CONTROL_TICK_RATE); //tickrate in ms
198 }
199 
200 
202 {
203  ctrlEvaluationTimer.stop();
204  speedCtrl->setNibble({0, 0});
205  rotaCtrl->setNibble({0, 0});
206 }
207 
208 
209 void PlatformUnitWidget::reportGlobalRobotPose(const ::armarx::TransformStamped& transformStamped, const ::Ice::Current&)
210 {
211  const Eigen::Isometry3f global_T_robot(transformStamped.transform);
212  const float x = global_T_robot.translation().x();
213  const float y = global_T_robot.translation().y();
214  const float yaw = simox::math::mat3f_to_rpy(global_T_robot.linear()).z();
215 
216  // moved to qt thread for thread safety
217  QMetaObject::invokeMethod(this, "setNewPlatformPoseLabels", Q_ARG(float, x), Q_ARG(float, y), Q_ARG(float, yaw));
218  platformRotation = yaw;
219 }
220 
221 
222 void PlatformUnitWidget::stopPlatform()
223 {
224  platformUnitProxy->stopPlatform();
225 }
226 
227 
228 void PlatformUnitWidget::controlPlatformWithKeyboard(int key)
229 {
230  pressedKeys.insert(key);
231  if (!ctrlEvaluationTimer.isActive())
232  {
233  ctrlEvaluationTimer.start();
234  }
235  if (!keyboardVelocityTimer.isActive())
236  {
237  keyboardVelocityControl();
238  keyboardVelocityTimer.start();
239  }
240 }
241 
242 
243 void PlatformUnitWidget::stopPlatformWithKeyboard(int key)
244 {
245  pressedKeys.remove(key);
246 
247  if (!keyboardVelocityTimer.isActive())
248  {
249  keyboardVelocityControl();
250  keyboardVelocityTimer.start();
251  }
252 }
253 
254 
255 void PlatformUnitWidget::keyboardVelocityControl()
256 {
257  if (!pressedKeys.contains(Qt::Key_A) && !pressedKeys.contains(Qt::Key_D))
258  {
259  currentKeyboardVelocityX *= deceleration;
260  if (fabs(currentKeyboardVelocityX) < 0.001)
261  {
262  currentKeyboardVelocityX = 0;
263  }
264  }
265  if (!pressedKeys.contains(Qt::Key_W) && !pressedKeys.contains(Qt::Key_S))
266  {
267  currentKeyboardVelocityY *= deceleration;
268  if (fabs(currentKeyboardVelocityY) < 0.001)
269  {
270  currentKeyboardVelocityY = 0;
271  }
272  }
273  if (!pressedKeys.contains(Qt::Key_Q) && !pressedKeys.contains(Qt::Key_E))
274  {
275  currentKeyboardVelocityAlpha *= deceleration;
276  if (fabs(currentKeyboardVelocityAlpha) < 0.001)
277  {
278  currentKeyboardVelocityAlpha = 0;
279  }
280  }
281 
282  for (auto key : pressedKeys)
283  {
284  switch (key)
285  {
286  case Qt::Key_Q:
287  currentKeyboardVelocityAlpha -= acceleration;
288  break;
289  case Qt::Key_E:
290  currentKeyboardVelocityAlpha += acceleration;
291  break;
292  case Qt::Key_W:
293  currentKeyboardVelocityY -= acceleration;
294  break;
295  case Qt::Key_S:
296  currentKeyboardVelocityY += acceleration;
297  break;
298  case Qt::Key_A:
299  currentKeyboardVelocityX -= acceleration;
300  break;
301  case Qt::Key_D:
302  currentKeyboardVelocityX += acceleration;
303  break;
304  default:
305  break;
306  }
307  }
308 
309  currentKeyboardVelocityAlpha = std::max(-1.f, currentKeyboardVelocityAlpha);
310  currentKeyboardVelocityAlpha = std::min(1.f, currentKeyboardVelocityAlpha);
311  currentKeyboardVelocityX = std::max(-1.f, currentKeyboardVelocityX);
312  currentKeyboardVelocityX = std::min(1.f, currentKeyboardVelocityX);
313  currentKeyboardVelocityY = std::max(-1.f, currentKeyboardVelocityY);
314  currentKeyboardVelocityY = std::min(1.f, currentKeyboardVelocityY);
315 
316  float y = sin(acos(currentKeyboardVelocityAlpha));
317  speedCtrl->setNibble(QPointF(currentKeyboardVelocityX, currentKeyboardVelocityY));
318  rotaCtrl->setNibble(QPointF(currentKeyboardVelocityAlpha, -y));
319 }
320 
321 
322 QPointer<QWidget> PlatformUnitWidget::getWidget()
323 {
324  if (!__widget)
325  {
326  __widget = new KeyboardPlatformHookWidget();
327  }
328 
329  return __widget;
330 }
331 
332 
333 void PlatformUnitWidget::controlTimerTick()
334 {
335  float translationFactor = ui.maxTranslationSpeed->value();
336  float rotationFactor = ui.maxRotationSpeed->value() * -1;
337  float rotationVel = rotaCtrl->getRotation() / M_PI_2 * rotationFactor;
339  << "Translation speed: (" << speedCtrl->getPosition().x() * translationFactor
340  << ", " << speedCtrl->getPosition().y() * translationFactor << ")"
341  << ", \t rotation speed: " << (rotationVel);
342 
343  platformUnitProxy->move(speedCtrl->getPosition().x() * translationFactor,
344  -1 * speedCtrl->getPosition().y() * translationFactor,
345  rotationVel);
346 
347  if (speedCtrl->getPosition().x() == 0
348  && speedCtrl->getPosition().y() == 0
349  && rotaCtrl->getRotation() == 0)
350  {
352  }
353 }
354 
355 
357 {
358  switch (event->key())
359  {
360  case Qt::Key_A:
361  case Qt::Key_W:
362  case Qt::Key_S:
363  case Qt::Key_D:
364  case Qt::Key_Q:
365  case Qt::Key_E:
366  if (!event->isAutoRepeat())
367  {
368  emit commandKeyPressed(event->key());
369  }
370  }
371  QWidget::keyPressEvent(event);
372 }
373 
374 
376 {
377  switch (event->key())
378  {
379  case Qt::Key_A:
380  case Qt::Key_W:
381  case Qt::Key_S:
382  case Qt::Key_D:
383  case Qt::Key_Q:
384  case Qt::Key_E:
385  if (!event->isAutoRepeat())
386  {
387  emit commandKeyReleased(event->key());
388  }
389  }
390  QWidget::keyReleaseEvent(event);
391 }
armarx::VariantType::Float
const VariantTypeId Float
Definition: Variant.h:918
armarx::PlatformUnitWidget::loadSettings
void loadSettings(QSettings *settings) override
Implement to load the settings that are part of the GUI configuration.
Definition: PlatformUnitGuiPlugin.cpp:136
armarx::PlatformUnitWidget::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: PlatformUnitGuiPlugin.cpp:90
armarx::PlatformUnitWidget::startControlTimer
void startControlTimer()
Definition: PlatformUnitGuiPlugin.cpp:195
armarx::KeyboardPlatformHookWidget::commandKeyReleased
void commandKeyReleased(int key)
armarx::PlatformUnitWidget::reportGlobalRobotPose
void reportGlobalRobotPose(const ::armarx::TransformStamped &transformStamped, const ::Ice::Current &=::Ice::emptyCurrent) override
Definition: PlatformUnitGuiPlugin.cpp:209
armarx::PlatformUnitWidget::onDisconnectComponent
void onDisconnectComponent() override
Hook for subclass.
Definition: PlatformUnitGuiPlugin.cpp:106
armarx::PlatformUnitWidget::PlatformUnitWidget
PlatformUnitWidget()
Definition: PlatformUnitGuiPlugin.cpp:55
armarx::KeyboardPlatformHookWidget::keyReleaseEvent
void keyReleaseEvent(QKeyEvent *event) override
Definition: PlatformUnitGuiPlugin.cpp:375
PlatformUnitGuiPlugin.h
PlatformUnitConfigDialog.h
armarx::PlatformUnitGuiPlugin::PlatformUnitGuiPlugin
PlatformUnitGuiPlugin()
Definition: PlatformUnitGuiPlugin.cpp:49
armarx::KeyboardPlatformHookWidget
Definition: PlatformUnitGuiPlugin.h:46
armarx::PlatformUnitWidget::saveSettings
void saveSettings(QSettings *settings) override
Implement to save the settings as part of the GUI configuration.
Definition: PlatformUnitGuiPlugin.cpp:143
armarx::PlatformUnitWidget::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: PlatformUnitGuiPlugin.cpp:99
armarx::PlatformUnitWidget::connectSlots
void connectSlots()
Definition: PlatformUnitGuiPlugin.cpp:150
armarx::PlatformUnitWidget::setNewPlatformPoseLabels
void setNewPlatformPoseLabels(float x, float y, float alpha)
Definition: PlatformUnitGuiPlugin.cpp:179
armarx::PlatformUnitConfigDialog
Definition: PlatformUnitConfigDialog.h:38
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
max
T max(T t1, T t2)
Definition: gdiam.h:48
armarx::PlatformUnitWidget::configured
void configured() override
This function must be implemented by the user, if he supplies a config dialog.
Definition: PlatformUnitGuiPlugin.cpp:129
armarx::JoystickControlWidget
Provides a simple joystick control.
Definition: JoystickControlWidget.h:124
armarx::KeyboardPlatformHookWidget::commandKeyPressed
void commandKeyPressed(int key)
ARMARX_LOG
#define ARMARX_LOG
Definition: Logging.h:163
armarx::ManagedIceObject::usingTopic
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
Definition: ManagedIceObject.cpp:248
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::JoystickControlWidget::setSteps
void setSteps(int stepCount)
Sets the steps of the control.
Definition: JoystickControlWidget.cpp:219
armarx::PlatformUnitWidget::stopControlTimer
void stopControlTimer()
Definition: PlatformUnitGuiPlugin.cpp:201
armarx::PlatformUnitWidget::moveTo
void moveTo()
Definition: PlatformUnitGuiPlugin.cpp:167
armarx::Logging::deactivateSpam
SpamFilterDataPtr deactivateSpam(float deactivationDurationSec=10.0f, const std::string &identifier="", bool deactivate=true) const
disables the logging for the current line for the given amount of seconds.
Definition: Logging.cpp:92
min
T min(T t1, T t2)
Definition: gdiam.h:42
ArmarXDataPath.h
armarx::PlatformUnitWidget::getWidget
QPointer< QWidget > getWidget() override
getWidget returns a pointer to the a widget of this controller.
Definition: PlatformUnitGuiPlugin.cpp:322
armarx::PlatformUnitWidget::setNewTargetPoseLabels
void setNewTargetPoseLabels(float x, float y, float alpha)
Definition: PlatformUnitGuiPlugin.cpp:187
armarx::ManagedIceObject::usingProxy
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Definition: ManagedIceObject.cpp:151
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::KeyboardPlatformHookWidget::keyPressEvent
void keyPressEvent(QKeyEvent *event) override
Definition: PlatformUnitGuiPlugin.cpp:356
armarx::PlatformUnitWidget::getConfigDialog
QPointer< QDialog > getConfigDialog(QWidget *parent=0) override
getConfigDialog returns a pointer to the a configuration widget of this controller.
Definition: PlatformUnitGuiPlugin.cpp:117
armarx::PlatformUnitWidget::onExitComponent
void onExitComponent() override
Hook for subclass.
Definition: PlatformUnitGuiPlugin.cpp:112
armarx::PlatformUnitWidget::ui
Ui::PlatformUnitGuiPlugin ui
Definition: PlatformUnitGuiPlugin.h:150