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 
17 /*****************************************************************************
18 ** Includes
19 *****************************************************************************/
20 
21 #include <QtGui>
22 #include <QMessageBox>
23 #include <iostream>
24 #include "../include/rh_p12_rn_gui/main_window.hpp"
25 
26 /*****************************************************************************
27 ** Namespaces
28 *****************************************************************************/
29 
30 namespace rh_p12_rn_gui {
31 
32 using namespace Qt;
33 
34 /*****************************************************************************
35 ** Implementation [MainWindow]
36 *****************************************************************************/
37 
38 MainWindow::MainWindow(int argc, char** argv, QWidget *parent)
39  : QMainWindow(parent)
40  , qnode(argc,argv)
41  , curr_mode_(-1)
42 {
43  ui.setupUi(this); // Calling this incidentally connects all ui's triggers to on_...() callbacks in this class.
44  QObject::connect(ui.actionAbout_Qt, SIGNAL(triggered(bool)), qApp, SLOT(aboutQt())); // qApp is a global variable for the application
45  QObject::connect(&qnode, SIGNAL(refreshValue(int,int)), this, SLOT(valueChanged(int,int)));
46 
47  QPixmap pix(":/images/RH-P12-RN.png");
48  ui.label_pic->setPixmap(pix);
49 
50  setWindowIcon(QIcon(":/images/icon.png"));
51  QObject::connect(&qnode, SIGNAL(rosShutdown()), this, SLOT(close()));
52 
53  /*********************
54  ** Logging
55  **********************/
56 
57  /*********************
58  ** Auto Start
59  **********************/
60  qnode.init();
61  usleep(300*1000);
62 
63  ui.torque_onoff_check->setChecked(qnode.getItemValue("torque_enable"));
64  usleep(50*1000);
65  ui.position_mode_radio->click();
66 
67  //ui.goal_current_slider->setValue(qnode.getItemValue("goal_current"));
68  ui.goal_current_slider->setValue(30);
69  ui.goal_vel_slider->setValue(qnode.getItemValue("goal_velocity"));
70  ui.goal_accel_slider->setValue(qnode.getItemValue("goal_acceleration"));
71  ui.goal_position_slider->setValue(qnode.getItemValue("goal_position"));
72 }
73 
75 
76 /*****************************************************************************
77 ** Implementation [Slots]
78 *****************************************************************************/
79 
81 {
82  ROS_INFO("POSITION_MODE : check(%d), curr_mode_(%d)", check, curr_mode_);
83  if (check == true && curr_mode_ != 5)
84  {
85  ROS_INFO("Current-based Positon Control Mode On");
86  robotis_controller_msgs::SyncWriteItem _torque_msg;
87  _torque_msg.item_name = "torque_enable";
88  _torque_msg.joint_name.push_back("gripper");
89  _torque_msg.value.push_back(0);
90 
91  if (ui.torque_onoff_check->checkState() == Checked)
92  qnode.setCtrlItem(_torque_msg);
93 
94  usleep(20*1000);
95 
96  robotis_controller_msgs::SyncWriteItem _mode_msg;
97  _mode_msg.item_name = "operating_mode";
98  _mode_msg.joint_name.push_back("gripper");
99  _mode_msg.value.push_back(5);
100  qnode.setCtrlItem(_mode_msg);
101  ROS_INFO("WRITE_MODE_MSG : set 5");
102 
103  usleep(20*1000);
104 
105  if (ui.torque_onoff_check->checkState() == Checked)
106  {
107  _torque_msg.value.clear();
108  _torque_msg.value.push_back(1);
109  qnode.setCtrlItem(_torque_msg);
110  }
111 
112  usleep(20*1000);
113 
114  int goal_curr_value = ui.goal_current_spin->value();
115  if (goal_curr_value < 0)
116  goal_curr_value = (-1) * goal_curr_value;
117  ui.goal_current_spin->setValue(goal_curr_value);
118 
119  ui.label_goal_velocity->setEnabled(true);
120  ui.label_goal_acc->setEnabled(true);
121  ui.label_goal_position->setEnabled(true);
122  ui.goal_vel_slider->setEnabled(true);
123  ui.goal_accel_slider->setEnabled(true);
124  ui.goal_position_slider->setEnabled(true);
125  ui.goal_vel_spin->setEnabled(true);
126  ui.goal_accel_spin->setEnabled(true);
127  ui.goal_position_spin->setEnabled(true);
128  ui.label_max_goal_velocity->setEnabled(true);
129  ui.label_max_goal_acc->setEnabled(true);
130  ui.label_max_goal_position->setEnabled(true);
131 
132  ui.min_position_button->setText("Min (Open)\nPosition");
133  ui.max_position_button->setText("Max (Close)\nPosition");
134  ui.goal_position_button->setEnabled(true);
135 
136  ui.go_goal_psoition_check->setEnabled(true);
137 
138  ui.goal_current_slider->setMinimum(0);
139  ui.goal_current_spin->setMinimum(0);
140 
141  curr_mode_ = 5;
142  }
143 }
144 
146 {
147  if (check == true && curr_mode_ != 0)
148  {
149  ROS_INFO("Current Control Mode On");
150  robotis_controller_msgs::SyncWriteItem _torque_msg;
151  _torque_msg.item_name = "torque_enable";
152  _torque_msg.joint_name.push_back("gripper");
153  _torque_msg.value.push_back(0);
154 
155  if(ui.torque_onoff_check->checkState() == Checked)
156  {
157  qnode.setCtrlItem(_torque_msg);
158  }
159 
160  usleep(20*1000);
161 
162  robotis_controller_msgs::SyncWriteItem _mode_msg;
163  _mode_msg.item_name = "operating_mode";
164  _mode_msg.joint_name.push_back("gripper");
165  _mode_msg.value.push_back(0);
166  qnode.setCtrlItem(_mode_msg);
167 
168  usleep(20*1000);
169 
170  if(ui.torque_onoff_check->checkState() == Checked)
171  {
172  _torque_msg.value.clear();
173  _torque_msg.value.push_back(1);
174  qnode.setCtrlItem(_torque_msg);
175  }
176 
177  ui.label_goal_velocity->setEnabled(false);
178  ui.label_goal_acc->setEnabled(false);
179  ui.label_goal_position->setEnabled(false);
180  ui.goal_vel_slider->setEnabled(false);
181  ui.goal_accel_slider->setEnabled(false);
182  ui.goal_position_slider->setEnabled(false);
183  ui.goal_vel_spin->setEnabled(false);
184  ui.goal_accel_spin->setEnabled(false);
185  ui.goal_position_spin->setEnabled(false);
186  ui.label_max_goal_velocity->setEnabled(false);
187  ui.label_max_goal_acc->setEnabled(false);
188  ui.label_max_goal_position->setEnabled(false);
189 
190  ui.min_position_button->setText("OPEN");
191  ui.max_position_button->setText("CLOSE");
192  ui.goal_position_button->setEnabled(false);
193 
194  ui.go_goal_psoition_check->setEnabled(false);
195 
196  ui.goal_current_slider->setMinimum(-820);
197  ui.goal_current_spin->setMinimum(-820);
198 
199  curr_mode_ = 0;
200  }
201 }
202 
204 {
205  robotis_controller_msgs::SyncWriteItem _torque_msg;
206  _torque_msg.item_name = "torque_enable";
207  _torque_msg.joint_name.push_back("gripper");
208 
209  if (check == true)
210  {
211  ROS_INFO("Torque On");
212  _torque_msg.value.push_back(1);
213  }
214  else
215  {
216  ROS_INFO("Torque Off");
217  _torque_msg.value.push_back(0);
218  }
219  qnode.setCtrlItem(_torque_msg);
220 }
221 
223 {
224  ROS_WARN("##goal current : %d (torque_enable: %d)", value, qnode.getItemValue("torque_enable"));
225  robotis_controller_msgs::SyncWriteItem _goal_current_msg;
226  _goal_current_msg.item_name = "goal_current";
227  _goal_current_msg.joint_name.push_back("gripper");
228  _goal_current_msg.value.push_back(value);
229  qnode.setCtrlItem(_goal_current_msg);
230 }
231 
233 {
234  ROS_WARN("goal vel. : %d", value);
235  robotis_controller_msgs::SyncWriteItem _goal_vel_msg;
236  _goal_vel_msg.item_name = "goal_velocity";
237  _goal_vel_msg.joint_name.push_back("gripper");
238  _goal_vel_msg.value.push_back(value);
239  qnode.setCtrlItem(_goal_vel_msg);
240 }
241 
243 {
244  ROS_WARN("goal accel. : %d", value);
245  robotis_controller_msgs::SyncWriteItem _goal_accel_msg;
246  _goal_accel_msg.item_name = "goal_acceleration";
247  _goal_accel_msg.joint_name.push_back("gripper");
248  _goal_accel_msg.value.push_back(value);
249  qnode.setCtrlItem(_goal_accel_msg);
250 }
251 
253 {
254  if(ui.go_goal_psoition_check->checkState() != Checked)
255  return;
256 
257  ROS_WARN("goal position : %d", value);
258  robotis_controller_msgs::SyncWriteItem _goal_position_msg;
259  _goal_position_msg.item_name = "goal_position";
260  _goal_position_msg.joint_name.push_back("gripper");
261  _goal_position_msg.value.push_back(value);
262  qnode.setPosition(_goal_position_msg);
263 }
264 
266 {
267  ROS_INFO("Min Position");
268 
269  if(ui.torque_onoff_check->checkState() == Unchecked)
270  {
271  ui.torque_onoff_check->click();
272  usleep(100*1000);
273  }
274 
275  if(ui.position_mode_radio->isChecked() == true)
276  {
277  robotis_controller_msgs::SyncWriteItem _goal_position_msg;
278  _goal_position_msg.item_name = "goal_position";
279  _goal_position_msg.joint_name.push_back("gripper");
280  _goal_position_msg.value.push_back(0);
281  qnode.setPosition(_goal_position_msg);
282 
283  ui.go_goal_psoition_check->setChecked(false);
284  }
285  else if(ui.current_mode_radio->isChecked() == true)
286  {
287  robotis_controller_msgs::SyncWriteItem _goal_current_msg;
288  _goal_current_msg.item_name = "goal_current";
289  _goal_current_msg.joint_name.push_back("gripper");
290  if(ui.goal_current_spin->value() > 0)
291  _goal_current_msg.value.push_back(-ui.goal_current_spin->value());
292  else
293  _goal_current_msg.value.push_back(ui.goal_current_spin->value());
294  qnode.setCtrlItem(_goal_current_msg);
295  }
296 
297  ui.auto_repeat_check->setChecked(false);
298 }
299 
301 {
302  ROS_INFO("Max Position");
303 
304  if(ui.torque_onoff_check->checkState() == Unchecked)
305  {
306  ui.torque_onoff_check->click();
307  usleep(100*1000);
308  }
309 
310  if(ui.position_mode_radio->isChecked() == true)
311  {
312  robotis_controller_msgs::SyncWriteItem _goal_position_msg;
313  _goal_position_msg.item_name = "goal_position";
314  _goal_position_msg.joint_name.push_back("gripper");
315  _goal_position_msg.value.push_back(740);
316  qnode.setPosition(_goal_position_msg);
317 
318  ui.go_goal_psoition_check->setChecked(false);
319  }
320  else if(ui.current_mode_radio->isChecked() == true)
321  {
322  robotis_controller_msgs::SyncWriteItem _goal_current_msg;
323  _goal_current_msg.item_name = "goal_current";
324  _goal_current_msg.joint_name.push_back("gripper");
325  if(ui.goal_current_spin->value() < 0)
326  _goal_current_msg.value.push_back(-ui.goal_current_spin->value());
327  else
328  _goal_current_msg.value.push_back(ui.goal_current_spin->value());
329  qnode.setCtrlItem(_goal_current_msg);
330  }
331 
332  ui.auto_repeat_check->setChecked(false);
333 }
334 
336 {
337  ROS_INFO("Goal Position");
338  if(ui.position_mode_radio->isChecked() == true)
339  {
340  robotis_controller_msgs::SyncWriteItem _goal_position_msg;
341  _goal_position_msg.item_name = "goal_position";
342  _goal_position_msg.joint_name.push_back("gripper");
343  _goal_position_msg.value.push_back(ui.goal_position_spin->value());
344  qnode.setPosition(_goal_position_msg);
345  }
346 
347  ui.auto_repeat_check->setChecked(false);
348 }
349 
350 void *MainWindow::autoRepeatFunc(void *main_window)
351 {
352  const int _max_stop_cnt = 7;
353  bool _dir = false;
354  int _stop_cnt = 0;
355  MainWindow *_main = (MainWindow*)main_window;
356 
357  while(_main->ui.auto_repeat_check->checkState() == Checked)
358  {
359  if(_main->qnode.getItemValue("is_moving") == 1)
360  {
361  _stop_cnt = 0;
362  }
363  else if(++_stop_cnt > _max_stop_cnt)
364  {
365  robotis_controller_msgs::SyncWriteItem _msg;
366  _msg.joint_name.push_back("gripper");
367  if(_main->ui.position_mode_radio->isChecked() == true)
368  {
369  _msg.item_name = "goal_position";
370  _msg.value.push_back( (_dir)? 0:740 );
371  _main->qnode.setPosition(_msg);
372  }
373  else if(_main->ui.current_mode_radio->isChecked() == true)
374  {
375  _msg.item_name = "goal_current";
376  _msg.value.push_back( (_dir)? _main->ui.goal_current_spin->value():(-1)*(_main->ui.goal_current_spin->value()) );
377  _main->qnode.setCtrlItem(_msg);
378  }
379  _dir = !_dir;
380  _stop_cnt = 0;
381  }
382 
383  usleep(100*1000);
384  }
385 
386  return NULL;
387 }
388 
390 {
391  if (check == true)
392  {
393  ROS_INFO("Auto Repeat On");
394  ui.go_goal_psoition_check->setChecked(false);
395  if(ui.torque_onoff_check->checkState() == Unchecked)
396  ui.torque_onoff_check->click();
397  auto_thread_ = pthread_create(&auto_thread_, NULL, autoRepeatFunc, (void *)this);
398  }
399  else
400  {
401  ROS_INFO("Auto Repeat Off");
402  }
403 }
404 
406 {
407  if(ui.torque_onoff_check->checkState() == Unchecked)
408  {
409  ui.torque_onoff_check->click();
410  usleep(100*1000);
411  }
412 
413  if (check == true)
414  {
415  ROS_INFO("Go Goal Position On");
416  robotis_controller_msgs::SyncWriteItem _goal_position_msg;
417  _goal_position_msg.item_name = "goal_position";
418  _goal_position_msg.joint_name.push_back("gripper");
419  _goal_position_msg.value.push_back(ui.goal_position_spin->value());
420  qnode.setPosition(_goal_position_msg);
421 
422  ui.auto_repeat_check->setChecked(false);
423  }
424  else
425  {
426  ROS_INFO("Go Goal Position Off");
427  }
428 }
429 
430 /*****************************************************************************
431 ** Implemenation [Slots][manually connected]
432 *****************************************************************************/
433 void MainWindow::valueChanged(int pos, int curr)
434 {
435  ui.label_present_position->setText(QString::number(pos));
436  ui.label_present_current->setText(QString::number(curr));
437 }
438 
439 /*****************************************************************************
440 ** Implementation [Menu]
441 *****************************************************************************/
442 
443 /*****************************************************************************
444 ** Implementation [Configuration]
445 *****************************************************************************/
446 
447 void MainWindow::closeEvent(QCloseEvent *event)
448 {
449  robotis_controller_msgs::SyncWriteItem _torque_msg;
450  _torque_msg.item_name = "torque_enable";
451  _torque_msg.joint_name.push_back("gripper");
452  _torque_msg.value.push_back(0);
453  qnode.setCtrlItem(_torque_msg);
454 
455  QMainWindow::closeEvent(event);
456 }
457 
458 } // namespace rh_p12_rn_gui
459 
void on_go_goal_psoition_check_clicked(bool check)
void on_goal_accel_slider_valueChanged(int value)
void on_auto_repeat_check_clicked(bool check)
void on_torque_onoff_check_clicked(bool check)
void on_goal_position_slider_valueChanged(int value)
void setPosition(const robotis_controller_msgs::SyncWriteItem &msg)
Definition: qnode.cpp:90
void on_goal_position_button_clicked(bool check)
void on_goal_current_slider_valueChanged(int value)
#define ROS_WARN(...)
static void * autoRepeatFunc(void *main_window)
MainWindow(int argc, char **argv, QWidget *parent=0)
Definition: main_window.cpp:38
void on_min_position_button_clicked(bool check)
#define ROS_INFO(...)
Qt central, all operations relating to the view part here.
Definition: main_window.hpp:45
void on_position_mode_radio_clicked(bool check)
Definition: main_window.cpp:80
uint32_t getItemValue(std::string item_name)
Definition: qnode.cpp:95
Ui::MainWindowDesign ui
Definition: main_window.hpp:82
void on_max_position_button_clicked(bool check)
void valueChanged(int pos, int curr)
void on_current_mode_radio_clicked(bool check)
void closeEvent(QCloseEvent *event)
void setCtrlItem(const robotis_controller_msgs::SyncWriteItem &msg)
Definition: qnode.cpp:85
void on_goal_vel_slider_valueChanged(int value)


rh_p12_rn_gui
Author(s): Zerom
autogenerated on Mon Jun 10 2019 14:42:12