diff --git a/src/include/joystick.h b/src/include/joystick.h new file mode 100644 index 0000000..26969d3 --- /dev/null +++ b/src/include/joystick.h @@ -0,0 +1,55 @@ +#ifndef JOYSTICK_H +#define JOYSTICK_H + +#include +#include +#include +#include +#include +#include +#include +class JoyStick : public QWidget { + Q_OBJECT + + public: + JoyStick(QWidget *parent = 0); + ~JoyStick(); + enum { + upleft = 0, + up, + upright, + left, + stop, + right, + downleft, + down, + downright + }; + signals: + void keyNumchanged(int num); + + protected: + void paintEvent(QPaintEvent *event) override; + void mouseMoveEvent(QMouseEvent *event) override; + void mouseReleaseEvent(QMouseEvent *event) override; + void mousePressEvent(QMouseEvent *event) override; + // void resizeEvent(QResizeEvent *event)override; + private: + int mouseX; + int mouseY; + int JoyStickX; //摇杆 + int JoyStickY; + int JoyStickR; + int padX; //底盘 + int padY; + int padR; + double handPadDis; //两圆圆心距离 + bool mousePressed; + QTimer *tim; + + private: + double Pointdis(int a, int b, int x, int y); //两点距离 + int getKeyNum(); +}; + +#endif // JoyStick_H diff --git a/src/include/qnode.hpp b/src/include/qnode.hpp index 8e43ba7..019414e 100644 --- a/src/include/qnode.hpp +++ b/src/include/qnode.hpp @@ -37,7 +37,7 @@ private: char **init_argv; ros::Subscriber cmdVel_sub; - ros::Publisher KeyboardControl_cmd_pub; + ros::Publisher cmd_pub; }; diff --git a/src/main/joystick.cpp b/src/main/joystick.cpp new file mode 100644 index 0000000..8d6eb41 --- /dev/null +++ b/src/main/joystick.cpp @@ -0,0 +1,105 @@ +#include "../include/joystick.h" + +#include +JoyStick::JoyStick(QWidget* parent) : QWidget(parent) { + setPalette(QPalette(Qt::white)); + resize(parent->width(), parent->height()); + setMinimumSize(100, 100); + mouseX = width() / 2; + mouseY = height() / 2; + tim = new QTimer(this); + connect(tim, &QTimer::timeout, this, + [=] { emit keyNumchanged(getKeyNum()); }); + // connect(this,&JoyStick::keyNumchanged,this,[=](int num){ + // qDebug()<pos().x(); + mouseY = event->pos().y(); + if (r == true) { + update(); + r = false; + } else { + r = true; + } +} +void JoyStick::mouseReleaseEvent(QMouseEvent* event) { + mouseX = width() / 2; + mouseY = height() / 2; + tim->stop(); + mousePressed = false; + emit keyNumchanged(JoyStick::stop); + update(); +} +void JoyStick::mousePressEvent(QMouseEvent* event) { + mouseX = event->pos().x(); + mouseY = event->pos().y(); + tim->start(100); + mousePressed = true; + update(); +} + +double JoyStick::Pointdis(int a, int b, int x, int y) { + return sqrt((double)((x - a) * (x - a) + (y - b) * (y - b))); +} +int JoyStick::getKeyNum() { + int x, y; + int keynum; + x = (int)(JoyStickX * 3.0 / (padR * 2)); + y = (int)(JoyStickY * 3.0 / (padR * 2)); + keynum = 3 * y + x; + return keynum; +} diff --git a/src/main/mainwindow.cpp b/src/main/mainwindow.cpp index a68296e..eb92692 100644 --- a/src/main/mainwindow.cpp +++ b/src/main/mainwindow.cpp @@ -9,7 +9,7 @@ MainWindow::MainWindow(int argc, char **argv, QWidget *parent) : { qnode.init(); ui->setupUi(this); - setBtnStates(); + initUis(); connections(); } @@ -19,41 +19,12 @@ MainWindow::~MainWindow() } +void MainWindow::initUis(){ + /* + * set PushButton state + */ -void MainWindow::slot_keyboard_control(){ - QPushButton *btn = qobject_cast(sender()); - std::string btn_name = btn->text().toStdString(); - char key = ' '; - - if (btn_name == "↙") - key = '1'; - else if (btn_name == "↓") - key = '2'; - else if (btn_name == "↘") - key = '3'; - else if (btn_name == "←") - key = '4'; - //else if (btn_name == "") - // key = '5'; - else if (btn_name == "→") - key = '6'; - else if (btn_name == "↖") - key = '7'; - else if (btn_name == "↑") - key = '8'; - else if (btn_name == "↗") - key = '9'; - - //速度 - float liner = ui->horizontalSlider_linear->value() * 0.01; - float turn = ui->horizontalSlider_raw->value() * 0.01; - //bool is_rage_mode = ui->checkBox_rage_mode->isChecked(); - std::cout< Forward: ↑ ui->pushButton_Forward->setShortcut(Qt::Key_8); ui->pushButton_Forward->setStyleSheet( "QPushButton{border-image: url(://images/up.png)}" @@ -61,67 +32,165 @@ void MainWindow::setBtnStates(){ "QPushButton:pressed{border-image: url(://images/up_2.png)}"); ui->pushButton_Forward->setFlat(true); - + // 2 -> Back: ↓ ui->pushButton_Back->setShortcut(Qt::Key_2); ui->pushButton_Back->setStyleSheet( - "QPushButton{border-image: url(://images/up.png)}" + "QPushButton{border-image: url(://images/down.png)}" "QPushButton{border:none}" - "QPushButton:pressed{border-image: url(://images/up_2.png)}"); + "QPushButton:pressed{border-image: url(://images/down_2.png)}"); ui->pushButton_Back->setFlat(true); + // 4 -> Left: ← ui->pushButton_Left->setShortcut(Qt::Key_4); - ui->pushButton_Left->setStyleSheet("border:none"); + ui->pushButton_Left->setStyleSheet( + "QPushButton{border-image: url(://images/left.png)}" + "QPushButton{border:none}" + "QPushButton:pressed{border-image: url(://images/left_2.png)}"); ui->pushButton_Left->setFlat(true); + // 6 -> Right: → ui->pushButton_Right->setShortcut(Qt::Key_6); - ui->pushButton_Right->setStyleSheet("border:none"); + ui->pushButton_Right->setStyleSheet( + "QPushButton{border-image: url(://images/right.png)}" + "QPushButton{border:none}" + "QPushButton:pressed{border-image: url(://images/right_2.png)}"); ui->pushButton_Right->setFlat(true); + // 7 -> Left_Forward: ↖ ui->pushButton_Left_Forward->setShortcut(Qt::Key_7); - ui->pushButton_Left_Forward->setStyleSheet("border:none"); + ui->pushButton_Left_Forward->setStyleSheet( + "QPushButton{border-image: url(://images/up_left.png)}" + "QPushButton{border:none}" + "QPushButton:pressed{border-image: url(://images/up_left_2.png)}"); ui->pushButton_Left_Forward->setFlat(true); + // 9 -> Right_Forward: ↗ ui->pushButton_Right_Forward->setShortcut(Qt::Key_9); - ui->pushButton_Right_Forward->setStyleSheet("border:none"); + ui->pushButton_Right_Forward->setStyleSheet( + "QPushButton{border-image: url(://images/up_right.png)}" + "QPushButton{border:none}" + "QPushButton:pressed{border-image: url(://images/up_right_2.png)}"); ui->pushButton_Right_Forward->setFlat(true); + // 1 -> Left_Back: ↙ ui->pushButton_Left_Back->setShortcut(Qt::Key_1); - ui->pushButton_Left_Back->setStyleSheet("border:none"); + ui->pushButton_Left_Back->setStyleSheet( + "QPushButton{border-image: url(://images/down_left.png)}" + "QPushButton{border:none}" + "QPushButton:pressed{border-image: url(://images/down_left_2.png)}"); ui->pushButton_Left_Back->setFlat(true); + // 3 -> Right_Back: ↘ ui->pushButton_Right_Back->setShortcut(Qt::Key_3); - ui->pushButton_Right_Back->setStyleSheet("border:none"); + ui->pushButton_Right_Back->setStyleSheet( + "QPushButton{border-image: url(://images/down_right.png)}" + "QPushButton{border:none}" + "QPushButton:pressed{border-image: url(://images/down_right_2.png)}"); ui->pushButton_Right_Back->setFlat(true); -} -void MainWindow::connections(){ - //绑定速度控制按钮 - // 8 -> Forward:↑ + /* + * init + */ - connect(ui->pushButton_Forward, SIGNAL(clicked()), this, SLOT(slot_keyboard_control())); + rock_widget = new JoyStick(ui->JoyStick_widget); + rock_widget->show(); - connect(ui->pushButton_Back, SIGNAL(clicked()), this, SLOT(slot_keyboard_control())); +} - connect(ui->pushButton_Left, SIGNAL(clicked()), this, SLOT(slot_keyboard_control())); +void MainWindow::slot_keyboard_control(){ + QPushButton *btn = qobject_cast(sender()); + std::string btn_name = btn->text().toStdString(); + char key = ' '; + if (btn_name == "↙") + key = '1'; + else if (btn_name == "↓") + key = '2'; + else if (btn_name == "↘") + key = '3'; + else if (btn_name == "←") + key = '4'; + //else if (btn_name == "") + // key = '5'; + else if (btn_name == "→") + key = '6'; + else if (btn_name == "↖") + key = '7'; + else if (btn_name == "↑") + key = '8'; + else if (btn_name == "↗") + key = '9'; - connect(ui->pushButton_Right, SIGNAL(clicked()), this, SLOT(slot_keyboard_control())); + //速度 + float liner = ui->horizontalSlider_linear->value() * 0.01; + float turn = ui->horizontalSlider_raw->value() * 0.01; + //bool is_rage_mode = ui->checkBox_rage_mode->isChecked(); + //std::cout<pushButton_Left_Forward, SIGNAL(clicked()), this, SLOT(slot_keyboard_control())); +void MainWindow::slot_rockKeyChange(int key){ + //速度 + float liner = ui->horizontalSlider_linear->value() * 0.01; + float turn = ui->horizontalSlider_raw->value() * 0.01; + switch (key) { + case upleft: + qnode.KeyboardMove('7', liner, turn); + break; + case up: + qnode.KeyboardMove('8', liner, turn); + break; + case upright: + qnode.KeyboardMove('9', liner, turn); + break; + case left: + qnode.KeyboardMove('4', liner, turn); + break; + case right: + qnode.KeyboardMove('6', liner, turn); + break; + case downleft: + qnode.KeyboardMove('1', liner, turn); + break; + case down: + qnode.KeyboardMove('2', liner, turn); + break; + case downright: + qnode.KeyboardMove('3', liner, turn); + break; + } - connect(ui->pushButton_Right_Forward, SIGNAL(clicked()), this, SLOT(slot_keyboard_control())); +} +void MainWindow::connections(){ + /* + * 绑定速度控制按钮 + */ + // 8 -> Forward: ↑ + connect(ui->pushButton_Forward, SIGNAL(clicked()), this, SLOT(slot_keyboard_control())); + // 2 -> Back: ↓ + connect(ui->pushButton_Back, SIGNAL(clicked()), this, SLOT(slot_keyboard_control())); + // 4 -> Left: ← + connect(ui->pushButton_Left, SIGNAL(clicked()), this, SLOT(slot_keyboard_control())); + // 6 -> Right: → + connect(ui->pushButton_Right, SIGNAL(clicked()), this, SLOT(slot_keyboard_control())); + // 7 -> Left_Forward: ↖ + connect(ui->pushButton_Left_Forward, SIGNAL(clicked()), this, SLOT(slot_keyboard_control())); + // 9 -> Right_Forward: ↗ + connect(ui->pushButton_Right_Forward, SIGNAL(clicked()), this, SLOT(slot_keyboard_control())); + // 1 -> Left_Back: ↙ connect(ui->pushButton_Left_Back, SIGNAL(clicked()), this, SLOT(slot_keyboard_control())); - - + // 3 -> Right_Back: ↘ connect(ui->pushButton_Right_Back, SIGNAL(clicked()), this, SLOT(slot_keyboard_control())); + connect(rock_widget, SIGNAL(keyNumchanged(int)), this, + SLOT(slot_rockKeyChange(int))); //绑定slider的函数 connect(ui->horizontalSlider_raw, SIGNAL(valueChanged(int)), this, diff --git a/src/main/qnode.cpp b/src/main/qnode.cpp index 8829c44..4db1038 100644 --- a/src/main/qnode.cpp +++ b/src/main/qnode.cpp @@ -27,7 +27,7 @@ bool QNode::init() { void QNode::SubAndPubTopic(){ ros::NodeHandle n; - KeyboardControl_cmd_pub = n.advertise("cmd_vel", 1); + cmd_pub = n.advertise("cmd_vel", 1); } void QNode::KeyboardMove(char key, float speed_linear, float speed_trun){ @@ -58,7 +58,7 @@ void QNode::KeyboardMove(char key, float speed_linear, float speed_trun){ twist.angular.y = 0; twist.angular.z = th * turn; - KeyboardControl_cmd_pub.publish(twist); + cmd_pub.publish(twist); ros::spinOnce(); } diff --git a/src/ui/mainwindow.hpp b/src/ui/mainwindow.hpp index f0cdf8c..64e953f 100644 --- a/src/ui/mainwindow.hpp +++ b/src/ui/mainwindow.hpp @@ -6,6 +6,7 @@ #include #include "ui_mainwindow.h" +#include "../include/joystick.h" #include "../include/qnode.hpp" namespace Ui { @@ -19,12 +20,24 @@ class MainWindow : public QMainWindow public: explicit MainWindow(int argc, char **argv, QWidget *parent = 0); ~MainWindow(); + enum { + upleft = 0, + up, + upright, + left, + stop, + right, + downleft, + down, + downright + }; public slots: void slot_keyboard_control(); //void slot_command_control(int linear, int angular); + void slot_rockKeyChange(int); void Slider_raw_valueChanged(int v); void Slider_linear_valueChanged(int v); @@ -32,9 +45,12 @@ public slots: private: Ui::MainWindow *ui; - void setBtnStates(); - void connections(); QNode qnode; + JoyStick *rock_widget; + + void initUis(); + void connections(); + }; diff --git a/src/ui/mainwindow.ui b/src/ui/mainwindow.ui index 6be3366..0e24c79 100644 --- a/src/ui/mainwindow.ui +++ b/src/ui/mainwindow.ui @@ -20,10 +20,10 @@ - 510 - 40 - 446 - 543 + 1045 + 160 + 387 + 566 @@ -265,41 +265,109 @@ QPushButton:pressed{border-image: url(://images/down_2.png);} - - - - - 线速度(cm/s): - - - + + + Qt::Vertical + + + QSizePolicy::Fixed + + + + 20 + 20 + + + + + + - - - 100 + + + Qt::Horizontal - - 1 + + QSizePolicy::Fixed - - 50 + + + 10 + 20 + + + + + + + + + 线速度(cm/s): + + + + + + + 100 + + + 1 + + + 50 + + + Qt::Horizontal + + + + + + + 50 + + + + + + + Qt::Horizontal - - - - - - 100 + + QSizePolicy::Fixed - + + + 20 + 20 + + + + + + + Qt::Horizontal + + + QSizePolicy::Fixed + + + + 10 + 20 + + + + @@ -327,8 +395,40 @@ QPushButton:pressed{border-image: url(://images/down_2.png);} + + + + Qt::Horizontal + + + QSizePolicy::Fixed + + + + 20 + 20 + + + + + + + + Qt::Vertical + + + QSizePolicy::Fixed + + + + 20 + 20 + + + + @@ -336,9 +436,12 @@ QPushButton:pressed{border-image: url(://images/down_2.png);} Qt::Horizontal + + QSizePolicy::Preferred + - 40 + 86 20 @@ -360,6 +463,9 @@ QPushButton:pressed{border-image: url(://images/down_2.png);} Qt::Horizontal + + QSizePolicy::Preferred + 40 @@ -383,48 +489,8 @@ QPushButton:pressed{border-image: url(://images/down_2.png);} - - - - 🔗断开 - - - - - - - 210 - 430 - 64 - 64 - - - - - 64 - 64 - - - - - 64 - 64 - - - - QPushButton{border-image: url(://images/down.png);} -QPushButton{border:none;} -QPushButton:pressed{border-image: url(://images/down_2.png);} - - - , - - - , - -