main_window.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright 2016 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 /* Authors: Taehun Lim (Darby) */
18 
19 #include <QMessageBox>
20 #include <iostream>
21 
22 #include "../include/dynamixel_workbench_single_manager_gui/main_window.hpp"
23 
24 using namespace main_window;
25 using namespace Qt;
26 
27 MainWindow::MainWindow(int argc, char** argv, QWidget *parent)
28  : QMainWindow(parent)
29  , qnode_(argc,argv)
30 {
31  ui_.setupUi(this);
32  QObject::connect(ui_.actionAbout_Qt, SIGNAL(triggered(bool)), qApp, SLOT(aboutQt()));
33  setWindowIcon(QIcon(":/images/icon.png"));
34 
35  ui_.tab_manager->setCurrentIndex(0);
36  QObject::connect(&qnode_, SIGNAL(rosShutdown()), this, SLOT(close()));
37 
38  InitConnect();
39 
40  qnode_.init();
41 
43 }
44 
46 {
47  timer_.stop();
48 }
49 
51 {
52  QMessageBox msgBox;
53  msgBox.setText("Couldn't find the ros master.");
54  msgBox.exec();
55  close();
56 }
57 
59 {
60  if(ui_.torque_enable_toggle_button->isChecked()) // Torque ON!!!!
61  {
62  ui_.torque_enable_toggle_button->setText("Torque_Disable");
63  qnode_.sendTorqueMsg(true);
64 
65  ui_.set_id_combo_box->setEnabled(false);
66  ui_.set_operating_mode_combo_box->setEnabled(false);
67  ui_.set_baud_rate_combo_box->setEnabled(false);
68 
69  ui_.set_position_zero_push_button->setVisible(false);
70  ui_.set_address_value_dial->setEnabled(false);
71  }
72  else // Torque OFF!!!
73  {
74  ui_.torque_enable_toggle_button->setText("Torque_Enable");
75  qnode_.sendTorqueMsg(false);
76 
77  ui_.set_id_combo_box->setEnabled(true);
78  ui_.set_operating_mode_combo_box->setEnabled(true);
79  ui_.set_baud_rate_combo_box->setEnabled(true);
80 
81  ui_.set_position_zero_push_button->setVisible(false);
82  ui_.set_address_value_dial->setEnabled(false);
83  }
84 
85  setAddressComboBox(ui_.torque_enable_toggle_button->isChecked());
86 }
87 
89 {
90  if (!qnode_.sendRebootMsg())
91  errorMsg();
92  else
93  rightMsg();
94 }
95 
97 {
98  if (!qnode_.sendResetMsg())
99  {
100  errorMsg();
101  }
102  else
103  {
104  ui_.set_id_combo_box->setCurrentIndex(0);
105  ui_.set_baud_rate_combo_box->setCurrentIndex(0);
106  ui_.set_operating_mode_combo_box->setCurrentIndex(0);
107 
108  if (ui_.torque_enable_toggle_button->isChecked())
109  ui_.torque_enable_toggle_button->setChecked(false);
110 
111  rightMsg();
112  }
113 }
114 
116 {
118  ui_.set_address_value_spin_box->setValue(dynamixel_tool_->getValueOfZeroRadianPosition());
119  ui_.set_address_value_dial->setValue(dynamixel_tool_->getValueOfZeroRadianPosition());
120 }
121 
123 {
124  if (ui_.set_id_combo_box->currentText().toStdString() != "Select ID")
125  {
126  if (!qnode_.sendSetIdMsg(ui_.set_id_combo_box->currentText().toInt()))
127  errorMsg();
128  else
129  {
130  rightMsg();
131  ui_.get_id_line_edit->setText(QString::number(ui_.set_id_combo_box->currentText().toInt()));
132  }
133  }
134 }
135 
137 {
138  if (ui_.set_baud_rate_combo_box->currentText().toStdString() != "Select Baudrate")
139  {
140  if (!qnode_.sendSetBaudrateMsg(ui_.set_baud_rate_combo_box->currentText().toLongLong()))
141  errorMsg();
142  else
143  {
144  rightMsg();
145  ui_.get_baud_rate_line_edit->setText(QString::number(ui_.set_baud_rate_combo_box->currentText().toLongLong()));
146  }
147  }
148 }
149 
151 {
152  if (ui_.set_operating_mode_combo_box->currentText().toStdString() != "Select Mode")
153  {
154  if (!qnode_.sendSetOperatingModeMsg(ui_.set_operating_mode_combo_box->currentText().toStdString(),
158  errorMsg();
159  else
160  rightMsg();
161  }
162 }
163 
165 {
166  if (ui_.set_address_name_combo_box->currentText().toStdString() == "Torque_Enable")
167  {
168  if (ui_.set_address_value_spin_box->value() == true)
169  ui_.torque_enable_toggle_button->setChecked(true);
170  else if (ui_.set_address_value_spin_box->value() == false)
171  ui_.torque_enable_toggle_button->setChecked(false);
172  }
173 
174  if (!qnode_.sendAddressValueMsg(ui_.set_address_name_combo_box->currentText().toStdString(), ui_.set_address_value_dial->value()))
175  errorMsg();
176 }
177 
179 {
180  if (index.toStdString() == "Goal_Position")
181  {
182  ui_.set_position_zero_push_button->setVisible(true);
183  ui_.set_address_value_dial->setEnabled(true);
186  }
187  else
188  {
189  ui_.set_position_zero_push_button->setVisible(false);
190  ui_.set_address_value_dial->setEnabled(false);
191  ui_.set_address_value_dial->setRange(-2048, 2048);
192  ui_.set_address_value_spin_box->setRange(-2048, 2048);
193  }
194 }
195 
196 void MainWindow::updateDynamixelInfoLineEdit(dynamixel_workbench_msgs::DynamixelInfo dynamixel_info)
197 {
199 
200  dynamixel_info_->lode_info.device_name = dynamixel_info.load_info.device_name;
201  dynamixel_info_->lode_info.baud_rate = dynamixel_info.load_info.baud_rate;
202  dynamixel_info_->lode_info.protocol_version = dynamixel_info.load_info.protocol_version;
203 
204  dynamixel_info_->model_id = dynamixel_info.model_id;
205  dynamixel_info_->model_number = dynamixel_info.model_number;
206  dynamixel_info_->model_name = dynamixel_info.model_name;
207 
210 
211  ui_.get_device_name_line_edit->setText(QString::fromStdString(dynamixel_info_->lode_info.device_name));
212  ui_.get_id_line_edit->setText(QString::number(dynamixel_info_->model_id));
213  ui_.get_baud_rate_line_edit->setText(QString::number(dynamixel_info_->lode_info.baud_rate));
214  ui_.get_protocol_version_line_edit->setText(QString::number(dynamixel_info_->lode_info.protocol_version));
215  ui_.get_model_name_line_edit->setText(QString::fromStdString(dynamixel_info_->model_name));
216 }
217 
219 {
221 }
222 
224 {
225  // Init log
226  ui_.view_logging->setModel(qnode_.loggingModel());
227 
228  // Init Timer
229  connect(&timer_, SIGNAL(timeout()), this, SLOT(updateListView()));
230  timer_.start(30);
231 
232  // Get Dynamixel Info
233  qRegisterMetaType<dynamixel_workbench_msgs::DynamixelInfo>("dynamixel_workbench_msgs::DynamixelInfo");
234  QObject::connect(&qnode_, SIGNAL(updateDynamixelInfo(dynamixel_workbench_msgs::DynamixelInfo)), this, SLOT(updateDynamixelInfoLineEdit(dynamixel_workbench_msgs::DynamixelInfo)));
235 
236  QObject::connect(ui_.set_id_combo_box, SIGNAL(currentIndexChanged(int)), this, SLOT(changeID()));
237  QObject::connect(ui_.set_baud_rate_combo_box, SIGNAL(currentIndexChanged(int)), this, SLOT(changeBaudrate()));
238  QObject::connect(ui_.set_operating_mode_combo_box, SIGNAL(currentIndexChanged(int)), this, SLOT(changeOperatingMode()));
239 
240  QObject::connect(ui_.set_address_value_spin_box, SIGNAL(valueChanged(int)), ui_.set_address_value_dial, SLOT(setValue(int)));
241  QObject::connect(ui_.set_address_value_dial, SIGNAL(valueChanged(int)), ui_.set_address_value_spin_box, SLOT(setValue(int)));
242  QObject::connect(ui_.set_address_value_spin_box, SIGNAL(valueChanged(int)), this, SLOT(changeControlTableValue()));
243 
244  // Set Address function
245  QObject::connect(ui_.set_address_name_combo_box, SIGNAL(activated(QString)), this, SLOT(setEachAddressFunction(QString)));
246 }
247 
249 {
250  int id = 0;
251 
252  ui_.set_id_combo_box->addItem((QString("Select ID")));
253 
254  for (id = 1; id < 16; id++)
255  {
256  ui_.set_id_combo_box->addItem(QString::number(id));
257  }
258 
259  // TODO : add scroll bar
260 
261  // QAbstractItemView* view = ui_.set_id_combo_box->view();
262  // view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
263  // view->setAutoScroll(true);
264  // view->setFixedHeight(70);
265 }
266 
268 {
269  ui_.set_baud_rate_combo_box->addItem((QString("Select Baudrate")));
270 
272  {
273  ui_.set_baud_rate_combo_box->addItem(QString::number(9600));
274  ui_.set_baud_rate_combo_box->addItem(QString::number(19200));
275  ui_.set_baud_rate_combo_box->addItem(QString::number(57600));
276  ui_.set_baud_rate_combo_box->addItem(QString::number(115200));
277  ui_.set_baud_rate_combo_box->addItem(QString::number(200000));
278  ui_.set_baud_rate_combo_box->addItem(QString::number(250000));
279  ui_.set_baud_rate_combo_box->addItem(QString::number(400000));
280  ui_.set_baud_rate_combo_box->addItem(QString::number(500000));
281  ui_.set_baud_rate_combo_box->addItem(QString::number(1000000));
282  }
283  else
284  {
285  ui_.set_baud_rate_combo_box->addItem(QString::number(9600));
286  ui_.set_baud_rate_combo_box->addItem(QString::number(57600));
287  ui_.set_baud_rate_combo_box->addItem(QString::number(115200));
288  ui_.set_baud_rate_combo_box->addItem(QString::number(1000000));
289  ui_.set_baud_rate_combo_box->addItem(QString::number(2000000));
290  ui_.set_baud_rate_combo_box->addItem(QString::number(3000000));
291  ui_.set_baud_rate_combo_box->addItem(QString::number(4000000));
292  ui_.set_baud_rate_combo_box->addItem(QString::number(4500000));
293  ui_.set_baud_rate_combo_box->addItem(QString::number(10500000));
294  }
295 }
296 
298 {
299  ui_.set_operating_mode_combo_box->addItem((QString("Select Mode")));
300 
301  if (dynamixel_info_->model_name.find("AX") != std::string::npos ||
302  dynamixel_info_->model_name.find("RX") != std::string::npos ||
303  dynamixel_info_->model_name.find("EX") != std::string::npos)
304  {
305  ui_.set_operating_mode_combo_box->addItem(QString("position_control"));
306  ui_.set_operating_mode_combo_box->addItem(QString("velocity_control"));
307  }
308  else if (dynamixel_info_->model_name.find("MX") != std::string::npos)
309  {
310  if (dynamixel_info_->model_name.find("MX-12W") != std::string::npos ||
311  dynamixel_info_->model_name.find("MX-28") != std::string::npos ||
312  dynamixel_info_->model_name.find("MX-64") != std::string::npos ||
313  dynamixel_info_->model_name.find("MX-106") != std::string::npos )
314  {
315  ui_.set_operating_mode_combo_box->addItem(QString("position_control"));
316  ui_.set_operating_mode_combo_box->addItem(QString("velocity_control"));
317  ui_.set_operating_mode_combo_box->addItem(QString("extended_position_control"));
318  }
319  else if (dynamixel_info_->model_name.find("MX-28-2") != std::string::npos)
320  {
321  ui_.set_operating_mode_combo_box->addItem(QString("position_control"));
322  ui_.set_operating_mode_combo_box->addItem(QString("velocity_control"));
323  ui_.set_operating_mode_combo_box->addItem(QString("extended_position_control"));
324  ui_.set_operating_mode_combo_box->addItem(QString("pwm_control"));
325  }
326  else if (dynamixel_info_->model_name.find("MX-64-2") != std::string::npos ||
327  dynamixel_info_->model_name.find("MX-106-2") != std::string::npos)
328  {
329  ui_.set_operating_mode_combo_box->addItem(QString("position_control"));
330  ui_.set_operating_mode_combo_box->addItem(QString("velocity_control"));
331  ui_.set_operating_mode_combo_box->addItem(QString("current_control"));
332  ui_.set_operating_mode_combo_box->addItem(QString("extended_position_control"));
333  ui_.set_operating_mode_combo_box->addItem(QString("current_based_position_control"));
334  ui_.set_operating_mode_combo_box->addItem(QString("pwm_control"));
335  }
336  }
337  else if (dynamixel_info_->model_name.find("XL") != std::string::npos)
338  {
339  if (dynamixel_info_->model_name.find("XL430-W250") != std::string::npos)
340  {
341  ui_.set_operating_mode_combo_box->addItem(QString("position_control"));
342  ui_.set_operating_mode_combo_box->addItem(QString("velocity_control"));
343  ui_.set_operating_mode_combo_box->addItem(QString("extended_position_control"));
344  ui_.set_operating_mode_combo_box->addItem(QString("pwm_control"));
345  }
346  else if (dynamixel_info_->model_name.find("XL-320") != std::string::npos)
347  {
348  ui_.set_operating_mode_combo_box->addItem(QString("position_control"));
349  ui_.set_operating_mode_combo_box->addItem(QString("velocity_control"));
350  }
351  }
352  else if (dynamixel_info_->model_name.find("XM") != std::string::npos ||
353  dynamixel_info_->model_name.find("XH") != std::string::npos)
354  {
355  ui_.set_operating_mode_combo_box->addItem(QString("position_control"));
356  ui_.set_operating_mode_combo_box->addItem(QString("velocity_control"));
357  ui_.set_operating_mode_combo_box->addItem(QString("current_control"));
358  ui_.set_operating_mode_combo_box->addItem(QString("extended_position_control"));
359  ui_.set_operating_mode_combo_box->addItem(QString("current_based_position_control"));
360  ui_.set_operating_mode_combo_box->addItem(QString("pwm_control"));
361  }
362  else if (dynamixel_info_->model_name.find("PRO") != std::string::npos)
363  {
364  if (dynamixel_info_->model_name.find("PRO_L42") != std::string::npos)
365  {
366  ui_.set_operating_mode_combo_box->addItem(QString("position_control"));
367  ui_.set_operating_mode_combo_box->addItem(QString("velocity_control"));
368  }
369  else
370  {
371  ui_.set_operating_mode_combo_box->addItem(QString("position_control"));
372  ui_.set_operating_mode_combo_box->addItem(QString("velocity_control"));
373  ui_.set_operating_mode_combo_box->addItem(QString("torque_control"));
374  ui_.set_operating_mode_combo_box->addItem(QString("extended_position_control"));
375  }
376  }
377 }
378 
380 {
381  if (ui_.get_protocol_version_line_edit->text().toFloat() == 2.0)
382  {
383  ui_.reboot_push_button->setEnabled(true);
384  }
385  else
386  {
387  ui_.reboot_push_button->setEnabled(false);
388  }
389 }
390 
391 void MainWindow::setAddressComboBox(bool torque_enable)
392 {
393  // Delete all Item on combo box
394  uint8_t index_num = ui_.set_address_name_combo_box->count();
395  for (uint8_t combo_box_index = 0; combo_box_index < index_num; combo_box_index++)
396  {
397  ui_.set_address_name_combo_box->removeItem(0);
398  }
399 
400  uint16_t torque_enable_address = 0;
401  const ControlItem* item_ptr = dynamixel_tool_->getControlTable();
402 
403  for (int item_num = 0; item_num < dynamixel_tool_->getTheNumberOfControlItem(); item_num++)
404  {
405  if (!strncmp(item_ptr[item_num].item_name, "Torque_Enable", strlen("Torque_Enable")))
406  {
407  torque_enable_address = item_num;
408  }
409  }
410 
411  // Add item on combo box
412  for (int item_num = 0; item_num < dynamixel_tool_->getTheNumberOfControlItem(); item_num++)
413  {
414  if (torque_enable)
415  {
416  if (item_num >= torque_enable_address)
417  ui_.set_address_name_combo_box->addItem(QString::fromStdString(item_ptr[item_num].item_name));
418  }
419  else
420  {
421  ui_.set_address_name_combo_box->addItem(QString::fromStdString(item_ptr[item_num].item_name));
422  }
423  }
424 }
425 
427 {
428  setIdComboBox();
431  setRebootButton();
432  setAddressComboBox(false);
433 
434  ui_.set_address_value_spin_box->setRange(-2048, 2048);
435  ui_.set_position_zero_push_button->setVisible(false);
436  ui_.set_address_value_dial->setEnabled(false);
437 }
438 
440 {
441  QMessageBox::about(this, tr("About ..."),tr("<h2>Dynamixel workbench 1.0</h2><p>Copyright Robotis</p>"));
442 }
443 
445 {
446  QMessageBox::about(this, tr("Error ..."),tr("<h2>Error!!!!!</h2><p>It didn't works!!</p>"));
447 }
448 
450 {
451  QMessageBox::about(this, tr("Right ..."),tr("<h2>Complete!!</h2><p>It works!!</p>"));
452 }
453 
454 void MainWindow::closeEvent(QCloseEvent *event)
455 {
456  QMainWindow::closeEvent(event);
457 }
bool sendResetMsg(void)
Definition: qnode.cpp:360
void on_reboot_push_button_clicked(bool check)
Definition: main_window.cpp:88
void on_torque_enable_toggle_button_toggled(bool check)
Definition: main_window.cpp:58
void setAddressComboBox(bool torque_enable)
bool sendRebootMsg(void)
Definition: qnode.cpp:352
int64_t getValueOfMaxRadianPosition(void)
MainWindow(int argc, char **argv, QWidget *parent=0)
Definition: main_window.cpp:27
Ui::MainWindowDesign ui_
Definition: main_window.hpp:80
DynamixelTool * dynamixel_tool_
Definition: main_window.hpp:85
const ControlItem * getControlTable(void)
QStringListModel * loggingModel()
Definition: qnode.hpp:51
void on_set_position_zero_push_button_clicked(bool check)
uint8_t getTheNumberOfControlItem(void)
bool sendSetBaudrateMsg(int64_t baud_rate)
Definition: qnode.cpp:95
void closeEvent(QCloseEvent *event)
bool sendAddressValueMsg(std::string addr_name, int64_t value)
Definition: qnode.cpp:381
int64_t getValueOfZeroRadianPosition(void)
bool init()
Definition: qnode.cpp:45
bool sendTorqueMsg(int64_t onoff)
Definition: qnode.cpp:344
DynamixelLoadInfo lode_info
Definition: main_window.hpp:49
void on_factory_reset_push_button_clicked(bool check)
Definition: main_window.cpp:96
void setEachAddressFunction(QString index)
DynamixelInfo * dynamixel_info_
Definition: main_window.hpp:84
bool addTool(const char *model_name, uint8_t id, const char **log=NULL)
int64_t getValueOfMinRadianPosition(void)
bool sendSetIdMsg(uint8_t set_id)
Definition: qnode.cpp:87
void updateDynamixelInfoLineEdit(dynamixel_workbench_msgs::DynamixelInfo dynamixel_info)
void writeReceivedDynamixelData()
Definition: qnode.cpp:544
bool sendSetOperatingModeMsg(std::string index, float protocol_version, std::string model_name, int32_t value_of_max_radian_position)
Definition: qnode.cpp:103
bool setPositionZeroMsg(int32_t zero_position)
Definition: qnode.cpp:373


dynamixel_workbench_single_manager_gui
Author(s): Darby Lim
autogenerated on Mon Jun 10 2019 13:06:15