remoteimuplugin.cc 8.49 KB
Newer Older
1
2
3
4
5
#include <remoteimuplugin.hh>

#include <QtPlugin>
#include <QtConcurrentRun>

6
7
#include <gepetto/gui/mainwindow.hh>
#include <gepetto/gui/windows-manager.hh>
8

9
10
#include <../hppwidgetsplugin/joint-action.hh>

Joseph Mirabel's avatar
Joseph Mirabel committed
11
12
namespace hpp {
  namespace gui {
13
14
15
    using gepetto::gui::ModelInterface;
    using gepetto::gui::MainWindow;

Joseph Mirabel's avatar
Joseph Mirabel committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    AttitudeEventSender::AttitudeEventSender()
    {
    }

    void AttitudeEventSender::mouseEvent(const remoteimu::MouseEventSender::Event e)
    {
      emit attitudeEvent(e.ori[0], e.ori[1], e.ori[2], e.ori[3]);
    }

    AttitudeDevice::AttitudeDevice () :
      port (6000),
      mouse_ (new remoteimu::UDPServer ("0.0.0.0", port), 5),
      hpp_ (0, 0),
      aes_ (), mask (new hpp::boolSeq)
    {
      hpp_.connect();
    }

    void AttitudeDevice::init ()
    {
      mouse_.setMouseEventSender(&aes_);

      mask->length(3);
      mask[0]=true;mask[1]=true;mask[2]=true;

      connect(&aes_, SIGNAL (attitudeEvent (double, double, double, double)),
42
43
          this, SLOT (updateJointAttitude (double, double, double, double)));

44
      gepetto::gui::MainWindow* m = gepetto::gui::MainWindow::instance();
45
      graphics::WindowsManager::Color_t black(1,1,1,1);
Joseph Mirabel's avatar
Joseph Mirabel committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
      m->osg()->addBox("hpp-gui/attitudeControl", 0.001f, 0.001f, 0.001f, black);
      m->osg()->addLandmark("hpp-gui/attitudeControl", 0.1f);
      m->osg()->setVisibility("hpp-gui/attitudeControl", "OFF");
    }

    void AttitudeDevice::jointName (const std::string jointName)
    {
      jn = jointName;
    }

    void AttitudeDevice::start()
    {
      if (lock_.isRunning()) stop ();

60
      MainWindow* m = gepetto::gui::MainWindow::instance();
Joseph Mirabel's avatar
Joseph Mirabel committed
61
62
63
64
65
66
67
68
69
70
71
      ModelInterface* model = m->pluginManager()->getFirstOf <ModelInterface> ();
      if (model == NULL) {
        qDebug () << "No ModelInterface found";
        return;
      }

      // Initialize the constraints
      hpp::Transform__var transform = hpp_.robot()->getJointPosition(jn.c_str());
      hpp::floatSeq pos1, pos2; pos1.length(3); pos2.length(3);
      for (int i = 0; i < 3; i++) pos1[i] = 0;
      for (int i = 0; i < 3; i++) pos2[i] = transform.in()[i];
72
73
      for (int i = 0; i < 3; i++) frameViz.position[i]=(float)pos2[i];
      frameViz.quat.set(0, 0, 0, 1);
Joseph Mirabel's avatar
Joseph Mirabel committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

      qDebug() << transform.in()[0+3] << ","
        << transform.in()[1+3] << ","
        << transform.in()[2+3] << ","
        << transform.in()[3+3];
      mouse_.setInitialQuat (Eigen::Quaterniond(transform.in()[0+3],
            transform.in()[1+3],
            transform.in()[2+3],
            transform.in()[3+3]));

      hpp_.problem()->createPositionConstraint (
          "attitudeDeviceControl/pos", "", jn.c_str(),
          pos2, pos1, mask.in());

      m->osg()->setVisibility("hpp-gui/attitudeControl", "ON");
      m->osg()->applyConfiguration("hpp-gui/attitudeControl", frameViz);
      std::string bodyName = model->getBodyFromJoint (jn);
      if (!jn.empty())
        m->osg()->addLandmark(bodyName.c_str (), 0.05f);
      m->osg()->refresh();
      lock_ = QtConcurrent::run (&mouse_, &remoteimu::Mouse::handleEvents, true);
    }

    void AttitudeDevice::stop()
    {
      mouse_.stopEventHandler();
100
      gepetto::gui::MainWindow* m = gepetto::gui::MainWindow::instance();
Joseph Mirabel's avatar
Joseph Mirabel committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
      ModelInterface* model = m->pluginManager()->getFirstOf <ModelInterface> ();
      if (model == NULL) {
        qDebug () << "No ModelInterface found";
        return;
      }

      m->osg()->setVisibility("hpp-gui/attitudeControl", "OFF");
      std::string bodyName = model->getBodyFromJoint (jn);
      if (!jn.empty())
        m->osg()->deleteLandmark(bodyName.c_str ());
      lock_.waitForFinished();
    }

    void AttitudeDevice::updateJointAttitude(double w, double x, double y, double z)
    {
116
      gepetto::gui::MainWindow* m = gepetto::gui::MainWindow::instance();
Joseph Mirabel's avatar
Joseph Mirabel committed
117

118
119
      q[3] = w; q[0] = x; q[1] = y; q[2] = z;
      frameViz.quat.set(x, y, z, w);
Joseph Mirabel's avatar
Joseph Mirabel committed
120
121
122
123
      m->osg()->applyConfiguration("hpp-gui/attitudeControl", frameViz);

      hpp::floatSeq pos1, pos2; pos1.length(3); pos2.length(3);
      for (int i = 0; i < 3; i++) pos1[i] = 0;
124
      for (int i = 0; i < 3; i++) pos2[i] = frameViz.position[i];
Joseph Mirabel's avatar
Joseph Mirabel committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

      hpp_.problem()->createPositionConstraint (
          "attitudeDeviceControl/pos", "", jn.c_str(),
          pos2, pos1, mask.in());
      hpp_.problem()->createOrientationConstraint (
          "attitudeDeviceControl/ori", "", jn.c_str(),
          q, mask.in());
      hpp_.problem()->resetConstraints ();
      hpp::Names_t n; n.length(2);
      hpp::intSeq prior; prior.length(2);
      n[0] = "attitudeDeviceControl/ori"; n[1] = "attitudeDeviceControl/pos";
      prior[0] = 0; prior[1] = 0;
      hpp_.problem()->setNumericalConstraints ("attitudeDeviceControl", n, prior);
      hpp::floatSeq_var qin = hpp_.robot()->getCurrentConfig ();
      hpp::floatSeq_var qproj;
      hpp::floatSeq_out qp_out (qproj);
      double err;
      bool res = hpp_.problem()->applyConstraints (qin.in(), qp_out, err);
      if (!res) {
        qDebug() << "Projection failed: " << err;
        hpp_.robot()->setCurrentConfig (qin.in());
      } else {
        hpp_.robot()->setCurrentConfig (qproj);
      }
      m->requestApplyCurrentConfiguration();
    }

    void AttitudeDevice::updateTargetPosition (double x, double y, double z) {
153
154
      Eigen::Quaternion<double> q
        (frameViz.quat.w(), frameViz.quat.x(), frameViz.quat.y(), frameViz.quat.z());
Joseph Mirabel's avatar
Joseph Mirabel committed
155
      Eigen::Vector3d dx = q.inverse() * Eigen::Vector3d (x,y,z);
156
157
158
      frameViz.position[0] += (float)dx[0];
      frameViz.position[1] += (float)dx[1];
      frameViz.position[2] += (float)dx[2];
Joseph Mirabel's avatar
Joseph Mirabel committed
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
    }

    AttitudeDeviceMsgBox::AttitudeDeviceMsgBox (QWidget *parent) :
      QMessageBox (QMessageBox::Information,  "Attitude Device","",
          QMessageBox::Close, parent,
          Qt::Dialog | Qt::WindowStaysOnTopHint),
      device_ ()
    {
      setModal (false);
      setText ("Configuration steps:\n"
          "1 - Configure your device to send its datas to " + device_.address() + "\n"
          "2 - Do not move your device for a short initialization time (corresponding to 100 measurements of your device).\n"
          "3 - Move your device, the frame should move as well.\n"
          "4 - Close this popup to stop the connection."
          );
    }

    void AttitudeDeviceMsgBox::show()
    {
      connect (this, SIGNAL(finished(int)), &device_, SLOT (stop()));
      connect (this, SIGNAL(finished(int)), this, SLOT (deleteLater()));
      device_.init ();
      device_.start ();
      QMessageBox::show ();
    }

    void AttitudeDeviceMsgBox::keyPressEvent(QKeyEvent *event)
    {
      double shift = 0.01;
      if (event->modifiers() == Qt::ControlModifier)
        shift /= 10;
      else if (event->modifiers() == Qt::ShiftModifier)
        shift *= 10;
      switch (event->key ()) {
        case Qt::Key_Up:
        case Qt::Key_W:
          qDebug () << "X+";
          device_.updateTargetPosition (shift, 0, 0);
          break;
        case Qt::Key_Down:
        case Qt::Key_S:
          qDebug () << "X-";
          device_.updateTargetPosition (-shift, 0, 0);
          break;
        case Qt::Key_Left:
        case Qt::Key_A:
          qDebug () << "Y+";
          device_.updateTargetPosition (0, shift, 0);
          break;
        case Qt::Key_Right:
        case Qt::Key_D:
          qDebug () << "Y-";
          device_.updateTargetPosition (0, -shift, 0);
          break;
        case Qt::Key_PageUp:
        case Qt::Key_Q:
          qDebug () << "Z+";
          device_.updateTargetPosition (0, 0, shift);
          break;
        case Qt::Key_PageDown:
        case Qt::Key_E:
          qDebug () << "Z-";
          device_.updateTargetPosition (0, 0, -shift);
          break;
      }

      QMessageBox::keyPressEvent(event);
    }

    RemoteImuPlugin::~RemoteImuPlugin()
    {
    }

    void RemoteImuPlugin::init() {
      msgBox_ = NULL;
    }

    QString RemoteImuPlugin::name() const
    {
      return QString ("Remote IMU");
    }

241
    QAction *RemoteImuPlugin::action(const std::string &jointName) const
Joseph Mirabel's avatar
Joseph Mirabel committed
242
243
244
245
246
247
248
249
250
251
252
253
254
255
    {
      JointAction* action = new JointAction (tr("Attach to attitude device..."), jointName, NULL);
      action->setIcon (QIcon::fromTheme("smartphone"));
      connect (action, SIGNAL (triggered(std::string)), SLOT (newDevice (std::string)));
      return action;
    }

    void RemoteImuPlugin::newDevice(const std::string jointName)
    {
      msgBox_ = new AttitudeDeviceMsgBox (NULL);
      msgBox_->setJointName (jointName);
      msgBox_->show();
    }

256
#if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
Joseph Mirabel's avatar
Joseph Mirabel committed
257
    Q_EXPORT_PLUGIN2 (remoteimuplugin, RemoteImuPlugin)
258
#endif
Joseph Mirabel's avatar
Joseph Mirabel committed
259
260
  } // namespace gui
} // namespace hpp