UASInfoWidget.cc 6.87 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
9 10 11 12 13 14 15 16 17 18 19 20 21


/**
 * @file
 *   @brief Implementation of class UASInfoWidget
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#include <QtGlobal>
#include <QTimer>
#include <QDir>
22 23 24
#include <QDebug>

#include <float.h>
25 26 27
#include <cstdlib>
#include <cmath>

28 29 30 31
#include "UASInfoWidget.h"
#include "MultiVehicleManager.h"
#include "QGC.h"
#include "UAS.h"
32
#include "QGCApplication.h"
33

34 35
UASInfoWidget::UASInfoWidget(const QString& title, QAction* action, QWidget *parent, QString name)
    : QGCDockWidget(title, action, parent)
36 37 38
    , _activeUAS(NULL)
    , _seqLossPercent(0)
    , _seqLossTotal(0)
39 40 41 42
{
    ui.setupUi(this);
    this->name = name;

43 44
    connect(qgcApp()->toolbox()->multiVehicleManager(), &MultiVehicleManager::activeVehicleChanged, this, &UASInfoWidget::_activeVehicleChanged);
    _activeVehicleChanged(qgcApp()->toolbox()->multiVehicleManager()->activeVehicle());
45

46
    startTime = QGC::groundTimeMilliseconds();
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

    // Set default values
    /** Set two voltage decimals and zero charge level decimals **/
    this->voltageDecimals = 2;
    this->loadDecimals = 2;

    this->voltage = 0;
    this->chargeLevel = 0;
    this->load = 0;
    receiveLoss = 0;
    sendLoss = 0;
    changed = true;
    errors = QMap<QString, int>();

    updateTimer = new QTimer(this);
62
    connect(updateTimer, &QTimer::timeout, this, &UASInfoWidget::refresh);
63 64 65
    updateTimer->start(updateInterval);

    this->setVisible(false);
66 67
    
    loadSettings();
68 69 70

    connect(qgcApp()->toolbox()->mavlinkProtocol(), &MAVLinkProtocol::receiveLossPercentChanged, this, &UASInfoWidget::updateSeqLossPercent);
    connect(qgcApp()->toolbox()->mavlinkProtocol(), &MAVLinkProtocol::receiveLossTotalChanged, this, &UASInfoWidget::updateSeqLossTotal);
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
}

UASInfoWidget::~UASInfoWidget()
{

}

void UASInfoWidget::showEvent(QShowEvent* event)
{
    // React only to internal (pre-display)
    // events
    Q_UNUSED(event);
    updateTimer->start(updateInterval);
}

void UASInfoWidget::hideEvent(QHideEvent* event)
{
    // React only to internal (pre-display)
    // events
    Q_UNUSED(event);
    updateTimer->stop();
}

94
void UASInfoWidget::_activeVehicleChanged(Vehicle* vehicle)
95
{
96 97 98 99 100 101
    if (_activeUAS) {
        disconnect(_activeUAS, &UASInterface::batteryChanged, this, &UASInfoWidget::updateBattery);
        disconnect(_activeUAS, &UASInterface::dropRateChanged, this, &UASInfoWidget::updateReceiveLoss);
        disconnect(static_cast<UAS*>(_activeUAS), &UAS::loadChanged, this, &UASInfoWidget::updateCPULoad);
        disconnect(_activeUAS, &UASInterface::errCountChanged, this, &UASInfoWidget::updateErrorCount);
        _activeUAS = NULL;
102 103 104
    }
    
    if (vehicle) {
105 106 107 108 109
        _activeUAS = vehicle->uas();
        connect(_activeUAS, &UASInterface::batteryChanged, this, &UASInfoWidget::updateBattery);
        connect(_activeUAS, &UASInterface::dropRateChanged, this, &UASInfoWidget::updateReceiveLoss);
        connect(static_cast<UAS*>(_activeUAS), &UAS::loadChanged, this, &UASInfoWidget::updateCPULoad);
        connect(_activeUAS, &UASInterface::errCountChanged, this, &UASInfoWidget::updateErrorCount);
110 111 112
    }
}

dongfang's avatar
dongfang committed
113
void UASInfoWidget::updateBattery(UASInterface* uas, double voltage, double current, double percent, int seconds)
114
{
dongfang's avatar
dongfang committed
115
    Q_UNUSED(current)
116 117 118 119 120 121 122
    setVoltage(uas, voltage);
    setChargeLevel(uas, percent);
    setTimeRemaining(uas, seconds);
}

void UASInfoWidget::updateErrorCount(int uasid, QString component, QString device, int count)
{
123 124
    //qDebug() << __FILE__ << __LINE__ << _activeUAS->getUASID() << "=" << uasid;
    if (_activeUAS->getUASID() == uasid) {
125 126 127 128 129 130 131 132 133 134
        errors.remove(component + ":" + device);
        errors.insert(component + ":" + device, count);
    }
}

/**
 *
 */
void UASInfoWidget::updateCPULoad(UASInterface* uas, double load)
{
135
    if (_activeUAS == uas) {
136 137 138 139 140 141 142 143 144 145
        this->load = load;
    }
}

void UASInfoWidget::updateReceiveLoss(int uasId, float receiveLoss)
{
    Q_UNUSED(uasId);
    this->receiveLoss = this->receiveLoss * 0.8f + receiveLoss * 0.2f;
}

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
void UASInfoWidget::updateSeqLossPercent(int uasId, float seqLossPercent)
{
    if (_activeUAS && _activeUAS->getUASID() == uasId) {
        _seqLossPercent = _seqLossPercent * 0.8f + seqLossPercent * 0.2f;
    } else {
        _seqLossPercent = 0;
    }
}

void UASInfoWidget::updateSeqLossTotal(int uasId, int seqLossTotal)
{
    if (_activeUAS && _activeUAS->getUASID() == uasId) {
        _seqLossTotal = seqLossTotal;
    } else {
        _seqLossTotal = 0;
    }
}

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
/**
  The send loss is typically calculated on the GCS based on packets
  that were received scrambled from the MAV
 */
void UASInfoWidget::updateSendLoss(int uasId, float sendLoss)
{
    Q_UNUSED(uasId);
    this->sendLoss = this->sendLoss * 0.8f + sendLoss * 0.2f;
}

void UASInfoWidget::setVoltage(UASInterface* uas, double voltage)
{
    Q_UNUSED(uas);
    this->voltage = voltage;
}

void UASInfoWidget::setChargeLevel(UASInterface* uas, double chargeLevel)
{
182
    if (_activeUAS == uas) {
183 184 185 186 187 188
        this->chargeLevel = chargeLevel;
    }
}

void UASInfoWidget::setTimeRemaining(UASInterface* uas, double seconds)
{
189
    if (_activeUAS == uas) {
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
        this->timeRemaining = seconds;
    }
}

void UASInfoWidget::refresh()
{
    ui.voltageLabel->setText(QString::number(this->voltage, 'f', voltageDecimals));
    ui.batteryBar->setValue(qMax(0,qMin(static_cast<int>(this->chargeLevel), 100)));

    ui.loadLabel->setText(QString::number(this->load, 'f', loadDecimals));
    ui.loadBar->setValue(qMax(0, qMin(static_cast<int>(this->load), 100)));

    ui.receiveLossBar->setValue(qMax(0, qMin(static_cast<int>(receiveLoss), 100)));
    ui.receiveLossLabel->setText(QString::number(receiveLoss, 'f', 2));

205 206 207 208 209
    ui.seqLossBar->setValue(qMax(0, qMin(static_cast<int>(_seqLossPercent), 100)));
    ui.seqLossLabel->setText(QString::number(_seqLossPercent, 'f', 2));

    ui.seqcntLossLabel->setText(QString::number(_seqLossTotal));

210 211 212 213 214 215 216 217
    ui.sendLossBar->setValue(sendLoss);
    ui.sendLossLabel->setText(QString::number(sendLoss, 'f', 2));

    ui.label_5->setText(QString::number(this->load, 'f', loadDecimals));
    ui.progressBar->setValue(qMax(0, qMin(static_cast<int>(this->load), 100)));

    QString errorString;
    QMapIterator<QString, int> i(errors);
218
    while (i.hasNext()) {
219 220 221 222 223 224 225 226 227 228
        i.next();
        errorString += QString(i.key() + ": %1 ").arg(i.value());

        // FIXME
        errorString.replace("IMU:", "");


    }
    ui.errorLabel->setText(errorString);
}