MainWindow.cpp 3.25 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
16
17
    m_rosNodeThread->init();
    qRegisterMetaType<ptrToMessage>("ptrToMessage");
    QObject::connect(m_rosNodeThread, SIGNAL(newViconData(const ptrToMessage&)), this, SLOT(updateNewViconData(const ptrToMessage&)));
18

19
20
21
    ros::NodeHandle nodeHandle("~");
    crazyRadioStatusSubscriber = nodeHandle.subscribe("/CrazyRadio/CrazyRadioStatus", 1, &MainWindow::crazyRadioStatusCallback, this);

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

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

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

33

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

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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!");
            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");
            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)
{
    this->setCrazyRadioStatus(msg.data);
}

78
79
80
void MainWindow::updateNewViconData(const ptrToMessage& p_msg) //connected to newViconData, from node
{
}
81
82
83
84
85

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

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");
    }
}