MainWindow.cpp 3.36 KB
Newer Older
roangel's avatar
roangel committed
1
2
#include "MainWindow.h"
#include "ui_MainWindow.h"
3
#include <string>
roangel's avatar
roangel committed
4

5
6
7
#include <ros/ros.h>
#include <ros/network.h>

8
MainWindow::MainWindow(int argc, char **argv, QWidget *parent) :
roangel's avatar
roangel committed
9
    QMainWindow(parent),
10
11
    ui(new Ui::MainWindow),
    m_radio_status(DISCONNECTED)
roangel's avatar
roangel committed
12
{
13
    m_rosNodeThread = new rosNodeThread(argc, argv, "student_GUI");
roangel's avatar
roangel committed
14
    ui->setupUi(this);
15
    m_rosNodeThread->init();
16
17
18
19

    std::string ros_namespace = ros::this_node::getNamespace();
    ROS_INFO("namespace: %s", ros_namespace.c_str());

20
21
    qRegisterMetaType<ptrToMessage>("ptrToMessage");
    QObject::connect(m_rosNodeThread, SIGNAL(newViconData(const ptrToMessage&)), this, SLOT(updateNewViconData(const ptrToMessage&)));
22

23
    ros::NodeHandle nodeHandle("~");
24
    crazyRadioStatusSubscriber = nodeHandle.subscribe(ros_namespace + "/CrazyRadioStatus", 1, &MainWindow::crazyRadioStatusCallback, this);
25

26
    // communication with PPS Client, just to make it possible to communicate through terminal also we use PPSClient's name
27
28
    ros::NodeHandle nh_PPSClient(ros_namespace + "/PPSClient");
    crazyRadioCommandPublisher = nh_PPSClient.advertise<std_msgs::Int32>("crazyRadioCommand", 1);
29
    PPSClientCommadPublisher = nh_PPSClient.advertise<std_msgs::Int32>("Command", 1);
30
31

    disableGUI();
roangel's avatar
roangel committed
32
33
}

34

roangel's avatar
roangel committed
35
36
37
38
MainWindow::~MainWindow()
{
    delete ui;
}
39

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
void MainWindow::disableGUI()
{
    ui->enable_disable_CF_button->setEnabled(false);
}

void MainWindow::enableGUI()
{
    ui->enable_disable_CF_button->setEnabled(true);
}

void MainWindow::setCrazyRadioStatus(int radio_status)
{
    // add more things whenever the status is changed
    switch(radio_status)
    {
        case CONNECTED:
            // change icon, the rest of the GUI is available now
            ui->connected_disconnected_label->setText("Connected!");
58
            enableGUI();
59
60
61
62
63
64
65
66
            break;
        case CONNECTING:
            // change icon
            ui->connected_disconnected_label->setText("Connecting...");
            break;
        case DISCONNECTED:
            // change icon, the rest of the GUI is disabled
            ui->connected_disconnected_label->setText("Disconnected");
67
            disableGUI();
68
69
70
71
72
73
74
75
76
77
            break;
        default:
    		ROS_ERROR_STREAM("unexpected radio status: " << m_radio_status);
            break;
    }
    this->m_radio_status = radio_status;
}

void MainWindow::crazyRadioStatusCallback(const std_msgs::Int32& msg)
{
78
    ROS_INFO("Callback CrazyRadioStatus called");
79
80
81
    this->setCrazyRadioStatus(msg.data);
}

82
83
84
void MainWindow::updateNewViconData(const ptrToMessage& p_msg) //connected to newViconData, from node
{
}
85
86
87
88
89

void MainWindow::on_RF_Connect_button_clicked()
{
    std_msgs::Int32 msg;
    msg.data = CMD_RECONNECT;
90
    this->crazyRadioCommandPublisher.publish(msg);
roangel's avatar
roangel committed
91
    ROS_INFO("command reconnect published");
92
}
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110

void MainWindow::on_enable_disable_CF_button_clicked()
{
    if(ui->enable_disable_CF_button->text().toStdString() == "EnableCF") //enabled, disable if success
    {
        std_msgs::Int32 msg;
        msg.data = CMD_USE_CRAZYFLY_DISABLE;
        this->PPSClientCommadPublisher.publish(msg);
        ui->enable_disable_CF_button->setText("DisableCF");
    }
    else                        //disabled, enable if success
    {
        std_msgs::Int32 msg;
        msg.data = CMD_USE_CRAZYFLY_ENABLE;
        this->PPSClientCommadPublisher.publish(msg);
        ui->enable_disable_CF_button->setText("EnableCF");
    }
}