main_window.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright 2018 ROBOTIS CO., LTD.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16 
25 /*****************************************************************************
26 ** Includes
27 *****************************************************************************/
28 
29 #include <QtGui>
30 #include <QMessageBox>
31 #include <iostream>
33 
34 /*****************************************************************************
35 ** Namespaces
36 *****************************************************************************/
37 
38 namespace manipulator_h_gui {
39 
40 using namespace Qt;
41 
42 /*****************************************************************************
43 ** Implementation [MainWindow]
44 *****************************************************************************/
45 
46 MainWindow::MainWindow(int argc, char** argv, QWidget *parent)
47  : QMainWindow(parent)
48  , qnode(argc,argv)
49 {
50  ui.setupUi(this); // Calling this incidentally connects all ui's triggers to on_...() callbacks in this class.
51  QObject::connect(ui.actionAbout_Qt, SIGNAL(triggered(bool)), qApp, SLOT(aboutQt())); // qApp is a global variable for the application
52 
53  setWindowIcon(QIcon(":/images/icon.png"));
54  ui.tab_manager->setCurrentIndex(0); // ensure the first tab is showing - qt-designer should have this already hardwired, but often loses it (settings?).
55  QObject::connect(&qnode, SIGNAL(rosShutdown()), this, SLOT(close()));
56 
57  joint_name.push_back("joint1");
58  joint_name.push_back("joint2");
59  joint_name.push_back("joint3");
60  joint_name.push_back("joint4");
61  joint_name.push_back("joint5");
62  joint_name.push_back("joint6");
63 
64  /*********************
65  ** Logging
66  **********************/
67  ui.view_logging->setModel(qnode.loggingModel());
68  QObject::connect(&qnode, SIGNAL(loggingUpdated()), this, SLOT(updateLoggingView()));
69 
70  joint_spinbox.append( ui.joint1_spinbox );
71  joint_spinbox.append( ui.joint2_spinbox );
72  joint_spinbox.append( ui.joint3_spinbox );
73  joint_spinbox.append( ui.joint4_spinbox );
74  joint_spinbox.append( ui.joint5_spinbox );
75  joint_spinbox.append( ui.joint6_spinbox );
76 
77  /****************************
78  ** Connect
79  ****************************/
80 
81  qRegisterMetaType<manipulator_h_base_module_msgs::JointPose>("manipulator_h_base_module_msgs::JointPose");
82  QObject::connect(&qnode, SIGNAL(updateCurrentJointPose(manipulator_h_base_module_msgs::JointPose)), this, SLOT(updateCurrJointPoseSpinbox(manipulator_h_base_module_msgs::JointPose)));
83 
84  qRegisterMetaType<manipulator_h_base_module_msgs::KinematicsPose>("manipulator_h_base_module_msgs::KinematicsPose");
85  QObject::connect(&qnode, SIGNAL(updateCurrentKinematicsPose(manipulator_h_base_module_msgs::KinematicsPose)), this, SLOT(updateCurrKinematicsPoseSpinbox(manipulator_h_base_module_msgs::KinematicsPose)));
86 
87  /*********************
88  ** Auto Start
89  **********************/
90  qnode.init();
91 }
92 
94 
95 /*****************************************************************************
96 ** Implementation [Slots]
97 *****************************************************************************/
98 
100 {
102 }
103 
105 {
106  manipulator_h_base_module_msgs::JointPose msg;
107 
108  for ( int _id = 0; _id < joint_spinbox.size(); _id++ )
109  {
110  msg.name.push_back( joint_name[ _id ] );
111  msg.value.push_back( ((QDoubleSpinBox *) joint_spinbox[ _id ])->value() * M_PI / 180.0 );
112  }
113 
114  qnode.sendJointPoseMsg( msg );
115 }
116 
118 {
119  std::string group_name = "arm";
120 
121  qnode.getKinematicsPose( group_name );
122 }
123 
125 {
126  manipulator_h_base_module_msgs::KinematicsPose msg;
127 
128  msg.name = "arm";
129 
130  msg.pose.position.x = ui.pos_x_spinbox->value();
131  msg.pose.position.y = ui.pos_y_spinbox->value();
132  msg.pose.position.z = ui.pos_z_spinbox->value();
133 
134  double roll = ui.ori_roll_spinbox->value() * M_PI / 180.0;
135  double pitch = ui.ori_pitch_spinbox->value() * M_PI / 180.0;
136  double yaw = ui.ori_yaw_spinbox->value() * M_PI / 180.0;
137 
138  Eigen::Quaterniond QR = rpy2quaternion( roll, pitch, yaw );
139 
140  msg.pose.orientation.x = QR.x();
141  msg.pose.orientation.y = QR.y();
142  msg.pose.orientation.z = QR.z();
143  msg.pose.orientation.w = QR.w();
144 
146 }
147 
149 {
150  std_msgs::String msg;
151 
152  msg.data ="ini_pose";
153 
154  qnode.sendIniPoseMsg( msg );
155 }
156 
158 {
159  std_msgs::String msg;
160 
161  msg.data ="set_mode";
162 
163  qnode.sendSetModeMsg( msg );
164 }
165 
166 void MainWindow::updateCurrJointPoseSpinbox( manipulator_h_base_module_msgs::JointPose msg )
167 {
168  for ( int _name_index = 0; _name_index < msg.name.size(); _name_index++ )
169  {
170  for ( int _id = 0; _id < joint_name.size(); _id++ )
171  {
172  if ( msg.name[ _id ] == joint_name[ _name_index ] )
173  {
174  ((QDoubleSpinBox *) joint_spinbox[ _name_index ])->setValue( msg.value[ _id ] * 180.0 / M_PI );
175  break;
176  }
177  }
178  }
179 }
180 
181 void MainWindow::updateCurrKinematicsPoseSpinbox( manipulator_h_base_module_msgs::KinematicsPose msg )
182 {
183  ui.pos_x_spinbox->setValue( msg.pose.position.x );
184  ui.pos_y_spinbox->setValue( msg.pose.position.y );
185  ui.pos_z_spinbox->setValue( msg.pose.position.z );
186 
187  Eigen::Quaterniond QR( msg.pose.orientation.w , msg.pose.orientation.x , msg.pose.orientation.y , msg.pose.orientation.z );
188 
189  Eigen::MatrixXd rpy = quaternion2rpy( QR );
190 
191  double roll = rpy.coeff( 0 , 0 ) * 180.0 / M_PI;
192  double pitch = rpy.coeff( 1 , 0 ) * 180.0 / M_PI;
193  double yaw = rpy.coeff( 2, 0 ) * 180.0 /M_PI;
194 
195  ui.ori_roll_spinbox->setValue( roll );
196  ui.ori_pitch_spinbox->setValue( pitch );
197  ui.ori_yaw_spinbox->setValue( yaw );
198 }
199 
200 Eigen::MatrixXd MainWindow::rotationX( double angle )
201 {
202  Eigen::MatrixXd _rotation( 3 , 3 );
203 
204  _rotation << 1.0, 0.0, 0.0,
205  0.0, cos( angle ), -sin( angle ),
206  0.0, sin( angle ), cos( angle );
207 
208  return _rotation;
209 }
210 
211 Eigen::MatrixXd MainWindow::rotationY( double angle )
212 {
213  Eigen::MatrixXd _rotation( 3 , 3 );
214 
215  _rotation << cos( angle ), 0.0, sin( angle ),
216  0.0, 1.0, 0.0,
217  -sin( angle ), 0.0, cos( angle );
218 
219  return _rotation;
220 }
221 
222 Eigen::MatrixXd MainWindow::rotationZ( double angle )
223 {
224  Eigen::MatrixXd _rotation(3,3);
225 
226  _rotation << cos( angle ), -sin( angle ), 0.0,
227  sin( angle ), cos( angle ), 0.0,
228  0.0, 0.0, 1.0;
229 
230  return _rotation;
231 }
232 
233 Eigen::MatrixXd MainWindow::rotation2rpy( Eigen::MatrixXd rotation )
234 {
235  Eigen::MatrixXd _rpy = Eigen::MatrixXd::Zero( 3 , 1 );
236 
237  _rpy.coeffRef( 0 , 0 ) = atan2( rotation.coeff( 2 , 1 ), rotation.coeff( 2 , 2 ) );
238  _rpy.coeffRef( 1 , 0 ) = atan2( -rotation.coeff( 2 , 0 ), sqrt( pow( rotation.coeff( 2 , 1 ) , 2 ) + pow( rotation.coeff( 2 , 2 ) , 2 ) ) );
239  _rpy.coeffRef( 2 , 0 ) = atan2 ( rotation.coeff( 1 , 0 ) , rotation.coeff( 0 , 0 ) );
240 
241  return _rpy;
242 }
243 
244 Eigen::MatrixXd MainWindow::rpy2rotation( double roll, double pitch, double yaw )
245 {
246  Eigen::MatrixXd _rotation = rotationZ( yaw ) * rotationY( pitch ) * rotationX( roll );
247 
248  return _rotation;
249 }
250 
251 Eigen::Quaterniond MainWindow::rpy2quaternion( double roll, double pitch, double yaw )
252 {
253  Eigen::MatrixXd _rotation = rpy2rotation( roll, pitch, yaw );
254 
255  Eigen::Matrix3d _rotation3d;
256  _rotation3d = _rotation.block( 0 , 0 , 3 , 3 );
257 
258  Eigen::Quaterniond _quaternion;
259 
260  _quaternion = _rotation3d;
261 
262  return _quaternion;
263 }
264 
265 Eigen::Quaterniond MainWindow::rotation2quaternion( Eigen::MatrixXd rotation )
266 {
267  Eigen::Matrix3d _rotation3d;
268 
269  _rotation3d = rotation.block( 0 , 0 , 3 , 3 );
270 
271  Eigen::Quaterniond _quaternion;
272  _quaternion = _rotation3d;
273 
274  return _quaternion;
275 }
276 
277 Eigen::MatrixXd MainWindow::quaternion2rpy( Eigen::Quaterniond quaternion )
278 {
279  Eigen::MatrixXd _rpy = rotation2rpy( quaternion.toRotationMatrix() );
280 
281  return _rpy;
282 }
283 
284 Eigen::MatrixXd MainWindow::quaternion2rotation( Eigen::Quaterniond quaternion )
285 {
286  Eigen::MatrixXd _rotation = quaternion.toRotationMatrix();
287 
288  return _rotation;
289 }
290 
291 /*****************************************************************************
292 ** Implemenation [Slots][manually connected]
293 *****************************************************************************/
294 
301  ui.view_logging->scrollToBottom();
302 }
303 
304 /*****************************************************************************
305 ** Implementation [Menu]
306 *****************************************************************************/
307 
309  QMessageBox::about(this, tr("About ..."),tr("<h2>PACKAGE_NAME Test Program 0.10</h2><p>Copyright Robotis</p><p>This package needs an about description.</p>"));
310 }
311 
312 /*****************************************************************************
313 ** Implementation [Configuration]
314 *****************************************************************************/
315 
316 void MainWindow::closeEvent(QCloseEvent *event)
317 {
318  QMainWindow::closeEvent(event);
319 }
320 
321 } // namespace manipulator_h_gui
322 
QList< QAbstractSpinBox * > joint_spinbox
Eigen::Quaterniond rotation2quaternion(Eigen::MatrixXd rotation)
void on_curr_pos_button_clicked(bool check)
Eigen::MatrixXd rotation2rpy(Eigen::MatrixXd rotation)
void sendSetModeMsg(std_msgs::String msg)
Definition: qnode.cpp:161
void sendJointPoseMsg(manipulator_h_base_module_msgs::JointPose msg)
Definition: qnode.cpp:140
Eigen::MatrixXd quaternion2rpy(Eigen::Quaterniond quaternion)
void closeEvent(QCloseEvent *event)
MainWindow(int argc, char **argv, QWidget *parent=0)
Definition: main_window.cpp:46
void getKinematicsPose(std::string group_name)
Definition: qnode.cpp:195
void on_des_pos_button_clicked(bool check)
void on_curr_joint_button_clicked(bool check)
Definition: main_window.cpp:99
Eigen::MatrixXd rotationX(double angle)
void getJointPose(std::vector< std::string > joint_name)
Definition: qnode.cpp:168
Eigen::Quaterniond rpy2quaternion(double roll, double pitch, double yaw)
std::vector< std::string > joint_name
Ui::MainWindowDesign ui
void sendKinematicsPoseMsg(manipulator_h_base_module_msgs::KinematicsPose msg)
Definition: qnode.cpp:147
Eigen::MatrixXd rotationZ(double angle)
void on_set_mode_button_clicked(bool check)
void updateCurrJointPoseSpinbox(manipulator_h_base_module_msgs::JointPose msg)
Qt based gui for manipulator_h_gui.
QStringListModel * loggingModel()
Definition: qnode.hpp:90
Eigen::MatrixXd quaternion2rotation(Eigen::Quaterniond quaternion)
INLINE Rall1d< T, V, S > sqrt(const Rall1d< T, V, S > &arg)
void on_ini_pose_button_clicked(bool check)
INLINE Rall1d< T, V, S > pow(const Rall1d< T, V, S > &arg, double m)
void updateCurrKinematicsPoseSpinbox(manipulator_h_base_module_msgs::KinematicsPose msg)
void on_des_joint_button_clicked(bool check)
INLINE Rall1d< T, V, S > atan2(const Rall1d< T, V, S > &y, const Rall1d< T, V, S > &x)
Eigen::MatrixXd rpy2rotation(double roll, double pitch, double yaw)
INLINE Rall1d< T, V, S > cos(const Rall1d< T, V, S > &arg)
INLINE Rall1d< T, V, S > sin(const Rall1d< T, V, S > &arg)
void sendIniPoseMsg(std_msgs::String msg)
Definition: qnode.cpp:154
Eigen::MatrixXd rotationY(double angle)


manipulator_h_gui
Author(s): SCH
autogenerated on Mon Jun 10 2019 14:02:56