MMMPlayerGuiPlugin.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 "MMMPlayerGuiPlugin.h"
25 
26 #include <IceUtil/Time.h>
27 
32 
33 #include <RobotComponents/gui-plugins/MMMPlayerPlugin/ui_MMMPlayerConfigDialog.h>
34 
35 #include "MMMPlayerConfigDialog.h"
36 
37 
38 // Qt headers
39 #include <filesystem>
40 
41 #include <QComboBox>
42 #include <QInputDialog>
43 #include <QMenu>
44 #include <QMessageBox>
45 #include <QPushButton>
46 #include <Qt>
47 #include <QtGlobal>
48 
51 using namespace armarx;
52 
53 #define PROJECT_DEFAULT "Armar4"
54 #define CONFIG_FILE_DEFAULT "data/MMM/punching.xml"
55 
56 #define POSITION_CONTROL 0
57 #define VELOCITY_CONTROL 1
58 
60 {
61  addWidget<MMMPlayerWidget>();
62 }
63 
65 {
66  // init gui
67  ui.setupUi(getWidget());
68  fileDialog2 = new QFileDialog();
69  fileDialog2->setModal(true);
70  fileDialog2->setFileMode(QFileDialog::ExistingFiles);
71  QStringList fileTypes;
72  fileTypes << tr("XML (*.xml)") << tr("All Files (*.*)");
73  fileDialog2->setNameFilters(fileTypes);
74 
75  jointList = new QListWidget();
76  updateTimer = new QTimer(this);
77 }
78 
79 /*
80 QPointer<QDialog> MMMPlayerWidget::getConfigDialog(QWidget* parent)
81 {
82  if(!dialog)
83  {
84  dialog = new MMMLoaderConfigDialog(parent);
85  std::filesystem::path dir(CONFIG_FILE_DEFAULT);
86 
87  dialog->fileDialog->setDirectory(QString::fromStdString(dir.remove_filename().string()));
88  dialog->ui->editConfigFile->setText(QString::fromStdString(CONFIG_FILE_DEFAULT));
89  }
90  return qobject_cast<MMMLoaderConfigDialog*>(dialog);
91 }
92 
93 void MMMPlayerWidget::configured()
94 {
95  ARMARX_VERBOSE << "MMMPlayerWidget::configured()";
96  configFile = dialog->ui->editConfigFile->text().toStdString();
97  ARMARX_VERBOSE << "config file " + configFile;
98 }
99 */
100 
101 
102 void
104 {
105  isComponentCreated = false;
106  if (dialog->needtoCreate)
107  {
108 
109  std::string kinematicUnitName =
110  dialog->kinematicUnitComponentProxyFinder->getSelectedProxyName()
111  .trimmed()
112  .toStdString();
113  std::string kinematicTopic = dialog->kinematicTopicName;
114 
115  Ice::StringSeq pros;
116  std::string packageName("RobotComponents");
117  armarx::CMakePackageFinder finder(packageName);
118 
119  std::string appPath = finder.getBinaryDir() + "/TrajectoryPlayerAppRun";
120 
121  pros.push_back(appPath);
122  Ice::PropertiesPtr properties = Ice::createProperties(pros);
123 
124  properties->setProperty("ArmarX.TrajectoryPlayer.KinematicUnitName", kinematicUnitName);
125  properties->setProperty("ArmarX.TrajectoryPlayer.KinematicTopicName", kinematicTopic);
126 
127  // TrajectoryPlayerPtr mplptr = Component::create<TrajectoryPlayer>(properties, "TrajectoryPlayer");
128  // getArmarXManager()->addObject(mplptr);
129 
130 
131  appPath = finder.getBinaryDir() + "/MMMPlayerAppRun";
132  MMMPlayerPtr mptr = Component::create<MMMPlayer>(Ice::createProperties(), "MMMPlayer");
133  getArmarXManager()->addObject(mptr);
134 
135  isComponentCreated = true;
136  }
137 
138  usingProxy(
139  dialog->mmmPlayerComponentProxyFinder->getSelectedProxyName().trimmed().toStdString());
140  usingProxy(
141  dialog->trajPlayerComponentProxyFinder->getSelectedProxyName().trimmed().toStdString());
142  // ui.frameSlider->setEnabled(false);
143 
144 
145  connectSlots();
146 }
147 
148 void
150 {
151  MMMLoader = getProxy<MMMPlayerInterfacePrx>(
152  dialog->mmmPlayerComponentProxyFinder->getSelectedProxyName().trimmed().toStdString());
153  trajPlayer = getProxy<TrajectoryPlayerInterfacePrx>(
154  dialog->trajPlayerComponentProxyFinder->getSelectedProxyName().trimmed().toStdString());
155 
156  configFile = MMMLoader->getMotionPath();
157  ui.editConfigFile->setText(QString::fromStdString(configFile));
158  std::filesystem::path dir(configFile);
159  fileDialog2->setDirectory(QString::fromStdString(dir.remove_filename().string()));
160  setupMotionList();
161 
162  if (MMMLoader->isMotionLoaded())
163  {
164  setupJointList();
165  loadTrajPlayer();
166  }
167 
168  ui.frameSlider->blockSignals(true);
169  ui.frameSlider->setMaximum(100);
170  on_loopPlayback_toggled(false);
171  //on_enableRobotPoseUnit_toggled(false);
172 }
173 
174 void
176 {
177 }
178 
179 void
181 {
182 }
183 
184 bool
186 {
187 
188  if (getIceManager())
189  {
190  getIceManager()->removeObject("MMMPlayerConfigDialog");
191 
192  if (isComponentCreated)
193  {
194  getIceManager()->removeObject(
195  dialog->mmmPlayerComponentProxyFinder->getSelectedProxyName()
196  .trimmed()
197  .toStdString());
198  }
199  }
200 
201 
203 }
204 
205 void
206 MMMPlayerWidget::loadSettings(QSettings* settings)
207 {
208 }
209 
210 void
211 MMMPlayerWidget::saveSettings(QSettings* settings)
212 {
213 }
214 
215 void
217 {
218  TrajectoryBasePtr t = MMMLoader->getJointTraj();
220  trajPlayer->loadJointTraj(t);
221  trajPlayer->loadBasePoseTraj(MMMLoader->getBasePoseTraj());
222  //ARMARX_INFO << "spinbox info: " << ui.speedSpinBox->value();
223  //ARMARX_INFO << "end traj time: " << trajPlayer->getTrajEndTime();
224  // trajPlayer->setEndTime(trajPlayer->getTrajEndTime() / ui.speedSpinBox->value());
225 }
226 
227 void
229 {
230  connect(ui.initializeButton, SIGNAL(clicked()), this, SLOT(on_initializeButton_clicked()));
231  connect(ui.startButton, SIGNAL(clicked()), this, SLOT(on_startButton_clicked()));
232  connect(ui.pauseButton, SIGNAL(clicked()), this, SLOT(on_pauseButton_clicked()));
233  connect(ui.stopButton, SIGNAL(clicked()), this, SLOT(on_stopButton_clicked()));
234  connect(ui.previewButton, SIGNAL(clicked()), this, SLOT(on_previewButton_clicked()));
235 
236  connect(ui.btnSelectConfig, SIGNAL(clicked()), fileDialog2, SLOT(show()));
237  connect(ui.selectJoints, SIGNAL(clicked()), jointList, SLOT(show()));
238  connect(fileDialog2, SIGNAL(accepted()), this, SLOT(configFileSelected()));
239  connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateSlider()));
240  // connect(ui.frameSlider, SIGNAL(valueChanged(int)), this, SLOT(setFrameNumber(int)));
241  // connect(ui.loopPlayback, SIGNAL(toggled(bool)), this, SLOT(on_loopPlayback_toggled(bool)));
242  connect(ui.loopPlayback, SIGNAL(toggled(bool)), this, SLOT(on_loopPlayback_toggled(bool)));
243  connect(ui.enableRobotPoseUnit,
244  SIGNAL(toggled(bool)),
245  this,
246  SLOT(on_enableRobotPoseUnit_toggled(bool)));
247  connect(ui.speedSpinBox,
248  SIGNAL(valueChanged(double)),
249  this,
250  SLOT(on_spinBoxFPS_valueChanged(double)));
251  connect(
252  ui.controlMode, SIGNAL(currentIndexChanged(int)), this, SLOT(on_controlMode_changed(int)));
253  connect(jointList,
254  SIGNAL(itemChanged(QListWidgetItem*)),
255  this,
256  SLOT(jointListChanged(QListWidgetItem*)));
257 }
258 
259 void
260 armarx::MMMPlayerWidget::on_initializeButton_clicked()
261 {
262  // set position control mode and move to first frame as initial pose
263 
264  trajPlayer->resetTrajectoryPlayer(true);
265  ui.startButton->setEnabled(true);
266  ui.previewButton->setEnabled(true);
267 
268  updateSlider();
269 }
270 
271 void
273 {
274  trajPlayer->setEndTime(trajPlayer->getTrajEndTime() / value);
275 }
276 
277 void
278 armarx::MMMPlayerWidget::on_startButton_clicked()
279 {
280  trajPlayer->setIsPreview(false);
281  setSpeed(ui.speedSpinBox->value());
282  on_controlMode_changed(ui.controlMode->currentIndex());
283  bool started = trajPlayer->startTrajectoryPlayer();
284 
285 
286  if (started)
287  {
288  updateTimer->start(100);
289  ui.initializeButton->setDisabled(started);
290  ui.startButton->setDisabled(started);
291  ui.previewButton->setDisabled(started);
292  ui.motionName->setDisabled(started);
293  ui.controlMode->setDisabled(started);
294  ui.pauseButton->setEnabled(started);
295  ui.pauseButton->setText("Pause");
296  ui.btnSelectConfig->setEnabled(false);
297  ui.speedSpinBox->setDisabled(started);
298  ui.stopButton->setEnabled(started);
299  ui.frameSlider->setDisabled(started);
300  ui.frameSlider->blockSignals(true);
301 
302  ui.status->setText("Real");
303  }
304 }
305 
306 void
307 armarx::MMMPlayerWidget::on_previewButton_clicked()
308 {
309  trajPlayer->setIsPreview(true);
310  ARMARX_INFO << "speed spin box val: " << ui.speedSpinBox->value();
311  setSpeed(ui.speedSpinBox->value());
312  trajPlayer->resetTrajectoryPlayer(false);
313  bool previewed = trajPlayer->startTrajectoryPlayer();
314 
315  if (previewed)
316  {
317  updateTimer->start(100);
318  ui.initializeButton->setDisabled(previewed);
319  ui.startButton->setDisabled(previewed);
320  ui.previewButton->setDisabled(previewed);
321  ui.motionName->setDisabled(previewed);
322  ui.controlMode->setDisabled(previewed);
323  ui.pauseButton->setEnabled(previewed);
324  ui.pauseButton->setText("Pause");
325  ui.btnSelectConfig->setEnabled(false);
326  ui.speedSpinBox->setDisabled(previewed);
327  ui.stopButton->setEnabled(previewed);
328  ui.frameSlider->setDisabled(previewed);
329  ui.frameSlider->blockSignals(true);
330 
331  ui.status->setText("Preview");
332  }
333 }
334 
335 void
336 MMMPlayerWidget::on_pauseButton_clicked()
337 {
338  bool paused = trajPlayer->pauseTrajectoryPlayer();
339 
340  if (paused)
341  {
342  ui.pauseButton->setText("Resume");
343  }
344  else
345  {
346  ui.pauseButton->setText("Pause");
347  }
348 
349  ui.controlMode->setEnabled(paused);
350  ui.frameSlider->setEnabled(paused);
351  ui.speedSpinBox->setEnabled(paused);
352  ui.frameSlider->blockSignals(!paused);
353 }
354 
355 void
356 armarx::MMMPlayerWidget::on_stopButton_clicked()
357 {
358  bool stopped = trajPlayer->stopTrajectoryPlayer();
359 
360  if (stopped)
361  {
362  updateTimer->stop();
363  ui.initializeButton->setEnabled(stopped);
364  ui.startButton->setDisabled(stopped);
365  ui.previewButton->setEnabled(stopped);
366  ui.pauseButton->setText("Pause");
367  ui.btnSelectConfig->setEnabled(true);
368  ui.motionName->setEnabled(stopped);
369  ui.controlMode->setEnabled(stopped);
370  ui.speedSpinBox->setEnabled(stopped);
371  ui.frameSlider->blockSignals(false);
372  ui.pauseButton->setDisabled(stopped);
373  ui.stopButton->setDisabled(stopped);
374 
375  ui.status->setText("");
376  }
377 }
378 
379 void
380 MMMPlayerWidget::configFileSelected()
381 {
382  configFile = (this->fileDialog2->selectedFiles()[0]).toStdString();
383  ui.editConfigFile->setText(this->fileDialog2->selectedFiles()[0]);
384 
385  // load mmm motion
386  MMMLoader->loadMMMFile(configFile, "");
387  ui.frameSlider->setMaximum(100);
388  setupJointList();
389  setupMotionList();
390 
391  if (MMMLoader->isMotionLoaded())
392  {
393  loadTrajPlayer();
394  }
395 }
396 
397 void
398 MMMPlayerWidget::setupMotionList()
399 {
400  Ice::StringSeq motionNames = MMMLoader->getMotionNames();
401  QObject::disconnect(
402  ui.motionName, SIGNAL(currentIndexChanged(int)), this, SLOT(motionChanged(int)));
403  ui.motionName->clear();
404 
405  for (size_t i = 0; i < motionNames.size(); ++i)
406  {
407  ui.motionName->addItem(QString::fromStdString(motionNames.at(i)));
408  }
409 
410  if (motionNames.size() > 0)
411  {
412  ui.motionName->setCurrentIndex(0);
413  connect(ui.motionName, SIGNAL(currentIndexChanged(int)), this, SLOT(motionChanged(int)));
414  }
415 }
416 
417 void
418 MMMPlayerWidget::setupJointList()
419 {
420  Ice::StringSeq jointNames = MMMLoader->getJointNames();
421  jointList->blockSignals(true);
422  jointList->clear();
423 
424  for (size_t i = 0; i < jointNames.size(); ++i)
425  {
426  std::string jointName = jointNames.at(i);
427  QListWidgetItem* joint = new QListWidgetItem(QString::fromStdString(jointName), jointList);
428  joint->setCheckState(Qt::Checked);
429  }
430 
431  jointList->blockSignals(false);
432 }
433 
434 void
435 MMMPlayerWidget::updateSlider()
436 {
437  double currentTime = trajPlayer->getCurrentTime();
438  double endTime = trajPlayer->getEndTime();
439  double posPer = currentTime / endTime;
440 
441  int sliderPos = posPer * 100;
442 
443  ui.frameSlider->blockSignals(true);
444  ui.frameSlider->setSliderPosition(sliderPos);
445  ui.frameSlider->blockSignals(false);
446  ui.currentFrame->display(currentTime);
447 }
448 
449 void
450 MMMPlayerWidget::jointListChanged(QListWidgetItem* joint)
451 {
452  bool jointState;
453 
454  if (joint->checkState() == Qt::Checked)
455  {
456  jointState = trajPlayer->setJointsInUse(joint->text().toStdString(), true);
457  }
458  else
459  {
460  jointState = trajPlayer->setJointsInUse(joint->text().toStdString(), false);
461  }
462 
463  if (jointState)
464  {
465  joint->setCheckState(Qt::Checked);
466  }
467  else
468  {
469  joint->setCheckState(Qt::Unchecked);
470  }
471 }
472 
473 void
474 MMMPlayerWidget::motionChanged(int)
475 {
476  std::string motionName = ui.motionName->currentText().toStdString();
477  ARMARX_INFO << "Choosing motion with name '" << motionName << "'";
478  MMMLoader->setMotionData(motionName);
479 
480  if (MMMLoader->isMotionLoaded())
481  {
482  setupJointList();
483  loadTrajPlayer();
484  }
485 }
486 
487 void
488 MMMPlayerWidget::on_spinBoxFPS_valueChanged(double value)
489 {
490  if (value <= 0)
491  {
492  return;
493  }
494 }
495 
496 void
497 MMMPlayerWidget::on_loopPlayback_toggled(bool state)
498 {
499  trajPlayer->setLoopPlayback(state);
500  ui.loopPlayback->setChecked(state);
501 }
502 
503 void
504 MMMPlayerWidget::on_controlMode_changed(int controlMode)
505 {
506  ui.controlMode->setCurrentIndex(controlMode);
507 
508  if (controlMode == POSITION_CONTROL)
509  {
510  trajPlayer->setIsVelocityControl(false);
511  }
512  else
513  {
514  trajPlayer->setIsVelocityControl(true);
515  }
516 }
517 
518 void
519 armarx::MMMPlayerWidget::on_enableRobotPoseUnit_toggled(bool state)
520 {
521  trajPlayer->enableRobotPoseUnit(state);
522  ui.enableRobotPoseUnit->setChecked(state);
523 }
armarx::MMMPlayerWidget::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: MMMPlayerGuiPlugin.cpp:103
armarx::MMMPlayerWidget::onExitComponent
void onExitComponent() override
Hook for subclass.
Definition: MMMPlayerGuiPlugin.cpp:175
armarx::ManagedIceObject::getIceManager
IceManagerPtr getIceManager() const
Returns the IceManager.
Definition: ManagedIceObject.cpp:366
SingleTypeVariantList.h
armarx::control::common::MPStatus::paused
@ paused
armarx::MMMPlayerWidget::setSpeed
void setSpeed(double value)
Definition: MMMPlayerGuiPlugin.cpp:272
MatrixVariant.h
armarx::ManagedIceObject::getArmarXManager
ArmarXManagerPtr getArmarXManager() const
Returns the ArmarX manager used to add and remove components.
Definition: ManagedIceObject.cpp:360
armarx::MMMPlayerWidget::onClose
bool onClose() override
If you overwrite this method, make sure to call this implementation at the end of your implementation...
Definition: MMMPlayerGuiPlugin.cpp:185
MMMPlayerConfigDialog.h
armarx::CMakePackageFinder
The CMakePackageFinder class provides an interface to the CMake Package finder capabilities.
Definition: CMakePackageFinder.h:52
armarx::MMMPlayerWidget::onDisconnectComponent
void onDisconnectComponent() override
Hook for subclass.
Definition: MMMPlayerGuiPlugin.cpp:180
armarx::MMMPlayerWidget::MMMLoader
MMMPlayerInterfacePrx MMMLoader
Definition: MMMPlayerGuiPlugin.h:177
TrajectoryPlayer.h
armarx::MMMPlayerWidget::ui
Ui::MMMPlayerGuiPlugin ui
Definition: MMMPlayerGuiPlugin.h:176
IceInternal::Handle<::Ice::Properties >
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
armarx::MMMPlayerWidget::configFile
std::string configFile
Definition: MMMPlayerGuiPlugin.h:175
Ice::createProperties
Ice::PropertiesPtr createProperties()
armarx::CMakePackageFinder::getBinaryDir
std::string getBinaryDir() const
Definition: CMakePackageFinder.h:174
TimestampVariant.h
armarx::MMMPlayerWidget::connectSlots
void connectSlots()
Definition: MMMPlayerGuiPlugin.cpp:228
armarx::MMMPlayerWidget::MMMPlayerWidget
MMMPlayerWidget()
Definition: MMMPlayerGuiPlugin.cpp:64
armarx::MMMPlayerWidget::loadTrajPlayer
void loadTrajPlayer()
Definition: MMMPlayerGuiPlugin.cpp:216
MMMPlayerGuiPlugin.h
armarx::MMMPlayerGuiPlugin::MMMPlayerGuiPlugin
MMMPlayerGuiPlugin()
Definition: MMMPlayerGuiPlugin.cpp:59
armarx::MMMPlayerWidget::saveSettings
void saveSettings(QSettings *settings) override
Implement to save the settings as part of the GUI configuration.
Definition: MMMPlayerGuiPlugin.cpp:211
armarx::MMMPlayerWidget::loadSettings
void loadSettings(QSettings *settings) override
Implement to load the settings that are part of the GUI configuration.
Definition: MMMPlayerGuiPlugin.cpp:206
armarx::MMMPlayerWidget::trajPlayer
TrajectoryPlayerInterfacePrx trajPlayer
Definition: MMMPlayerGuiPlugin.h:178
armarx::MMMPlayerWidget::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: MMMPlayerGuiPlugin.cpp:149
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
MMMPlayer.h
armarx::ArmarXWidgetController::getWidget
virtual QPointer< QWidget > getWidget()
getWidget returns a pointer to the a widget of this controller.
Definition: ArmarXWidgetController.cpp:54
POSITION_CONTROL
#define POSITION_CONTROL
Definition: MMMPlayerGuiPlugin.cpp:56
ArmarXDataPath.h
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:154
armarx::ArmarXComponentWidgetController::onClose
bool onClose() override
If you overwrite this method, make sure to call this implementation at the end of your implementation...
Definition: ArmarXComponentWidgetController.cpp:44
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27