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