joint-tree-item.cc 13.8 KB
Newer Older
1
2
3
4
5
//
// Copyright (c) CNRS
// Authors: Joseph Mirabel
//

Joseph Mirabel's avatar
Joseph Mirabel committed
6
#include "hppwidgetsplugin/joint-tree-item.hh"
7
8
9

#include <iostream>

10
11
12
13
14
15
#include <QDoubleSpinBox>
#include <QMenu>
#include <QPushButton>

#include <omniORB4/CORBA.h>

16
17
18
#include <hpp/corbaserver/client.hh>
#include <gepetto/viewer/group-node.h>

19
#include "gepetto/gui/mainwindow.hh"
20

Joseph Mirabel's avatar
Joseph Mirabel committed
21
22
23
24
#include "hppwidgetsplugin/jointtreewidget.hh"
#include "hppwidgetsplugin/pathplayer.hh"
#include "hppwidgetsplugin/joint-action.hh"

Florent Lamiraux's avatar
Florent Lamiraux committed
25
26
using CORBA::ULong;

Joseph Mirabel's avatar
Joseph Mirabel committed
27
28
namespace hpp {
  namespace gui {
29
30
    using CORBA::ULong;

Joseph Mirabel's avatar
Joseph Mirabel committed
31
32
33
34
    const int JointTreeItem::IndexRole      = Qt::UserRole + 1;
    const int JointTreeItem::LowerBoundRole = Qt::UserRole + 2;
    const int JointTreeItem::UpperBoundRole = Qt::UserRole + 3;
    const int JointTreeItem::TypeRole       = Qt::UserRole + 10;
35

36
    JointTreeItem::JointTreeItem(const char* name,
37
38
        const ULong& idxQ,
        const ULong& idxV,
39
        const hpp::floatSeq &q,
Joseph Mirabel's avatar
Joseph Mirabel committed
40
        const hpp::floatSeq &b,
41
42
43
44
45
        const ULong& nbDof, const NodesPtr_t& nodes)
      : QStandardItem (QString (name)), name_ (name)
      , idxQ_ (idxQ), idxV_ (idxV)
      , nq_ (q.length()), nv_ (nbDof)
      , nodes_ (nodes), value_ ()
Joseph Mirabel's avatar
Joseph Mirabel committed
46
47
48
    {
      setData((int)-1, IndexRole);
      setData(SkipType, TypeRole);
49
      for (size_t i = 0; i < nq_; ++i) {
Joseph Mirabel's avatar
Joseph Mirabel committed
50
51
52
53
54
55
56
57
58
59
60
        QStandardItem *joint = new QStandardItem;
        QStandardItem *upper = new QStandardItem;
        QStandardItem *lower = new QStandardItem;
        joint->setData(static_cast<int>(i), IndexRole);
        QList <QStandardItem*> row;
        row << joint << lower << upper;
        value_.append(row);
        appendRow(row);
      }
      updateConfig(q);
      updateBounds(b);
61
62
    }

Joseph Mirabel's avatar
Joseph Mirabel committed
63
64
65
66
67
    JointTreeItem::~JointTreeItem ()
    {
      qDeleteAll(actions_);
    }

Joseph Mirabel's avatar
Joseph Mirabel committed
68
69
70
71
    QStandardItem *JointTreeItem::clone() const
    {
      hpp::floatSeq q = hpp::floatSeq();
      q.length(value_.size());
Joseph Mirabel's avatar
Joseph Mirabel committed
72
      hpp::floatSeq b = hpp::floatSeq();
Joseph Mirabel's avatar
Joseph Mirabel committed
73
74
75
76
77
78
      b.length (2*value_.size());
      for (size_t i = 0; i < q.length(); ++i) {
        q[(ULong) i] = value_[(ULong) i][0]->data(Qt::EditRole).toFloat();
        b[2*(ULong) i] = value_[(ULong) i][0]->data(LowerBoundRole).toFloat();
        b[2*(ULong) i+1] = value_[(ULong) i][0]->data(UpperBoundRole).toFloat();
      }
79
      return new JointTreeItem (name_.c_str(), idxQ_, idxV_, q, b, nv_, nodes_);
80
81
    }

Joseph Mirabel's avatar
Joseph Mirabel committed
82
83
84
85
86
87
88
89
    hpp::floatSeq JointTreeItem::config() const
    {
      hpp::floatSeq q = hpp::floatSeq();
      q.length(value_.size());
      for (size_t i = 0; i < q.length(); ++i)
        q[(ULong) i] = value_[(ULong) i][0]->data(Qt::EditRole).toFloat();
      return q;
    }
90

Joseph Mirabel's avatar
Joseph Mirabel committed
91
    hpp::floatSeq JointTreeItem::bounds() const
Joseph Mirabel's avatar
Joseph Mirabel committed
92
    {
Joseph Mirabel's avatar
Joseph Mirabel committed
93
      hpp::floatSeq b = hpp::floatSeq();
Joseph Mirabel's avatar
Joseph Mirabel committed
94
95
96
97
98
99
      b.length (2*value_.size());
      for (int i = 0; i < value_.size(); ++i) {
        b[2*i  ] = value_[i][1]->data(Qt::EditRole).toFloat();
        b[2*i+1] = value_[i][2]->data(Qt::EditRole).toFloat();
      }
      return b;
100
101
    }

Joseph Mirabel's avatar
Joseph Mirabel committed
102
103
104
105
106
107
    void JointTreeItem::updateConfig (const hpp::floatSeq& c)
    {
      assert ((int)c.length() == value_.size());
      for (int i = 0; i < value_.size(); ++i)
        value_[i][0]->setData(c[i], Qt::EditRole);
    }
108

109
110
111
112
113
114
115
    void JointTreeItem::updateFromRobotConfig (const hpp::floatSeq& rc)
    {
      assert (idxQ_ + value_.size() <= rc.length());
      for (int i = 0; i < value_.size(); ++i)
        value_[i][0]->setData(rc[(int)idxQ_ + i], Qt::EditRole);
    }

Joseph Mirabel's avatar
Joseph Mirabel committed
116
    void JointTreeItem::updateBounds(const hpp::floatSeq& b)
Joseph Mirabel's avatar
Joseph Mirabel committed
117
118
119
120
121
122
123
124
125
126
127
128
    {
      assert ((int)b.length() == 2*value_.size());
      for (int i = 0; i < value_.size(); ++i) {
        QStandardItem *lower = value_[i][1];
        QStandardItem *upper = value_[i][2];
        lower->setData(BoundType, TypeRole);
        upper->setData(BoundType, TypeRole);
        lower->setData(b[2*i  ], Qt::EditRole);
        upper->setData(b[2*i+1], Qt::EditRole);
      }
      updateTypeRole();
    }
129

Joseph Mirabel's avatar
Joseph Mirabel committed
130
131
    void JointTreeItem::updateTypeRole()
    {
132
      // planar and freeflyer joints
133
      int threshold = value_.size();
134
      // TODO SO3 joint fall in that case too whereas threshold should be 0 for them.
135
      if (nv_ == 3 && nq_ == 4 )
136
        threshold = 2;
137
      else if (nv_ == 6 && nq_ == 7 )
138
        threshold = 3;
Joseph Mirabel's avatar
Joseph Mirabel committed
139
140
141
      for (int i = 0; i < value_.size(); ++i) {
        float lo = value_[i][1]->data (Qt::EditRole).toFloat();
        float up = value_[i][2]->data (Qt::EditRole).toFloat();
142
143
144
        if (i >= threshold) value_[i][0]->setData(IntegratorType,     TypeRole);
        else if (lo < up)   value_[i][0]->setData(BoundedValueType,   TypeRole);
        else                value_[i][0]->setData(UnboundedValueType, TypeRole);
Joseph Mirabel's avatar
Joseph Mirabel committed
145
146
      }
    }
147

Joseph Mirabel's avatar
Joseph Mirabel committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    void JointTreeItem::setupActions (HppWidgetsPlugin* plugin)
    {
      JointAction* a;

      a = new JointAction (QObject::tr("Move &joint..."), name_, 0);
      plugin->jointTreeWidget()->connect (a, SIGNAL (triggered(std::string)),
          SLOT (openJointMoveDialog(std::string)));
      actions_.append(a);

      a = new JointAction (QObject::tr("Set &bounds..."), name_, 0);
      plugin->jointTreeWidget()->connect (a, SIGNAL (triggered(std::string)),
          SLOT (openJointBoundDialog(std::string)));
      actions_.append(a);

      a = new JointAction (QObject::tr("Add joint &frame"), name_, 0);
      plugin->connect (a, SIGNAL (triggered(std::string)),
          SLOT (addJointFrame(std::string)));
      actions_.append(a);

      a = new JointAction (QObject::tr("Display &roadmap"), name_, 0);
      plugin->connect (a, SIGNAL (triggered(std::string)),
          SLOT (displayRoadmap(std::string)));
      actions_.append(a);

      a = new JointAction (QObject::tr("Display &waypoints of selected path"), name_, 0);
      plugin->pathPlayer()->connect (a, SIGNAL (triggered(std::string)),
          SLOT (displayWaypointsOfPath(std::string)));
      actions_.append(a);

      a = new JointAction (QObject::tr("Display selected &path"), name_, 0);
      plugin->pathPlayer()->connect (a, SIGNAL (triggered(std::string)),
          SLOT (displayPath(std::string)));
      actions_.append(a);
    }

    const QList<QAction*>& JointTreeItem::actions () const
    {
      return actions_;
    }

188
    JointItemDelegate::JointItemDelegate(QPushButton *forceVelocity, HppWidgetsPlugin *plugin, gepetto::gui::MainWindow *parent)
Joseph Mirabel's avatar
Joseph Mirabel committed
189
190
191
192
      : QItemDelegate (parent), main_ (parent),
      plugin_ (plugin),
      forceIntegrator_ (forceVelocity)
    {}
193

Joseph Mirabel's avatar
Joseph Mirabel committed
194
195
196
197
198
199
200
201
    void JointItemDelegate::updateTypeRole (JointTreeItem::ItemType& type) const
    {
      if (forceIntegrator_ && forceIntegrator_->isChecked () && (
            type == JointTreeItem::UnboundedValueType
            || type == JointTreeItem::BoundedValueType
            ))
        type = JointTreeItem::IntegratorType;
    }
202

Joseph Mirabel's avatar
Joseph Mirabel committed
203
204
205
206
207
208
209
210
211
    QWidget *JointItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/*option*/, const QModelIndex &index) const
    {
      JointTreeItem::ItemType type = (JointTreeItem::ItemType)index.data(JointTreeItem::TypeRole).toInt();
      updateTypeRole(type);
      const QStandardItemModel* m = static_cast <const QStandardItemModel*> (index.model());
      const JointTreeItem* ji = dynamic_cast <const JointTreeItem*> (m->itemFromIndex(index)->parent());
      switch (type) {
        case JointTreeItem::SkipType:
          return 0;
212
213
214
215
216
217
218
219
220
221
        case JointTreeItem::IntegratorType:
          assert (ji);
          return new IntegratorWheel (Qt::Horizontal, plugin_, parent, main_,
              ji, std::min ( (ULong)index.data(JointTreeItem::IndexRole).toInt(),
                             ji->numberDof()-1)
              );
        case JointTreeItem::BoundedValueType:
          assert (ji);
          return new SliderBoundedJoint (Qt::Horizontal, plugin_, parent,
              main_, ji, index.data(JointTreeItem::IndexRole).toInt());
Joseph Mirabel's avatar
Joseph Mirabel committed
222
223
224
225
226
227
228
229
230
231
232
        case JointTreeItem::UnboundedValueType:
        case JointTreeItem::BoundType: {
                                         QDoubleSpinBox* spinbox = new QDoubleSpinBox (parent);
                                         spinbox->setMinimum(-DBL_MAX);
                                         spinbox->setMaximum(DBL_MAX);
                                         spinbox->setSingleStep(0.01);
                                         spinbox->setDecimals(3);
                                         return spinbox;
                                       }
        default:
                                       return 0;
233
234
235
      }
    }

Joseph Mirabel's avatar
Joseph Mirabel committed
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
    void JointItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    {
      JointTreeItem::ItemType type = (JointTreeItem::ItemType)index.data(JointTreeItem::TypeRole).toInt();
      updateTypeRole(type);
      float q = index.data(Qt::EditRole).toFloat ();
      switch (type) {
        case JointTreeItem::SkipType:
          return;
        case JointTreeItem::IntegratorType:
          return;
        case JointTreeItem::BoundedValueType:
          return;
        case JointTreeItem::UnboundedValueType:
        case JointTreeItem::BoundType:{
                                        QDoubleSpinBox* spinbox = static_cast <QDoubleSpinBox*> (editor);
                                        spinbox->setValue(q);
                                        break;
                                      }
        default:
                                      break;
256
      }
Joseph Mirabel's avatar
Joseph Mirabel committed
257
    }
258

Joseph Mirabel's avatar
Joseph Mirabel committed
259
260
261
262
263
264
265
266
267
268
269
    void JointItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
        const QModelIndex &index) const
    {
      JointTreeItem::ItemType type = (JointTreeItem::ItemType)index.data(JointTreeItem::TypeRole).toInt();
      updateTypeRole(type);
      QStandardItemModel* m = static_cast <QStandardItemModel*> (model);
      JointTreeItem* ji = dynamic_cast <JointTreeItem*> (m->itemFromIndex(index)->parent());
      double q;
      switch (type) {
        case JointTreeItem::SkipType:
          return;
270
271
        case JointTreeItem::IntegratorType:
          return;
Joseph Mirabel's avatar
Joseph Mirabel committed
272
273
274
275
276
277
278
279
280
281
282
283
284
        case JointTreeItem::BoundedValueType: {
                                                SliderBoundedJoint* slider = static_cast <SliderBoundedJoint*> (editor);
                                                q = slider->getValue();
                                                break;
                                              }
        case JointTreeItem::UnboundedValueType:
        case JointTreeItem::BoundType: {
                                         QDoubleSpinBox* spinbox = static_cast <QDoubleSpinBox*> (editor);
                                         q = spinbox->value();
                                         break;
                                       }
        default:
                                       return;
285
      }
Joseph Mirabel's avatar
Joseph Mirabel committed
286
287
      model->setData(index, q, Qt::EditRole);
      assert (ji);
288
      ULong idxCfg = (ULong)index.data(JointTreeItem::IndexRole).toInt();
Joseph Mirabel's avatar
Joseph Mirabel committed
289
290
291
292
      switch (type) {
        case JointTreeItem::BoundedValueType:
          return;
        case JointTreeItem::UnboundedValueType:
293
          plugin_->currentConfig()[ji->rankInConfig()+idxCfg] = q;
Joseph Mirabel's avatar
Joseph Mirabel committed
294
295
296
297
298
299
300
          break;
        case JointTreeItem::BoundType:
          plugin_->client()->robot()->setJointBounds (ji->name().c_str(), ji->bounds());
          ji->updateTypeRole();
          break;
        default:
          break;
301
      }
Joseph Mirabel's avatar
Joseph Mirabel committed
302
      main_->requestApplyCurrentConfiguration();
303
304
    }

Joseph Mirabel's avatar
Joseph Mirabel committed
305
306
307
308
    void JointItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
    {
      editor->setGeometry(option.rect);
    }
309

Joseph Mirabel's avatar
Joseph Mirabel committed
310
    IntegratorWheel::IntegratorWheel(Qt::Orientation o, HppWidgetsPlugin *plugin, QWidget *parent,
311
312
313
314
        gepetto::gui::MainWindow *main, JointTreeItem const* item, int index)
      : QSlider (o, parent), rate_ (100), main_ (main), plugin_ (plugin),
      item_ (item), bound_ (100), maxVelocity_ (0.1),
      index_ (index)
Joseph Mirabel's avatar
Joseph Mirabel committed
315
316
317
    {
      setMinimum(-bound_);
      setMaximum(bound_);
318
319
320
321
322
      q_.length (item_->configSize());
      dq_.length (item_->numberDof());
      for (ULong i = 0; i < q_.length(); ++i)
        q_ [ i] = plugin_->currentConfig()[item_->rankInConfig()+i];
      for (ULong i = 0; i < dq_.length(); ++i) dq_[ i] = 0;
Joseph Mirabel's avatar
Joseph Mirabel committed
323
324
325
326
327
      setValue (0);
      connect(this, SIGNAL (sliderReleased()), this, SLOT (reset()));
      connect(this, SIGNAL (sliderMoved(int)), this, SLOT (updateIntegrator(int)));
      timerId_ = startTimer(rate_);
    }
328

Joseph Mirabel's avatar
Joseph Mirabel committed
329
330
331
    void IntegratorWheel::timerEvent(QTimerEvent *)
    {
      killTimer(timerId_);
332
333
334
335
336
      if (dq_[index_] != 0) {
        hpp::floatSeq_var q = plugin_->client()->robot ()->jointIntegrate (q_, item_->name().c_str(), dq_, true);
        q_ = q.in();
        for (ULong i = 0; i < q_.length(); ++i)
          plugin_->currentConfig()[item_->rankInConfig()+i] = q_ [ i];
Joseph Mirabel's avatar
Joseph Mirabel committed
337
338
339
        main_->requestApplyCurrentConfiguration();
      }
      timerId_ = startTimer(rate_);
340
341
    }

Joseph Mirabel's avatar
Joseph Mirabel committed
342
343
    void IntegratorWheel::reset()
    {
344
      dq_[index_] = 0;
Joseph Mirabel's avatar
Joseph Mirabel committed
345
346
      setValue(0);
    }
347

Joseph Mirabel's avatar
Joseph Mirabel committed
348
349
    void IntegratorWheel::updateIntegrator(int value)
    {
350
      dq_[index_] = maxVelocity_ * (double)value / (double)bound_;
Joseph Mirabel's avatar
Joseph Mirabel committed
351
    }
352

Joseph Mirabel's avatar
Joseph Mirabel committed
353
    SliderBoundedJoint::SliderBoundedJoint(Qt::Orientation orientation, HppWidgetsPlugin *plugin, QWidget *parent,
354
355
356
        gepetto::gui::MainWindow *main, JointTreeItem const* item, int index)
      : QSlider (orientation, parent), main_ (main), plugin_ (plugin),
      item_ (item), index_ (index)
Joseph Mirabel's avatar
Joseph Mirabel committed
357
    {
358
359
360
361
362
      value_ = item_->config()[index];
      hpp::floatSeq bounds = item_->bounds();
      m_ = bounds[2*index  ];
      M_ = bounds[2*index+1];

Joseph Mirabel's avatar
Joseph Mirabel committed
363
364
      setMinimum(0);
      setMaximum(100);
365
      setValue ((int)(100*(value_ - m_)/(M_ - m_)));
Joseph Mirabel's avatar
Joseph Mirabel committed
366
367
      connect (this, SIGNAL (sliderMoved(int)), this, SLOT (updateConfig(int)));
    }
368

Joseph Mirabel's avatar
Joseph Mirabel committed
369
370
    double SliderBoundedJoint::getValue()
    {
371
      return value_;
Joseph Mirabel's avatar
Joseph Mirabel committed
372
    }
373

Joseph Mirabel's avatar
Joseph Mirabel committed
374
375
    void SliderBoundedJoint::updateConfig(int value)
    {
376
377
      value_ = m_ + (double)(value - 0) * (M_ - m_) / (double)100;
      plugin_->currentConfig()[item_->rankInConfig()+index_] = value_;
Joseph Mirabel's avatar
Joseph Mirabel committed
378
379
380
381
      main_->requestApplyCurrentConfiguration();
    }
  } // namespace gui
} // namespace hpp