YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
display.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6#ifdef MSVC
7 #define _USE_MATH_DEFINES
8#endif
9#include <cmath>
10
11#include <yarp/os/Time.h>
12#include <yarp/os/Log.h>
13#include <yarp/os/LogStream.h>
14
15#include "display.h"
16#include <QGraphicsPixmapItem>
17#include <QBitmap>
18#include <QKeyEvent>
19
20
21
23{
24 if (mainTimer)
25 {
26 delete mainTimer;
27 mainTimer = nullptr;
28 }
29 if (ui)
30 {
31 delete ui;
32 ui = nullptr;
33 }
34 if (joy)
35 {
36 delete joy;
37 joy = nullptr;
38 }
40}
41
43{
44 switch (e->key())
45 {
46 case Qt::Key_A: pressed_left = false; break;
47 case Qt::Key_W: pressed_up = false; break;
48 case Qt::Key_S: pressed_down = false; break;
49 case Qt::Key_D: pressed_right = false; break;
50 case Qt::Key_Q: pressed_turn_left = false; break;
51 case Qt::Key_E: pressed_turn_right = false; break;
52 }
53}
54
55void MainWindow::keyPressEvent(QKeyEvent* e)
56{
57 switch (e->key())
58 {
59 case Qt::Key_A: pressed_left = true; break;
60 case Qt::Key_W: pressed_up = true; break;
61 case Qt::Key_S: pressed_down = true; break;
62 case Qt::Key_D: pressed_right = true; break;
63 case Qt::Key_Q: pressed_turn_left = true; break;
64 case Qt::Key_E: pressed_turn_right = true; break;
65 case Qt::Key_I: max_vel_lin += lin_vel_step;
66 snprintf(buff, 100, s_max_lin_vel, max_vel_lin);
67 yDebug() << buff;
68 ui->label_max_lin_vel->setText(buff);
69 break;
70 case Qt::Key_K: if (max_vel_lin > 0) { max_vel_lin -= lin_vel_step; }
71 snprintf(buff, 100, s_max_lin_vel, max_vel_lin);
72 yDebug() << buff;
73 ui->label_max_lin_vel->setText(buff);
74 break;
75 case Qt::Key_O: max_vel_theta += ang_vel_step;
76 snprintf(buff, 100, s_max_ang_vel, max_vel_theta);
77 yDebug() << buff;
78 ui->label_max_ang_vel->setText(buff);
79 break;
80 case Qt::Key_L: if (max_vel_theta > 0) { max_vel_theta -= ang_vel_step; }
81 snprintf(buff, 100, s_max_ang_vel, max_vel_theta);
82 yDebug() << buff;
83 ui->label_max_ang_vel->setText(buff);
84 break;
85 }
86}
87
88void MainWindow::updateMain()
89{
92 else { output_data.vel_y = 0; }
93
96 else { output_data.vel_x = 0; }
97
100 else { output_data.vel_theta = 0; }
101
102 double vel_lin_percent = 0;
103 double vel_dir = 0;
104 this->joy->getStatus(vel_lin_percent, vel_dir);
105 if (vel_lin_percent > 0)
106 {
107 output_data.vel_x = -vel_lin_percent * max_vel_lin * cos(vel_dir);
108 output_data.vel_y = -vel_lin_percent * max_vel_lin * sin(vel_dir);
109 }
110
111
114
116
117 snprintf(buff, 100, s_cur_lin_vel_x, cur_vel_lin_x);
118 ui->label_cur_lin_vel_x->setText(buff);
119 snprintf(buff, 100, s_cur_lin_vel_y, cur_vel_lin_y);
120 ui->label_cur_lin_vel_y->setText(buff);
121
122 snprintf(buff, 100, s_cur_ang_vel, cur_vel_theta);
123 ui->label_cur_ang_vel->setText(buff);
124
125 m_stamp.update();
126 outputport.setEnvelope(m_stamp);
128
129 return;
130}
131
132MainWindow::MainWindow(const yarp::os::ResourceFinder& rf, QWidget *parent, double refresh_period) :
133 QMainWindow(parent),
134 ui(new Ui::MainWindow)
135{
136 ui->setupUi(this);
137 this->setWindowTitle("yarpMobilebaseGUI");
138
139 mainTimer = new QTimer(this);
140 connect(mainTimer, SIGNAL(timeout()), this, SLOT(updateMain()));
141 mainTimer->start(1000*refresh_period); //10 seconds
142
143 //this->setWindowFlags(Qt::BypassWindowManagerHint); //Set window with no title bar
144 //this->setWindowFlags(Qt::CustomizeWindowHint); //Set window with no title bar
145 this->setWindowFlags(Qt::MSWindowsFixedSizeDialogHint); //Set window to fixed size
146 // this->setWindowFlags(Qt::FramelessWindowHint); //Set a frameless window
147 this->setWindowFlags(Qt::WindowStaysOnTopHint); //Always on top
148
149
150 yInfo() << "--port <name> the full name of the output port";
151 yInfo() << "--step_lin_vel the increase step of linear velocity (m/s)";
152 yInfo() << "--step_ang_vel the increase step of angular velocity (deg/s)";
153 yInfo() << "--default_lin_vel the default value for max linear velocity (m/s)";
154 yInfo() << "--default_ang_vel the default value for max angular velocity (deg/s)";
155
156 if (rf.check("port"))
157 { portname = rf.find("port").asString(); }
158 if (rf.check("step_lin_vel"))
159 { lin_vel_step = rf.find("step_lin_vel").asFloat64(); }
160 if (rf.check("step_ang_vel"))
161 { ang_vel_step = rf.find("step_ang_vel").asFloat64(); }
162 if (rf.check("default_lin_vel"))
163 { max_vel_lin = rf.find("default_lin_vel").asFloat64(); }
164 if (rf.check("default_ang_vel"))
165 { max_vel_theta = rf.find("default_ang_vel").asFloat64(); }
166
168
169 connect(ui->pushButton_W, SIGNAL(pressed()), this, SLOT(handleButton_Wp()));
170 connect(ui->pushButton_W, SIGNAL(released()), this, SLOT(handleButton_Wr()));
171 connect(ui->pushButton_S, SIGNAL(pressed()), this, SLOT(handleButton_Sp()));
172 connect(ui->pushButton_S, SIGNAL(released()), this, SLOT(handleButton_Sr()));
173 connect(ui->pushButton_A, SIGNAL(pressed()), this, SLOT(handleButton_Ap()));
174 connect(ui->pushButton_A, SIGNAL(released()), this, SLOT(handleButton_Ar()));
175 connect(ui->pushButton_D, SIGNAL(pressed()), this, SLOT(handleButton_Dp()));
176 connect(ui->pushButton_D, SIGNAL(released()), this, SLOT(handleButton_Dr()));
177 connect(ui->pushButton_Q, SIGNAL(pressed()), this, SLOT(handleButton_Qp()));
178 connect(ui->pushButton_Q, SIGNAL(released()), this, SLOT(handleButton_Qr()));
179 connect(ui->pushButton_E, SIGNAL(pressed()), this, SLOT(handleButton_Ep()));
180 connect(ui->pushButton_E, SIGNAL(released()), this, SLOT(handleButton_Er()));
181
182 connect(ui->pushButton_I, SIGNAL(clicked()), this, SLOT(handleButton_I()));
183 connect(ui->pushButton_K, SIGNAL(clicked()), this, SLOT(handleButton_K()));
184 connect(ui->pushButton_O, SIGNAL(clicked()), this, SLOT(handleButton_O()));
185 connect(ui->pushButton_L, SIGNAL(clicked()), this, SLOT(handleButton_L()));
186
187 snprintf(buff, 100, s_max_lin_vel, max_vel_lin);
188 ui->label_max_lin_vel->setText(buff);
189
190 snprintf(buff, 100, s_cur_lin_vel_x, cur_vel_lin_x);
191 ui->label_cur_lin_vel_x->setText(buff);
192
193 snprintf(buff, 100, s_cur_lin_vel_y, cur_vel_lin_y);
194 ui->label_cur_lin_vel_y->setText(buff);
195
196 snprintf(buff, 100, s_max_ang_vel, max_vel_theta);
197 ui->label_max_ang_vel->setText(buff);
198
199 snprintf(buff, 100, s_cur_ang_vel, cur_vel_theta);
200 ui->label_cur_ang_vel->setText(buff);
201
202 joy = new Joywidget(ui->frame_2, "joy1",135);
203 joy->move(QPoint(102,35));
204 joy->show();
205
206 QTimer::singleShot(0, this, &MainWindow::updateMain);
207}
208
209
210void MainWindow::handleButton_Wp()
211{
212 pressed_up = true;
213}
214void MainWindow::handleButton_Wr()
215{
216 pressed_up = false;
217}
218
219void MainWindow::handleButton_Sp()
220{
221 pressed_down = true;
222}
223void MainWindow::handleButton_Ap()
224{
225 pressed_left = true;
226}
227void MainWindow::handleButton_Dp()
228{
229 pressed_right = true;
230}
231void MainWindow::handleButton_Qp()
232{
233 pressed_turn_left = true;
234}
235void MainWindow::handleButton_Ep()
236{
237 pressed_turn_right = true;
238}
239
240void MainWindow::handleButton_Sr()
241{
242 pressed_down = false;
243}
244void MainWindow::handleButton_Ar()
245{
246 pressed_left = false;
247}
248void MainWindow::handleButton_Dr()
249{
250 pressed_right = false;
251}
252void MainWindow::handleButton_Qr()
253{
254 pressed_turn_left = false;
255}
256void MainWindow::handleButton_Er()
257{
258 pressed_turn_right = false;
259}
260
261
262void MainWindow::handleButton_I()
263{
265 if (max_vel_lin < 0) {
266 max_vel_lin = 0;
267 }
268 snprintf(buff,100, s_max_lin_vel, max_vel_lin);
269 ui->label_max_lin_vel->setText(buff);
270}
271void MainWindow::handleButton_K()
272{
274 if (max_vel_lin < 0) {
275 max_vel_lin = 0;
276 }
277 snprintf(buff, 100, s_max_lin_vel, max_vel_lin);
278 ui->label_max_lin_vel->setText(buff);
279}
280void MainWindow::handleButton_O()
281{
283 if (max_vel_theta < 0) {
284 max_vel_theta = 0;
285 }
286 snprintf(buff, 100, s_max_ang_vel, max_vel_theta);
287 ui->label_max_ang_vel->setText(buff);
288}
289void MainWindow::handleButton_L()
290{
292 if (max_vel_theta < 0) {
293 max_vel_theta = 0;
294 }
295 snprintf(buff, 100, s_max_ang_vel, max_vel_theta);
296 ui->label_max_ang_vel->setText(buff);
297}
#define yInfo(...)
Definition Log.h:319
#define yDebug(...)
Definition Log.h:275
int SIGNAL(int pid, int signum)
void getStatus(double &vel_lin_percent, double &vel_dir)
MainWindow class.
Definition display.h:22
bool pressed_turn_left
Definition display.h:88
MainWindow(const yarp::os::ResourceFinder &rf, yarp::dev::IBattery *ibat, QWidget *parent=0, double refresh_period=10.0)
Definition display.cpp:186
std::string portname
Definition display.h:67
yarp::os::Port outputport
Definition display.h:83
double max_vel_theta
Definition display.h:61
void keyPressEvent(QKeyEvent *e) override
Definition display.cpp:55
yarp::dev::MobileBaseVelocity output_data
Definition display.h:90
double max_vel_lin
Definition display.h:60
double ang_vel_step
Definition display.h:66
bool pressed_up
Definition display.h:85
bool pressed_left
Definition display.h:84
double cur_vel_theta
Definition display.h:64
double cur_vel_lin_x
Definition display.h:62
double cur_vel_lin_y
Definition display.h:63
bool pressed_turn_right
Definition display.h:89
double lin_vel_step
Definition display.h:65
bool pressed_down
Definition display.h:86
bool pressed_right
Definition display.h:87
void keyReleaseEvent(QKeyEvent *e) override
Definition display.cpp:42
double vel_theta
angular velocity of the robot [deg/s]
double vel_y
velocity of the robot [m/s]
double vel_x
velocity of the robot [m/s]
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
Definition Port.cpp:436
bool setEnvelope(PortWriter &envelope) override
Set an envelope (e.g., a timestamp) to the next message which will be sent.
Definition Port.cpp:547
void close() override
Stop port activity.
Definition Port.cpp:363
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition Port.cpp:79
Helper class for finding config files and other external resources.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
void update()
Set the timestamp to the current time, and increment the sequence number (wrapping to 0 if the sequen...
Definition Stamp.cpp:124
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
Definition Value.cpp:222
virtual std::string asString() const
Get string value.
Definition Value.cpp:234
Definition aboutdlg.h:11
#define s_cur_lin_vel_x
Definition display.h:71
#define s_max_ang_vel
Definition display.h:70
#define s_cur_lin_vel_y
Definition display.h:72
#define s_max_lin_vel
Definition display.h:69
#define s_cur_ang_vel
Definition display.h:73