teleop.cpp
Go to the documentation of this file.
1 
9 #include <gui/teleop.hpp>
10 #include <QGridLayout>
11 #include <QToolButton>
12 #include <QIcon>
13 #include <QPainter>
14 
15 
16 /* The constructor need the main window containing this widget,
17  * and main()'s arguments (they can be forwarded by the ROS node
18  * to ROS).
19  *
20  * Parameters: parent The containing graphical object,
21  * control the controller whose behaviour is shown,
22  * motion_model the motion model.
23  */
24 TeleopWidget::TeleopWidget(QWidget& parent, KbdCtrl& control,
25  const MotionModel& motion_model)
26  : ControlWidget(control, motion_model), QGroupBox(&parent),
27  vel_drawing(*this, control, motion_model), mapper(this) {
28  setTitle( tr("Teleoperation") );
29  // numbers of buttons added, number of direction buttons,
30  // number of shortcuts for each button
31  static const int nb_buttons = 7, nb_dir_btns = 4, nb_shortcuts = 2;
32  // array of the shortcuts
33  static const Qt::Key shortcuts[nb_buttons][nb_shortcuts] =
34  { {Qt::Key_8, Qt::Key_Up}, {Qt::Key_Period, Qt::Key_PageDown},
35  {Qt::Key_2, Qt::Key_Down}, {Qt::Key_0, Qt::Key_End},
36  {Qt::Key_4, Qt::Key_Left}, {Qt::Key_5, Qt::Key_Home},
37  {Qt::Key_6, Qt::Key_Right} };
38  // array of the tooltips
39  static const char *tips[nb_buttons] =
40  { "Accelerate motion", "Stop motion", "Decelerate motion",
41  "Stop all", "Accelerate rotation toward left",
42  "Stop rotation", "Accelerate rotation toward right" };
43  // array of the Qt arrow types for the direction buttons
44  static const Qt::ArrowType arrows[nb_dir_btns] =
45  { Qt::UpArrow, Qt::DownArrow, Qt::LeftArrow, Qt::RightArrow };
46  // base file name for the other buttons
47  static const QString file(":/files/%1.svg");
48  // array of the icons' name for the other buttons
49  static const char *names[] = {"pause", "stop", "eject"};
50  // velocities' drawing's size
51  static const int disp_size = 3;
52  // setting up the widget
53  setMinimumSize(200, 200);
54  // create a layout and set it to this object
55  QGridLayout *layout = new QGridLayout(this);
56  // for each button
57  for(int id = 0; id < nb_buttons; id++) {
58  QToolButton *button = new QToolButton(this); // define it
59  if (id % 2 == 0) // if it is a direction button
60  button->setArrowType(arrows[id / 2]); // set the Qt arrow type
61  else // other buttons have an icon
62  button->setIcon( QIcon( file.arg(names[id / 2]) ) );
63  // add it to the widget at the right position
64  layout->addWidget(button, id < disp_size ? id : disp_size,
65  id < disp_size ? 0 : id - disp_size);
66  // for each shortcut
67  for(int short_id = 0; short_id < nb_shortcuts; short_id++)
68  button->setShortcut(shortcuts[id][short_id]); // set it
69  // add tooltip
70  button->setToolTip(tips[id]);
71  // map the button to its key
72  mapper.setMapping(button, shortcuts[id][0]);
73  connect( button, SIGNAL( pressed() ), &mapper, SLOT( map() ) );
74  // releasing the button stops the acceleration
75  connect( button, SIGNAL( released() ), this, SLOT( stopAcc() ) );
76  } // end of for (each button)
77  // connect the mapper action to the widget's react method
78  connect( &mapper, SIGNAL( mapped(const int&) ),
79  this, SLOT( react(const int&) ) );
80  // add the velocities' drawing
81  layout->addWidget(&( vel_drawing.widget() ),
82  0, 1, disp_size, disp_size);
83 } // end of TeleopWidget::TeleopWidget(QWidget&, KbdCtrl&, ...) ------
84 
85 /* This widget reacts to commands, which are Qt key values.
86  *
87  * The commands are currently fixed, there will be later a dialog
88  * to change the values associated to each action. These commands
89  * modify the ROS node velocities (with ROS node acceleration,
90  * stop or stop rotation methods).
91  * The robot can be stopped (zero velocities) by pressing either
92  * '0' or 'End'.
93  * The robot can otherwise change its velocity in two ways:
94  * +---+---+---+ +---+-----+---+
95  * | 7 | 8 | 9 | | | ↑ | |
96  * +---+---+---+ +---+-----+---+
97  * | 4 | 5 | 6 | or | ← | Home| → |
98  * +---+---+---+ +---+-----+---+
99  * | 1 | 2 | 3 | | | ↓ | |
100  * +---+---+---+ +---+-----+---+
101  *
102  * Directions are in the robot's frame, up meaning forward
103  * (acceleration) while down means backward (deceleration).
104  * Center ('5' or 'Home') stops the robot's rotation, while
105  * '.' or 'Pg Down' stops the robot's translation.
106  *
107  * Parameters key The command, one of Qt key values.
108  *
109  * See QKeyEvent.
110  */
111 void TeleopWidget::react(const int& key) {
112  KbdCtrl& kbd_ctrl = (KbdCtrl&)(ctrl);
113  switch (key) {
114  case Qt::Key_0 : case Qt::Key_End : kbd_ctrl.stopMotion(); break;
115  case Qt::Key_1 : kbd_ctrl.acc(-1,1); break;
116  case Qt::Key_2 : case Qt::Key_Down : kbd_ctrl.acc(-1,0); break;
117  case Qt::Key_3 : kbd_ctrl.acc(-1,-1); break;
118  case Qt::Key_4 : case Qt::Key_Left : kbd_ctrl.acc(0,1); break;
119  case Qt::Key_5: case Qt::Key_Home: kbd_ctrl.stopRotation(); break;
120  case Qt::Key_6 : case Qt::Key_Right : kbd_ctrl.acc(0,-1); break;
121  case Qt::Key_7 : kbd_ctrl.acc(1,1); break;
122  case Qt::Key_8 : case Qt::Key_Up : kbd_ctrl.acc(1,0); break;
123  case Qt::Key_9 : kbd_ctrl.acc(1,-1); break;
124  case Qt::Key_Period: case Qt::Key_PageDown:
125  kbd_ctrl.stopTranslation();
126  }
127 } // end of void TeleopWidget::react(const int&) ---------------------
void stopMotion()
Stops the robot (i.e. sets both velocities to zero).
Definition: controller.hpp:99
KbdCtrl allows to control the robot with the keyboard.
Definition: keyboard.hpp:23
void react(const int &key)
This widget reacts to commands, which are Qt key values.
Definition: teleop.cpp:111
void stopAcc()
Stops the acceleration of the ROS node.
Definition: teleop.hpp:91
QSignalMapper mapper
The Qt object mapping the widget&#39;s buttons with the parameter transmitted to react().
Definition: teleop.hpp:41
This abstract class is inherited by all those containing a Qt widget intended to show the controller&#39;...
Definition: ctrlWdgt.hpp:24
ViewCtrlWdgt vel_drawing
This widget draws the velocities&#39; evolutions.
Definition: teleop.hpp:36
void stopTranslation()
Stops the robot&#39;s translation.
Definition: keyboard.hpp:44
void acc(const int transFact, const int rotFact)
Changes both velocities (translation and rotation) by adding or removing the related increment (accel...
Definition: keyboard.hpp:57
QWidget & widget()
Returns the widget used to display the behaviour.
Definition: viewCtrl.hpp:82
Qt based teleoperation widget for qt_ctrl.
This class defines a motion model, with the motion limits.
Definition: motion.hpp:28
Controller & ctrl
This widget is the GUI of a controller.
Definition: ctrlWdgt.hpp:29
TeleopWidget(QWidget &parent, KbdCtrl &control, const MotionModel &motion_model)
The constructor needs the main window containing this widget, and the ROS node to control...
Definition: teleop.cpp:24
void stopRotation()
Stops the robot&#39;s rotation.
Definition: keyboard.hpp:41


qt_ctrl
Author(s): Alexis Scheuer
autogenerated on Wed Dec 16 2020 15:51:32