MainToolBarController.cc 10 KB
Newer Older
dogmaphobic's avatar
dogmaphobic committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009, 2015 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QGROUNDCONTROL is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

/**
 * @file
 *   @brief QGC Main Tool Bar
 *   @author Gus Grubba <mavlink@grubba.com>
 */

#include <QQmlContext>
#include <QQmlEngine>

33
#include "MainToolBarController.h"
Don Gagne's avatar
Don Gagne committed
34
#include "ScreenToolsController.h"
35
#include "MainWindow.h"
dogmaphobic's avatar
dogmaphobic committed
36
#include "UASMessageView.h"
37 38
#include "UASMessageHandler.h"
#include "QGCApplication.h"
39
#include "MultiVehicleManager.h"
40
#include "UAS.h"
dogmaphobic's avatar
dogmaphobic committed
41

42 43
MainToolBarController::MainToolBarController(QObject* parent)
    : QObject(parent)
44
    , _vehicle(NULL)
Don Gagne's avatar
Don Gagne committed
45
    , _mav(NULL)
dogmaphobic's avatar
dogmaphobic committed
46
    , _connectionCount(0)
47
    , _progressBarValue(0.0f)
48
    , _remoteRSSI(0)
dogmaphobic's avatar
dogmaphobic committed
49
    , _remoteRSSIstore(100.0)
50 51
    , _telemetryRRSSI(0)
    , _telemetryLRSSI(0)
52
    , _toolbarMessageVisible(false)
dogmaphobic's avatar
dogmaphobic committed
53 54 55
{
    emit configListChanged();
    emit connectionCountChanged(_connectionCount);
56
    _activeVehicleChanged(qgcApp()->toolbox()->multiVehicleManager()->activeVehicle());
dogmaphobic's avatar
dogmaphobic committed
57

dogmaphobic's avatar
dogmaphobic committed
58
    // Link signals
59 60 61
    connect(qgcApp()->toolbox()->linkManager(),     &LinkManager::linkConfigurationChanged, this, &MainToolBarController::_updateConfigurations);
    connect(qgcApp()->toolbox()->linkManager(),     &LinkManager::linkConnected,            this, &MainToolBarController::_linkConnected);
    connect(qgcApp()->toolbox()->linkManager(),     &LinkManager::linkDisconnected,         this, &MainToolBarController::_linkDisconnected);
dogmaphobic's avatar
dogmaphobic committed
62

63
    // RSSI (didn't like standard connection)
64
    connect(qgcApp()->toolbox()->mavlinkProtocol(),
65 66
        SIGNAL(radioStatusChanged(LinkInterface*, unsigned, unsigned, unsigned, unsigned, unsigned, unsigned, unsigned)), this,
        SLOT(_telemetryChanged(LinkInterface*, unsigned, unsigned, unsigned, unsigned, unsigned, unsigned, unsigned)));
dogmaphobic's avatar
dogmaphobic committed
67

68
    connect(qgcApp()->toolbox()->multiVehicleManager(), &MultiVehicleManager::activeVehicleChanged, this, &MainToolBarController::_activeVehicleChanged);
dogmaphobic's avatar
dogmaphobic committed
69 70
}

71
MainToolBarController::~MainToolBarController()
dogmaphobic's avatar
dogmaphobic committed
72 73 74 75
{

}

76
void MainToolBarController::onSetupView()
77
{
78
    MainWindow::instance()->showSetupView();
79 80
}

81
void MainToolBarController::onPlanView()
82
{
83
    MainWindow::instance()->showPlanView();
84 85
}

86
void MainToolBarController::onFlyView()
dogmaphobic's avatar
dogmaphobic committed
87
{
88
    MainWindow::instance()->showFlyView();
dogmaphobic's avatar
dogmaphobic committed
89 90
}

91
void MainToolBarController::onDisconnect(QString conf)
dogmaphobic's avatar
dogmaphobic committed
92
{
dogmaphobic's avatar
dogmaphobic committed
93 94 95 96
    if(conf.isEmpty()) {
        // Disconnect Only Connected Link
        int connectedCount = 0;
        LinkInterface* connectedLink = NULL;
97
        QList<LinkInterface*> links = qgcApp()->toolbox()->linkManager()->getLinks();
dogmaphobic's avatar
dogmaphobic committed
98 99 100 101
        foreach(LinkInterface* link, links) {
            if (link->isConnected()) {
                connectedCount++;
                connectedLink = link;
dogmaphobic's avatar
dogmaphobic committed
102 103
            }
        }
dogmaphobic's avatar
dogmaphobic committed
104 105 106
        Q_ASSERT(connectedCount   == 1);
        Q_ASSERT(_connectionCount == 1);
        Q_ASSERT(connectedLink);
107
        qgcApp()->toolbox()->linkManager()->disconnectLink(connectedLink);
dogmaphobic's avatar
dogmaphobic committed
108
    } else {
dogmaphobic's avatar
dogmaphobic committed
109
        // Disconnect Named Connected Link
110
        QList<LinkInterface*> links = qgcApp()->toolbox()->linkManager()->getLinks();
dogmaphobic's avatar
dogmaphobic committed
111 112 113
        foreach(LinkInterface* link, links) {
            if (link->isConnected()) {
                if(link->getLinkConfiguration() && link->getLinkConfiguration()->name() == conf) {
114
                    qgcApp()->toolbox()->linkManager()->disconnectLink(link);
dogmaphobic's avatar
dogmaphobic committed
115 116 117 118 119 120
                }
            }
        }
    }
}

121
void MainToolBarController::onConnect(QString conf)
dogmaphobic's avatar
dogmaphobic committed
122
{
dogmaphobic's avatar
dogmaphobic committed
123 124 125 126 127
    // Connect Link
    if(conf.isEmpty()) {
        MainWindow::instance()->manageLinks();
    } else {
        // We don't want the list updating under our feet
128
        qgcApp()->toolbox()->linkManager()->suspendConfigurationUpdates(true);
dogmaphobic's avatar
dogmaphobic committed
129
        // Create a link
130
        LinkInterface* link = qgcApp()->toolbox()->linkManager()->createConnectedLink(conf);
dogmaphobic's avatar
dogmaphobic committed
131 132 133 134
        if(link) {
            // Save last used connection
            MainWindow::instance()->saveLastUsedConnection(conf);
        }
135
        qgcApp()->toolbox()->linkManager()->suspendConfigurationUpdates(false);
dogmaphobic's avatar
dogmaphobic committed
136 137 138
    }
}

139
void MainToolBarController::_activeVehicleChanged(Vehicle* vehicle)
140
{
141 142
    // Disconnect the previous one (if any)
    if (_vehicle) {
143 144
        disconnect(_mav, &UASInterface::remoteControlRSSIChanged, this, &MainToolBarController::_remoteControlRSSIChanged);
        disconnect(_vehicle->autopilotPlugin(), &AutoPilotPlugin::parameterListProgress, this, &MainToolBarController::_setProgressBarValue);
145
        _mav = NULL;
146
        _vehicle = NULL;
147
    }
dogmaphobic's avatar
dogmaphobic committed
148

dogmaphobic's avatar
dogmaphobic committed
149
    // Connect new system
150
    if (vehicle)
dogmaphobic's avatar
dogmaphobic committed
151
    {
152 153
        _vehicle = vehicle;
        _mav = vehicle->uas();
154 155
        connect(_mav, &UASInterface::remoteControlRSSIChanged, this, &MainToolBarController::_remoteControlRSSIChanged);
        connect(_vehicle->autopilotPlugin(), &AutoPilotPlugin::parameterListProgress, this, &MainToolBarController::_setProgressBarValue);
dogmaphobic's avatar
dogmaphobic committed
156
    }
157 158
}

159
void MainToolBarController::_updateConfigurations()
160 161
{
    QStringList tmpList;
162
    QList<LinkConfiguration*> configs = qgcApp()->toolbox()->linkManager()->getLinkConfigurationList();
163 164 165 166 167 168 169 170 171 172 173 174 175 176
    foreach(LinkConfiguration* conf, configs) {
        if(conf) {
            if(conf->isPreferred()) {
                tmpList.insert(0,conf->name());
            } else {
                tmpList << conf->name();
            }
        }
    }
    // Any changes?
    if(tmpList != _linkConfigurations) {
        _linkConfigurations = tmpList;
        emit configListChanged();
    }
dogmaphobic's avatar
dogmaphobic committed
177 178
}

179
void MainToolBarController::_telemetryChanged(LinkInterface*, unsigned, unsigned, unsigned rssi, unsigned remrssi, unsigned, unsigned, unsigned)
180
{
181
    // We only care if we haveone single connection
182
    if(_connectionCount == 1) {
183 184 185 186 187 188 189 190 191
        if((unsigned)_telemetryLRSSI != rssi) {
            // According to the Silabs data sheet, the RSSI value is 0.5db per bit
            _telemetryLRSSI = rssi >> 1;
            emit telemetryLRSSIChanged(_telemetryLRSSI);
        }
        if((unsigned)_telemetryRRSSI != remrssi) {
            // According to the Silabs data sheet, the RSSI value is 0.5db per bit
            _telemetryRRSSI = remrssi >> 1;
            emit telemetryRRSSIChanged(_telemetryRRSSI);
192 193 194 195
        }
    }
}

196
void MainToolBarController::_remoteControlRSSIChanged(uint8_t rssi)
197
{
dogmaphobic's avatar
dogmaphobic committed
198
    // We only care if we have one single connection
199
    if(_connectionCount == 1) {
dogmaphobic's avatar
dogmaphobic committed
200 201 202 203 204 205 206 207
        // Low pass to git rid of jitter
        _remoteRSSIstore = (_remoteRSSIstore * 0.9f) + ((float)rssi * 0.1);
        uint8_t filteredRSSI = (uint8_t)ceil(_remoteRSSIstore);
        if(_remoteRSSIstore < 0.1) {
            filteredRSSI = 0;
        }
        if(_remoteRSSI != filteredRSSI) {
            _remoteRSSI = filteredRSSI;
208 209
            emit remoteRSSIChanged(_remoteRSSI);
        }
210 211 212
    }
}

213
void MainToolBarController::_linkConnected(LinkInterface*)
dogmaphobic's avatar
dogmaphobic committed
214 215 216 217
{
    _updateConnection();
}

218
void MainToolBarController::_linkDisconnected(LinkInterface* link)
dogmaphobic's avatar
dogmaphobic committed
219 220 221 222
{
    _updateConnection(link);
}

223
void MainToolBarController::_updateConnection(LinkInterface *disconnectedLink)
dogmaphobic's avatar
dogmaphobic committed
224 225 226 227 228
{
    QStringList connList;
    int oldCount = _connectionCount;
    // If there are multiple connected links add/update the connect button menu
    _connectionCount = 0;
229
    QList<LinkInterface*> links = qgcApp()->toolbox()->linkManager()->getLinks();
dogmaphobic's avatar
dogmaphobic committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    foreach(LinkInterface* link, links) {
        if (disconnectedLink != link && link->isConnected()) {
            _connectionCount++;
            if(link->getLinkConfiguration()) {
                connList << link->getLinkConfiguration()->name();
            }
        }
    }
    if(oldCount != _connectionCount) {
        emit connectionCountChanged(_connectionCount);
    }
    if(connList != _connectedList) {
        _connectedList = connList;
        emit connectedListChanged(_connectedList);
    }
245
    // Update telemetry RSSI display
246 247 248 249 250 251 252
    if(_connectionCount != 1 && _telemetryRRSSI > 0) {
        _telemetryRRSSI = 0;
        emit telemetryRRSSIChanged(_telemetryRRSSI);
    }
    if(_connectionCount != 1 && _telemetryLRSSI > 0) {
        _telemetryLRSSI = 0;
        emit telemetryLRSSIChanged(_telemetryLRSSI);
253
    }
254 255
    if(_connectionCount != 1 && _remoteRSSI > 0) {
        _remoteRSSI = 0;
dogmaphobic's avatar
dogmaphobic committed
256
        emit remoteRSSIChanged(_remoteRSSI);
257
    }
dogmaphobic's avatar
dogmaphobic committed
258 259
}

260
void MainToolBarController::_setProgressBarValue(float value)
261 262 263 264
{
    _progressBarValue = value;
    emit progressBarValueChanged(value);
}
Don Gagne's avatar
Don Gagne committed
265

266
void MainToolBarController::showToolBarMessage(const QString& message)
267 268
{
    _toolbarMessageQueueMutex.lock();
dogmaphobic's avatar
dogmaphobic committed
269

270
    if (_toolbarMessageQueue.count() == 0 && !_toolbarMessageVisible) {
271
        QTimer::singleShot(500, this, &MainToolBarController::_delayedShowToolBarMessage);
272
    }
dogmaphobic's avatar
dogmaphobic committed
273

274
    _toolbarMessageQueue += message;
dogmaphobic's avatar
dogmaphobic committed
275

276 277 278
    _toolbarMessageQueueMutex.unlock();
}

279
void MainToolBarController::_delayedShowToolBarMessage(void)
280 281
{
    QString messages;
dogmaphobic's avatar
dogmaphobic committed
282

283 284
    if (!_toolbarMessageVisible) {
        _toolbarMessageQueueMutex.lock();
dogmaphobic's avatar
dogmaphobic committed
285

286 287 288 289
        foreach (QString message, _toolbarMessageQueue) {
            messages += message + "\n";
        }
        _toolbarMessageQueue.clear();
dogmaphobic's avatar
dogmaphobic committed
290

291
        _toolbarMessageQueueMutex.unlock();
dogmaphobic's avatar
dogmaphobic committed
292

293 294 295 296 297 298 299
        if (!messages.isEmpty()) {
            _toolbarMessageVisible = true;
            emit showMessage(messages);
        }
    }
}

300
void MainToolBarController::onToolBarMessageClosed(void)
301 302 303
{
    _toolbarMessageVisible = false;
    _delayedShowToolBarMessage();
Don Gagne's avatar
Don Gagne committed
304
}
Don Gagne's avatar
Don Gagne committed
305 306 307 308 309

void MainToolBarController::showSettings(void)
{
    MainWindow::instance()->showSettings();
}