diff --git a/src/include/qnode.hpp b/src/include/qnode.hpp index 0fa77ad..0a79ed1 100644 --- a/src/include/qnode.hpp +++ b/src/include/qnode.hpp @@ -1,8 +1,29 @@ -#include -#include -#include -#include -#include -#include +#ifndef Air_Ground_CEC_QNODE_HPP_ +#define Air_Ground_CEC_QNODE_HPP_ +#ifndef Q_MOC_RUN +#include +#endif + +#include + +#include + +class QNode : public QThread{ + Q_OBJECT +public: + QNode(int argc, char **argv); + virtual ~QNode(); + bool init(); + void KeyboardMove(char key); + +private: + int init_argc; + char **init_argv; + ros::NodeHandle n; + ros::Subscriber cmdVel_sub; + ros::Publisher cmd_pub; +}; + +#endif diff --git a/src/main/main.cpp b/src/main/main.cpp index 2183b46..b4d3b2b 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -1,13 +1,24 @@ #include "../include/roskeyboardteleopnode.h" +#include "../ui/mainwindow.hpp" +#include - +#include int main(int argc, char** argv) { + + + ros::init(argc,argv,"tbk", ros::init_options::AnonymousName | ros::init_options::NoSigintHandler); + + QApplication app(argc, argv); + MainWindow CEC(0,0,0); + CEC.show(); + return app.exec(); +/* RosKeyboardTeleopNode tbk; boost::thread t = boost::thread(boost::bind(&RosKeyboardTeleopNode::keyboardLoop, &tbk)); @@ -18,7 +29,7 @@ int main(int argc, char** argv) t.join(); tbk.stopRobot(); tcsetattr(kfd, TCSANOW, &cooked); - +*/ return(0); } diff --git a/src/main/mainwindow.cpp b/src/main/mainwindow.cpp index c613981..60c5b93 100644 --- a/src/main/mainwindow.cpp +++ b/src/main/mainwindow.cpp @@ -1,14 +1,59 @@ #include "../ui/mainwindow.hpp" -MainWindow::MainWindow(QWidget *parent) : - QMainWindow(parent), +MainWindow::MainWindow(int argc, char **argv, QWidget *parent) : + QMainWindow(parent), qnode(argc, argv), ui(new Ui::MainWindow) { ui->setupUi(this); + + connections(); } MainWindow::~MainWindow() { delete ui; } + + + +void MainWindow::slot_keyboard_control(){ + QPushButton *btn = qobject_cast(sender()); + std::string btn_name = btn->text().toStdString(); + char key = ' '; + if (btn_name == "左"){ + key = 'a'; + } + else if (btn_name == "右") { + key = 'd'; + } + else if (btn_name == "前") { + key = 'w'; + } + else if (btn_name == "后") { + key = 's'; + } + /* + else if (btn_name == "加速") { + key = 'f'; + } + */ + + + qnode.KeyboardMove(key); + std::cout<pushButton_W->setShortcut(Qt::Key_W); + connect(ui->pushButton_W, SIGNAL(clicked()), this, SLOT(slot_keyboard_control())); + ui->pushButton_S->setShortcut(Qt::Key_S); + connect(ui->pushButton_S, SIGNAL(clicked()), this, SLOT(slot_keyboard_control())); + ui->pushButton_A->setShortcut(Qt::Key_A); + connect(ui->pushButton_A, SIGNAL(clicked()), this, SLOT(slot_keyboard_control())); + ui->pushButton_D->setShortcut(Qt::Key_D); + connect(ui->pushButton_D, SIGNAL(clicked()), this, SLOT(slot_keyboard_control())); + connect(ui->pushButton_shift, SIGNAL(clicked()), this, SLOT(slot_keyboard_control())); +} diff --git a/src/main/qnode.cpp b/src/main/qnode.cpp index 8b13789..036425b 100644 --- a/src/main/qnode.cpp +++ b/src/main/qnode.cpp @@ -1 +1,47 @@ +#include "../include/qnode.hpp" +#include +#include +#include + +QNode::QNode(int argc, char **argv) : init_argc(argc), init_argv(argv){ + cmd_pub = n.advertise("cmd_vel", 1); +} + + +QNode::~QNode() { + if (ros::isStarted()) { + ros::shutdown(); // explicitly needed since we use ros::start(); + ros::waitForShutdown(); + } + wait(); +} + +bool QNode::init() { + ros::init(init_argc, init_argv, "cyrobot_monitor", + ros::init_options::AnonymousName); + if (!ros::master::check()) { + return false; + } + ros::start(); // explicitly needed since our nodehandle is going out of + // scope. + start(); + return true; +} + +void QNode::KeyboardMove(char key){ + std::map> moveBindings { + // {'key', {speed, turn}} + {'w', {1, 0}}, {'s', {-1, 0}}, {'a', {0, 1}}, {'d', {0, -1}}, + {'W', {1, 0}}, {'S', {-1, 0}}, {'A', {0, 1}}, {'D', {0, -1}} + }; + + float x = moveBindings[key][0]; + float z = moveBindings[key][1]; + + geometry_msgs::Twist twist; + twist.linear.x = x; + twist.angular.z = z; + cmd_pub.publish(twist); + ros::spinOnce(); +} diff --git a/src/ui/mainwindow.hpp b/src/ui/mainwindow.hpp index f2c04c7..dc416b9 100644 --- a/src/ui/mainwindow.hpp +++ b/src/ui/mainwindow.hpp @@ -3,6 +3,8 @@ #include #include "ui_mainwindow.h" +#include "../include/qnode.hpp" + namespace Ui { class MainWindow; } @@ -12,11 +14,17 @@ class MainWindow : public QMainWindow Q_OBJECT public: - explicit MainWindow(QWidget *parent = nullptr); + explicit MainWindow(int argc, char **argv, QWidget *parent = 0); ~MainWindow(); +public slots: + void slot_keyboard_control(); + private: Ui::MainWindow *ui; + void connections(); + QNode qnode; + }; #endif // MAINWINDOW_H diff --git a/src/ui/mainwindow.ui b/src/ui/mainwindow.ui index 0a06936..ea78c87 100644 --- a/src/ui/mainwindow.ui +++ b/src/ui/mainwindow.ui @@ -1,24 +1,216 @@ + - - - MainWindow - + 0 0 - 800 - 600 + 1600 + 900 MainWindow - - - + + + + + 410 + 260 + 314 + 164 + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + 100 + 50 + + + + + 100 + 50 + + + + + 15 + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + 100 + 50 + + + + + 100 + 50 + + + + + 15 + + + + + + + + + + + + 100 + 50 + + + + + 100 + 50 + + + + + 15 + + + + 加速 + + + + + + + + 100 + 50 + + + + + 100 + 50 + + + + + 15 + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + 100 + 50 + + + + + 100 + 50 + + + + + 15 + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + 0 + 0 + 1600 + 22 + + + + - +